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 01-30-2010, 08:32 PM   PM User | #1
simolokid
New Coder

 
Join Date: Jan 2010
Posts: 29
Thanks: 1
Thanked 1 Time in 1 Post
simolokid is an unknown quantity at this point
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 ^^
simolokid is offline   Reply With Quote
Old 01-30-2010, 08:35 PM   PM User | #2
bdl
Regular Coder

 
Join Date: Apr 2007
Location: Camarillo, CA US
Posts: 590
Thanks: 4
Thanked 83 Times in 82 Posts
bdl is an unknown quantity at this point
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.
bdl is offline   Reply With Quote
Old 01-31-2010, 02:55 AM   PM User | #3
thekooliest
New Coder

 
Join Date: Mar 2009
Posts: 25
Thanks: 2
Thanked 3 Times in 3 Posts
thekooliest is an unknown quantity at this point
Please supply me with more PHP and MySQL code (mainly the query is what I'm looking for) so I can help you.

-Sam
thekooliest is offline   Reply With Quote
Old 01-31-2010, 08:57 AM   PM User | #4
MattF
Senior Coder

 
Join Date: Jul 2009
Location: South Yorkshire, England
Posts: 2,322
Thanks: 6
Thanked 304 Times in 303 Posts
MattF will become famous soon enoughMattF will become famous soon enough
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>');
}
MattF is offline   Reply With Quote
Old 01-31-2010, 11:52 AM   PM User | #5
simolokid
New Coder

 
Join Date: Jan 2010
Posts: 29
Thanks: 1
Thanked 1 Time in 1 Post
simolokid is an unknown quantity at this point
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!
simolokid is offline   Reply With Quote
Reply

Bookmarks

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 10:31 AM.


Advertisement
Log in to turn off these ads.