Show single button if dates are same

hi all

i am showing products records from database

while($row=mysql_fetch_array($result))
{
    echo $row['date'];
    echo $row['product'];
    echo $row['price'];
    echo "<a href=''>button</a>";

}

here is the result

2016-08-10  samsung  300.00  button
2016-08-10  nokia    120.00  button
2016-08-10  lg       80.00   button

2016-08-05  samsung  200.00  button

2016-08-01  nokia    180.00  button

In the above you see there is individual button for every individual row.

i want to have only one button instead of 3 individual buttons if the records dates are same.

means for date 2016-08-10 i want to show only one button.

how to show single button for date 2016-08-10.

vineet

The principle goes something like this:

$lastSeenDate = '';
while ($row = mysql_fetch_array($result))
{
    echo $row['date'];
    echo $row['product'];
    echo $row['price'];
    if ($row['date'] !== $lastSeenDate)
    {
        echo "<a href=''>button</a>";
    }
    $lastSeenDate = $row['date'];
}

As an aside, be advised that the mysql extension is deprecated. You should switch to mysqli (note the i at the end) or PDO. I would personally recommend the latter.

Thanks ScallioXTX

working perfect

vineet

hi

why is the below code showing extra button on the right side of table ??

i am merging last two < td >< /td > with rowspan.

so the extra button should get omit.

but that button is getting echo again. why ??

see the screenshot

<table width="901" cellpadding="6" cellspacing="0">
     
           <tr>
        <td>ID </td>
        <td>Date </td>
        <td>User Email</td>
        <td>Product Name </td>
        <td>Price</td>
        <td>Quantity</td>
        <td>Total Cost</td>
           <td>Action</td>
      </tr>
      <?php
      
    $lastSeenDate = '';
    
    $mnum = mysql_num_rows($result);
    if(mysql_num_rows($result)>0)
    {
        while($row=mysql_fetch_array($result))
        {
                                
            echo "<tr>";
            echo "<td>". $row['id'] . "</td>";
            echo "<td>". $row['date'] . "</td>";
            echo "<td>". $row['user_email'] . "</td>";
            echo "<td>". $row['product_name'] . "</td>";
            echo "<td>". $row['price'] . "</td>";
            echo "<td>". $row['quantity'] . "</td>";
            echo "<td>". $row['total_cost'] . "</td>";
            
            if($row['date'] !== $lastSeenDate)
            {
            echo "<td rowspan=$mnum>". "<a href''>SEND MSG</a>". "</td>";            
            }
            $lastSeenDate = $row['date'];
            echo "</tr>";    
            }
    }
    ?>
    </table>

vineet

One problem is that you set the rowspan value to be $mnum, which is the total number of rows you return from the query. What if you return five rows in total, the first two are the same and the next three are all different? You’ll have a rowspan=5 in there. You need a separate counter for the rowspan value which gets reset to 1 any time the date changes. Presumably your query sorts by date primarily?

In your example above, it performs the query which returns two rows. You then read the first row, see that the date in that row is different from the last seen date (because that’s still empty) and so you put “rowspan=2” in the button. You then read the second row, that’s got a different date as well, so then you put “rowspan=2” in that as well.

hi droopsnoot

instead of date i decided to combine results with user email

as suggested by you, i tried and added a separate counter for rowspan

 <?php
      
    $lastSeenUser = '';
    $rowcounter=0;

    if(mysql_num_rows($result)>0)
    {
        while($row=mysql_fetch_array($result))
        {
                                
            echo "<tr>";
            echo "<td>". $row['id'] . "</td>";
            echo "<td>". $row['date'] . "</td>";
            echo "<td>". $row['user_email'] . "</td>";
            echo "<td>". $row['product_name'] . "</td>";
            echo "<td>". $row['price'] . "</td>";
            echo "<td>". $row['quantity'] . "</td>";
            echo "<td>". $row['total_cost'] . "</td>";
            
            if($row['user_email'] !== $lastSeenUser)
            {
            $rowcounter++;
            echo "<td rowspan=$rowcounter>". "<a href=''>SEND MSG</a>". "</td>";            
            }
            $lastSeenUser = $row['user_email'];
            echo "</tr>";    
            }
    }
    ?>

but still not getting rowspan desired result

what am i missing ??

vineet

You need to increment the counter when the email address hasn’t changed, and reset it when it does change.

if($row['user_email'] !== $lastSeenUser)
        {
        echo "<td rowspan=$rowcounter>". "<a href=''>SEND MSG</a>". "</td>";            
        $rowcounter = 1; // 
        }
else
  {
  $rowcounter++;
  }

But, that’s not going to work properly. You want to draw the button at the end of the last row for a group of email addresses, but you’re drawing it at the end of the first row of a new email address, after you’ve drawn the first row with the new address. I’d say the logic needs to be something like:

get next record {
    if (email != last-email) { 
      if (last-email != blank ) { // only do this if it's not the first time through
        draw the button
        reset the row coutner
        }  // end of if last-email not blank
      } // end of if email has changed
    draw record contents
    } // end of get-next-record loop
draw button for final record

Can’t edit the post now, but the last section might not be true, it might work the way you’ve been doing it.

hi droopsnoot

But the way i tried in post #6 it didnt worked

can you explain more on what you want to describe

vineet

It’s a bit hard to be sure without being able to test it out. But “still not getting desired result” doesn’t help a lot - you need to show what result you’re getting, and why it’s not what you want, as you did earlier.

But the counter is updating incorrectly - you need to reset it when you draw the button, and increment it when you don’t draw the button. The times you are not drawing the button are the “extra” rows that you want to specify in your “rowspan” value, and without testing I also can quite figure out whether you want to be base one or zero.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.