Jump to content

[SOLVED] Help with multiple variables and $_GET


john010117
 Share

Recommended Posts

Let me first show you the part of the code I'm having trouble with.

 

if ($_GET['type'] == "title") {

   if(!isset($_GET['page'])){
       $page = 1;
   } else {
       $page = $_GET['page'];
   }

   $max_results = 10;
   $from = (($page * $max_results) - $max_results); 
   $result = mysql_query ("SELECT * FROM bnetupdates WHERE title = '$word' LIMIT $from, $max_results");

   if(mysql_affected_rows()>0)
   {
    while ($row = mysql_fetch_assoc($result)) {
       extract($row);
          print "<p><table border=\"0\" cellspacing=\"5\" cellpadding=\"0\">
          <tr><td><img src=\"$image\"></td><td valign=\"center\"><a href=\"archive.php?id=$id\">$title</a><br /><font size=\"1\">Posted on: $date</font></td></tr></table></p>";
       }

   $total_results_q = mysql_query("SELECT * FROM bnetupdates");
   $total_results = mysql_affected_rows($total_results_q);
   $total_pages = ceil($total_results / $max_results);
   echo "<center>Select a Page<br />";

   if($page > 1){
       $prev = ($page - 1);
       echo "<a href=\"".$_SERVER['PHP_SELF']."?word=$word&type=title&page=$prev\"><<Previous</a> ";
   }

   for($i = 1; $i <= $total_pages; $i++){
       if(($page) == $i){
           echo "$i ";
           } else {
               echo "<a href=\"".$_SERVER['PHP_SELF']."?word=$word&type=title&page=$i\">$i</a> ";
       }
   }

   if($page < $total_pages){
       $next = ($page + 1);
       echo "<a href=\"".$_SERVER['PHP_SELF']."?word=$word&type=title&page=$next\">Next>></a>";
       }
   }
   else{
        print "<p>The search failed to find the title of the Bungie Weekly Update story. Please make sure that the title is EXACT and the spelling is correct. Please try again or see the list of all the updates <a href=\"archive.php\">here</a>.</p>";
       }
   }

 

It shows the first page of results just fine (without the page variable in the URL). But whenever I go to another page of results, it shows "The search failed to find..." error message. Can anyone help please?

Link to comment
Share on other sites

Hi john

 

in your code you are using mysql_affected_rows() after each select query .

 

but accoriding to php manual mysql_affected_rows() returns the number of rows affected by the last INSERT, UPDATE or DELETE.

 

mysql_affected_rows() does not work with SELECT statements; only on statements which modify records.

 

To retrieve the number of rows returned by a SELECT, use mysql_num_rows()

 

 

Thanks

ShaymoL

Link to comment
Share on other sites

Ok, I changed that to this:

if ($_GET['type'] == "title") {

   if(!isset($_GET['page'])){
       $page = 1;
   } else {
       $page = $_GET['page'];
   }

   $max_results = 10;
   $from = (($page * $max_results) - $max_results); 
   $result = mysql_query ("SELECT * FROM bnetupdates WHERE title = '$word' LIMIT $from, $max_results");

   if(mysql_num_rows($result)>0)
   {
    while ($row = mysql_fetch_assoc($result)) {
       extract($row);
          print "<p><table border=\"0\" cellspacing=\"5\" cellpadding=\"0\">
          <tr><td><img src=\"$image\"></td><td valign=\"center\"><a href=\"archive.php?id=$id\">$title</a><br /><font size=\"1\">Posted on: $date</font></td></tr></table></p>";
       }

   $total_results_q = mysql_query("SELECT * FROM bnetupdates");
   $total_results = mysql_num_rows($total_results_q);
   $total_pages = ceil($total_results / $max_results);
   echo "<center>Select a Page<br />";

   if($page > 1){
       $prev = ($page - 1);
       echo "<a href=\"".$_SERVER['PHP_SELF']."?word=$word&type=title&page=$prev\"><<Previous</a> ";
   }

   for($i = 1; $i <= $total_pages; $i++){
       if(($page) == $i){
           echo "$i ";
           } else {
               echo "<a href=\"".$_SERVER['PHP_SELF']."?word=$word&type=title&page=$i\">$i</a> ";
       }
   }

   if($page < $total_pages){
       $next = ($page + 1);
       echo "<a href=\"".$_SERVER['PHP_SELF']."?word=$word&type=title&page=$next\">Next>></a>";
       }
   }
   else{
        print "<p>The search failed to find the title of the Bungie Weekly Update story. Please make sure that the title is EXACT and the spelling is correct. Please try again or see the list of all the updates <a href=\"archive.php\">here</a>.</p>";
       }
   }

 

...but it still doesn't work. First page (w/o ?page=1 in the URL) shows up fine, but the next page doesn't show up. Can you see any additional problems with the code?

Link to comment
Share on other sites

Hi john

please check the following code that you wrote

$result = mysql_query ("SELECT * FROM bnetupdates WHERE title = '$word' LIMIT $from, $max_results");

 

here in your where clause you are trying to show those records whose title = '$word'

but $word is a Super Global Variable .

So you have to use $_GET['word'] in stead of $word.

 

$result = mysql_query ("SELECT * FROM bnetupdates WHERE title = '$_GET['word']' LIMIT $from, $max_results");

 

I think it would be work for you

 

Thanks

ShaymoL

 

Link to comment
Share on other sites

Ok, I've changed that also. But the second page still shows the "not found" message.

 

<?php
if ($_GET['type'] == "title") {

   if(!isset($_GET['page'])){
       $page = 1;
   } else {
       $page = $_GET['page'];
   }

   $max_results = 10;
   $from = (($page * $max_results) - $max_results); 
   $result = mysql_query ("SELECT * FROM bnetupdates WHERE title = '$_GET[word]' LIMIT $from, $max_results");

   if(mysql_num_rows($result)>0)
   {
    while ($row = mysql_fetch_assoc($result)) {
       extract($row);
          print "<p><table border=\"0\" cellspacing=\"5\" cellpadding=\"0\">
          <tr><td><img src=\"$image\"></td><td valign=\"center\"><a href=\"archive.php?id=$id\">$title</a><br /><font size=\"1\">Posted on: $date</font></td></tr></table></p>";
       }

   $total_results_q = mysql_query("SELECT * FROM bnetupdates");
   $total_results = mysql_num_rows($total_results_q);
   $total_pages = ceil($total_results / $max_results);
   echo "<center>Select a Page<br />";

   if($page > 1){
       $prev = ($page - 1);
       echo "<a href=\"".$_SERVER['PHP_SELF']."?word=$word&type=title&page=$prev\"><<Previous</a> ";
   }

   for($i = 1; $i <= $total_pages; $i++){
       if(($page) == $i){
           echo "$i ";
           } else {
               echo "<a href=\"".$_SERVER['PHP_SELF']."?word=$word&type=title&page=$i\">$i</a> ";
       }
   }

   if($page < $total_pages){
       $next = ($page + 1);
       echo "<a href=\"".$_SERVER['PHP_SELF']."?word=$word&type=title&page=$next\">Next>></a>";
       }
   }
   else{
        print "<p>The search failed to find the title of the Bungie Weekly Update story. Please make sure that the title is EXACT and the spelling is correct. Please try again or see the list of all the updates <a href=\"archive.php\">here</a>.</p>";
       }
   }
?>

Any suggestions?

Link to comment
Share on other sites

I don't believe I'm missing a } at the end of the script (just ran it through Notepad 2). I'm getting the error that I have coded in: "The search failed to find the title of the Bungie Weekly Update story. Please make sure that the title is EXACT and the spelling is correct. Please try again or see the list of all the updates here".

Link to comment
Share on other sites

I think the problem is that $word is not defined (unless youre using register_globals on which by all accounts is a bad thing)

 

Try putting the search term in your error message, eg:

 

<?
print "<p>The search failed to find the title <b>$word</b> in the Bungie Weekly Update story archive. Please make sure that the title is EXACT and the spelling is correct. Please try again or see the list of all the updates <a href=\"archive.php\">here</a>.</p>"; ?>

 

Heres how I would do it anyway

 

<?php
if ($_GET['type'] == "title") {

$theWord = $_GET['word'];

   if(!isset($_GET['page'])){
       $page = 1;
   } else {
       $page = $_GET['page'];
   }

   $max_results = 10;
   $from = (($page * $max_results) - $max_results); 
   $result = mysql_query ("SELECT * FROM bnetupdates WHERE title = '$theWord' LIMIT $from, $max_results");

   if(mysql_num_rows($result)>0)
   {
    while ($row = mysql_fetch_assoc($result)) {
       extract($row);
          print "<p><table border=\"0\" cellspacing=\"5\" cellpadding=\"0\">
          <tr><td><img src=\"$image\"></td><td valign=\"center\"><a href=\"archive.php?id=$id\">$title</a><br /><font size=\"1\">Posted on: $date</font></td></tr></table></p>";
       }

   $total_results_q = mysql_query("SELECT * FROM bnetupdates");
   $total_results = mysql_num_rows($total_results_q);
   $total_pages = ceil($total_results / $max_results);
   echo "<center>Select a Page<br />";

   if($page > 1){
       $prev = ($page - 1);
       echo "<a href=\"".$_SERVER['PHP_SELF']."?word=$theWord&type=title&page=$prev\"><<Previous</a> ";
   }

   for($i = 1; $i <= $total_pages; $i++){
       if(($page) == $i){
           echo "$i ";
           } else {
               echo "<a href=\"".$_SERVER['PHP_SELF']."?word=$theWord&type=title&page=$i\">$i</a> ";
       }
   }

   if($page < $total_pages){
       $next = ($page + 1);
       echo "<a href=\"".$_SERVER['PHP_SELF']."?word=$theWord&type=title&page=$next\">Next>></a>";
       }
   }
   else{
        print "<p>The search failed to find the title <b>$theWord</b> in the Bungie Weekly Update story archive. Please make sure that the title is EXACT and the spelling is correct. Please try again or see the list of all the updates <a href=\"archive.php\">here</a>.</p>";
       }
   }
?>

Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

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.

 Share

×
×
  • 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.