显示来自内部联接的名称

时间:2017-05-09 11:05:33

标签: php arrays oop mysqli

我有这个问题:

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

我知道我的答案很正确,但请有人帮忙吗?

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>`

使用此解决方案我只有查询向我显示国家/地区,但我需要使用下拉菜单在每个国家/地区显示所有可用的细分

我需要你的帮助

2 个答案:

答案 0 :(得分: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>

希望这会有所帮助。 :)

答案 1 :(得分:0)

UiObject loginButton = mDevice.findObject(new UiSelector().resourceId(your_package_name+":id/login_loginBtn"));

loginButton.clickAndWaitForNewWindow(15 * 1000); //Again at least 15s to login

它将提供所需的国家/地区的所有细分,或者您可以删除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). 条款以获取所有国家/地区的所有细分,然后您可以使用结果。