Diether Posted November 22, 2012 Share Posted November 22, 2012 Good day, just new in php and really confuse. i have a dynamic Grid below in my screenshot,that is only straight. the codes are working well but i want to put it in 3 columns. so i research about dynamic grid output, My problem is after trying to insert this in my codes,,, it gives me different error.. sa mga mabubuti po ang loob dyan ,, pls guide me to make my images in 3 columns. thanks in advance current image: my desired output: Here the codes in my index.php <?php // Run a select query to get my letest 6 items // Connect to the MySQL database include "includes/connect_to_mysql.php"; $dynamicList = ""; $sql = mysql_query("SELECT * FROM tbl_products ORDER BY date_added DESC LIMIT 6"); $productCount = mysql_num_rows($sql); // count the output amount if ($productCount > 0) { while($row = mysql_fetch_array($sql)){ $id = $row["id"]; $product_name = $row["product_name"]; $price = $row["price"]; $date_added = strftime("%b %d, %Y", strtotime($row["date_added"])); $dynamicList .= '<table width="418" border="2" cellpadding="6"> <tr> <td width="124" valign="top"><a href="product.php?id=' . $id . '"><img src="inventory_images/' . $id . '.jpg" alt="' . $product_name . '" width="120" height="121" /></a></td> <td width="256" valign="top">' . $product_name . '<br /> $' . $price . '<br /> <a href="product.php?id=' . $id . '">View Product Details</a></td> </tr> </table>'; } } else { $dynamicList = "We have no products listed in our store yet"; } mysql_close(); ?> code that i want to insert: <?php // Include database connection include_once 'connect_to_mysql.php'; // SQL query to interact with info from our database $sql = mysql_query("SELECT id, member_name FROM member_table ORDER BY id DESC LIMIT 15"); $i = 0; // Establish the output variable $dyn_table = '<table border="1" cellpadding="10">'; while($row = mysql_fetch_array($sql)){ $id = $row["id"]; $member_name = $row["member_name"]; if ($i % 3 == 0) { // if $i is divisible by our target number (in this case "3") $dyn_table .= '<tr><td>' . $member_name . '</td>'; } else { $dyn_table .= '<td>' . $member_name . '</td>'; } $i++; } $dyn_table .= '</tr></table>'; ?> <html> <body> <h3>Dynamic PHP Grid Layout From a MySQL Result Set</h3> <?php echo $dyn_table; ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/271043-helpphp-dynamic-grid-in-three-columns/ Share on other sites More sharing options...
Muddy_Funster Posted November 22, 2012 Share Posted November 22, 2012 I can't be the only one thinking if($i % 3 == 0){ is screwed up. can I? what's wrong with if($i==2){$i=0;... and just move the $i++ inside the else. Anyway - debugging errors is a whole world easier if you actualy tell us when the error is. Quote Link to comment https://forums.phpfreaks.com/topic/271043-helpphp-dynamic-grid-in-three-columns/#findComment-1394456 Share on other sites More sharing options...
Christian F. Posted November 22, 2012 Share Posted November 22, 2012 I don't know if you're the only one, Muddy_Funster, but it is a commonly used design pattern. I daresay it might even be the recommended way of doing something like this. Diether: A very good tips for instances like these, is to sit down and write a short step-by-step list of exactly what you want the code do to. Try to explain it in detail, but only using a couple words per item in the list. So that you accurately describe the logic behind each statement in the PHP code, before you write the code. Once you've done this you'll find that you have a much better understanding of what the script has to do, and thus it will be a lot easier to actually write the code. Since you don't have to design the logic and translate it from basic English at the same time. An example of such a step-by-step planning list could look like this: - Fetch ID and name from DB. - Open table and table row - Set counter to 1 - Loop through each row from DB. - Check if counter is dividable by 3. - Close existing table row, and open new. - Add table cell. - Increase counter by 1 - Close last table row and table. As you hopefully can see, making such a list makes things a lot easier. Also, the above list isn't quite complete. If the total number of rows are not dividable by 3, you'll be missing some cells in the last row. I'll leave it as an exercise for you to complete it. Quote Link to comment https://forums.phpfreaks.com/topic/271043-helpphp-dynamic-grid-in-three-columns/#findComment-1394533 Share on other sites More sharing options...
Diether Posted November 23, 2012 Author Share Posted November 23, 2012 thanks Christian F. i will try your suggestion. i think thats a very good idea for a beginner like me.. Quote Link to comment https://forums.phpfreaks.com/topic/271043-helpphp-dynamic-grid-in-three-columns/#findComment-1394549 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.