...

View Full Version : foreach a query with 5 'things'.



simolokid
01-30-2010, 09:32 PM
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 ^^ :)

bdl
01-30-2010, 09:35 PM
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.

thekooliest
01-31-2010, 03:55 AM
Please supply me with more PHP and MySQL code (mainly the query is what I'm looking for) so I can help you.

-Sam

MattF
01-31-2010, 09:57 AM
while ($row = mysqli_fetch_assoc($query))
{
print('<tr><td>'.$row['id'].'</td>');
print('<tr><td>'.$row['username'].'</td>');
print('<td>'.$row['email'].'</td></tr>');
}

simolokid
01-31-2010, 12:52 PM
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:


$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! :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum