0

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_*.

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for the answer. The only problem is that summa 0.01 are 4 rows and 0.02 are 3 rows. It should be 0.01 - 3 rows, 0.02 - 4 rows. Also, the first row is only 1 result. The last one row should contain 1 result, not the first one. prntscr.com/fnll53
After your edit, the situation is better but why the first row is empty (only 1 record). The last row should be with 1 record. prntscr.com/fnlnda
It's because the order of records from the database, your first record satisfies the second option.
Oh, yes, now I understand. Also, first $leftAdded = false; is not needed. Thank you very much for your efforts to help me! :) +1
0

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 } ?>

1 Comment

Sorry, that is not what I need.
0

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++;
   } ?>

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.