2017-05-09 6 views
-2

に参加しますすべてのセグメント可能な、何も誰かが助けることができるなら、私は私の偉大な、 は私が目を試してみましたショーの名前は、私はこのクエリを持っているID

なしの答えと多くのことを、試してみました

Uに感謝します、私と一緒に働いていません遠く正解から

select * from countrysegments inner join country on countrysegments.country_id = country.country inner join segments on countrysegments.segment_id = segments.segment

iknowイム誰でも助けることができるしてくださいますか?

COUNTRY_IDは segment_idには、セグメントテーブルのidの外部キーである国のテーブルのidのforeginキーである

私のデータベーススキーマ:

テーブル名:countrysegments

id  country_id segment_id 

テーブル名:国

id  country 

テーブル名:セグメントは

id   segment 

これはClass.phpとである:public function select(){ $stmt = $this->conn->prepare("SELECT country FROM") or die($this->conn->error); if($stmt->execute()){ $result = $stmt->get_result(); return $result; } }

、これは、このソリューションでindex.php`

      <th class="text-center">country</th> 

          <th class="text-center">segments</th> 
      </thead> 
      <tbody> 
      <?php 

       require 'class.php'; 

       $conn = new db_class(); 
       $read = $conn->select(); 

       while($fetch = $read->fetch_array(MYSQLI_ASSOC)){ 
             foreach($fetch as $field=>$value){ 
    echo '<tr><td>' . $value . '</td>'; 
} 


}  


      ?> 




      </tbody> 
     </table>` 

に私は、クエリを持っています国を表示しますが、すべてのセグメントをすべての国でドロップダウンメニューを使って表示する必要があります

私はそれが国を必要としたり、アルの国のすべてのセグメントを取得するためにwhere句を削除することができ、その後、あなたは結果と遊ぶことができるのすべてのセグメントを与えるすべての

+0

DB内のテーブルを表示 –

+0

セグメントは:idセグメント国:id国@BilalAhmed –

+0

テーブル構造を表示します。サンプル出力 –

答えて

0
select country.id, country.country,segment.segment from countrysegments 
inner join country on countrysegments.country_id = country.id 
inner join segments on countrysegments.segment_id = segments.id where country.id = (any id of country). 

あなたの助けを必要としてください。

1

私のバージョンにはいくつかのコードがあります。 ;)

<table> 
<thead> 
    <tr> 
     <th class="text-center">country</th> 
     <th class="text-center">segments</th> 
    </tr> 
</thead> 
<tbody> 
<?php 
    require 'class.php'; 

    $conn = new db_class(); 
    $read = $conn->select(); // <-- here you'd call a query like this: 
/* 
"SELECT country.id AS countryID, country.country, segments.id AS segmentID, segments.segment 
FROM countrysegments 
inner join country on countrysegments.country_id = country.id 
inner join segments on countrysegments.segment_id = segments.id 
ORDER BY country.id, segments.id " 
*/ 

// Then do some transformation for easier readability when creating the table!! 
    $countryId = 0; 
    $countries = []; 
    while($fetch = $read->fetch_array(MYSQLI_ASSOC)) { 
     if($countryId != $fetch['countryID']){ 
      $countryId = $fetch['countryID']; 

      $countries[$countryId] = [ 
       'country' => $fetch['country'], 
       'segments' => [], 
      ]; 
     } 

     $countries[$countryId]['segments'][] = [ 
      'segmentID' => $fetch['segmentID'], 
      'segment' => $fetch['segment'], 
     ]; 
    } 

    // Here you can read the code to build the table easily ;) 
    foreach($countries as $country){ 
     echo "<tr>"; 
      echo "<td>{$country['country']}</td>"; 
      echo "<td><select>"; 
      foreach($country['segments'] as $segment){ 
       echo "<option value=\"{$segment['segmentID']}\">{$segment['segment']}</option>"; 
      } 
      echo "</select></td>"; 
     echo "</tr>"; 
    } 
    ?> 
</tbody> 
</table> 

これが役に立ちます。 :)

関連する問題