Jump to content

query string problem is whooping my behind..help please!


Stan007

Recommended Posts

this is what i want in the url 
http://localhost/board/italian.php?page=2&country=italian

when this link is clicked 
echo "<a href='?page=".($page_number + 1)." ".(&)." country=".($cleaned_current_page_country)."'>Next</a>";

but i keep getting this error:
Parse error: syntax error, unexpected '&' in C:\xampp\htdocs\board\includes\menuboard_main_display.php on line 105

appreciate any help

Link to comment
Share on other sites

you should use http_build_query() when building the query string part of urls (it automatically urlencodes the values for you.) you should start with a copy of any existing $_GET parameters (before the start of you loop), so that your code only modifies the one(s) they are responsible for. you should use &amp; as the separator in links.

 

 

Link to comment
Share on other sites

Remember that you're constructing a PHP string that just happens to contain some HTML markup that a web browser can make sense of.  Nothing magical is going to happen to the way PHO does that building just because you're building "HTML" (or "SQL"). 

You must follow PHP Rules for constructing strings and having that "loose" ampersand floating around in the middle, whether or not it's wrapped in braces, just won't work.

How about something more like this: 

printf( '<a href='?page=%d&country=%s'>%s</a>', $page_number + 1, $cleaned_current_page_country, 'Next' ); 

Regards, 
   Phill  W.

 

Link to comment
Share on other sites

@mac_gyver gave the correct way to do it - http_build_query()

EG

$url = 'http://localhost/board/italian.php';

$data = [  'page'    => 2,
           'country' => 'italian',
           'comment' => 'this is a (simple) comment'
        ];
        
$qstr = http_build_query($data);
$url .= '?' . $qstr;

echo $url;                   //--> http://localhost/board/italian.php?page=2&country=italian&comment=this+is+a+%28simple%29+comment

Note the encoding of the query string

  • Like 1
Link to comment
Share on other sites

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.