PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
<?php
//Return the string after the domain name e.g /home
$currentpage = $_SERVER['REQUEST_URI'];
//Check to see if the current page is part of the Catalog
If ($currentpage=='/Catalog') //Check this syntax
{
// set up a switch and set the currency value appropriately
switch ($currentpage) {
case "/catalog/UK/";
//Set the currency value to UK here
break;
case "/catalog/EU/";
//Set the currency value to EU here
break;
case "/catalog/NonEU/";
//Set the currency value to NonEU here
break;
case "/catalog/US/";
//Set the currency value to US here
break;
case "/catalog/ROTW";
Echo $currentpage;
break;
}
}
?>
The problem I am having is, in the IF Statement I want to put "If $currentpage=='/Catalog/*". Where the * means anything can come after it, but the start of the string must stay as "/Catalog/".
The same in the case statements, I want to allow any value to appear after that, I just need the start to be "/Catalog/<option>/"
If there a wild card symbol I can use to put in place after the last "/". Is there a way I can also make everything insensitive to case.
You can use stripos() or preg_match() for the if. The switch would need to be something like this, though there is probably a better way to it than switch:
switch (true) {
case stripos($currentpage, "catalog/UK/"):
//Set the currency value to UK here
break;
///etc...
}
If you know the structure of the paths and they will be consistant, then you should be able to use something like this to get an array of things you need:
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
In the switch case you have to omit the starting / because it will match and return position 0 which will not == true.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
<?php
//Return the string after the domain name e.g /home
$currentpage = $_SERVER['REQUEST_URI'];
If (stripos($currentpage)=="/Catalog/")
// set up a switch and set the currency value appropriately
switch ($currentpage) {
case stripos($currentpage, "/catalog/UK");
//Set the currency value
break;
case stripos($currentpage, "/catalog/EU");
//Set the currency value
break;
case stripos($currentpage, "/catalog/NonEU");
//Set the currency value
break;
case stripos($currentpage, "/catalog/US");
//Set the currency value
break;
case stripos($currentpage, "/catalog/ROTW");
//Set the currency value
break;
}
}
?>
Everything works as expected except the IF. Im sure Im just being stupid. Can you see what I am doing wrong?
// preferred
if (stripos($currentpage, "/Catalog/") !== false)
// or
if (stripos($currentpage, "Catalog/"))
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
// preferred
if (stripos($currentpage, "/Catalog/") !== false)
[/quote]
Works perfectly. Thankyou for your help. And to others that posted on this.
Kind Regards
Tom Noble
P.S for anyone having a similar issue, the final code is here. If I can contribute back to people then all the better
[syntax=php]<?php
//Return the string after the domain name e.g /home
$currentpage = $_SERVER['REQUEST_URI'];
if (stripos($currentpage, "/Catalog/") !== false)
{
// set up a switch and set the currency value appropriately
switch ($currentpage) {
case stripos($currentpage, "/catalog/UK");
//Set the currency value
break;
case stripos($currentpage, "/catalog/EU");
//Set the currency value
break;
case stripos($currentpage, "/catalog/NonEU");
//Set the currency value
break;
case stripos($currentpage, "/catalog/US");
//Set the currency value
break;
case stripos($currentpage, "/catalog/ROTW");
Echo $currentpage;
break;
}
}
?>