Jump to content

Count Question


stackumhi

Recommended Posts

Hi All,

The following code works but for some reason it is leaving 1 result off of each page when displaying results from DB. I assume there is something wrong with the count but I cannot find the problem.

 

Thank you for any help.

 


<?php   
         
$page = $_GET['page'];

$result2 = mysql_query("SELECT * FROM vendors WHERE vendor_type_id = '$vdrt' AND active ='Yes' ORDER BY RAND()");
$total_results = mysql_num_rows($result2); 
   
if (empty($total_results)) {echo "Sorry, but we do not have any results for that search at this time. <br/><br/><a href='index.php'>Please try your search again</a>";} 

else {

$total_pages = ceil($total_results / $results_limit); // $results_limit is set to 7 in config file
if (empty($page)) 
    { 
        $page = "1"; 
    } 
$offset = ($page - 1) * $results_limit;

$result = mysql_query("SELECT * FROM vendors WHERE vendor_type_id = '$vdrt' AND active ='Yes' LIMIT $offset, $results_limit") or die ("$DatabaseError"); 

$row = mysql_fetch_assoc($result);
$vendor_type_id = $row['vendor_type_id'];
$num = mysql_numrows($result); 
  if ($num == 0) : 
  print "<br><br><p>Sorry, no vendors could be found.</p><br><br>"; 
  
  elseif ($num > 0) :

echo "Page - "; 
if ($page != 1) 
{ 
echo "<a href=$PHP_SELF?city=Orlando&state=FL&vdrt=$vendor_type_id&page=1><< First</a>   "; 
$prevpage = $page - 1; 
} 
        if ($page == $total_pages) 
            { 
                  $to = $total_pages; 
            } 
        elseif ($page == $total_pages-1) 
            { 
                  $to = $page+1; 
            } 
        elseif ($page == $total_pages-2) 
            { 
                  $to = $page+2; 
            } 
        else 
            { 
                  $to = $page+3; 
            } 
        if ($page == 1 || $page == 2 || $page == 3) 
            { 
                  $from = 1; 
            } 
        else 
            { 
                  $from = $page-3; 
            } 
             
for ($i = $from; $i <= $to; $i++) 

    { 
    if ($i == $total_results) $to=$total_results; 
    if ($i != $page) 
        { 
        echo "<a href=$PHP_SELF?city=Orlando&state=FL&vdrt=$vendor_type_id&showold=yes&page=$i>$i</a>"; 
        } 
    else 
        { 
        echo "<b><span class='current-page'>[$i]</span></b>";		

        } 
    if ($i != $total_pages) 
        echo " "; 
    } 
if ($page != $total_pages) 
{ 
$nextpage = $page + 1; 

echo "   <a href=$PHP_SELF?city=Orlando&state=FL&vdrt=$vendor_type_id&page=$total_pages>Last >></a>";
}
if ($num == 0)  { echo "<br><br>";} else {echo "<br><br>";}

    $i = 0; 
    while ($i < $num) : 

        while ($row = mysql_fetch_assoc($result)) {
            $vdrid = $row['vendor_id'];
		$vendor_name = $row['vendor_name'];
		$vendor_name_enc = urlencode($row['vendor_name']);
		$vendor_city = $row['vendor_city'];
            $vendor_state = $row['vendor_state'];
		$vendor_phone = $row['vendor_phone'];
		$vendor_overview = $row['vendor_overview'];

		// Trim overview
		if(strlen($vendor_overview) > 385) {$trimed_vendor_overview = substr($vendor_overview,0,385) . "...";
		} else {
		$trimed_vendor_overview = $vendor_overview;
		}

            echo "<strong><a href='listing.php?$vendor_name_enc-$vendor_city-$vendor_state-&vdrid=$vdrid'>$vendor_name</a> </strong> - $vendor_city, $vendor_state $vendor_phone<br/>";           	
		echo "$trimed_vendor_overview<br/><br/>";
        }


        $i++; 
    endwhile; 

  endif;
  

}

?>

Link to comment
Share on other sites

The line I commented below moves the data pointer to the second record returned from the database.

$result = mysql_query("SELECT * FROM vendors WHERE vendor_type_id = '$vdrt' AND active ='Yes' LIMIT $offset, $results_limit") or die ("$DatabaseError"); 

$row = mysql_fetch_assoc($result); // <---- THIS ONE HERE
$vendor_type_id = $row['vendor_type_id'];

 

It looks as though that line is necessary for your page to function as desired, so rather than run the query again, you can simply move the pointer back to the first record before entering the while() loop, using mysql_data_seek(). I've only used it a handful of times, but I believe I've got the syntax correct. Try it and see what happens.

 

$i = 0;
mysql_data_seek($result, 0); // <---- HERE
while ($i < $num) : 
        while ($row = mysql_fetch_assoc($result)) {

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.

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