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条)

报告相同问题?

悬赏问题

  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 这个复选框什么作用?
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下