dsdsds12222 2019-03-04 09:31
浏览 73
已采纳

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 } } }
  • 写回答

2条回答 默认 最新

  • douci1541 2019-03-04 09:37
    关注

    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?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!