razvypp Posted April 25, 2010 Share Posted April 25, 2010 hello , i have a problem with the following code, it should print a 6x5 table with values taken from the database , but when the items are multiple of 6, it insert an empty <tr> at the nest row. like this $query = "SELECT field1, field2 FROM tbl_name LIMIT 30"; $result = mysql_query($query); $count = mysql_num_rows($result); $need = (6 - ceil($count / 6) ); $i = 0; $rows = 0; echo "<table border=\"1\"><tr>"; while( $i < $count ) { while( $array = mysql_fetch_assoc($result) ) { echo "<td>" .$array['img'] . "</td>"; $i++; if( is_int($i / 6) ) { echo "</tr>\n<tr>"; $rows ++; } } while ( !is_int($i / 6) ) { echo '<td></td>'; $i++; } } while( $rows < $need ) { echo "<tr><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td></tr>"; $rows ++; } Link to comment https://forums.phpfreaks.com/topic/199717-table-help/ Share on other sites More sharing options...
ScotDiddle Posted April 26, 2010 Share Posted April 26, 2010 razvypp, I found a missing </tr> in your code. The following code seems to work... Scot L. Diddle, Richmond VA <?php Header("Cache-control: private, no-cache"); Header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); Header("Pragma: no-cache"); require('includes/DB_Connect.php'); $query = "SELECT sku, vendor_full FROM shopping_cart LIMIT 30"; $result = mysql_query($query); $count = mysql_num_rows($result); $need = (6 - ceil($count / 6) ); $i = 0; $rows = 0; echo "<table border=\"1\"> \n\n"; echo "\t<tr> \n"; while( $i < $count ) { while( $array = mysql_fetch_assoc($result) ) { $currentVendor = $array['vendor_full']; echo "\t\t<td>" . $currentVendor . "</td> \n"; $i++; if( is_int($i / 6) ) { echo "\n\t</tr>\n\n"; echo "\t<tr>\n\n"; $rows ++; } } while ( !is_int($i / 6) ) { echo '\t\t<td> </td>'; $i++; } } echo "\t</tr> \n"; while( $rows < $need ) { echo "\t<tr>\n \t\t<td> </td> \t\t<td> </td> \t\t<td> </td> \t\t<td> </td> \t\t<td> </td> \t\t<td> </td> \t<tr>\n"; $rows ++; } echo "\n </table> \n"; ?> Link to comment https://forums.phpfreaks.com/topic/199717-table-help/#findComment-1048695 Share on other sites More sharing options...
razvypp Posted April 26, 2010 Author Share Posted April 26, 2010 still doesn't work, here is my exact code . $own=$_SESSION['uname']; $query = "SELECT * FROM inventory where `ownerid`='$x' and `trade`='0' LIMIT 30"; $result = mysql_query($query); $count = mysql_num_rows($result); $need = (6 - ceil($count / 6) ); $i = 0; $rows = 0; echo "<table border=\"1\"> \n\n"; echo "\t<tr> \n"; while( $i < $count ) { while( $array = mysql_fetch_assoc($result) ) { $y=$array['itemid']; $queryt = "SELECT * FROM items where `itemid`='$y' LIMIT 30"; $resultt = mysql_query($queryt); $gt = mysql_fetch_array($resultt); $price="Value : $gt[price]"; $name=$gt['item_name']; $desc=$gt['description']; $itemt=$gt['item_type']; if($gt['patk']=='0' && $gt['pdef']!='0') { $e1="Pdef : $gt[pdef] / "; $e2="Mdef : $gt[mdef]"; } else if($gt['patk']!='0' && $gt['pdef']=='0') {$e1="Patk : $gt[patk] / "; $e2="Matk : $gt[matk]";} else if($gt['patk']=='0' && $gt['pdef']=='0') {$e1=""; $e2="";} echo "\t\t<td><a onMouseOver=\"zs('$e1 $e2','Item type : $itemt','$ico','$name','$price','$desc')\" onmouseout=\"return nd();\" onClick=\"window.location='inv2.php?id=$array[id]'\"><img src='$gt[icon]' height=35 width=35/></a></td> \n"; $i++; if( is_int($i / 6) ) { echo "\n\t</tr>\n\n"; echo "\t<tr>\n\n"; $rows ++; } } while ( !is_int($i / 6) ) { echo '\t\t<td> </td>'; $i++; } } echo "\t</tr> \n"; while( $rows < $need ) { echo "\t<tr>\n \t\t<td> </td> \t\t<td> </td> \t\t<td> </td> \t\t<td> </td> \t\t<td> </td> \t\t<td> </td> \t<tr>\n"; $rows ++; } echo "\n </table> \n"; [attachment deleted by admin] Link to comment https://forums.phpfreaks.com/topic/199717-table-help/#findComment-1048781 Share on other sites More sharing options...
razvypp Posted April 26, 2010 Author Share Posted April 26, 2010 i don't know why but i can't edit my post to add more info, the problem is that when nr of items taken from the database is <=6 the table is 6x6 than 6x5 , and /t/t/t/t appear above the table Link to comment https://forums.phpfreaks.com/topic/199717-table-help/#findComment-1048849 Share on other sites More sharing options...
ScotDiddle Posted April 26, 2010 Share Posted April 26, 2010 razvypp, Sorry, Without your tables loaded on my site, I can provide no futher assistance. If you post your SQL table defintions, and supply 30+ sample data rows for each of your tables, I will try again. My first example works based on your orignal code... Here is the output I got from my original code: <table border="1"> <tr> <td>Anvil</td> <td>Rawlings</td> <td>Liberty Bags</td> <td>Jerzees T's and Fleece</td> <td>Gildan</td> <td>Gildan</td> </tr> <tr> <td>Rabbit Skins</td> <td>Anvil</td> <td>Outer Banks</td> <td>Jerzees Golf</td> <td>Van Heusen</td> <td>Gildan</td> </tr> <tr> <td>Pro-line</td> <td>Gildan</td> <td>Outer Banks</td> <td>Anvil</td> <td>Gildan</td> <td>Anvil</td> </tr> <tr> <td>Gildan</td> <td>Gildan</td> <td>Head To Toe</td> <td>Gildan</td> <td>Jerzees T's and Fleece</td> <td>Outer Banks</td> </tr> <tr> <td>Outer Banks</td> <td>Outer Banks</td> <td>Eagle, USA</td> <td>Sierra Pacific</td> <td>Liberty Bags</td> <td>Gildan</td> </tr> <tr> </tr> </table> Scot L. Diddle, Richmond VA Link to comment https://forums.phpfreaks.com/topic/199717-table-help/#findComment-1048852 Share on other sites More sharing options...
razvypp Posted April 26, 2010 Author Share Posted April 26, 2010 this is my source output. as u can see i get some extra <tr></tr> also here is my db and values inserted to them as you requested . thnkx for your help The problem is when u get less values from your database . lets say 5 or 12 items not 30.if i get 30 items from the database the source is good. <span class="style1"> \t\t<table border="1"> <tbody><tr> <td><a onmouseover="zs('Pdef : 90 / Mdef : 0','Item type : body','','Armor of lightning','Value : 70000','Armor forged on Mount Olymp as a gift to Zeus.')" onmouseout="return nd();" onclick="window.location='inv2.php?id=1'"><img src="img/items/armors/armor9.png" width="35/" height="35"></a></td> <td><a onmouseover="zs('Pdef : 55 / Mdef : 0','Item type : body','','Armor of death','Value : 55000','Once owned by Death lord Ipos')" onmouseout="return nd();" onclick="window.location='inv2.php?id=2'"><img src="img/items/armors/armor2.png" width="35/" height="35"></a></td> <td><a onmouseover="zs('Pdef : 55 / Mdef : 0','Item type : body','','Armor of death','Value : 55000','Once owned by Death lord Ipos')" onmouseout="return nd();" onclick="window.location='inv2.php?id=3'"><img src="img/items/armors/armor2.png" width="35/" height="35"></a></td> <td><a onmouseover="zs('Pdef : 55 / Mdef : 0','Item type : body','','Armor of death','Value : 55000','Once owned by Death lord Ipos')" onmouseout="return nd();" onclick="window.location='inv2.php?id=4'"><img src="img/items/armors/armor2.png" width="35/" height="35"></a></td> <td><a onmouseover="zs('Pdef : 59 / Mdef : 0','Item type : body','','Armor of fire','Value : 58000','Forged in the land of fire , by BlackFire himself.')" onmouseout="return nd();" onclick="window.location='inv2.php?id=7'"><img src="img/items/armors/armor3.png" width="35/" height="35"></a></td> <td> </td> </tr> <tr> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> </tr><tr> </tr><tr> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> </tr><tr> </tr><tr> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> </tr><tr> </tr><tr> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> </tr><tr> </tr><tr> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> </tr><tr> </tr></tbody></table> </span> CREATE TABLE `inventory` ( `id` int(50) NOT NULL AUTO_INCREMENT, `ownerid` int(50) NOT NULL, `itemid` int(50) NOT NULL, `type` varchar(25) NOT NULL, `number` int(25) NOT NULL, `enchant` tinyint(3) NOT NULL, `trade` tinyint(1) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=44 DEFAULT CHARSET=latin1 AUTO_INCREMENT=44 ; -- -- Dumping data for table `inventory` -- INSERT INTO `inventory` VALUES (1, 1, 24, 'body', 1, 0, 0); INSERT INTO `inventory` VALUES (2, 1, 23, 'body', 1, 0, 0); INSERT INTO `inventory` VALUES (3, 1, 23, 'body', 1, 0, 0); INSERT INTO `inventory` VALUES (4, 1, 23, 'body', 1, 0, 0); INSERT INTO `inventory` VALUES (7, 1, 27, 'body', 1, 0, 0); INSERT INTO `inventory` VALUES (10, 1, 24, 'body', 1, 0, 0); INSERT INTO `inventory` VALUES (11, 1, 10, 'weapon', 1, 0, 0); INSERT INTO `inventory` VALUES (12, 1, 10, 'weapon', 1, 0, 0); INSERT INTO `inventory` VALUES (13, 1, 10, 'weapon', 1, 0, 0); INSERT INTO `inventory` VALUES (14, 1, 10, 'weapon', 1, 0, 0); INSERT INTO `inventory` VALUES (15, 1, 10, 'weapon', 1, 0, 0); INSERT INTO `inventory` VALUES (16, 1, 10, 'weapon', 1, 0, 0); INSERT INTO `inventory` VALUES (17, 1, 10, 'weapon', 1, 0, 0); INSERT INTO `inventory` VALUES (18, 1, 10, 'weapon', 1, 0, 0); INSERT INTO `inventory` VALUES (19, 1, 10, 'weapon', 1, 0, 0); INSERT INTO `inventory` VALUES (20, 1, 10, 'weapon', 1, 0, 0); INSERT INTO `inventory` VALUES (21, 1, 10, 'weapon', 1, 0, 0); INSERT INTO `inventory` VALUES (22, 1, 10, 'weapon', 1, 0, 0); INSERT INTO `inventory` VALUES (23, 1, 10, 'weapon', 1, 0, 0); INSERT INTO `inventory` VALUES (24, 1, 10, 'weapon', 1, 0, 0); INSERT INTO `inventory` VALUES (25, 1, 10, 'weapon', 1, 0, 0); INSERT INTO `inventory` VALUES (26, 1, 10, 'weapon', 1, 0, 0); INSERT INTO `inventory` VALUES (27, 1, 10, 'weapon', 1, 0, 0); INSERT INTO `inventory` VALUES (28, 1, 10, 'weapon', 1, 0, 0); INSERT INTO `inventory` VALUES (29, 1, 10, 'weapon', 1, 0, 0); INSERT INTO `inventory` VALUES (30, 1, 10, 'weapon', 1, 0, 0); INSERT INTO `inventory` VALUES (31, 1, 10, 'weapon', 1, 0, 0); INSERT INTO `inventory` VALUES (32, 1, 10, 'weapon', 1, 0, 0); INSERT INTO `inventory` VALUES (33, 1, 10, 'weapon', 1, 0, 0); INSERT INTO `inventory` VALUES (34, 1, 10, 'weapon', 1, 0, 0); INSERT INTO `inventory` VALUES (35, 1, 10, 'weapon', 1, 0, 0); INSERT INTO `inventory` VALUES (36, 1, 10, 'weapon', 1, 0, 0); INSERT INTO `inventory` VALUES (37, 1, 10, 'weapon', 1, 0, 0); INSERT INTO `inventory` VALUES (38, 1, 10, 'weapon', 1, 0, 0); INSERT INTO `inventory` VALUES (39, 1, 10, 'weapon', 1, 0, 0); INSERT INTO `inventory` VALUES (40, 1, 10, 'weapon', 1, 0, 0); INSERT INTO `inventory` VALUES (41, 1, 10, 'weapon', 1, 0, 0); INSERT INTO `inventory` VALUES (42, 1, 10, 'weapon', 1, 0, 0); INSERT INTO `inventory` VALUES (43, 1, 10, 'weapon', 1, 0, 0); Link to comment https://forums.phpfreaks.com/topic/199717-table-help/#findComment-1048869 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.