Jump to content

[SOLVED] Print tables out in rows of two


fireice87

Recommended Posts

Hey

Im creating a catalog type script. I've wrote code to stick the data in the database and pull it back out in a table in a format im happy with.

This table displays one catalog item. the script runs through all rows and prints the tables vertically so you get one tabel at the top then one bellow it and so on.

Im trying to write the script so that instead of going straight down the tables are out putted in rows of two.

I thought this would be quite easy but cant get it to work.

I sent up a count and tried to put the table with the info inside a parent table that would print a new cell when the counter hits two but this had the effect of printing each table the same way but out horizontally instead of vertically!

 

My code: (version that prints horizontally

<?
$sql = "Select * FROM ug_products";  
$result= @mysql_query($sql, $conn )or die("Could not pull products");

        $c = 0;

echo "<table>"; 
while($row = mysql_fetch_array($result)) {
	$product = $row['product'];
	$image = $row['image'];
	$price = $row['price'];
	$desc = $row['description'];
	$disc_display = substr($desc, 0, 210);


	if($c=1) echo "<td>";
			 echo "<table>"; 
			 echo "<tr height=\"20px\">";  
			   		 	echo "<td width=\"160\" height=\"150\" rowspan=\"5\" > <img src='".$image."' alt=\"Image Unavailable\" />   </td>";
				 		echo "<td align=\"center\" width=\"177\" class=\"product\">$product</td>";
			 echo "</tr>";
			 echo "<tr>";
						echo "<td align=\"center\"  height=\"20\" class=\"price\">Just £$price</td>";
			 echo "</tr>";
			 echo "<tr>";
						 echo "<td align=\"center\" height=\"20\" class=\"moreinfo\"> more info </td>";
			 echo "</tr>";
			 echo "<tr>";
						 echo "<td align=\"center\" height=\"20\" class=\"moreinfo\"> Customer rating  </td>";
			 echo "</tr>";	
			 echo "<tr>";
					     echo "<td align=\"center\" height=\"20\"><img src=\"Images/Addtobasket.gif\" alt=\"Add To Basket\" /></td>";
		     echo "</tr>";	
			 echo "<tr>"; 
			 			  echo "<td colspan=\"2\" align=\"left\" class=\"desc\">$disc_display.......................</td>";
			 echo "</tr>";
			 echo "</table>";
	if($c=1) echo"</td>";
	   
		   $c++;
			 	}													
echo "</table>";
?>

 

Thanks for any help

 

Link to comment
Share on other sites

Hey I have been playing with this code still to no avail. Im trying to use the % operator to say when tostart a new row. But am unsure if it will work with the values im using.

to create a row.

if($c%1 == 0) echo "<tr>"; so if the c variable has gone throught twice make a new row.

and at the end

if($c%1 !== 0) echo "</tr>"; if this is the seconed count end the row

 

Am i using this wrong as by my understanding of the % operator that looks ok but is still not printing correctly.

prints the tables out vertically

<?php
        $c = 0;

echo "<table>"; 
while($row = mysql_fetch_array($result)) {
	$product = $row['product'];
	$image = $row['image'];
	$price = $row['price'];
	$desc = $row['description'];
	$disc_display = substr($desc, 0, 210);

 if($c%1 == 0) echo "<tr>";
			 echo "<tr height=\"20px\">";  
			   		 	echo "<td width=\"160\" height=\"150\" rowspan=\"5\" > <img src='".$image."' alt=\"Image Unavailable\" />   </td>";
				 		echo "<td align=\"center\" width=\"177\" class=\"product\">$product</td>";
			 echo "</tr>";
			 echo "<tr>";
						echo "<td align=\"center\"  height=\"20\" class=\"price\">Just £$price</td>";
			 echo "</tr>";
			 echo "<tr>";
						 echo "<td align=\"center\" height=\"20\" class=\"moreinfo\"> more info </td>";
			 echo "</tr>";
			 echo "<tr>";
						 echo "<td align=\"center\" height=\"20\" class=\"moreinfo\"> Customer rating 4/5 </td>";
			 echo "</tr>";	
			 echo "<tr>";
					     echo "<td align=\"center\" height=\"20\"><img src=\"Images/Addtobasket.gif\" alt=\"Add To Basket\" /></td>";
		     echo "</tr>";	
			 echo "<tr>"; 
			 			  echo "<td colspan=\"2\" align=\"left\" class=\"desc\">$disc_display.......................</td>";
			 echo "</tr>";
if($c%1 !== 0) echo "</tr>";

	   
		   $c++;
			 	}													
echo "</table>";
?>

 

 

Link to comment
Share on other sites

Set it up the way you have it but replace the 1 with a 2.

Remainder of 0/2 = 0 - It creates the row.

Remainder of 1/2 = 5 - It ends the row.

2/2 = 0 for remainder, and 3/2 = 5 for remainder and it continues. The remainder is never "1" so you want to use if == 0 and !== 0.

Link to comment
Share on other sites

I haven't really read your code, so i may be wrong, but couldn't you just use a counter in the while loop.

something like:

while($row = mysql_fetch_array($result)) {
if $counter = $MyNumber { echo "<tr>" }

-rest of code-

$counter += $counter; }

 

 

Link to comment
Share on other sites

to:Zhadus

ah thanks for clearing that up for me its much appreciated but unfourtently its still printing the same way 1 table per row going down vertically....

 

if($c%2 == 0) echo "<tr>";

code

if($c%2 !== 0) echo "</tr>";

 

 

to: dark_mirage

 

Thanks yeah thats a good way to do it but doesnt work so well when you dont know how many rows need to be printed. i could use that method and have '$MyNumber' as 2 for example but that would only work for starting one more row.

Link to comment
Share on other sites

I see the problem now. What I had you change for the % symbol works. But there was a problem with your formatting. You are using ONE table and just adding an extra row when those conditions were true. Try this:

<?php
        $c = 0;

echo "<table>"; 
while($row = mysql_fetch_array($result)) {
	$product = $row['product'];
	$image = $row['image'];
	$price = $row['price'];
	$desc = $row['description'];
	$disc_display = substr($desc, 0, 210);

 if($c%2 == 0) echo "<tr>";
                                                    echo "<td><table>";
			 echo "<tr height=\"20px\">";  
			   		 	echo "<td width=\"160\" height=\"150\" rowspan=\"5\" > <img src='".$image."' alt=\"Image Unavailable\" />   </td>";
				 		echo "<td align=\"center\" width=\"177\" class=\"product\">$product</td>";
			 echo "</tr>";
			 echo "<tr>";
						echo "<td align=\"center\"  height=\"20\" class=\"price\">Just £$price</td>";
			 echo "</tr>";
			 echo "<tr>";
						 echo "<td align=\"center\" height=\"20\" class=\"moreinfo\"> more info </td>";
			 echo "</tr>";
			 echo "<tr>";
						 echo "<td align=\"center\" height=\"20\" class=\"moreinfo\"> Customer rating 4/5 </td>";
			 echo "</tr>";	
			 echo "<tr>";
					     echo "<td align=\"center\" height=\"20\"><img src=\"Images/Addtobasket.gif\" alt=\"Add To Basket\" /></td>";
		     echo "</tr>";	
			 echo "<tr>"; 
			 			  echo "<td colspan=\"2\" align=\"left\" class=\"desc\">$disc_display.......................</td>";
			 echo "</tr>";
                                                    echo "</table></td>";
if($c%2 !== 0) echo "</tr>";

	   
		   $c++;
			 	}													
echo "</table>";
?>

Tested this time :) I formatted each result into a table and put that table into TD tags for that specific row.

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.