conker87 Posted June 22, 2007 Share Posted June 22, 2007 I've got this as a function: function styleLink($colour) { if ($_SERVER['QUERY_STRING']) { echo $_SERVER['PHP_SELF'] . "?" . $_SERVER['QUERY_STRING'] . "&style=" . $colour; } else { echo $_SERVER['PHP_SELF'] . "?style=" . $colour; } } This works great, but if there's already a '?style=' it will still add another '&style=' and that's obviously not good. How would I check the $_SERVER['QUERY_STRING'] contains this? Link to comment https://forums.phpfreaks.com/topic/56739-checking-a-string-contains-characters/ Share on other sites More sharing options...
effigy Posted June 22, 2007 Share Posted June 22, 2007 strpos. Link to comment https://forums.phpfreaks.com/topic/56739-checking-a-string-contains-characters/#findComment-280208 Share on other sites More sharing options...
conker87 Posted June 22, 2007 Author Share Posted June 22, 2007 Ah, excellent works great. But now I have another problem. Here is the code: function styleLink($colour) { if ($_SERVER['QUERY_STRING']) if (strpos($_SERVER['QUERY_STRING'], "style") === false) { echo $_SERVER['PHP_SELF'] . "?" . $_SERVER['QUERY_STRING'] . "&style=" . $colour; } else { echo $_SERVER['PHP_SELF'] . "?" . $_SERVER['QUERY_STRING']; } else { echo $_SERVER['PHP_SELF'] . "?style=" . $colour; } } At the moment, if there's already the style var there then it'll just echo the 'index.php?style=blue' (as an example), hoever if I try and click the purple, orange and green links it will just show 'index.php?style=blue' rather than 'index.php?style=colour'. Would anyone know how to correct it so that it shows the right colour in the url in the style var is already there? Apologies for the really terrible explanation, it's difficult to word it. Link to comment https://forums.phpfreaks.com/topic/56739-checking-a-string-contains-characters/#findComment-280213 Share on other sites More sharing options...
effigy Posted June 22, 2007 Share Posted June 22, 2007 Cleaning up the code might help: function styleLink($colour) { if ($_SERVER['QUERY_STRING']) { if (strpos($_SERVER['QUERY_STRING'], "style") === false) { echo $_SERVER['PHP_SELF'] . "?" . $_SERVER['QUERY_STRING'] . "&style=" . $colour; } else { echo $_SERVER['PHP_SELF'] . "?" . $_SERVER['QUERY_STRING']; } } else { echo $_SERVER['PHP_SELF'] . "?style=" . $colour; } } Link to comment https://forums.phpfreaks.com/topic/56739-checking-a-string-contains-characters/#findComment-280304 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.