Page 1 of 1

$_GET and address bar variables

Posted: Wed Nov 13, 2002 3:51 pm
by evilmonkey
Hello. I have this code:

Code: Select all

$start = $_GETї"id"];
and I want to add

Code: Select all

$category = $_GETї"category"];
and use it for:

Code: Select all

$sql = "SELECT id, title FROM userjokes WHERE category='.$category' LIMIT " .$start. ", " .$records; //notice category=''
In the address bar, it should look something like this:
something.php?id=1&category=string

The id is an integer and the category is a string. How would I do this, just put it in the code (look below), or do i have to add something else?

Here is the ORIGINAL CODE that works (just with $start (id))

Code: Select all

<HTML>
<body>
<script type="text/javascript"> 
function popup_all(id) 
{ 
window.open("archive2.php?id=" + id, "window_name", "0","0","0","0","yes","yes","200","300"); 
} 
</script>

<?php 
$db = mysql_connect("localhost","","");
mysql_select_db("",$db);;
$callsign = $HTTP_POST_VARSї'id']; 
$records = 10; // Number of records per page 
$start = $_GETї"id"]; // Get "id" value from address line  

if ($start == NULL) // No address line variable 
{ 
$start = 0; // Default value 
} 
$sql = "SELECT id, title FROM userjokes WHERE category='something' LIMIT " .$start. ", " .$records; 
$result=mysql_query ($sql, $db) or die($sql.' :'.mysql_error());
while ($row = mysql_fetch_object ($result)) 
{
 print("<td><a href="javascript:popup_all($row->id)">$row->title</a></td>"); 
} 

  // Add number of records per page to starting value to get records for next page 
  $next = $start + $records; 

  echo '<form method="GET" action="', $_SERVERї'PHP_SELF'], '">'; 
echo '<input type="hidden" name="id" value="', $next, '" />'; 

  print("<input type="submit" value="Next">"); 
  print("</form>"); 
?>
and here it is modified:

Code: Select all

<HTML>
<body>
<script type="text/javascript"> 
function popup_all(id) 
{ 
window.open("archive2.php?id=" + id, "window_name", "0","0","0","0","yes","yes","200","300"); 
} 
</script>

<?php 
$db = mysql_connect("localhost","","");
mysql_select_db("",$db);;
$callsign = $HTTP_POST_VARSї'id']; 
$records = 10; // Number of records per page 
$start = $_GETї"id"]; // Get "id" value from address line  
$category = $_GETї"category"];

if ($start == NULL) // No address line variable 
{ 
$start = 0; // Default value 
} 
$sql = "SELECT id, title FROM userjokes WHERE category='.$category' LIMIT " .$start. ", " .$records; 
$result=mysql_query ($sql, $db) or die($sql.' :'.mysql_error());
while ($row = mysql_fetch_object ($result)) 
{
 print("<td><a href="javascript:popup_all($row->id)">$row->title</a></td>"); 
} 

  // Add number of records per page to starting value to get records for next page 
  $next = $start + $records; 

  echo '<form method="GET" action="', $_SERVERї'PHP_SELF'], '">'; 
echo '<input type="hidden" name="id" value="', $next, '" />'; 

  print("<input type="submit" value="Next">"); 
  print("</form>"); 
?>
Would this work, and if not, how should I change it?

Thanks.

One thing

Posted: Wed Nov 13, 2002 5:22 pm
by musashi
Just one thing right off the bat I noticed:

Code: Select all

$sql = "SELECT id, title FROM userjokes WHERE category='.$category' LIMIT " .$start. ", " .$records;
You are saying '.$category' which should be '$category' You also might want to consider doing some string-length checks, if nothing else.

Next step is just to load it up and see if it runs :D

Posted: Wed Nov 13, 2002 5:38 pm
by evilmonkey
Hello.

Thanks for that suggestion. It works!

However, when I press next, to go to the next 10 rows, insted of:

script.php?category=something&id=something

it just gives me

script.php?id=something

I have a feeling it has something to do with this:

Code: Select all

// Add number of records per page to starting value to get records for next page 
  $next = $start + $records;  

  echo '<form method="GET" action="', $_SERVERї'PHP_SELF'], '">';  
echo '<input type="hidden" name="id" value="', $next, '" />';  

  print("<input type="submit" value="Next">");  
  print("</form>");
Any suggestions?

Thanks.

Posted: Wed Nov 13, 2002 6:02 pm
by Bill H
yep: you need to pass the $category as a hidden variable same as id.

Code: Select all

echo '<form method="GET" action="', $_SERVER&#1111;'PHP_SELF'], '">';   
  echo '<input type="hidden" name="id" value="', $next, '" />';   
  echo '<input type="hidden" name="category" value="', $category, '" />';   
  print("<input type="submit" value="Next">");   
  print("</form>");