[liphp] RE: Next Page Help

Alexander (Sasha) Mejenkov [email protected]
Sun, 19 Oct 2003 13:45:14 +0400


Hello Troy,

Here are changes you should do IMHO:

1) In the beginnig of your script you initiate variables:
$tempcat_name = $_GET[cat_name];
$_SESSION[cat_id] = $_GET[cat_id]; 
$_SESSION[tempcat_name] = $tempcat_name; 
$cat_id = $_SESSION[cat_id]; 
$page = $_GET[page];

Seems that quotes around cat_name, cat_id, and page are missed (because they
are not a predefined constants)
Code should look like:

$tempcat_name = $_GET['cat_name'];
$_SESSION['cat_id'] = $_GET['cat_id'];
$_SESSION['tempcat_name'] = $tempcat_name;
$cat_id = $_SESSION['cat_id'];
$page = $_GET['page'];

Remember: you don't need to use quotes only when you use these
variables in quoted string like
echo "$_GET[var_name]";

but you have to use quotes in other cases.

2) You assume that your script receives $_GET variables but you DON'T pass them
in your <a href="..."> tags
For example see your code
echo("<a href=\"$_SERVER[PHP_SELF]?page=$pageprev\">PREV</a> ");

Here you have only one parameter: $_GET['page']
You should add cat_id and cat_name as well, so you code should looke like

echo("<a
href=\"$_SERVER[PHP_SELF]?page=$pageprev&cat_id=$cat_id&cat_name=$cat_name\">PREV</a> ");

Same changes should be done for
echo("<a href=\"$_SERVER[PHP_SELF]?page=$i\">$i</a> ");
and
echo("<a href=\"$_SERVER[PHP_SELF]?page=$pagenext\">NEXT</a>");

3) Question. What happens when you call your script first time without
any parameters?
I think that you should foresee this situation and use something like:

if (isset($_GET['cat_name'])) {
   $tempcat_name = $_GET['cat_name'];
}
else {
     $tempcat_name = "some initial default value";
}
$_SESSION['tempcat_name'] = $tempcat_name;

if (isset($_GET['cat_id']) ) {
   $_SESSION['cat_id'] = (int) $_GET['cat_id'];
}
else {
    $_SESSION['cat_id'] = Some_default_ID;
}

$cat_id = $_SESSION['cat_id'];

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

Hope this helps

Sasha



Thursday, October 16, 2003, 6:09:23 PM, you wrote:

TT> Thank very much for the help.

TT> I did a search as you suggested i found an ok script and i've been able
TT> to make itsum what work with my code. 

TT> What happens is I get the "prev 1 2 3 4 5 next" and everything else to
TT> display correctly with out a problem on the first page however when I
TT> click one of the links I get this error: 

TT> Warning: mysql_num_rows(): supplied argument is not a valid MySQL result
TT> resource in view.php on line 18 
TT> Error: You have an error in your SQL syntax near 'limit 2,2' at line 1 

TT> The 'limit 2,2' changes to other numbers like 6,2 but i understand why
TT> that is.  

TT> here is the new code for the view.php: 
TT> Code:

TT> <? 
TT> session_start(); 
TT> $tempcat_name = $_GET[cat_name]; 
TT> $_SESSION[cat_id] = $_GET[cat_id]; 
TT> $_SESSION[tempcat_name] = $tempcat_name; 
TT> $cat_id = $_SESSION[cat_id]; 
TT> $page = $_GET[page]; 
TT> $limit = 2; 

TT> $db_name ="alpha"; 
TT> $connection = mysql_connect("****","****","*****") or
TT> die(mysql_error()); 
TT> $db = mysql_select_db($db_name,$connection) or die(mysql_error()); 

TT> $query_count = "SELECT userid, username, company, cat_name, pro_id,
TT> pro_name, pro_desc FROM user_table, category, products WHERE
TT> user_table.userid = products.user_id AND products.cat_id =
TT> category.cat_id AND category.cat_id = $cat_id"; 

TT> $result_count = mysql_query($query_count); 
TT> // Pulls what we want from the database 
TT> $totalrows = mysql_num_rows($result_count); 

TT> if(empty($page)){ 
TT> $page = 1; 
TT> } 

TT> $limitvalue = $page * $limit - ($limit); 

TT> $query = "SELECT * FROM user_table, category, products WHERE
TT> user_table.userid = products.user_id AND products.cat_id =
TT> category.cat_id AND category.cat_id = $cat_id limit $limitvalue,
TT> $limit"; 

TT> $result = mysql_query($query) or die("Error: " . mysql_error()); 


TT> if(mysql_num_rows($result) == 0){ 
TT> echo("Nothing to Display!"); 
TT> } 

TT> $bgcolor = "#efefef"; 

TT> while($row3 = mysql_fetch_array($result)){ 

TT> // Start The Background Check 
TT> if($bgcolor == "#e4e4e4"){ 

TT> $bgcolor = "#efefef"; 

TT> } else { 

TT> $bgcolor = "#e4e4e4"; 
TT> } 

TT> $company = $row3['company']; 
TT> $user_id = $row3['user_id']; 
TT> $cat_id = $row3['cat_id']; 
TT> $cat_name = $row3['cat_name']; 
TT> $pro_id = $row3['pro_id']; 
TT> $pro_name = $row3['pro_name']; 
TT> $pro_desc = $row3['pro_desc']; 
TT> $cutpro_desc = substr($pro_desc,0,75); 

TT> $option_block .= "<tr bgcolor=$bgcolor><td height=45
valign=top>><b>Company: </b>$company<br><b>Product Name: </b><a
TT> href=\"viewproduct.php?pro_id=$pro_id\">$pro_name</a> - $cutpro_desc
TT> ...</td><td width=5 ><input type=\"checkbox\"></td></tr>"; 

TT> } 

TT> if($page != 1){ 
TT> $pageprev = $page-1; 

TT> echo("<a href=\"$_SERVER[PHP_SELF]?page=$pageprev\">PREV</a> "); 

TT> } else { 

TT> echo("PREV "); 

TT> } 
TT> $numofpages = $totalrows / $limit; 

TT> for($i = 1; $i <= $numofpages; $i++) { 

TT> if($i == $page){ 

TT> echo($i." "); 

TT> } else { 

TT> echo("<a href=\"$_SERVER[PHP_SELF]?page=$i\">$i</a> "); 

TT> } 

TT> } 

TT> if(($totalrows % $limit) != 0) { 

TT> if($i == $page){ 

TT> echo($i." "); 

TT> } else { 

TT> echo("<a href=\"$_SERVER[PHP_SELF]?page=$i\">$i</a> "); 

TT> } 
TT> } 

TT> if(($totalrows - ($limit * $page)) > 0){ 

TT> $pagenext = $page +1; 

TT> echo("<a href=\"$_SERVER[PHP_SELF]?page=$pagenext\">NEXT</a>"); 

TT> } else { 

TT> echo("NEXT"); 
TT> } 
TT> mysql_free_result($result); 

?>> 
TT> TIA, 

TT> troinfo





-- 
Best regards,
 Alexander                            mailto:[email protected]