Jump to content

Create an Organized Table from MySQL


QuackMasterDan

Recommended Posts

Hello there, I am creating a website for a client who has specifically requested a layout of data from a MYSQL table. The table named states has the columns id, and statename (its a list of states) and I have created a script to organize the statenames into a table, which will later have links added to them. The table must only be three cells (<td>) wide, and rows (<tr>) should be able to expand forever, as a lot of content will be added. Here is the code:

 

$query = "SELECT * FROM states";

$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

 

if (mysql_num_rows($result) > 0) {

echo "<table cellpadding=\"15px\" border=\"1\"><tr><td align=\"middle\" colspan=\"100%\" valign=\"middle\"><h1>States of Russia</h1></td></tr><tr valign=\"middle\">";

while ($row = mysql_fetch_row($result)) {

echo "<td align=\"middle\" width=\"200px\" height=\"200px\" cellpadding=\"25px\"><h3>".$row[1]."</h3></td>";

 

}

echo "</tr>";

echo "</table>";

}

else {

echo "No rows found!";

}

 

mysql_free_Result($result);

mysql_close($connection);

?>

 

Pretty simple stuff, however the way it is setup it just writes every entry into a cell, all in one row. How can I create a new row after every third cell, and each new row continues to list states where the previous cell left off?

 

 

Link to comment
Share on other sites

For a pictographical representation:

 

 

______________________________________________

                                                                        |

                        States of Russia                          |

______________________________________________

|                    |                        |                        |

|                    |                        |                        |

|    $row[1] #1 |    $row[1] #2  |    $row[1] [#3]  |

|                    |                        |                        |

|                    |                        |                        |

|                    |                        |                        |

_____________________________________________ | NEW ROW

|                    |                        |                        |

|                    |                        |                        |

|    $row[1] #1 |    $row[1] #2  |    $row[1] [#3]  |

|                    |                        |                        |

|                    |                        |                        |

|                    |                        |                        |

_____________________________________________ | NEW ROW

 

I'm asking a friend who thinks it should be done with an array and a counter that counts: 0,1,2 and then resets, but I'm not sure how to implement it.

Link to comment
Share on other sites

After about an hour and a half of coding, I solved it. I created an array from the data that comes from the MySQL table. I set a variable ($i) that starts off at 0. A while loop takes information from the table and each time it repeats adds 1 to $i ($i++). Then $i is checked on what number it is. Since I want it to be 3 columns long, the number is 3. So once $i=3, it is reset to 0. This continues to mysql_fetch_row is no longer true, and stops. Here is the code:

 

/* CODE STARTS HERE */

$query = "SELECT * FROM states";

$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

 

echo "<table cellpadding=\"15px\"><tr><td align=\"middle\" colspan=\"100%\" valign=\"middle\"><h1>States</h1></td></tr>

 

<tr valign=\"center\" height=\"200px\">";

 

$data = array();

$i=0;

while ($row = mysql_fetch_row($result))

{

$i++;

$data[$i] = $row[1];

 

 

if ($i == 1) {echo "<td width=\"200px\">$data[1]</td>";}

if ($i == 2) {echo "<td width=\"200px\">$data[2]</td>";}

if ($i == 3) {echo "<td width=\"200px\">$data[3]</td></tr><tr valign=\"center\" height=\"200px\">";}

if ($i == 3) {$i = 0;}

}

/* CODE ENDS HERE */

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.