Enjoy an ad free experience by logging in. Not a member yet?
Register .
12-05-2012, 01:57 PM
PM User |
#1
New to the CF scene
Join Date: Jan 2008
Posts: 4
Thanks: 2
Thanked 0 Times in 0 Posts
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 <ul><li>:
How can I make it display the results in a 3 column table like:
Here is the code I currently have:
Code:
<h2><a name="locations" style="color:#fff">Office Locations</a></h2>
<?php $total=get_post_meta($post->ID,'total_addresses',true);?>
<ul style="list-style: none;">
<?php for($i=1;$i<=$total;$i++): ?>
<?php $address=get_post_meta($post->ID,"em_address_$i",true); ?>
<li><?php echo $address; ?></li>
<?php endfor;?>
</ul>
Please forgive me if I have not provided enough information. I am imagining that is the only area that needs edited
12-06-2012, 10:29 AM
PM User |
#2
Supreme Master coder!
Join Date: Mar 2007
Location: N/A
Posts: 14,678
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
Quote:
PHP Code:
<ul style="list-style: none;">
<?php for( $i = 1 ; $i <= $total ; $i ++): ?>
<?php $address = get_post_meta ( $post -> ID , "em_address_$i" , true ); ?>
<li><?php echo $address ; ?> </li>
<?php endfor; ?>
</ul>
Instead of the above,
Try
PHP Code:
<?php
echo '<table>' ;
for( $i = 1 ; $i <= $total ; $i ++):
if(( $i - 1 )% 3 = 0 ) echo '<tr>' ;
$address = get_post_meta ( $post -> ID , "em_address_$i" , true );
echo "<td> $address </td>" ;
if(( $i - 1 )% 3 = 0 ) echo '</tr>' ;
endfor;
echo "</table>" ;
?>
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
Users who have thanked abduraooft for this post:
12-06-2012, 02:30 PM
PM User |
#3
New to the CF scene
Join Date: Jan 2008
Posts: 4
Thanks: 2
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by
abduraooft
Instead of the above,
Try
PHP Code:
<?php
echo '<table>' ;
for( $i = 1 ; $i <= $total ; $i ++):
if(( $i - 1 )% 3 = 0 ) echo '<tr>' ;
$address = get_post_meta ( $post -> ID , "em_address_$i" , true );
echo "<td> $address </td>" ;
if(( $i - 1 )% 3 = 0 ) echo '</tr>' ;
endfor;
echo "</table>" ;
?>
Sorry no go! That gives a server error 500.
12-06-2012, 03:06 PM
PM User |
#4
God Emperor
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
The error may be caused by the attempted assignment to the modulus operation. I don't know why it would throw a 500 against it though. It looks to me that it won't format the </td>'s quite proper as this would open and close in the same record (instead of closing and opening).
Try this way:
PHP Code:
$iCols = 3 ;
$total = get_post_meta ( $post -> ID , 'total_addresses' , true );
$iPad = $iCols - ( $total % $iCols );
if ( $total > 0 )
{
print( '<table><tr>' );
for ( $i = 0 ; $i < $total ; ++ $i )
{
if ( $i != 0 && $i % 3 == 0 )
{
print( '</tr><tr>' );
}
printf ( '<td>%s</td>' , get_post_meta ( $post -> ID , 'em_address_' . ( $i + 1 ), true ));
}
for ( $i = 0 ; $i < $iPad ; ++ $i )
{
print( '<td> </td>' );
}
print( '</tr></table>' );
}
Not sure if you want to throw the header into the table itself or just leave it, so I left it out.
Users who have thanked Fou-Lu for this post:
12-06-2012, 03:14 PM
PM User |
#5
New to the CF scene
Join Date: Jan 2008
Posts: 4
Thanks: 2
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by
Fou-Lu
The error may be caused by the attempted assignment to the modulus operation. I don't know why it would throw a 500 against it though. It looks to me that it won't format the </td>'s quite proper as this would open and close in the same record (instead of closing and opening).
Try this way:
PHP Code:
$iCols = 3 ;
$total = get_post_meta ( $post -> ID , 'total_addresses' , true );
$iPad = $iCols - ( $total % $iCols );
if ( $total > 0 )
{
print( '<table><tr>' );
for ( $i = 0 ; $i < $total ; ++ $i )
{
if ( $i != 0 && $i % 3 == 0 )
{
print( '</tr><tr>' );
}
printf ( '<td>%s</td>' , get_post_meta ( $post -> ID , 'em_address_' . ( $i + 1 ), true ));
}
for ( $i = 0 ; $i < $iPad ; ++ $i )
{
print( '<td> </td>' );
}
print( '</tr></table>' );
}
Not sure if you want to throw the header into the table itself or just leave it, so I left it out.
THAT WORKED PERFECT!!!!!
Question though. Where would I set the spacing between columns? In the <table> tag I assume?
12-06-2012, 03:31 PM
PM User |
#6
Supreme Master coder!
Join Date: Mar 2007
Location: N/A
Posts: 14,678
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
You may set margin or padding in CSS to the td selector.
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
12-06-2012, 05:52 PM
PM User |
#7
God Emperor
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Quote:
Originally Posted by
abduraooft
You may set margin or padding in CSS to the td selector.
This for sure.
We're well past the old days of embedding anything in the HTML directly (that it doesn't need of course).
Edit:
Interesting enough, the vb tore this out: print('<td> </td>'); . I had a in there. I don't know if there is a better way to force the height of the cell aside from the nbsp;.
Jump To Top of Thread
Thread Tools
Rate This Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT +1. The time now is 08:35 AM .
Advertisement
Log in to turn off these ads.