PHP display results equivalent to comma separated values in the database

别说谁变了你拦得住时间么 提交于 2019-12-25 01:48:46

问题


I have a database structure say this:

table: advertisements

ad_id  | ad_memberships
   1   | 1,2
   2   | 1,2,3
   3   | 1,3
   4   | 2,3

table: memberships

mbs_id | mbs_color
   1   | red
   2   | yellow
   3   | green

I want to display the colors for the values that are there in ad_memberships field. This field contains the IDs from memberships tables which is mbs_id. Now since ad_memberships contains 1,2 then the red and yellow color should be displayed. If it contains 1,3 or 2,3 or 1,2,3 then red, green or yellow, green or red, yellow, green colors should be displayed respectively. Now, I know and I understand that I should be normalizing the database and not using a comma separated values but here in my case I need to go like this only. So the problem is I am not being able to execute this. I somehow managed to get this far:

SELECT *, GROUP_CONCAT(memberships.mbs_color) FROM advertisements
                            INNER JOIN memberships ON FIND_IN_SET(memberships.mbs_id, advertisements.ad_memberships)
                            LEFT JOIN ads_category ON advertisements.ad_category = ads_category.ac_id
                            WHERE ad_credits >= ac_credits AND ad_category = :cat AND ad_status = 'active'
                            GROUP BY advertisements.ad_id

You can see the example of the above code here http://sqlfiddle.com/#!9/746169/3 but my version of code is given above and in the code below. And this is what I exactly want https://prnt.sc/hvj91s.

PROBLEM :
Now the problem is that when I run the following code all I get is just one color throughout that is color for mbs_id 1. Say, I get red for all the field. I want all the colors to be displayed for which the values are there in the database field ad_memberships. I made it extremely clear now please help me guys. Stuck in this for too long now.

Code

<div class="ads-container">
  <?php
    $cat = $pdo->prepare("SELECT * FROM ads_category");
    $cat-> execute();

    $i = 0;
    while($s = $cat->fetch()){
      $ads = $pdo->prepare("SELECT *, GROUP_CONCAT(memberships.mbs_color) FROM advertisements
                            INNER JOIN memberships ON FIND_IN_SET(memberships.mbs_id, advertisements.ad_memberships)
                            LEFT JOIN ads_category ON advertisements.ad_category = ads_category.ac_id
                            WHERE ad_credits >= ac_credits AND ad_category = :cat AND ad_status = 'active'
                            GROUP BY advertisements.ad_id");
      $ads-> bindValue(':cat', $s['ac_id']);
      $ads-> execute();

      while($a = $ads->fetch()){ extract($a); 
  ?>
      <div class="" <?php if($i++ != 0){ echo "style='margin-top: 30px'"; } ?>>
        <i class="fa fa-bullhorn" aria-hidden="true"></i> <?php echo $ac_category; ?>
      </div>
      <div class="col-sm-4">
        <div class="adcover">
          <div class="ad-title">
            <a href="surf.php?ad=<?php echo $ad_id; ?>" target="_blank"><?php echo $ad_title; ?></a>
          </div>
          <div class="ad-footer-two">
            <span class="membership-indicator" style="background: <?php echo $mbs_color; ?>; margin-top: 4px; margin-left: 5px"></span>
          </div>
        </div>
      </div>
  <?php } } ?>
</div>

来源:https://stackoverflow.com/questions/48080764/php-display-results-equivalent-to-comma-separated-values-in-the-database

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!