Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #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

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,866
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    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>";
             
    ?>
    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:

    joecanty12345 (12-06-2012)

  • #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 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.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 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 != && $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.

  • Users who have thanked Fou-Lu for this post:

    joecanty12345 (12-06-2012)

  • #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 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?

  • #6
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,866
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    You may set margin or padding in CSS to the td selector.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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;.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •