Lukas Naujokaitis
Lukas Naujokaitis

Reputation: 109

PHP While loop does not return data in the one row

I have a problem using while loop. While loop works but it does not return data in the one row. I know why it is going like that but I do not know how to fix it.

$bonusai = mysql_query("SELECT summa, date FROM tb_history WHERE type = 
    'bonus' AND user_id = '$usid' ORDER BY id DESC");

while ($r1 = mysql_fetch_assoc($bonusai)) { ?>
    <tr>
        <td>
            <?php echo ($r1['summa'] == 0.01) ? date('Y-m-d H:i:s', $r1['date']) : ''; ?>
        </td>
        <td colspan="2">
            <?php echo ($r1['summa'] == 0.01) ? '+' : ''; ?>
        </td>
        <td>
            <?php echo ($r1['summa'] == 0.02) ? date('Y-m-d H:i:s', $r1['date']) : ''; ?>
        </td>
        <td colspan="2"><?php echo ($r1['summa'] == 0.02) ? '+' : ''; ?></td>
    </tr>
<?php } ?>

$bonusai query results:

enter image description here

In the website, it looks like this:enter image description here

As you can see, only one result is in the row. It should display both data in the same row like this (changed using inspect element):

enter image description here

In my opinion, I should change something with the if the ternary operator and while loop or change query structure. Maybe I should use nested while loop? All data are in the same MySQL table, so I cannot use JOIN. What should I use to make every MySQL record in the same row?

P.S I know, I should not use deprecated mysql_*.

Upvotes: 0

Views: 1095

Answers (3)

Nigel Ren
Nigel Ren

Reputation: 57121

You are trying to output 2 rows for each row in the database your fetching.

        $bonusai = mysql_query("SELECT summa, date FROM tb_history WHERE type = 
            'bonus' AND user_id = '$usid' ORDER BY id DESC");

        $leftAdded = false;
        while ($r1 = mysql_fetch_assoc($bonusai)) {
            if ($r1['summa'] == 0.01) {
               echo '<tr><td>'.date('Y-m-d H:i:s', $r1['date']) .'</td>';
               $leftAdded = true;
            }
            else {
                if ( $leftAdded == false ) {
                    echo '<tr><td></td>';
                }
                $leftAdded = false;
                echo '<td>'. date('Y-m-d H:i:s', $r1['date']) .'</td></tr>';
        } 
}

What this does is put out the tr tag with the first bit and the /tr with the second bit. The only thing is that if yo don't get a second bit with the SQL, the close tr tag will vbe missing, so yo may need to add code to catch this if it is needed.

Edit: I've added code in case the left column isn't already added.

Upvotes: 2

Bhunesh  Satpada
Bhunesh Satpada

Reputation: 810

You can Try this:

$bonusai = mysql_query("SELECT summa, date FROM tb_history WHERE type = 
    'bonus' AND user_id = '$usid' ORDER BY id DESC");
 $i=1;
while ($r1 = mysql_fetch_assoc($bonusai)) { ?>
    <?php if($i%2 != 0 ){ echo '<tr>'; ?>
        <td>
            <?php echo date('Y-m-d H:i:s', $r1['date']); ?>
        </td>
        <td>+</td>
        <td>
            <?php echo date('Y-m-d H:i:s', $r1['date']); ?>
        </td>
        <td>+</td>
    <?php if($i%2 == 0 ){ echo '</tr>'; ?>
    <?php $i++;
   } ?>

Upvotes: 0

Heena Patel
Heena Patel

Reputation: 19

You can try this code if it is helpful.

    <?php
        $sel="select * from demo";
        $res=$con->query($sel);
        while($r=$res->fetch_object())
        {
    ?>
    <tr>
        <td><input type="checkbox" name="ch[]" value="<?php echo $r->id; ?>" /></td>
        <td><?php echo $r->id; ?></td>
        <td><?php echo $r->name; ?></td>
        <td><?php echo $r->email ?></td>
        <td><?php echo $r->password; ?></td>
        <td><?php echo $r->gender; ?></td>
        <td><?php echo $r->number ?></td>
        <td><?php echo $r->address?></td>
        <td><?php echo $r->city; ?></td>
        <td><?php echo $r->hobby; ?></td>
        <td><a href="delete.php?did=<?php echo $r->id; ?>">Delete</a></td>
        <td><a href="edit.php?eid=<?php echo $r->id; ?>">Edit</a></td>
    </tr>
    <?php } ?>

Upvotes: 0

Related Questions