Jump to content

[Help]Php Dynamic Grid In Three Columns


Diether

Recommended Posts

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:

16935314a4a8e325756b68bfa2e5ce6228c82ab7.jpg

 

my desired output:

16935313296eeb89b5d6e1d47baeb0f2ee05cced.jpg

 

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>


Link to comment
Share on other sites

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.

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.