0

I have saved some values as comma separated in table 1 column.

I want to fetch all rows containing values from table 2 where these comma separated values are.

But my current code is fetching only one row with first value.

Tables structure is like below :

Table 1

id   |  faculties           | another_field
1    |  DFGHFGD1,ERTGDFGE2  | xyzxyzxyz

Table 2

id   |  unique_id   | full_name
1    |  DFGHFGD1    | AAA AAA
2    |  THOGHFGD1   | BBB BBB
3    |  ERTGDFGE2   | CCC CCC
4    |  LLMNBHUH1   | DDD DDD
5    |  HGFGDHHD3   | EEE EEE
6    |  RGDJHFGN4   | FFF FFF
7    |  PLYHTGGD1   | HHH HHH

Now I want to fetch both rows with unique_id = DFGHFGD1 And ERTGDFGE2 in Table 2.

I tried following code. But it is fetching only first value(DFGHFGD1) row.

 <?php 
        $faculty ="DFGHFGD1,ERTGDFGE2"; // Fetch with another query for table 1
        $var = explode(",", $faculty );
        ?>
          <ol>
           <?php
             foreach($var as  $value){
             $fquery = "select * from table_2 where unique_id = '$value'";          
             $result = $database->get_results($fquery);
                foreach($result as $data){
            ?> 
                <li><?php echo $data['full_name'];?></li>
            <?php }  ?>
          </ol>
  <?php } ?>

Result should be like

  1. AAA AAA
  2. CCC CCC

But I am Getting only

  1. AAA AAA
darshan
  • 319
  • 3
  • 16
  • "I have saved some values as comma separated in table 1 column." -- Big mistake! Read ["Is storing a delimited list in a database column really that bad?"](https://stackoverflow.com/questions/3653462/is-storing-a-delimited-list-in-a-database-column-really-that-bad?r=SearchResults&s=1|192.6363) and normalize the schema. – sticky bit May 04 '20 at 11:36
  • Are you sure the `` is in the right place, should it be outside both the `foreach()` loops? – Nigel Ren May 04 '20 at 11:38
  • @stickybit should I save values in table 1 with comma separate in separate mysql table in rows ? – darshan May 04 '20 at 11:38
  • @NigelRen I changes place of
      and
    and tried just now but same result
    – darshan May 04 '20 at 11:40
  • @darshan: Yes, exactly. – sticky bit May 04 '20 at 12:01
  • @stickybit I have created new table to store id and unique_ids in different row... but when I fetch, till it is returning only first value... loop is not working... – darshan May 04 '20 at 13:21

1 Answers1

1

<ol> Tag will be outside of first foreach loop.

Correct code will be :

<?php 
$faculty ="DFGHFGD1,ERTGDFGE2";
$var = explode(",", $faculty);
?>
<ol>
<?php
foreach($var as $value) {
  $fquery = "select * from `table_2` where `unique_id` = '$value'"; 
  $result = $database->get_results($fquery);
  foreach($result as $data){ ?>
    <li><?php echo $data['full_name']; ?></li>
  <?php 
  } 
}
?></ol>
MILAN SAHANA
  • 76
  • 11
  • Code-only answers are discouraged. Please click on [edit] and add some words explaining how your code addresses the question. Thanks – Nick May 04 '20 at 12:15