dongzhong5756 2017-02-14 16:32
浏览 86
已采纳

无法显示其他表格

I'm a little bit new to the SQL and PHP how do I display the output with three different tables? I can only display it in one table but I don't know how to group them up in 3 different tables? Here's the output:

enter image description here

<?php

include "Connection.php";

$sql = "SELECT artist.artistName, artistcd.artistID, artistcd.cdID, artistcd.cdTitle, artistcd.cdGenre, artistcd.cdPrice FROM artist, artistcd WHERE artist.artistID = artistcd.artistID ORDER BY artistcd.artistID" ;


 $query = mysqli_query($connect, $sql) or die("Error: ".mysqli_error($connect));;


?>
<table width="70%" cellpadding="5" cellspace="5">

<tr>
    <th>Genre</th>
    <th>CD Identification</th>
    <th>Title</th>
    <th>Price</th>
    </tr>

<?php 

    $name = mysqli_fetch_assoc($query);
    echo $name["artistID"] . $name["artistName"];

    while ($row = mysqli_fetch_assoc($query)) { 
        echo "<tr>";
        echo "<td>" . $row['cdGenre']. "</td>";
        echo "<td>" . $row['cdID']. "</td>";
        echo "<td>" . $row['cdTitle']. "</td>";
        echo "<td>" . $row['cdPrice']. "</td>";
        echo "</tr>";
}
?>

<table width="70%" cellpadding="5" cellspace="5">

</table>
  • 写回答

2条回答 默认 最新

  • doutao5419 2017-02-14 17:12
    关注

    Ok, the simplest thing would be for you to repeat that code block 3 times. Each time, with a different Artist ID passed to the Query:

    <?php
    include "Connection.php";
    $sql = "SELECT artist.artistName, artistcd.artistID, artistcd.cdID, artistcd.cdTitle, artistcd.cdGenre, artistcd.cdPrice 
            FROM artist, artistcd 
            WHERE artist.artistID = "$ArtistID1";
    
    $query = mysqli_query($connect, $sql) or die("Error: ".mysqli_error($connect));;
    $name = mysqli_fetch_assoc($query);
        echo "<p>" . $name["artistID"] . $name["artistName"] . "</p>";
    ?>
    
    <table width="70%" cellpadding="5" cellspace="5">
    <tr>
        <th>Genre</th>
        <th>CD Identification</th>
        <th>Title</th>
        <th>Price</th>
        </tr>
    
        <?php 
            while ($row = mysqli_fetch_assoc($query)) { 
                echo "<tr>";
                echo "<td>" . $row['cdGenre']. "</td>";
                echo "<td>" . $row['cdID']. "</td>";
                echo "<td>" . $row['cdTitle']. "</td>";
                echo "<td>" . $row['cdPrice']. "</td>";
                echo "</tr>";
        }
        ?>
    
    <table width="70%" cellpadding="5" cellspace="5">
    </table>
    
    
    <?php
    $sql = "SELECT artist.artistName, artistcd.artistID, artistcd.cdID, artistcd.cdTitle, artistcd.cdGenre, artistcd.cdPrice 
            FROM artist, artistcd 
            WHERE artist.artistID = "$ArtistID2";
    
    $query = mysqli_query($connect, $sql) or die("Error: ".mysqli_error($connect));;
    $name = mysqli_fetch_assoc($query);
        echo "<p>" . $name["artistID"] . $name["artistName"] . "</p>";
    ?>
    
    <table width="70%" cellpadding="5" cellspace="5">
    <tr>
        <th>Genre</th>
        <th>CD Identification</th>
        <th>Title</th>
        <th>Price</th>
        </tr>
    
        <?php 
            while ($row = mysqli_fetch_assoc($query)) { 
                echo "<tr>";
                echo "<td>" . $row['cdGenre']. "</td>";
                echo "<td>" . $row['cdID']. "</td>";
                echo "<td>" . $row['cdTitle']. "</td>";
                echo "<td>" . $row['cdPrice']. "</td>";
                echo "</tr>";
        }
        ?>
    
    <table width="70%" cellpadding="5" cellspace="5">
    </table>
    
    
    <?php
    $sql = "SELECT artist.artistName, artistcd.artistID, artistcd.cdID, artistcd.cdTitle, artistcd.cdGenre, artistcd.cdPrice 
            FROM artist, artistcd 
            WHERE artist.artistID = "$ArtistID3";
    
    $query = mysqli_query($connect, $sql) or die("Error: ".mysqli_error($connect));;
    $name = mysqli_fetch_assoc($query);
        echo "<p>" . $name["artistID"] . $name["artistName"] . "</p>";
    ?>
    
    <table width="70%" cellpadding="5" cellspace="5">
    <tr>
        <th>Genre</th>
        <th>CD Identification</th>
        <th>Title</th>
        <th>Price</th>
        </tr>
    
        <?php 
            while ($row = mysqli_fetch_assoc($query)) { 
                echo "<tr>";
                echo "<td>" . $row['cdGenre']. "</td>";
                echo "<td>" . $row['cdID']. "</td>";
                echo "<td>" . $row['cdTitle']. "</td>";
                echo "<td>" . $row['cdPrice']. "</td>";
                echo "</tr>";
        }
        ?>
    
    <table width="70%" cellpadding="5" cellspace="5">
    </table>
    

    Another way would be to query once for all the artists, and build the 3 tables by filtering the artists right from your associative array. But as you say you are just starting, i think this solution will do it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件
  • ¥15 使用ESP8266连接阿里云出现问题
  • ¥15 BP神经网络控制倒立摆