1

ive got 1 database table contains row:

TABLE FROM reservations:

attandee01  
attandee02  
attandee03  
attandee04  
attandee05  
attandee06

PHP CODE:

$q = $db->query("SELECT * FROM bs_events
LEFT JOIN bs_reservations ON bs_reservations.id_event = bs_events.id");

while($r = $q->fetch_array(MYSQLI_ASSOC)):
   echo '<td>' . $r['attandee1'] . '</td>';
   echo '<td>' . $r['attandee2'] . '</td>'
   echo '<td>' . $r['attandee3'] . '</td>'
endwhile;

or is there any simple way using foreach to echo attandee1 - attandee10?

7
  • Why dobn't you use mysql_ ? database is sensitive specially on large websites , using classes to deal with the database might slowdown your server . Commented Feb 15, 2011 at 11:35
  • 4
    @Ronan Congratulations, you won "Most ridiculous comment" award! Commented Feb 15, 2011 at 11:41
  • @Col. Shrapnel lol , but have you ever been the owner of a large website that gets like 5 million page views a day ? and there were no place to use caching because the data need to be up-to-date ? Commented Feb 15, 2011 at 11:48
  • You should consider changing your DB design... it seems you have hardcoded the number of attendees. Commented Feb 15, 2011 at 12:12
  • 1
    @Ronan Congratulations - you won the 2nd Most ridiculous comment as well ... 5 million page view does not mean you can not apply cache ... Commented Feb 15, 2011 at 12:18

6 Answers 6

4
$q = $db->query("SELECT * FROM bs_events
LEFT JOIN bs_reservations ON bs_reservations.id_event = bs_events.id");

while($r = $q->fetch_array(MYSQLI_ASSOC)):
   foreach($r as $value) {
       echo '<td>' . $value . '</td>';
   }
endwhile;

Should echo each column value per table row.

EDIT: If you only want the attendees:

$q = $db->query("SELECT * FROM bs_events
LEFT JOIN bs_reservations ON bs_reservations.id_event = bs_events.id");

while($r = $q->fetch_array(MYSQLI_ASSOC)):
   for($i = 1; $i < 11 $i++) {
       echo '<td>' . $r['attendee'.$i] . '</td>';
   }
endwhile;
Sign up to request clarification or add additional context in comments.

Comments

1

you can use foreach, of course, as $r is a regular array and can be iterated using foreach() operator. Did you try it?

However, looking at the field names, I suspect serious design flaw in your data structure.
It seems attandees should be stored in another table.

You may also consider using templates. Printing data directly from the database loop is very bad practice. You have to get all your data first and only then start printing it out.

Comments

1

Well yes:

for($i=1; $i<11; ++$i) {
  echo "<td>".$r["attandee".$i]. "</td>";
}

But that is columns, what you want is the row based solution for that I'd use something like:

for($r=$q->fetch_assoc(); !is_null($r); $r= $q->fetch_assoc()) {
   echo "<td>".array_pop($r)."</td>"; // output the only element in the array?
}
$q->free(); // don't forget to free the memory of the result set!

1 Comment

thanks! just like i wanted! but it echoing the row that is empty!
1

don't know if i understood your question... is this what you're looking for:

while($row = $q->fetch_array(MYSQLI_ASSOC)):
    foreach($row as $field){
       echo '<td>' . $field . '</td>';
    }
endwhile;

Comments

0

Not really sure if this is what you are looking for, but give it a shot:

while($r = $q->fetch_array(MYSQLI_ASSOC)) {
   foreach($r as $value) {
      echo '<td>' . $value . '</td>';
   }
}

Comments

0

As example you can use alternate syntax.

<table>
<?php foreach ($mysqli->query($sql) as $row ): ?>
<tr><td><?php echo $row['row_name1']; ?></td><td><?php echo $row['row_name2']; ?></td></tr>
<?php endforeach; ?> 
</table>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.