Reputation: 35
Currently, my plan is to add a gender icon next to the username. The problem is that even if there are more than 1 table record in the table, it shows only 1 row and some records suddenly gone. This is how it looks like:
The code I am using:
$pdo = new PDO('connection');
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt1 = $pdo->prepare("SELECT id, user_id, user, bid, date
FROM auction_bids ORDER BY date DESC");
$stmt2 = $pdo->prepare("SELECT user_id, user, bid, date
FROM auction_bids ORDER BY date DESC LIMIT 30");
$stmt11 = $pdo->prepare("SELECT pol FROM tb_users WHERE id = :user_id");
$stmt2->execute();
$r1 = $stmt1->fetch(PDO::FETCH_ASSOC);
$stmt11->execute(array(':user_id' => $r1['user_id']));
$id1 = $r1['id'];
echo '<table>';
while ($r1 = $stmt2->fetch(PDO::FETCH_ASSOC) && $r2 = $stmt11- >fetch(PDO::FETCH_ASSOC)) {
echo '<tr>
<td>' . $id1 . '</td>
<td><img height="16" width="16" alt="gender icon" src
="../images/' . ($r2['pol'] == 1 ? 'male.png' : 'female.png') . '" />
' . $r1['user'] . '</td>
<td class="border-right">' . $r10['level'] . '</td>
<td>' . $r1['bid'] . ' FA</td>
<td><img height="16" width="16" alt="calendar" src
="../images/calendar.png" />
' . date($dateFormatBids, strtotime($r1['date'])) . '</td>
</tr>
';
$id1--;
}
echo '<table>
Using while ($r1 = $stmt2->fetch(PDO::FETCH_ASSOC)) {..}
loop without $r2
, it is working (except that the gender color is the same, for test it should be red, for Lukas, should be blue):
If there is something that you need, please say. I am quite new to PHP and PDO.
Upvotes: 0
Views: 369
Reputation: 3850
from the line: $stmt11->execute(array(':user_id' => $r1['user_id']));
you are getting a single record so the while loop prints single row.
Write the query like:
$stmt2 = $pdo->prepare("SELECT tu.pol, user_id, user, bid, date
FROM auction_bids ab left join tb_users tu on ab.user_id = tu.id
ORDER BY date DESC LIMIT 30");
now :
while ($r1 = $stmt2->fetch(PDO::FETCH_ASSOC){
//rest of the code should work... user $r1['pol'] instead of $r2
}
Upvotes: 1
Reputation: 339
Check that link to understand while loop : http://www.w3schools.com/js/js_loop_while.asp
The problem you have condition should TRUE, and you ($r1 && $r2) = TRUE only when both Variable are TRUE, $r2 first time it's TRUE both second time will get FALSE.
You should loop for only $r1 and call $r1 inside the loop.
Upvotes: 0