fireice87 Posted April 12, 2008 Share Posted April 12, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/100799-solved-print-tables-out-in-rows-of-two/ Share on other sites More sharing options...
fireice87 Posted April 14, 2008 Author Share Posted April 14, 2008 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>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/100799-solved-print-tables-out-in-rows-of-two/#findComment-516983 Share on other sites More sharing options...
Zhadus Posted April 14, 2008 Share Posted April 14, 2008 You are using the % incorrectly. You are checking the remainder, but no integers divided by 1 have a remainder. Change it to $c%2 and it should work for you. Quote Link to comment https://forums.phpfreaks.com/topic/100799-solved-print-tables-out-in-rows-of-two/#findComment-516988 Share on other sites More sharing options...
fireice87 Posted April 14, 2008 Author Share Posted April 14, 2008 Thanks but i have tried it as c%2 == 0 and c%2 == 1 And still no success. I thought $c%1 == 0 would work before as the count starts from 0 so 1 has the value of 2 so the integer would be being divded by 2? Quote Link to comment https://forums.phpfreaks.com/topic/100799-solved-print-tables-out-in-rows-of-two/#findComment-516999 Share on other sites More sharing options...
Zhadus Posted April 14, 2008 Share Posted April 14, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/100799-solved-print-tables-out-in-rows-of-two/#findComment-517019 Share on other sites More sharing options...
dark_mirage Posted April 14, 2008 Share Posted April 14, 2008 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; } Quote Link to comment https://forums.phpfreaks.com/topic/100799-solved-print-tables-out-in-rows-of-two/#findComment-517021 Share on other sites More sharing options...
fireice87 Posted April 14, 2008 Author Share Posted April 14, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/100799-solved-print-tables-out-in-rows-of-two/#findComment-517031 Share on other sites More sharing options...
dark_mirage Posted April 14, 2008 Share Posted April 14, 2008 hmm ok, how about: while($row = mysql_fetch_array($result)) { if $counter = $MyNumber { echo "<tr>"; $counter=0; } -rest of code- $counter += $counter; } so then, each time it creates a new row, the counter is reset again. Quote Link to comment https://forums.phpfreaks.com/topic/100799-solved-print-tables-out-in-rows-of-two/#findComment-517034 Share on other sites More sharing options...
Zhadus Posted April 14, 2008 Share Posted April 14, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/100799-solved-print-tables-out-in-rows-of-two/#findComment-517041 Share on other sites More sharing options...
fireice87 Posted April 14, 2008 Author Share Posted April 14, 2008 ;D perfect that works exactly right, thanks to both of you for taking the time to help me out! Quote Link to comment https://forums.phpfreaks.com/topic/100799-solved-print-tables-out-in-rows-of-two/#findComment-517048 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.