Generate HTML Table from MYSQL query

I have the following code that pulls addresses from mysql. Not that some have one address and others may have nine addresses. Right now it lists the addresses in

  • :

    How can I make it display the results in a 3 column table

    Here is the code I currently have:

    [CODE]

    Office Locations

    <?php $total=get_post_meta($post->ID,'total_addresses',true);?>
      <?php for($i=1;$i<=$total;$i++): ?> <?php $address=get_post_meta($post->ID,"em_address_$i",true); ?>
    • <?php echo $address; ?>
    • <?php endfor;?>
    [/CODE]

    Please forgive me if I have not provided enough information. I am imagining that is the only area that needs edited

You using a database? Where is the data call?

[php]

Office Locations

<?php /*connect to your database here if not already connected*/ $query = "SELECT * FROM table1"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)){ $name = $row[0]; $street = $row[1]; $city = $row[2]; echo ""; } ?>
$name $stree $city<?td>
[/php]

Obviously your data table will not match with what I have. If this doesn’t answer your question please clarify your data source.

OK well right now the data in the MySql table is like:

Main Office
223 Newbury Street
Boston, MA 02116
(617) 226-8853

So all of the info would be in one column. I want to make it able to have 3 columns across and generate a new row if there are more than 3 addresses.

em_address_$i would be be the address (em_address_1, em_address_2, em_address_3, em_address_4, etc.)

Some have 9 addresses and some only one.

you can see an example of the multiple addresses at: http://hoamanagement.com/association-management-companies/eugene-burger-management-corporation#locations

Have you looked at the dynamic grid style? http://www.zackgrossbart.com/hackito/jsgrid/

Better yet try this http://www.qualitycodes.com/tutorial.php?articleid=33&title=Displaying-Records-in-Multiple-Columns-Using-PHP

That will work perfect! THANK YOU

Thanks! Would you be my first karma giver?

I would but do not know how…lol

Also this is a newb question but do you know how I would implement the code on that page onto mine. I don’t see how to use the $col along with my $total. This is in wordpress so I don’t need the $query=“select * from members”;

Mine: [php]

Office Locations

<?php $total=get_post_meta($post->ID,'total_addresses',true);?>
    <?php for($i=1;$i<=$total;$i++): ?> <?php $address=get_post_meta($post->ID,"em_address_$i",true); ?>
  • <?php echo $address; ?>
  • <?php endfor;?>
[/php]

Example code:

[php]<?
$query=“select * from members”;
$result=mysql_query($query);

$cols=3;		// Here we define the number of columns
echo "<table>";	// The container table with $cols columns
do{
	echo "<tr>";
	for($i=1;$i<=$cols;$i++){	// All the rows will have $cols columns even if
								// the records are less than $cols
		$row=mysql_fetch_array($result);
		if($row){
			$img = $row['image_path'];

?>









<?=$row['name'] ?>

<?=$row['email'] ?>

<?=$row['password'] ?>

 

<? } else{ echo " "; //If there are no more records at the end, add a blank column } } } while($row); echo ""; ?>[/php]

Karma is in the sites help section. But, to save time, you add Karma by pressing the small (+) next to
a member’s name when they post. You can look back a couple of posts and press it next to his name!

As far as the two codes you posted goes, your code is Wordpress and it is for printing one address.
The sample code reads a database and pulls ALL of the addresses into an array and then parses thru
and prints them three across. You would have to try to use Wordpress’s commands for loading ALL
of the addresses instead of just one at a time, then combine that with the sample code. You might
want to ask this question on the Wordpress forum as they probably already had this question asked.

Not sure if this helps or not. Good luck.

Sponsor our Newsletter | Privacy Policy | Terms of Service