Jump to content


Photo

columned results with next previous


  • Please log in to reply
22 replies to this topic

#1 jerastraub

jerastraub
  • Members
  • PipPipPip
  • Advanced Member
  • 77 posts

Posted 28 September 2006 - 04:51 PM

I am trying to make product page that display 3 columns with 9 products per page. I also want it to display something like: <<prev 1 2 3 next >>>. But I want it to count the number of products. So if there are only 18 product in the category, it will only show << prev 1 2 next >> and on page 2 show the next 9 products.

Here is my code I keep getting an error of:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/w21510/public_html/pluswomen/shoescats.php on line 199

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/w21510/public_html/pluswomen/shoescats.php on line 204

and it shows all the products in the category below with no page numbers.

<? include("config.php"); ?><? // Sort products by Name as default
if(!$sortby){ $sortby="ProductName"; } ?>	

<?
if(!$start){
$start = 0;
}
if(!$limit){
$limit = $perpage;
}
$end=($start+$limit);

$query = "SELECT *
	    FROM womensproducts
        WHERE CategoryID='$cat'";
$result = mysql_query($query) or die("There was a problem with the SQL query: " . mysql_error()); 
if($result && mysql_num_rows($result) > 0)
{
    $i = 0;
    $max_columns = 3;
    while($row = mysql_fetch_array($result))        
   {
       // make the variables easy to deal with
       extract($row);

       // open row if counter is zero
       if($i == 0)
          echo "<tr>";

       // make sure we have a valid product
     // make sure we have a valid product
// if($product != "" && $product != null) // temporary comment out
   echo <<<HTML
    <td align=center><table class="quick" width="100%" height="350" border="0" align=center cellspacing="0" cellpadding="0">
  <tr>
    <td align=center><a href="http://pluswomen.thefreestuffvault.com/product-{$ProductID}.htm">
<img src="{$BigImage}" border="0" height=180 alt="{$Name}"></a></td>
  </tr>
  <tr>
    <td align=center><p id="productname"><a href="http://pluswomen.thefreestuffvault.com/product-{$ProductID}.htm">{$ProductName}</A></p></td>
  </tr>
  <tr>
    <td align=center><font size="2">Price: {$prodPrice}</font><font color=red> Now: ${$prodSale}</td>
  </tr>
</table></td>
HTML;
    
       // increment counter - if counter = max columns, reset counter and close row
       if(++$i == $max_columns) 
       {
           echo "</tr>";
           $i=0;
       }  // end if 
   } // end while
} // end if results

// clean up table - makes your code valid!
if($i < $max_columns)
{
    for($j=$i; $j<$max_columns;$j++)
        echo "";
}
$cat2 = urlencode($category);
$prv = ($start-$limit);
$nxt = ($start+$limit);
if($prv < 0){
$previous = "";
}else {
$previous = "<a href=\"http://pluswomen.thefreestuffvault.com/moreshoe-$cat2-$sortby-$prv-$limit.htm\"><<< Previous $limit Products</a>";
}
if(mysql_num_rows(mysql_query($sql)) < $nxt){
$next = "";
}else {
$next = "<a href=\"http://pluswomen.thefreestuffvault.com/moreshoe-$cat2-$sortby-$nxt-$limit.htm\">Next $limit Products >>></a>";
}
if(mysql_num_rows(mysql_query($sql)) > $limit){
echo "
<br>
<table border=1 bordercolor=#999999 align=center width=90% bgcolor=#ffffff cellpadding=3 cellspacing=0><TR>
<TD align=center>
<b>$previous</b> &nbsp; &nbsp; &nbsp; <b>$next</b>
</font></TR></TD></table><P>
";
}
mysql_free_result($result);
?>


#2 alpine

alpine
  • Members
  • PipPipPip
  • Advanced Member
  • 756 posts
  • LocationNorway

Posted 28 September 2006 - 05:19 PM

I can't see where you define $sql that mysql_num_rows() is checking - hense your two errors.
(mysql_num_rows(mysql_query($sql)) < $nxt)

It should probably be looking for $result.
(mysql_num_rows(mysql_query($result)) < $nxt)

Anyhow, you need to have a second query to select with limit and offset to get the pagination to work


$result= mysql_query("SELECT * FROM womensproducts WHERE CategoryID='$cat'") or die(mysql_error());
$result_main = mysql_query("SELECT * FROM womensproducts WHERE CategoryID='$cat' limit $start, $limit") or die(mysql_query());
if(mysql_num_rows($result_main) > 0)
{
    $i = 0;
    $max_columns = 3;
    while($row = mysql_fetch_array($result_main))
   {


and


if(mysql_num_rows($result) < $nxt){
$next = "";
}else {
$next = "<a href=\"http://pluswomen.thefreestuffvault.com/moreshoe-$cat2-$sortby-$nxt-$limit.htm\">Next $limit Products >>></a>";
}
if(mysql_num_rows($result) > $limit){



#3 jerastraub

jerastraub
  • Members
  • PipPipPip
  • Advanced Member
  • 77 posts

Posted 28 September 2006 - 05:30 PM

Even with changing this, i still get the same error. I have to be missing something here.

#4 alpine

alpine
  • Members
  • PipPipPip
  • Advanced Member
  • 756 posts
  • LocationNorway

Posted 28 September 2006 - 05:54 PM

Did you get my edit before you tested ?

#5 jerastraub

jerastraub
  • Members
  • PipPipPip
  • Advanced Member
  • 77 posts

Posted 28 September 2006 - 08:46 PM

Hey Alpine,

I added the code you posted and now i get a blank page, no errors no results. :(

<? include("config.php"); ?><? // Sort products by Name as default
if(!$sortby){ $sortby="ProductName"; } ?>	

<?
if(!$start){
$start = 0;
}
if(!$limit){
$limit = $perpage;
}
$end=($start+$limit);

$query = "SELECT *
	    FROM womensproducts
        WHERE CategoryID='$cat'";
$result= mysql_query("SELECT * FROM womensproducts WHERE CategoryID='$cat'") or die(mysql_error());
$result_main = mysql_query("SELECT * FROM womensproducts WHERE CategoryID='$cat' limit $start, $limit") or die(mysql_query());
if(mysql_num_rows($result_main) > 0)
{
    $i = 0;
    $max_columns = 3;
    while($row = mysql_fetch_array($result_main))
   {
       // make the variables easy to deal with
       extract($row);

       // open row if counter is zero
       if($i == 0)
          echo "<tr>";

       // make sure we have a valid product
     // make sure we have a valid product
// if($product != "" && $product != null) // temporary comment out
   echo <<<HTML
    <td align=center><table class="quick" width="100%" height="350" border="0" align=center cellspacing="0" cellpadding="0">
  <tr>
    <td align=center><a href="http://pluswomen.thefreestuffvault.com/product-{$ProductID}.htm">
<img src="{$BigImage}" border="0" height=180 alt="{$Name}"></a></td>
  </tr>
  <tr>
    <td align=center><p id="productname"><a href="http://pluswomen.thefreestuffvault.com/product-{$ProductID}.htm">{$ProductName}</A></p></td>
  </tr>
  <tr>
    <td align=center><font size="2">Price: {$prodPrice}</font><font color=red> Now: ${$prodSale}</td>
  </tr>
</table></td>
HTML;
    
       // increment counter - if counter = max columns, reset counter and close row
       if(++$i == $max_columns) 
       {
           echo "</tr>";
           $i=0;
       }  // end if 
   } // end while
} // end if results

// clean up table - makes your code valid!
if($i < $max_columns)
{
    for($j=$i; $j<$max_columns;$j++)
        echo "";
}
$cat2 = urlencode($category);
$prv = ($start-$limit);
$nxt = ($start+$limit);
if($prv < 0){
$previous = "";
}else {
$previous = "<a href=\"http://pluswomen.thefreestuffvault.com/moreshoe-$cat2-$sortby-$prv-$limit.htm\"><<< Previous $limit Products</a>";
}
if(mysql_num_rows($result) < $nxt){
$next = "";
}else {
$next = "<a href=\"http://pluswomen.thefreestuffvault.com/moreshoe-$cat2-$sortby-$nxt-$limit.htm\">Next $limit Products >>></a>";
}
if(mysql_num_rows($result) > $limit){
echo "
<br>
<table border=1 bordercolor=#999999 align=center width=90% bgcolor=#ffffff cellpadding=3 cellspacing=0><TR>
<TD align=center>
<b>$previous</b> &nbsp; &nbsp; &nbsp; <b>$next</b>
</font></TR></TD></table><P>
";
}
mysql_free_result($result);
?>


#6 alpine

alpine
  • Members
  • PipPipPip
  • Advanced Member
  • 756 posts
  • LocationNorway

Posted 28 September 2006 - 10:00 PM

i (did it again) wrote wrong error check in the $result_main, should be "or die(mysql_error());"
See if that gives anything.....

Your $perpage is defined elsewhere i assume ?
Also you are running relying on register globals to be ON, but i assume this is your current php setup

You can ditch the $query since that is not used, replaced by $result

#7 jerastraub

jerastraub
  • Members
  • PipPipPip
  • Advanced Member
  • 77 posts

Posted 28 September 2006 - 10:14 PM

I changed the sql error statement. Also yes my $perpage is defined in my onfig.php which is set to 7. Also I checked phpinfo and my current setting is indeed register_globals On.

I also removed
$query = "SELECT *
	    FROM womensproducts
        WHERE CategoryID='$cat'";

from
$query = "SELECT *
	    FROM womensproducts
        WHERE CategoryID='$cat'";
$result= mysql_query("SELECT * FROM womensproducts WHERE CategoryID='$cat'") or die(mysql_error());
$result_main = mysql_query("SELECT * FROM womensproducts WHERE CategoryID='$cat' limit $start, $limit") or die(mysql_error());

And I am still only getting a blank page :(

I use:

<?php
include ("config.php");

?>
<?php
$query = "SELECT *
	    FROM womensproducts
        WHERE CategoryID='9272' limit 9";
$result = mysql_query($query) or die("There was a problem with the SQL query: " . mysql_error()); 
if($result && mysql_num_rows($result) > 0)
{
    $i = 0;
    $max_columns = 3;
    while($row = mysql_fetch_array($result))        
   {
       // make the variables easy to deal with
       extract($row);

       // open row if counter is zero
       if($i == 0)
          echo "<tr>";

       // make sure we have a valid product
     // make sure we have a valid product
// if($product != "" && $product != null) // temporary comment out
   echo <<<HTML
    <td align=center><table class="quick" width="100%" height="350" border="0" align=center cellspacing="0" cellpadding="0">
  <tr>
    <td align=center><a href="http://pluswomen.thefreestuffvault.com/product-{$ProductID}.htm">
<img src="{$BigImage}" border="0" height=180 alt="{$Name}"></a></td>
  </tr>
  <tr>
    <td align=center><p id="productname"><a href="http://pluswomen.thefreestuffvault.com/product-{$ProductID}.htm">{$ProductName}</A></p></td>
  </tr>
  <tr>
    <td align=center><font size="2">Price: {$prodPrice}</font><font color=red> Now: ${$prodSale}</td>
  </tr>
</table></td>
HTML;
    
       // increment counter - if counter = max columns, reset counter and close row
       if(++$i == $max_columns) 
       {
           echo "</tr>";
           $i=0;
       }  // end if 
   } // end while
} // end if results

// clean up table - makes your code valid!
if($i < $max_columns)
{
    for($j=$i; $j<$max_columns;$j++)
        echo "";
}
 ?>
for just displaying 9 products in 3 column format, but when I start adding code for the:
<< prev 1 2 3 next >>>

that is when i start having problems.

#8 jerastraub

jerastraub
  • Members
  • PipPipPip
  • Advanced Member
  • 77 posts

Posted 29 September 2006 - 04:02 AM

Anyone have any ideas, I have been trying to troubleshoot this and nothing!

#9 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 29 September 2006 - 09:26 AM

Can you provide me with the full code for the page you're trying to paginate, and also a list of variable names and values that you're getting from the 'included' config file?

Let's help you get this cleared up for you :D

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#10 jerastraub

jerastraub
  • Members
  • PipPipPip
  • Advanced Member
  • 77 posts

Posted 29 September 2006 - 02:19 PM

Okay, Here's my page code:

<? include("config.php"); ?><? // Sort products by Name as default
if(!$sortby){ $sortby="ProductName"; } ?>

	

<?
if(!$start){
$start = 0;
}
if(!$limit){
$limit = $perpage;
}
$end=($start+$limit);

$result= mysql_query("SELECT * FROM womensproducts WHERE CategoryID='$cat'") or die(mysql_error());
$result_main = mysql_query("SELECT * FROM womensproducts WHERE CategoryID='$cat' limit $start, $limit") or die(mysql_error());
if(mysql_num_rows($result_main) > 0)
{
    $i = 0;
    $max_columns = 3;
    while($row = mysql_fetch_array($result_main))
   {
       // make the variables easy to deal with
       extract($row);

       // open row if counter is zero
       if($i == 0)
          echo "<tr>";

       // make sure we have a valid product
     // make sure we have a valid product
// if($product != "" && $product != null) // temporary comment out
   echo <<<HTML
    <td align=center><table class="quick" width="100%" height="350" border="0" align=center cellspacing="0" cellpadding="0">
  <tr>
    <td align=center><a href="http://pluswomen.thefreestuffvault.com/product-{$ProductID}.htm">
<img src="{$BigImage}" border="0" height=180 alt="{$Name}"></a></td>
  </tr>
  <tr>
    <td align=center><p id="productname"><a href="http://pluswomen.thefreestuffvault.com/product-{$ProductID}.htm">{$ProductName}</A></p></td>
  </tr>
  <tr>
    <td align=center><font size="2">Price: {$prodPrice}</font><font color=red> Now: ${$prodSale}</td>
  </tr>
</table></td>
HTML;
    
       // increment counter - if counter = max columns, reset counter and close row
       if(++$i == $max_columns) 
       {
           echo "</tr>";
           $i=0;
       }  // end if 
   } // end while
} // end if results

// clean up table - makes your code valid!
if($i < $max_columns)
{
    for($j=$i; $j<$max_columns;$j++)
        echo "";
}
$cat2 = urlencode($category);
$prv = ($start-$limit);
$nxt = ($start+$limit);
if($prv < 0){
$previous = "";
}else {
$previous = "<a href=\"http://pluswomen.thefreestuffvault.com/moreshoe-$cat2-$sortby-$prv-$limit.htm\"><<< Previous $limit Products</a>";
}
if(mysql_num_rows($result) < $nxt){
$next = "";
}else {
$next = "<a href=\"http://pluswomen.thefreestuffvault.com/moreshoe-$cat2-$sortby-$nxt-$limit.htm\">Next $limit Products >>></a>";
}
if(mysql_num_rows($result) > $limit){
echo "
<br>
<table border=1 bordercolor=#999999 align=center width=90% bgcolor=#ffffff cellpadding=3 cellspacing=0><TR>
<TD align=center>
<b>$previous</b> &nbsp; &nbsp; &nbsp; <b>$next</b>
</font></TR></TD></table><P>
";
}
mysql_free_result($result);
?>

This is my config.php:

<?
// How Many Products Do You Want To Show Per Page On Your (Category Pages) Pages?
$perpage = "7";

// Do You Want Your Links To Open In a New Window When Visitors Leave Your Site?
$target = "blank"; //Change to: "top"; to stay in the same browser 

// Do Not Edit Below This Line !!!
$dbhost = "localhost";
$dbuser = "w21510_jerastrau";
$dbname = "w21510_allthefreestuffvaultsubdomain";
$dbpass = "spunkee1";
$db = mysql_connect("$dbhost","$dbuser","$dbpass");
mysql_select_db("$dbname",$db);
?>

Varaibles I need to pulled for each product are:

$prodSale
$ProductID
$ProductName
$prodPrice
$BigImage
$Name

I am trying to get a display of 3 columns wide by 3 rows. With navigation of << prev 1 2 next >>>, which would change depending on how many products are in that category.



#11 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 29 September 2006 - 03:17 PM

I've almost finished the code and it looks good, but I have a question, why if you want three rows of three (nine in total) does your $perpage variable show a value of 7...  lol

I'd recoment changing $perpage to a value of max_row * max_col, and I'll include that in my code.

Also, I'm trying a few different methods, can you possibly let me know the order your columns are in in the database?

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#12 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 29 September 2006 - 05:56 PM

Give this a try and let me know how you get on.  It was written on the fly and as such is untested, so don't dispair if it doesn't work, just let me know what the error is...

There's a commented line in there where I need you to add a default category.

<?php
include("config.php");

// Sort products by Name as default
//if(!$sortby){ $sortby="ProductName"; }  // Commented out as it's not used anywhere, it was incorrect in your links

// Select total results for pagination
$result = mysql_query("SELECT count(*) FROM womensproducts WHERE CategoryID = '$cat'");
$num_records = mysql_result($result,0,0);

// Set maximum number of rows and columns
$max_num_rows = 3;
$max_num_columns = 3;
$per_page = $max_num_columns * $max_num_rows;

// Work out how many pages there are
$total_pages = ceil($num_records / $per_page);

// Get the current page number
if (isset($_GET['page'])) $page = $_GET['page']; else $page = 1;

// Get the category or assign a defailt
if (isset($_GET['cat'])) $cat = $_GET['cat']; else $cat = "default"; // enter a default category here

// Work out the limit offset
$start = ($page - 1) * $per_page;

// Run your query with the limit and offset in place
$result_main = mysql_query("SELECT * FROM womensproducts WHERE CategoryID='$cat' limit $start, $per_page") or die(mysql_error());
$num_columns = ceil(mysql_num_rows($result_main)/$max_num_rows);
$num_rows = ceil(mysql_num_rows($result_main)/$num_columns);

// Lets start creating tables and echoing code
echo "<table>\n";

$c = 0;
while($row = mysql_fetch_array($result_main)){
   
   // make the variables easy to deal with
   extract($row);

   // open row if counter is zero
   if($c == 0){
      echo "<tr>";
   }
   
   // echo the individual cells
   echo <<<HTML
    <td align=center><table class="quick" width="100%" height="350" border="0" align=center cellspacing="0" cellpadding="0">
  <tr>
    <td align=center><a href="http://pluswomen.thefreestuffvault.com/product-{$ProductID}.htm">
<img src="{$BigImage}" border="0" height=180 alt="{$Name}"></a></td>
  </tr>
  <tr>
    <td align=center><p id="productname"><a href="http://pluswomen.thefreestuffvault.com/product-{$ProductID}.htm">{$ProductName}</A></p></td>
  </tr>
  <tr>
    <td align=center><font size="2">Price: {$prodPrice}</font><font color=red> Now: ${$prodSale}</td>
  </tr>
</table></td>
HTML;

   // increment counter - if counter = max columns, reset counter and close row
   if(++$c == $max_columns){
      echo "</tr>";
      $c = 0;
   }
}

// clean up table - makes your code valid!
if($c < $max_columns){
   for($e = $c; $e < $max_columns; $e++){
      echo "&nbsp;";
   }
}

// If there's more than one page, show links
if ($total_pages > 1){
   echo <<<HTML
   <table>
     <tr>
       <td align="center">Please select a page</td>
     </tr>
     <tr>
       <td align="center">
HTML;

   // Build the previous link
   if ($page > 1){
      $prev = ($page -1);
      echo "<a href=\"{$_SERVER['PHP_SELF']}?cat=$cat&page=$prev\">&lt;&lt; Previous</a> ";
   }
   
   // Build the page numbers
   for($i = 1; $i <= $total_pages; $i++){
      if($page == $i){
         echo "$i ";
      }
      else {
         echo "<a href=\"{$_SERVER['PHP_SELF']}?cat=$cat&page=$i\">$i</a> ";
      }
   }
   
   // Build the next link
   if ($page < $total_pages){
      $next = ($page +1);
      echo "<a href=\"{$_SERVER['PHP_SELF']}?cat=$cat&page=$prev\">Next &gt;&gt;</a>";
   }
   
   // Close off the links
   echo <<<HTML
       </td>
     </tr>
   </table>
HTML;
}
?>

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#13 jerastraub

jerastraub
  • Members
  • PipPipPip
  • Advanced Member
  • 77 posts

Posted 29 September 2006 - 06:54 PM

Huggybear,

It almost works,

Here is the page that i put the code, so you can see what it is displaying. http://pluswomen.the...2.html?cat=9245

I also made a txt version so you can see the code behind:

http://pluswomen.the...columns2php.txt

It just displays the products in one row.

After changing :
echo "<a href=\"{$_SERVER['PHP_SELF']}?cat=$cat&page=$prev\">Next &gt;&gt;</a>";

to
echo "<a href=\"{$_SERVER['PHP_SELF']}?cat=$cat&page=$next\">Next &gt;&gt;</a>";

the next feature worked perfectly.

All that needs to be found out it it to get it in 3 columns and 3 rows!

Also the LIMIT 7 was a typo on my part, sry if that made it confusing.


#14 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 29 September 2006 - 07:03 PM

ok, try changing this...
   // increment counter - if counter = max columns, reset counter and close row
   if(++$c == $max_columns){
      echo "</tr>";
      $c = 0;
   }
}

// clean up table - makes your code valid!
if($c < $max_columns){
   for($e = $c; $e < $max_columns; $e++){
      echo "&nbsp;";
   }
}

To this:
   // increment counter - if counter = max columns, reset counter and close row
   if(++$c == $max_num_columns){
      echo "</tr>";
      $c = 0;
   }
}

// clean up table - makes your code valid!
if($c < $max_num_columns){
   for($e = $c; $e < $max_num_columns; $e++){
      echo "&nbsp;";
   }
}

Did you see that by adding in a default page and category, you can go direct to http://pluswomen.the...stcolumns2.html without any variables in the url and still get a valid page up.

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#15 jerastraub

jerastraub
  • Members
  • PipPipPip
  • Advanced Member
  • 77 posts

Posted 29 September 2006 - 07:23 PM

Your, awesome Huggybear.

I actually use mod_rewrites to get rid of the ?cat=4644.

Where my url's actually look like

http://pluswomen.the...othing-9250.htm

which would normally be http://pluswomen.the...s.php?cat=9250.

Thanks so much.

I finally don't have to just settle for a page nav of just

<<< Previous 7 Products      Next 7 Products >>>. This isn't customer friendly cause they don't know how many products their are.

Also I have another question about getting the nav on top and bottom of the results.

Kinda like:

<< Previous 1 2 3 4 Next >>

Picture      Picture      Picture

Picture      Picture      Picture

Picture      Picture      Picture

<< Previous 1 2 3 4 Next >>

is this a deficult task?


#16 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 29 September 2006 - 07:28 PM

Not at all, just paste this...

// If there's more than one page, show links
if ($total_pages > 1){
   echo <<<HTML
   <table>
     <tr>
       <td align="center">Please select a page</td>
     </tr>
     <tr>
       <td align="center">
HTML;

   // Build the previous link
   if ($page > 1){
      $prev = ($page -1);
      echo "<a href=\"{$_SERVER['PHP_SELF']}?cat=$cat&page=$prev\">&lt;&lt; Previous</a> ";
   }
   
   // Build the page numbers
   for($i = 1; $i <= $total_pages; $i++){
      if($page == $i){
         echo "$i ";
      }
      else {
         echo "<a href=\"{$_SERVER['PHP_SELF']}?cat=$cat&page=$i\">$i</a> ";
      }
   }
   
   // Build the next link
   if ($page < $total_pages){
      $next = ($page +1);
      echo "<a href=\"{$_SERVER['PHP_SELF']}?cat=$cat&page=$prev\">Next &gt;&gt;</a>";
   }
   
   // Close off the links
   echo <<<HTML
       </td>
     </tr>
   </table>
HTML;
}

Between these two line...

$num_rows = ceil(mysql_num_rows($result_main)/$num_columns);

<---- Here

// Lets start creating tables and echoing code

Regards
Huggie


Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#17 jerastraub

jerastraub
  • Members
  • PipPipPip
  • Advanced Member
  • 77 posts

Posted 29 September 2006 - 08:06 PM

I figured it out, Tks again huggy, you da man.

#18 jerastraub

jerastraub
  • Members
  • PipPipPip
  • Advanced Member
  • 77 posts

Posted 29 September 2006 - 09:45 PM

Okay,

Found one problem. I rely on the $cat value to populate my pages, let's say I don't have any products in that category at this time and don't want to display a default value as this would confuse a customer.

I get a "Division by zero" warning rather than it displaying:

No Products In This Category - Please try another category!

How do I fix this issue, as my datafeeds are downloaded everyday and one day there may be a product in a certain category and the next day none?

I was thinking of adding something like :


if(mysql_num_rows(mysql_query($sql)) < 1){
echo "No Products In This Category - Please try another category!";
}
 else {
         $num_records = mysql_result($result,0,0);
		 
      }




}

right before

// Select total results for pagination
$result = mysql_query("SELECT count(*) FROM womensproducts WHERE CategoryID = '$cat'");

but this give even more errors, but does display "No Products In This Category - Please try another category!"


I even tried adding:

$sql = "SELECT *
	    FROM womensproducts
        WHERE CategoryID='$cat'
	   ";
	   
if(mysql_num_rows(mysql_query($sql)) < 1){
echo "No Products In This Category - Please try another category!";

}

 else {
         $result = mysql_query("SELECT count(*) FROM womensproducts WHERE CategoryID = '$cat'");
		 
      }





#19 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 29 September 2006 - 11:22 PM

ok, if you don't want to display a default category, then maybe something like this at the top of the page...

<?php
if (empty($_GET['cat'])){
   header("Location: index.php");
}
?>

This will redirect them to index.php if they don't provide a category, and we can do something else for if they provide a category, but there's no items in it.

At the top of your page, insert this:
if ($num_records == 0){
   echo "There are no records to display in the selected category, please select another\n";
   exit;
}

Between these lines of code:
$num_records = mysql_result($result,0,0);

<--- Insert here

// Set maximum number of rows and columns

This isn't very tidy or helpful, but it gives you the bare bones to add the flesh too ;)

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#20 jerastraub

jerastraub
  • Members
  • PipPipPip
  • Advanced Member
  • 77 posts

Posted 29 September 2006 - 11:36 PM

wow,

I can't believe I didn't think of just adding the

exit;
at the end. Sometime it's the little things that get over looked!

Thanks again Huggy!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users