Jump to content

Recommended Posts

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

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.

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;
}
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.