mdburden Posted March 18, 2009 Share Posted March 18, 2009 I am using php 5 on an apache 2.2 server and a mySQL 6 database. I am trying to create a catalog page that will display items founs as a result of the database. I can get the data to display correctly in a table with one item per row. I can get the table to display in 3 columns, but the item is the same in each column of the row. The next row has a new item, but that is also displayed in each column of that row. I am using the following php code: // Setup and run query $query = "select * from items where name like '%".$searchterm."%'"; $result = $db->query($query); $num_results = $result->num_rows; echo "<p>Number of items found: ".$num_results."</p>"; // Display results echo "<table border= 1>"; for ($i=0; $i <$num_results; $i++) { $row = $result->fetch_assoc(); $Imge = stripslashes($row['imge']); echo "<tr>"; for ($c=0; $c < 3; $c++) { echo "<td>"; echo "<p><strong>Number: "; echo stripslashes($row['number']); echo "\t \t"; echo htmlspecialchars(stripslashes($row['title'])); echo stripslashes($row['name']); echo "</strong><br /></p>"; echo "<p align='center'><img border=\"0\" src=\"images/".$Imge."\"><br /></p>"; echo stripslashes($row['description']); echo "<br />Price: "; echo stripslashes($row['price']); echo "</p>"; echo "</td>"; } echo "</tr>"; } echo "</table>"; Can anyone help? Link to comment https://forums.phpfreaks.com/topic/150049-multi-column-table/ Share on other sites More sharing options...
samshel Posted March 18, 2009 Share Posted March 18, 2009 what r u trying to achieve? you want to show records in 3 columns? try this.. <?php // Setup and run query $query = "select * from items where name like '%".$searchterm."%'"; $result = $db->query($query); $num_results = $result->num_rows; echo "<p>Number of items found: ".$num_results."</p>"; // Display results echo "<table border= 1>"; for ($i=0; $i <$num_results; $i=$i+3) { echo "<tr>"; for ($c=0; $c < 3; $c++) { if((($i*3) + $c) < $num_results) { $row = $result->fetch_assoc(); $Imge = stripslashes($row['imge']); echo "<td>"; echo "<p><strong>Number: "; echo stripslashes($row['number']); echo "\t \t"; echo htmlspecialchars(stripslashes($row['title'])); echo stripslashes($row['name']); echo "</strong><br /></p>"; echo "<p align='center'><img border=\"0\" src=\"images/".$Imge."\"><br /></p>"; echo stripslashes($row['description']); echo "<br />Price: "; echo stripslashes($row['price']); echo "</p>"; echo "</td>"; } else { echo "<td> </td>"; } } echo "</tr>"; } echo "</table>"; ?> PS: the code is not tested. EDIT: updated if condition to multiply by 3 Link to comment https://forums.phpfreaks.com/topic/150049-multi-column-table/#findComment-788040 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.