2017-05-09 59 views
-2

我有這個疑問:顯示從內部名稱與編號加入

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

所有我需要知道的是如何顯示錶的國家內的國家名稱,

,各國家展所有段可用,沒有什麼與我的工作,如果有人可以幫助我將我很大的, 感謝ü

我嘗試了很多沒有答案,

我想個是:

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

iknow IM遠離正確的答案,但請誰能幫助?

COUNTRY_ID是id的國家表foregin關鍵 SEGMENT_ID是id的分段表的外鍵

我的數據庫模式:

表名: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>` 

這種解決方案我只有查詢向我展示這些國家,但我需要在每個國家/地區使用下拉菜單展示所有細分受衆羣

請我需要你的幫助,所有

+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). 

它會給各階層需要的國家,也可以刪除where子句,以獲得人國家的所有部分,然後你可以玩的結果。

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> 

希望這會有所幫助。 :)