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 10 of 10
  1. #1
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    PHP Foreach result in new <tr>

    Hi there,

    i have the following code which puts mi results in a html table and makes a break after each result and puts them in one column.

    How can i modify this so that it displays each result in a new row <tr> instead of all the results in one column <td> ?

    PHP Code:
    <table border="1">
                <thead>
                <tr>
                    <th>MMAID</th>
                    <th>DEVICEID</th>
                    <th>TIMESTAMP</th>
                    <th>MSG</th>
                </tr>
                </thead>
                <tbody>
                <tr>
                    <td><? foreach($query_log as $row) {  echo $row->MMAID."<br>\n"; }?></td>
                    <td><? foreach($query_log as $row) {  echo $row->DEVICEID."<br>\n"; }?></td>
                    <td><? foreach($query_log as $row) {  echo $row->TIMESTAMP."<br>\n"; }?></td>
                    <td><? foreach($query_log as $row) {  echo $row->MSG."<br>\n"; }?></td>
                </tr>
                </tbody>
            </table>

  • #2
    Banned
    Join Date
    Mar 2012
    Posts
    306
    Thanks
    1
    Thanked 28 Times in 28 Posts
    Quote Originally Posted by joni.kwg View Post
    How can i modify this so that it displays each result in a new row <tr> instead of all the results in one column <td> ?
    You need a WHILE loop.

    The while loop loops through the result set from your sql query. On each pass through the loop create a tr. Then echo the column values of the current row in the result set to td's. At the bottom of the while loop close the row with </tr>

  • #3
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    thanks for the reply,

    i understand what you mean, sounds logical but i didnt manage to get it runnig correctly. I now got this:

    PHP Code:
    <td><? $i=0; while ($i 5) {echo "<tr>".$row->MSG."</tr>"$i++;}?></td>
    but it only displays it in 1 row.

    would you perhaps be so keen to write me that line of code?

  • #4
    Banned
    Join Date
    Mar 2012
    Posts
    306
    Thanks
    1
    Thanked 28 Times in 28 Posts
    Quote Originally Posted by joni.kwg View Post
    PHP Code:
    <td><? $i=0; while ($i 5) {echo "<tr>".$row->MSG."</tr>"$i++;}?></td>
    but it only displays it in 1 row.
    Have a look at the html code your php creates by using "View Source" in your browser. Your code is wrting a tr directly inside a td which is not valid html.

    You need something like this

    PHP Code:
    <table>
    <?php
    while($row=mysql_fetch_assoc($rs)){
        echo 
    '<tr>';
          
    // now echo the column data for this $row in $rs in td's
        
    echo '</tr>';
    }
    ?>
    </table>

  • #5
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    okay, thanks for your effort but there are multiple issues why i cant do it like that unfortunatelly

    1. i work with oracle not mysql
    2. i work with codeigniter and dont put queries (fetch statements) in my view only the model handled by my controller

    im going to try and try and try till i get it to work. eventually i'll figure it out.

    cheers anyways

  • #6
    Banned
    Join Date
    Mar 2012
    Posts
    306
    Thanks
    1
    Thanked 28 Times in 28 Posts
    ok but the concept of how to do it will be the same as I posted but the actual code will be different if using codeigniter and oracle.

  • #7
    New Coder
    Join Date
    May 2010
    Posts
    69
    Thanks
    3
    Thanked 10 Times in 10 Posts
    Easy to do.
    PHP Code:
    <table border="1"> 
                <thead> 
                <tr> 
                    <th>MMAID</th> 
                    <th>DEVICEID</th> 
                    <th>TIMESTAMP</th> 
                    <th>MSG</th> 
                </tr> 
                </thead> 
                <tbody> 
    <? foreach($query_log as $row) { ?>
                <tr> 
                    <td><? echo $row->MMAID."<br>\n"?></td> 
                    <td><? echo $row->DEVICEID."<br>\n"?></td> 
                    <td><? echo $row->TIMESTAMP."<br>\n"?></td> 
                    <td><? echo $row->MSG."<br>\n"?></td> 
                </tr> <? ?>
                </tbody> 
            </table>
    Like helpful posts in your topic? Thank the people offering you help :)

  • Users who have thanked rvincenten for this post:

    joni.kwg (04-05-2012)

  • #8
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,389
    Thanks
    13
    Thanked 353 Times in 349 Posts
    Quote Originally Posted by Mishu View Post
    You need a WHILE loop.

    The while loop loops through the result set from your sql query. […]
    if PDO were used (not uncommon for CodeIgniter), foreach() is the loop construct of choice.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #9
    Banned
    Join Date
    Mar 2012
    Posts
    306
    Thanks
    1
    Thanked 28 Times in 28 Posts
    Quote Originally Posted by Dormilich View Post
    if PDO were used (not uncommon for CodeIgniter), foreach() is the loop construct of choice.
    Perhaps so, but I suggested a while loop in post 2 and the op didn't reveal they are using codeigniter until post 5.

  • #10
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by rvincenten View Post
    Easy to do.
    PHP Code:
    <table border="1"> 
                <thead> 
                <tr> 
                    <th>MMAID</th> 
                    <th>DEVICEID</th> 
                    <th>TIMESTAMP</th> 
                    <th>MSG</th> 
                </tr> 
                </thead> 
                <tbody> 
    <? foreach($query_log as $row) { ?>
                <tr> 
                    <td><? echo $row->MMAID."<br>\n"?></td> 
                    <td><? echo $row->DEVICEID."<br>\n"?></td> 
                    <td><? echo $row->TIMESTAMP."<br>\n"?></td> 
                    <td><? echo $row->MSG."<br>\n"?></td> 
                </tr> <? ?>
                </tbody> 
            </table>
    Thank you very much rvincenten, this is exactly what i needed. Many thanks


  •  

    Posting Permissions

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