Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-05-2012, 01:57 PM   PM User | #1
joecanty12345
New to the CF scene

 
Join Date: Jan 2008
Posts: 4
Thanks: 2
Thanked 0 Times in 0 Posts
joecanty12345 is an unknown quantity at this point
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
joecanty12345 is offline   Reply With Quote
Old 12-06-2012, 10:29 AM   PM User | #2
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,678
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
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)
abduraooft is offline   Reply With Quote
Users who have thanked abduraooft for this post:
joecanty12345 (12-06-2012)
Old 12-06-2012, 02:30 PM   PM User | #3
joecanty12345
New to the CF scene

 
Join Date: Jan 2008
Posts: 4
Thanks: 2
Thanked 0 Times in 0 Posts
joecanty12345 is an unknown quantity at this point
Quote:
Originally Posted by abduraooft View Post
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.
joecanty12345 is offline   Reply With Quote
Old 12-06-2012, 03:06 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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 != && $i == 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.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
joecanty12345 (12-06-2012)
Old 12-06-2012, 03:14 PM   PM User | #5
joecanty12345
New to the CF scene

 
Join Date: Jan 2008
Posts: 4
Thanks: 2
Thanked 0 Times in 0 Posts
joecanty12345 is an unknown quantity at this point
Quote:
Originally Posted by Fou-Lu View Post
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 != && $i == 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?
joecanty12345 is offline   Reply With Quote
Old 12-06-2012, 03:31 PM   PM User | #6
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,678
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
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)
abduraooft is offline   Reply With Quote
Old 12-06-2012, 05:52 PM   PM User | #7
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Quote:
Originally Posted by abduraooft View Post
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 &nbsp; in there. I don't know if there is a better way to force the height of the cell aside from the nbsp;.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Tags
css, htm tables, mysql, php

Jump To Top of Thread


Thread Tools
Rate This Thread
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:35 AM.


Advertisement
Log in to turn off these ads.