Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

mysql - How can i list has same id data with while loop in PHP?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Order your results by series_id, so all the products with the same value will be together.

$stmt = $pdo->prepare("SELECT series_id, product_name
                       FROM yourTable
                       ORDER BY series_id");
$stmt->execute();

Then when displaying the results, show the Series header and start a new <ul> whenever it changes:

$last_series = null;
echo "<ul>
";
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    if ($row['series_id'] != $last_series) {
        if ($last_series) {
            echo "</ul></li>
";
        }
        echo "<li>" . $row['series_id'] . " Series Product
";
        echo "<ul>
";
        $last_series = $row['series_id'];
    }
    echo "<li>" . $row['product_name'] . "</li>
";
}
if ($last_series) {
    echo "</li>
";
}
echo "</ul>
";

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...