Skip to main content
Inspiring
May 11, 2006
Answered

php & query string variables in a url

  • May 11, 2006
  • 1 reply
  • 798 views
I've been scratching my head for a couple days now. What I'm trying to do seems soooooo easy but it doesn't work. Maybe someone can spot my problem in the code.

What I'm doing is breaking a single blob mysql field into multiple "pages" by inserting a code snippet into the field. That part works fine. The url for the page in question with a sample article is
http://208.106.143.80/articles/articles.php?idq=141&cat=4&pg=1

The url contains the article id, article category and page number. At the bottom of the page is the pagination that reloads the page with another "page" of the article. The code for that is:

$totalpages = $total;
if ($totalpages > 1) {
echo "<p style='text-align:right;'><strong>Page:</strong> ";
if ($i > 1) {
echo " <a href='".$_SERVER['PHP_SELF']."?pg=".($i-1)."'><< Prev</a> ";
}
for ($x = 1; $x <= $totalpages; $x++) {
if ($x == $i) {
echo "<strong>[".$x."]</strong> ";
}
else {
echo "<strong><a href='".$_SERVER['PHP_SELF']."?pg=".$x."'>".$x."</a></strong> ";
}
}
if ($i < $total) {
echo " <a href='".$_SERVER['PHP_SELF']."?pg=".$k."'>Next >></a></p>";
}
}

Basically all I'm doing is throwing the new querystring pg=$x to the end of the current page url and reloading the current page. And that is the problem. Even though the page links look right in the browser, it just keeps loading the same "page" over and over again. I can go into the url in the browser and manually change from page 1 to page 2, etc. and that works as it should.

Ideas from anyone smarter than I?

What am I missing here.
This topic has been closed for replies.
Correct answer aerobicdesign
Thanks Micha. That is the missing bit of the puzzle I wasn't getting from my end. I wasn't getting the error. I will work on the script a bit more and see if I can see where I went wrong. If you are still around, I'll send another posting that the file has been changed. Mind you, if I get it fixed, I guess I should see it working and problem solved.
Thanks all. I messed around with the script a bit more and despite the fact that I thought I had already tried adding the missing urlstring values, this time when I did it, it worked like it was supposed to. Problem history.

1 reply

Participant
May 11, 2006
Is it that you are missing the idq and cat variables from the url you generate?

If you type in http://208.106.143.80/articles/articles.php?pg=2 for example, which is an example of a link destination from your page, it doesn't work.
It seems that the idq is essential, but cat isn't.
Inspiring
May 12, 2006
.oO(aerobicdesign)

>Hi Peter. No, that I can add if need be. But if you go to the link I provided
>to you below, then go down to the bottom of the page and click on any of the
>page numbers, eventhough the link appears to be correct (as in the pg=x
>changes), the page, when it reloads, is still the same page.

The response from the server is somewhat broken. In some browsers this
might lead to a reload of the current page, in others like Opera and
Lynx you'll get a "Object Moved" response instead, containing a link to
the same page.

This is your posted link:

http://208.106.143.80/articles/articles.php?idq=141&cat=4&pg=1

These are the links at the bottom of that article:

http://208.106.143.80/articles/articles.php?pg=2
http://208.106.143.80/articles/articles.php?pg=3

But of course this won't work.

> It is like something is just eating up the querystring I'm sending to that
>page.

No, you're just not creating a complete query string. The article and
category IDs are missing, so the script doesn't know what to display.

Micha
Inspiring
May 12, 2006
.oO(aerobicdesign)

>Thanks guys. I realize that it is an incomplete url string. If you click on the
>page links at the bottom of the article, are you actually getting a 404 error
>or is it just coming back to the same original page that you started from?

The server returns a 302 status code, which is usually used to redirect
the browser to another page. But it doesn't tell where to redirect to,
so some browsers will simply "redirect" to the current page again, while
others show the plain message received from the server:

| Object Moved
| This document may be found here

The word "here" is an empty link.

There's something wrong with your script I guess. If some parameters are
missing (like article ID for example) it should return an error message
or something like that.

> The problem I'm having is that even if I add the article id and category to
>the url string, the page just keeps reloading with the same urlstring thing and
>I can't seem to update it to a new value.

Can you upload your updated script? If you include the missing IDs it
should work as expected.

Micha

Thanks Micha. That is the missing bit of the puzzle I wasn't getting from my end. I wasn't getting the error. I will work on the script a bit more and see if I can see where I went wrong. If you are still around, I'll send another posting that the file has been changed. Mind you, if I get it fixed, I guess I should see it working and problem solved.