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 8 of 8
  1. #1
    New Coder
    Join Date
    Nov 2010
    Location
    Kenya
    Posts
    42
    Thanks
    4
    Thanked 1 Time in 1 Post

    Display mysql data in a dynamically generated table

    Hi, i want to display data fetched from the database in a 3x3 table or a 4x3 table but i'm having trouble looping the rows correctly. Here's 2 sample code

    PHP Code:
    echo "<table border=\"0\">";
    //...some code
    $query mysql_query($sql);
    while (
    $row mysql_fetch_array($query) {
    //loop table rows and columns here
    }
    echo 
    "</table>"

    Thanks in advance.

  • #2
    Regular Coder cyrus709's Avatar
    Join Date
    Dec 2008
    Posts
    102
    Thanks
    22
    Thanked 5 Times in 5 Posts
    PHP Code:
    echo "<table border=\"0\">"
    //...some code 
    $query mysql_query($sql); 
    while (
    $row mysql_fetch_array($query) { 
    //loop table rows and columns here 
    echo "<tr><td>" $row['my_row_name_here'] . "</td></tr>";

    echo 
    "</table>"
    Basically just add your html for the table data every time you loop.
    Last night I lay in bed looking up at the stars in the sky and I thought to myself, where the heck is the ceiling.

  • #3
    New Coder
    Join Date
    Nov 2010
    Location
    Kenya
    Posts
    42
    Thanks
    4
    Thanked 1 Time in 1 Post
    Here's a sample of how i want data to be displayed
    [HTML]
    <table border="0">
    <tr><td>item1</td><td>item2</td><td>item3</td></tr>
    <tr><td>item4</td><td>item5</td><td>item6</td></tr></table>[/HTML]

  • #4
    New Coder
    Join Date
    Nov 2010
    Location
    Kenya
    Posts
    42
    Thanks
    4
    Thanked 1 Time in 1 Post
    Quote Originally Posted by cyrus709 View Post
    PHP Code:
    echo "<table border=\"0\">"
    //...some code 
    $query mysql_query($sql); 
    while (
    $row mysql_fetch_array($query) { 
    //loop table rows and columns here 
    echo "<tr><td>" $row['my_row_name_here'] . "</td></tr>";

    echo 
    "</table>"
    Basically just add your html for the table data every time you loop.
    the loop you stated will loop a pre-determined number of rows or columns. I want a loop statement that will allow each item to be in its own cell. Just like how online catalogues display their products. Here's a sample of how i want data to be displayed

    <table border="0">
    <tr>
    <td>item1</td>
    <td>item2</td>
    <td>item3</td>
    </tr>
    <tr>
    <td>item4</td>
    <td>item5</td>
    <td>item6</td>
    </tr>
    </table>

  • #5
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    Assuming each row in $query contains just 1 field, then one option is to set a counter going from 1 to the number of items you want on each html table row.

    So in your case you want 3 items per <tr>. When the counter reaches 3, close the <tr>, reset the counter to 1, open another <tr> and continue looping through $query

  • #6
    New Coder
    Join Date
    Nov 2010
    Location
    Kenya
    Posts
    42
    Thanks
    4
    Thanked 1 Time in 1 Post
    Quote Originally Posted by bullant View Post
    Assuming each row in $query contains just 1 field, then one option is to set a counter going from 1 to the number of items you want on each html table row.

    So in your case you want 3 items per <tr>. When the counter reaches 3, close the <tr>, reset the counter to 1, open another <tr> and continue looping through $query

    no, each query has more details relating to each item such as photo n price

  • #7
    Regular Coder cyrus709's Avatar
    Join Date
    Dec 2008
    Posts
    102
    Thanks
    22
    Thanked 5 Times in 5 Posts
    Quote Originally Posted by bngari View Post
    no, each query has more details relating to each item such as photo n price

    Does each row that you are retrieving contain more then one detail? or are there multiple rows each one retrieving a different detail? ex a row for price another row for photo
    If it is the latter do what bullant said (as that was a repeat of his question). Otherwise we will need more information about your script. such as how your splitting up the information from a row and where it needs to go.
    Last night I lay in bed looking up at the stars in the sky and I thought to myself, where the heck is the ceiling.

  • #8
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    Quote Originally Posted by bngari View Post
    no, each query has more details relating to each item such as photo n price
    ok, then it looks like you're only drip feeding information about what your data looks like and since my crystal ball isn't back from its 1000 year service yet, I can't use it to see exactly what you have in your code and data. Hopefully someone else will be able to help.


  •  

    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
    •