使用SELECT查询使用多维数组

时间:2016-06-06 14:27:53

标签: php mysql arrays multidimensional-array xampp

我想使用SELECT Query从数据库中检索数据,该数据库使用多维数组显示类别列表和每个类别中的DVD数量。

Table Name: dvds_table 
dv_id   dv_caid dv_name
1   4   Matrix Reloaded
2   2   Johnny English
3   4   The Recruit      
4   4   Minority Report     
5   3   Two Weeks Notice 
6   2   Bend It Like Beckham

Table Name: categories_table 
ca_id   ca_name
2   Comedy
4   Action
1   Drama
3   Romance
5   TV

这是我到目前为止所提出的,请原谅我的基本知识:

<?php

$link = mysqli_connect("localhost", "root", "", "dvddb");

if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}

$categories=getNumDvdsInCategories();


function getNumDvdsInCategories() {


$this->db->select('*');
$this->db->from('categories_table');
$this->db->order_by('ca_name', 'DESC');

$query = $this->db->get();
return $query->result();
}


$this->load->model("model");
$data['results'] = $this->model->list_categories();
$this->load->view('categories_list_view', $data);


?>

<html>
<body>
<table border=1>
    <tr>
            <td>Category ID</td>
            <td>Category Name</td>
            <td>Num. DVDs</td>
    </tr>

<?php foreach ($categories as $category) { ?>
    <tr>
            <td><?php echo $category['ca_id']; ?></td>
            <td><?php echo $category['ca_name']; ?></td>
            <td><?php echo $category['num']; ?></td>
    </tr>
<?php } ?>

</table>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

我相信这段代码会给你想要的结果。请Google获取有关mysql COUNT的信息,该信息用于计算选择中的行数,LEFT JOIN用于将两个表中的行连接在一起。

我试图尽可能多地保留原始代码。只有查询部分已更改。我建议你阅读有关SQL的内容。

您可以使用AS别名或删除它们。请注意它们会影响$ catetory键名。也许你想从选择中删除ID。但这取决于你。!

<?php
// * These where the create queries that I have used to make a similar table as you.
// create table dvds_table ( dv_id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, dv_caid INT NOT NULL, FOREIGN KEY (dv_caid) references categories_table(ca_id), dv_name varchar(20));
// create table categories_table (ca_id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, ca_name varchar(20));

$link = mysqli_connect('localhost', 'root', '', 'dvddb');

if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

// * use $sql example below if you dont want the ID (Then also remove $category['ID'] in your HTML). 
// * Also you can remove the AS aliases or rename them as you see fit (then also change the $category key names).
// $sql = 'SELECT categories_table.ca_name AS Category, COUNT(dvds_table.dv_caid) AS Amount FROM categories_table LEFT JOIN dvds_table ON dvds_table.dv_caid = categories_table.ca_id GROUP BY categories_table.ca_name ORDER BY categories_table.ca_name DESC;';

$sql = 'SELECT  categories_table.ca_id AS ID,
            categories_table.ca_name AS Category,
            COUNT(dvds_table.dv_caid) AS Amount
            FROM categories_table
            LEFT JOIN dvds_table
            ON dvds_table.dv_caid = categories_table.ca_id
            GROUP BY categories_table.ca_id
            ORDER BY categories_table.ca_name DESC;';
$result = mysqli_query($link, $sql);

?>

<html>
<body>
<table border=1>
    <tr>
        <td>Category ID</td>
        <td>Category Name</td>
        <td>Num. DVDs</td>
    </tr>
    <?php foreach ($result as $category) { ?>
    <tr>
        <td><?php echo $category['ID']; ?></td>
        <td><?php echo $category['Category']; ?></td>
        <td><?php echo $category['Amount']; ?></td>
    </tr>
<?php } ?>
</table>
</body>
</html>

<?php
mysqli_free_result($result);
mysqli_close($link);
?>