Pagination (AFTER visiting tutorials!)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cdickson
Forum Contributor
Posts: 120
Joined: Mon Apr 05, 2004 1:30 pm
Location: Michigan, USA

Pagination (AFTER visiting tutorials!)

Post by cdickson »

Based on information I found in this forum I have checked out a number of pagination tutorials, but I can't quite get mine to work.

I finally ended up athttp://www.phpfreaks.com/tutorials/43/0.php and copied the code. My PHP code now reads as follows:

Code: Select all

<?php
<?php 
foreach ($_GET as $key => $value) { 
   $_GET[$key] = trim($value); 
} 

$limit = 10;                
    // Sets how many results shown per page
$search_name = (!empty($_GET['member_name'])) ? $_GET['member_name'] : ''; 
mysql_select_db($database_hschamber, $hschamber); 
$sql = "SELECT member_name, member_address1, member_address2, member_city, member_state, member_zip, member_phone, member_fax, member_email, contact_name, web_address FROM Members WHERE member_name LIKE '%$search_name%'"; 
$result_count = mysql_query($query_count);  
// Pulls what we want from the database 
$totalrows = mysql_num_rows($result_count);
if(empty($page)){    // Checks if the $page variable is empty (not set) 
        $page = 1;      // If it is empty, we're on page 1 
    } 

$limitvalue = $page * $limit - ($limit); 
    // Ex: (2 * 25) - 25 = 25 <- data starts at 25 
     
$query  = "SELECT * FROM Members LIMIT $limitvalue, $limit";         
$result = mysql_query($query) or die("Error: " . mysql_error()); 
    // Selects all the data from table. 
    // mysql_error() will print an error if one occurs. 
     
    /* Tip: The MySQL LIMIT value syntax is as follows: 
    LIMIT $row_to_start_at, $how_many_rows_to_return 
    */ 
	if(mysql_num_rows($result) == 0){ 
        echo("Nothing to Display!"); 
    } 
    // This reads the number of rows returned 
    // from the result above. 

?>
When I test the page, I get the following error message:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /path/searchNameTEST.php on line 14
Line 14 is:

Code: Select all

$totalrows = mysql_num_rows($result_count);
but I can't figure out why it is not a valid argument. :?:
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post by Steveo31 »

I don't see where $result_count is. Maybe that and $query_count should be $sql?
phait
Forum Commoner
Posts: 46
Joined: Wed Apr 07, 2004 4:41 am
Location: watford / leicester, UK

Post by phait »

hi,
I agree with steve. The '$query_count' var should be '$sql' to get this to work. At present you are passing a non-existent variable to the mysql_query function so it will fail:

Code: Select all

mysql_select_db($database_hschamber, $hschamber); 
$sql = "SELECT member_name, member_address1, member_address2, member_city, member_state, member_zip, member_phone, member_fax, member_email, contact_name, web_address FROM Members WHERE member_name LIKE '%$search_name%'"; 
$result_count = mysql_query($query_count);  
// Pulls what we want from the database 
$totalrows = mysql_num_rows($result_count);
should be:

Code: Select all

mysql_select_db($database_hschamber, $hschamber); 
$sql = "SELECT member_name, member_address1, member_address2, member_city, member_state, member_zip, member_phone, member_fax, member_email, contact_name, web_address FROM Members WHERE member_name LIKE '%$search_name%'"; 
$result_count = mysql_query($sql); //  <---change here to use $sql var
// Pulls what we want from the database 
$totalrows = mysql_num_rows($result_count);
Unless you have somehow defined the $query_count var further up in the script.

You also have two starting php tags which may or may not just have been a paste problem, but there should only be one.

Code: Select all

<?php 
<?php 
foreach ($_GET as $key => $value) &#123; 
   $_GET&#1111;$key] = trim($value); 
&#125; 
......

hth
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

I believe it's a pasting issue. Hitting "PHP" in the post reply page generates a <?php
Post Reply