PHP,如果productid对于另一个页面中的每个循环都是肯定的

Hope I can explain this as clear as possible:

I have a page (info.php) where cards (coupons) are claimed, these cards each have a product id. In the page, if a user selects the "claim" button, the card gets saved to another (list-claimed.php). This other page displays all cards claimed by the user within the timespan of 48 hours. Of course it only shows the claimed cards of the certain user that is logged in.

  • info.php info.php

  • list-claimed.php list-claimed.php

  • table (order) database

The table in the screenshot is where i get the data from (SELECT SQL)

Here is a snippet of the code from list-claimed.php to display the claimed cards:

  <h2 class="bold_font text-align-center">LIST OF CLAIMED CARDS</h2><br>
        <?php
          $i = 0;
          foreach($lstOrders as $rowOrder) {
          if ($rowOrder['expirationdate'] >  date("Y-m-d H:i:s")){
        ?>
          <div class="col-md-4 spacer">
            <div class="col-md-12">
              <?php
                $productid = $rowOrder['productid'];
                $rowProduct = $mcProduct->SelectObj_ProductId($db, $productid);
                $rowSelect = $mcOrder->SelectLst($db, $pageNum, $limit);
              ?>
            </div>

Now to the main concern, in my info.php I would like to show the words "You have already claimed this coupon" if the card is currently in list-claimed.php. In other words, the cards that meet the condition of getting claimed by the user within the 48 hour period.

    <?php
    foreach($lstOrders as $rowOrder) {
    if ($rowOrder['expirationdate'] <  date("Y-m-d H:i:s")) { ?>

      <div class="col-md-12 text-align-center">
        <h3 class="color-red">You have already claimed this coupon.</h3>
      </div>

    <?php } }

How can I modify the php code to get what I need?

My goal: (pseudocode)

 if ($productid is in list-claimed.php){

    <div class="col-md-12 text-align-center">
      <h3 class="color-red">You have already claimed this coupon.</h3>
    </div>
}

Any help will be appreciated, thanks!

UPDATE:

Solved this issue using an array to check if the productid of the coupon is in the list-claimed.php page using "in_array".

    <?php
     foreach($lstInfo as $rowOrder) {
       if (isset($rowOrder['productid']) && ($rowOrder['expirationdate'] >  date("Y-m-d H:i:s"))) {

         //array (visually seen in list-claimed.php)
         $claimed = array($rowOrder['productid']);

         //check if current card exists in claimed array
         if (in_array($rowProduct['productid'] , $claimed) ) {
     ?>
     <div class="col-md-12 text-align-center">
       <h3 class="color-red">You have already claimed this coupon.</h3>
    </div>
    <?php } } }

As I don't have enough to give you a custom code answer, I can give you a pseudocode answer instead:

  1. Write SQL to return all the coupons that the given user has already claimed, store in already-claimed array.
  2. Write SQL to return all the coupons. Store in all-coupons array.
  3. Write a foreach() that goes thru the all-coupons array.
  4. Check if a given coupon is in the already-claimed list by comparing the coupon ID with the IDs in the already-claimed array. If there is a match, it has already been claimed.

Does that make sense?

Requirement:

Need to allow users to save coupons and display them on another page.

Solution:

You need to first save the coupons to $_SESSION

So that you get an array of coupons like: $_SESSION['coupons']

The key of the array should be the coupon id.

General structure of this array:

array(
[789799]['name'] = 'Name Of Coupon 1',
[789799]['date'] = '2019-03-04- 10:10:10',
)

Also, you need to append coupons claimed in last session (in the window of 48 hours.)

And in the listing page,

Just check if the key (coupon id) exists in the array by isset()

if (isset($_SESSION['coupons'][$rowOrder['productid']])) {
?>
<div class="col-md-12 text-align-center">
      <h3 class="color-red">You have already claimed this coupon.</h3>
    </div>
<?php
}