Hi everyone, I am like so proud of myself because I am building a php web page. I get stuck now and then, but eventually get it after two or three days:(

I have one big problem that I have put on the back burner for a while and I have been focusing on my other bugs and issues.

First I will explain how the page initially look and how I made it and my main goal for the look.
1. first the page images was vertical (position goin downward)
2. Then I position it going horizontal (left to right) **which is good I wanted it to go to left to right, but I wanted 3 images per line/row on each page. Not three image per page.

When I increased the record per page everything is going horizontal(all the way to the right).

Is there any way that I can changed my code to display the images still horizontal but 3 image per row, 6 images per page. Like this below?

image image image
image image image
image image image
next page


Thank you in advance

<?php

   function showproducts($catid, $page, $currentpage, $newpage)
   {
      $query = "Select count(prodid) from products where catid = $catid";
      $result = mysql_query($query);
      $row = mysql_fetch_array($result);
      if ($row[0] == 0)
	  
      {
         echo "<h2><br>Sorry, there are no products in this category</h2>\n";
      }
      else
      {
         $thispage = $page;
         $totrecords = $row[0];
         $recordsperpage = 3;
         $offset = ($thispage - 1) * $recordsperpage;
         $totpages = ceil($totrecords / $recordsperpage);
         echo "<table width=\"100%\" cellpadding=\"1\" border=\"0\"><tr>\n";
         $query = "SELECT * from products WHERE catid=$catid LIMIT $offset,$recordsperpage";
         $result = mysql_query($query); 
         while($row=mysql_fetch_array($result, MYSQL_ASSOC))
         {
            $prodid = $row['prodid'];
            $description = $row['description'];
            $price = $row['price'];
            $quantity = $row['quantity'];
            $onsale = $row['onsale'];

            echo "<td>\n";
               echo "<img src=\"showimage.php?id=$prodid\" width=\"150\" height=\"150\">\n";

			     echo "<a href=\"$newpage&id=$prodid\">$description\n";
               echo "$" . $price . "\n";

            if ($onsale)
               echo "On sale!\n";
            else
               echo "&nbsp;\n";
            echo "</td>\n";
          
         }
         echo "</tr></table>\n";   // Code to implement paging
         if ($thispage > 1)
         {
            $page = $thispage - 1;
            $prevpage = "<a href=\"$currentpage&cat=$catid&page=$page\">Previous page</a>";
         } else
         {
            $prevpage = " ";
         }         if ($thispage < $totpages)
         {
            $page = $thispage + 1;
            $nextpage = " <a href=\"$currentpage&cat=$catid&page=$page\">Next page</a>";
         } else
         {
            $nextpage = " ";
         }         if ($totpages > 1)
            echo $prevpage . "  " . $nextpage;      }
   }

?>

Recommended Answers

All 9 Replies

Try the following:

<?php

   function showproducts($catid, $page, $currentpage, $newpage)
   {
      $query = "Select count(prodid) from products where catid = $catid";
      $result = mysql_query($query);
      $row = mysql_fetch_array($result);
      if ($row[0] == 0)
	  
      {
         echo "<h2><br>Sorry, there are no products in this category</h2>\n";
      }
      else
      {
         $thispage = $page;
         $totrecords = $row[0];
         $recordsperpage = 3;
         $offset = ($thispage - 1) * $recordsperpage;
         $totpages = ceil($totrecords / $recordsperpage);
         echo "<table width=\"100%\" cellpadding=\"1\" border=\"0\">\n";
         $query = "SELECT * from products WHERE catid=$catid LIMIT $offset,$recordsperpage";
         $result = mysql_query($query); 
         $var=1;
         while ($var=mysql_fetch_array($result, MYSQL_ASSOC))
         {
         unset($var);
         echo '<tr>';
         for ($x=0; $x<4, $row=mysql_fetch_array($result, MYSQL_ASSOC) ; $x++) {
            $prodid = $row['prodid'];
            $description = $row['description'];
            $price = $row['price'];
            $quantity = $row['quantity'];
            $onsale = $row['onsale'];

            echo "<td>\n";
               echo "<img src=\"showimage.php?id=$prodid\" width=\"150\" height=\"150\">\n";

			     echo "<a href=\"$newpage&id=$prodid\">$description\n";
               echo "$" . $price . "\n";

            if ($onsale)
               echo "On sale!\n";
            else
               echo "&nbsp;\n";
            echo "</td>\n";
            }
         echo '</tr>';
         $var=$row;
         }
         echo "</table>\n";   // Code to implement paging
         if ($thispage > 1)
         {
            $page = $thispage - 1;
            $prevpage = "<a href=\"$currentpage&cat=$catid&page=$page\">Previous page</a>";
         } else
         {
            $prevpage = " ";
         }         if ($thispage < $totpages)
         {
            $page = $thispage + 1;
            $nextpage = " <a href=\"$currentpage&cat=$catid&page=$page\">Next page</a>";
         } else
         {
            $nextpage = " ";
         }         if ($totpages > 1)
            echo $prevpage . "  " . $nextpage;      }
   }

?>

I thank yo Cwarn23, I tried the code and unfortunately it did not work. It came out exactly like the result I had. The only difference is that it minus by one, the amount of item shown on the page.

I am trying the code below, but I am getting undefine variable for $table and my content for row on the $table .= "<td>{$row}</td>"; line I think is wrong. Because it mark as undefine
This is the code that I am now trying from Keith. Knowing me I am screwing up something. The other guy from the other thread successfully made his page work just like how I want mines to work. But I don't know what I am doing wrong. I even found out by messing aournd with Cwarn and Keith code that my table I had thought was going from left to right I made it wrong. So now I have another problem because it is going downward: vertically. But I think it might correct itself if the page is set up for the images to display as:
image image image
image image image
image image
next page

<?php

   function showproducts($catid, $page, $currentpage, $newpage)
   {
      $query = "Select count(prodid) from products where catid = $catid";
      $result = mysql_query($query);
      $row = mysql_fetch_array($result);
      if ($row[0] == 0)
	  
      {
         echo "<h2><br>Sorry, there are no products in this category</h2>\n";
      }
      else
      {
         $thispage = $page;
         $totrecords = $row[0];
         $recordsperpage = 3;
         $offset = ($thispage - 1) * $recordsperpage;
         $totpages = ceil($totrecords / $recordsperpage);
         echo "<table width=\"100%\" cellpadding=\"0\" border=\"0\"><tr>\n";
         $query = "SELECT * from products WHERE catid=$catid LIMIT $offset,$recordsperpage";
         $result = mysql_query($query); 
		 if ( mysql_num_rows( $result) > 0 ) {
 $i = 0;}
         while($row=mysql_fetch_array($result, MYSQL_ASSOC))
         {
		 
            $prodid = $row['prodid'];
            $description = $row['description'];
            $price = $row['price'];
            $onsale = $row['onsale'];
$table .= '</tr><tr>';
        if ( $i == 3 ) {
             $i = 0;
        }
$table .= "<td>{$row['prodid, description, price, onsale']}</td>";
        $i++;
   }
}
else {
    $table .= '<td>No data found!</td>';
}


               echo "<td><img src=\"showimage.php?id=$prodid\" width=\"150\" height=\"150\"></td>\n";
			   
	            echo "</tr><tr>\n";
		   
			   
			     echo "<td><a href=\"$newpage&id=$prodid\">$description</td>\n";
				 
		echo "</tr><tr>\n";
		
            echo "<td>\n";
               echo "$" . $price . "</td>\n";
		echo "</tr><tr><td>\n";

            if ($onsale)
               echo "On sale!\n";
            else
               echo "&nbsp;\n";
            echo "</td></tr>\n";
          
         }

         echo "</table>\n";  
 // Code to implement paging: This area below is perfect. Its the above thats killing me.

         if ($thispage > 1)
         {
            $page = $thispage - 1;
            $prevpage = "<a href=\"$currentpage&cat=$catid&page=$page\">Previous page</a>";
         } else
         {
            $prevpage = " ";
         }         if ($thispage < $totpages)
         {
            $page = $thispage + 1;
            $nextpage = " <a href=\"$currentpage&cat=$catid&page=$page\">Next page</a>";
         } else
         {
            $nextpage = " ";
         }         if ($totpages > 1)
            echo $prevpage . "  " . $nextpage;      }
   }

?>

I thank yo Cwarn23, I tried the code and unfortunately it did not work. It came out exactly like the result I had. The only difference is that it minus by one, the amount of item shown on the page.

I am trying the code below, but I am getting undefine variable for $table and my content for row on the $table .= "<td>{$row}</td>"; line I think is wrong. Because it mark as undefine
This is the code that I am now trying from Keith. Knowing me I am screwing up something. The other guy from the other thread successfully made his page work just like how I want mines to work. But I don't know what I am doing wrong. I even found out by messing aournd with Cwarn and Keith code that my table I had thought was going from left to right I made it wrong. So now I have another problem because it is going downward: vertically. But I think it might correct itself if the page is set up for the images to display as:
image image image
image image image
image image
next page

Well by reading that I don't 100% understand how you want the images to be displayed. From that it looks like you want to have an empty cell at the end of the table due to the number of images. And do you want pegination? Looks like it judging by the next page text at the end of the quote. So is the following how you want it done:

Page 1:
image1  image4  image7
image2  image5  image8
image3  image6
next page link

        Page 2:
image9  image12 image15
image10 image13 image16
image11 image14
next page link

To sort the images in that order it would require sorting the array before starting the loop. So is the above design what you want?

Exactly, it could look like
image1 image2 image3
image4 image5 image6
image7 image8 image9
next page

or exactly how you have it below. It does not matter on the numbering (both our concept are the same) because the images are taken from the database by the images, product name (I call it description), and price are taken from the database. Therefore, it would really look like this. It does not have to be in my number setting it could be in the number setting as yours because the order is not of an importance since it is only displaying images, price, and name description

image1 image2 image3
name of item name of item name of item
price price price

image4 image5 image6
name of item name of item name of item
price price price

image7 image8
name of item name of item
price price

next page

I have been trying to figure how to have it display for weeks now. Everytime I place it on the back burner to work on other part of my php pages (get migrain headache from the display not displaying properly). I have been working on this recently for many hours trying to get it to display as above and as yours but no luck.

Right now it is displaying like this, but six time:

image
name of item
price

image
name of item
price

image
name of item
price

image
name of item
price
on sale

next page

and this is not the look that I am trying to get.
I know for sure my problem is that my info such as <img src=\"showimage.php?id=$prodid\" width=\"150\" height=\"150\">

<a href=\"$newpage&id=$prodid\">$description

price and the onsale info are all pulling from the database. So it is hard me to actually make a table to look the way that I want it. Instead it comes out looking like a long horizontal column

P.S. I just learn a new word (pagination). I looked it up and did not really understand though. From what I read it mention a certain organization/format of a page. If it is what we both are speaking about then yes.

I really appreciate you having patient in me and helping me. I Thank You

Well by reading that I don't 100% understand how you want the images to be displayed. From that it looks like you want to have an empty cell at the end of the table due to the number of images. And do you want pegination? Looks like it judging by the next page text at the end of the quote. So is the following how you want it done:

Page 1:
image1  image4  image7
image2  image5  image8
image3  image6
next page link

        Page 2:
image9  image12 image15
image10 image13 image16
image11 image14
next page link

To sort the images in that order it would require sorting the array before starting the loop. So is the above design what you want?

I am working on a solution. Should be done soon.

try this. it is untested but i think it should fit your needs. i cleaned up the code and organized it better. let me know of any errors and i will fix them.

<?php

function showProducts( $catid,$page,$currentPage,$newPage ) {
	$html = '';
	$catid = mysql_real_escape_string( $catid );
	$query = mysql_query("SELECT COUNT(`prodid`) FROM `products` WHERE `catid` = {$catid}");
	list( $count ) = mysql_fetch_row( $query );
	if ( $count == 0 ) {
		$html .= "<h2>Sorry, there are no products in this category</h2>\n";
	}
	else {
		$recordsPerPage = 9;
		$recordsPerRow = 3;
		$limit = ( ( $page - 1 ) * $recordsPerPage );
		$total = ceil( $count / $recordsPerPage );
		$query = mysql_query("SELECT `prodid`,`description`,`price`,`onsale` FROM `products` WHERE `catid` = {$catid} LIMIT {$limit},{$recordsPerPage}");
		$html .= "<table width=\"100%\" cellpadding=\"2\" border=\"0\">\n";
		$html .= "\t<tr>\n";
		$i = 0;
		while( list( $prodid,$desc,$price,$onsale ) = mysql_fetch_row( $query ) ) {
			if ( $i == $recordsPerRow ) {
				$html .= "\t</tr>\n\t<tr>\n";
				$i = 0;
			}
			$html .= "\t\t<td>\n";
			$html .= "\t\t\t<table>\n";
			$html .= "\t\t\t\t<tr>\n\t\t\t\t\t<td><img src=\"showimage.php?id={$prodid}\" width=\"\" height=\"\" /></td>\n\t\t\t\t</tr>\n";
			$html .= "\t\t\t\t<tr>\n\t\t\t\t\t<td><a href=\"{$newPage}&id={$prodid}\">{$desc}</a></td>\n\t\t\t\t</tr>\n";
			$html .= "\t\t\t\t<tr>\n\t\t\t\t\t<td>\${$price}</td>\n\t\t\t\t</tr>\n";
			$html .= ( $onsale ? "\t\t\t\t<tr>\n\t\t\t\t\t<td>On Sale!</td>\n\t\t\t\t</tr>\n" : '' );
			$html .= "\t\t\t</table>";
			$html .= "\t\t</td>\n";
			$i++;
		}
		$html .= '</tr></table>';
		$prev = '';
		if ( $page > 1 ) {
			$prev = "<a href=\"{$currentPage}&cat={$catid}&page=" . ( $page - 1 ) . "\">Previous Page</a>";
		}
		$next = '';
		if ( $page < $total ) {
			$next = "<a href=\"{$currentPage}&cat={$catid}&page=" . ( $page + 1 ) . "\">Next Page</a>";
		}
		if ( $total > 1 ) {
			$html .= "{$prev}   {$next}";
		}
	}
	echo $html;
}

?>

Try the following:

<?php

   function showproducts($catid, $page, $currentpage, $newpage)
   {
      $query = "Select count(prodid) from products where catid = $catid";
      $result = mysql_query($query);
      $row = mysql_fetch_array($result);
      if ($row[0] == 0)
	  
      {
         echo "<h2><br>Sorry, there are no products in this category</h2>\n";
      }
      else
      {
         $thispage = $page;
         $totrecords = $row[0];
         $recordsperpage = 3;
         $offset = ($thispage - 1) * $recordsperpage;
         $totpages = ceil($totrecords / $recordsperpage);
         echo "<table width=\"100%\" cellpadding=\"1\" border=\"0\">\n";
         $query = "SELECT * from products WHERE catid=$catid LIMIT $offset,$recordsperpage";
         $result = mysql_query($query); 
         $var=1;
         while ($var=mysql_fetch_array($result, MYSQL_ASSOC))
         {
         unset($var);
         echo '<tr>';
         for ($x=0; $x<4, $row=mysql_fetch_array($result, MYSQL_ASSOC) ; $x++) {
            $prodid = $row['prodid'];
            $description = $row['description'];
            $price = $row['price'];
            $quantity = $row['quantity'];
            $onsale = $row['onsale'];

            echo "<td>\n";
               echo "<img src=\"showimage.php?id=$prodid\" width=\"150\" height=\"150\">\n";

			     echo "<a href=\"$newpage&id=$prodid\">$description\n";
               echo "$" . $price . "\n";

            if ($onsale)
               echo "On sale!\n";
            else
               echo "&nbsp;\n";
            echo "</td>\n";
            }
         echo '</tr>';
         $var=$row;
         }
         echo "</table>\n";   // Code to implement paging
         if ($thispage > 1)
         {
            $page = $thispage - 1;
            $prevpage = "<a href=\"$currentpage&cat=$catid&page=$page\">Previous page</a>";
         } else
         {
            $prevpage = " ";
         }         if ($thispage < $totpages)
         {
            $page = $thispage + 1;
            $nextpage = " <a href=\"$currentpage&cat=$catid&page=$page\">Next page</a>";
         } else
         {
            $nextpage = " ";
         }         if ($totpages > 1)
            echo $prevpage . "  " . $nextpage;      }
   }

?>

I just tested that script for the unknown error you mentioned and found that the for loop opening line needed to be replacing with the following:

for ($x=0; $x<3, $row=mysql_fetch_array($result, MYSQL_ASSOC) ; $x++) {

Then that just leaves the pegination.

It work it work it work!!! I tried both code to see if they both would give me the result I am looking for and at the same time taking notes on how to do things since I just finish up my php class last semester and trying to advance myself with a real world more functioning look than what I have learned from class. So this is cool. I am learning different way to write codes!

Cwarn I tried your code first but something is wrong. For some odd reason it now does not want to show the image, name, or price and there were no error message or anything. I tried to play around with the codes for a few hours and still can't figure out what is wrong.

Then I tried Keith's code and it worked. The image did not show up but I added the width and height dimension and then the image showed. I am not for sure why. But I know when I put in the dimension it showed. But it is looking exactly like I wanted.

I thank "Both" of you very much because you both took time out to help me. Plus I am learning so much from both of you. I am taking bits and pieces and breaking it down for understanding of why you choose to use certain codes and format.

So THANK YOU, THANK YOU, THANK YOU BOTH!!!!!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.