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 5 of 5
  1. #1
    New Coder
    Join Date
    Jan 2010
    Posts
    29
    Thanks
    1
    Thanked 1 Time in 1 Post

    foreach a query with 5 'things'.

    Hi there,

    Second topic!

    How would I foreach to an array containing 5 elements? id, username, email etc. for each member there is, and how would that come together into a table?

    I tried these ways:

    foreach($id as $key1){
    echo "<tr><td>$key1</td>"
    foreach($key1 as $key2){
    echo "<td>$key2</td>";
    foreach($key2 as $key3{
    echo "<td>$key3</td>
    foreach $key3 as $key4

    etc...

    didn't work.

    Then i tried doing result['nameofheader'] way. so instead of key1/2/3/4 i tried $result[id] as $result[email] etc.

    That didn't work either.

    Then i tried while($result = mysqli_fetch_array($query)){
    echo "lots of td closing and opening and tr's on the side within tds: $result[id'] etc."

    Didnt work..

    Can someone give me a clue? =P

    Thanks ^^

  • #2
    bdl
    bdl is offline
    Regular Coder
    Join Date
    Apr 2007
    Location
    Camarillo, CA US
    Posts
    590
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Before we go there, what does the array look like? Is it data coming from each record in your MySQL query statement or something else? Show us an example data set in an array and we can provide a solution for traversing that array.

  • #3
    New Coder
    Join Date
    Mar 2009
    Posts
    25
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Please supply me with more PHP and MySQL code (mainly the query is what I'm looking for) so I can help you.

    -Sam

  • #4
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Code:
    while ($row = mysqli_fetch_assoc($query))
    {
        print('<tr><td>'.$row['id'].'</td>');
        print('<tr><td>'.$row['username'].'</td>');
        print('<td>'.$row['email'].'</td></tr>');
    }

  • #5
    New Coder
    Join Date
    Jan 2010
    Posts
    29
    Thanks
    1
    Thanked 1 Time in 1 Post
    Quote Originally Posted by MattF View Post
    Code:
    while ($row = mysqli_fetch_assoc($query))
    {
        print('<tr><td>'.$row['id'].'</td>');
        print('<tr><td>'.$row['username'].'</td>');
        print('<td>'.$row['email'].'</td></tr>');
    }
    Thanks for this one, it worked out:

    PHP Code:
    $query1  mysqli_query($link,
    "SELECT id, is_admin, username, email FROM members") or die("SQL error: ".mysqli_error($link));
       
    # Begin tabel
        
    echo '<table>';
        echo 
    "<tr><td>ID </td><td> is_admin </td><td>username</td><td>email </td></tr>";
        
      while(
    $row mysqli_fetch_assoc($query1)) { 
        
          
    # Geeft lijst met username en email van alle gebruikers terug
        
    echo '<tr><td>'.$row['id'].'</td><td>'.$row['is_admin'].'</td><td>'.$row['username'].'</td><td>'.$row['email'].'</td></tr>'
      } 
      
      
    # Einde tabel
        
    echo '</table>';
    ?> 
    Thanks for that beginning

    Kind regards!


  •  

    Posting Permissions

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