0

After submitting a mysqli_query to select open/unfulfilled orders from a cart database the results is output to the screen with a standard WHILE LOOP.

while($row = mysqli_fetch_array($oresult)) { include('orders-open.php');}

orders-open.php is simply the internal part of a TABLE:

 <tr style="color:#FFF;">
    <td><?php echo $row['Buyer']; ?></td>
    <td><?php echo $row['Cart_Date']; ?></td>
    <td><?php echo $row['Item_Number']; ?></td>
    <td><?php echo $row['Item_Title']; ?></td>
    <td><?php echo $row['Item_FPrice']; ?></td>
    <td><?php echo $row['Item_Qty']; ?></td>
    </tr>

So here is my question, I want to apply a simple HTML "<HR>" tag between the records at the point where the value in $row['Buyer'] changes, so by example:

John Doe    9/11/13       123456     Item 1       $5.99     5
John Doe    9/11/13       123654     Item 2       $8.99     3
John Doe    9/9/13        321456     Item 3       $4.99     2

(HR - Horizontal Rule Tag here)

Mike Doe    9/7/13       123555     Item 1       $9.99     2
Mike Doe    9/7/13       123777     Item 2       $2.99     6

What would be the best way to write the conditional statement inside the WHILE LOOP to compare the $row[Buyer'] result to the previous $row['Buyer'] result?

1
  • Just save $row['Buyer'] value in first loop and compare it with next values (refreshing when value changes). Commented Sep 11, 2013 at 13:57

4 Answers 4

1
$first_run = TRUE;
$previous_buyer = NULL;
while($row = mysqli_fetch_array($oresult)) { 
      if($first_run) {
         $first_run = FALSE;
         $previous_buyer = $row['Buyer'];
      }
   include('orders-open.php');
}

And then in your include file:

<?php if($previous_buyer != $row['Buyer']) { 
    echo '<HR width="100%">'; 
    $previous_buyer = $row['Buyer'];
} ?>
<tr style="color:#FFF;">
    <td><?php echo $row['Buyer']; ?></td>
    <td><?php echo $row['Cart_Date']; ?></td>
    <td><?php echo $row['Item_Number']; ?></td>
    <td><?php echo $row['Item_Title']; ?></td>
    <td><?php echo $row['Item_FPrice']; ?></td>
    <td><?php echo $row['Item_Qty']; ?></td>
</tr>
Sign up to request clarification or add additional context in comments.

2 Comments

It occurs to me after the fact that you are inside a table. I'm not sure how well <HR> will render between Table Rows. You might want to replace echo '<HR width="100%">'; with <TR><TD colspan=6><HR width="100%"></TD></TR>
Thank you, this did work but the HR tag did not render as you mentioned (Actually it did... but it rendered at top of table and not between records) - After the change mentioned in your comment it all rendered properly, thank you.
0
$prev_buyer = '';
while($row = mysqli_fetch_array($oresult))
{
  if($prev_buyer !== $row['Buyer'])
  {
    //Do Something
  }

  include('orders-open.php');

  $prev_buyer = $row['Buyer']
}

Comments

0

Just as Elon said above - change this:

while($row = mysqli_fetch_array($oresult)) { include('orders-open.php');}

to

$old_buyer = null;
while($row = mysqli_fetch_array($oresult)) {
    if ($row['Buyer'] != $old_buyer) {
        echo '<hr>';
    }
    $old_buyer = $row['Buyer'];
    include('orders-open.php');
}

5 Comments

The $old_buyer variable declaration needs to happen inside the if block for it to work.
$old_buyer will be assigned on every iteration instead of just in case it's different but it will still work this way.
Hmm, ok, it will work. But it's not necessary to update the variable if it's not different.
Agreed :) Refactoring makes perfect.
-1 This code will display a horizontal rule on the first row.
0

You could do it like this:

$buyer = null ;  //Cache buyer in a variable, because $row is reset in the loop.

while($row = mysqli_fetch_array($oresult)) {
   include('orders-open.php');
   if ($buyer !== $row["Buyer"]){
     echo ($buyer !== null) ? "<hr/>" : "" ;
     $buyer = $row["Buyer"] ;
   }
}

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.