我的php数组中有两篇文章

时间:2017-02-07 12:20:51

标签: php sql arrays ajax

我正在尝试为作业创建一个网上商店,我无法弄清楚如何显示我的结帐中有两个相同的产品。这是我想要显示的代码:Article1 .. 2x Article2 ... 5x等。

<?php
session_start();

//read out session with article numbers
$array = $_SESSION['mandje'];
echo '<center>';
echo '<h1> Uw mandje: </h1>';
echo '<table style="border: 2px solid black">';

//array that checks if there are two of the same article numbers
$mandje = array();

//read out array
foreach ($array as $artikel)
{
    echo '</br>';
    echo $artikel;
    if (in_array($artikel, $mandje)){
        //I need to display the article that i have multiple times here i guess
    } else {
        //getting the articles out of the database
        require ('config.php');
        $query = "SELECT * FROM mphp6_meubels WHERE artikelnr = $artikel";
        $results = mysqli_query($mysqli, $query);
        while($meubel = mysqli_fetch_array($results)){
            $video = $meubel['naam'];
            $nmr = $meubel['artikelnr'];
            echo '<tr>';
            echo '<td> <img src="Meubels/'.$video.'.jpg" width="150" height="150" alt="Meubel"></td>';
            echo '</tr>';
        }
    }
    //adding the article to array so i can check if there are multiple of the same articles in array
    $mandje[] = $artikel;
}
echo '</table>';
echo '</center>';

1 个答案:

答案 0 :(得分:0)

只需按artikelnr字段分组。

foreach ($array as $artikel)
{
    require ('config.php');
    $query = "SELECT mphp6_meubels.*, count(*) as cnt FROM mphp6_meubels WHERE artikelnr = $artikel GROUP BY artikelnr";
    $results = mysqli_query($mysqli, $query);
    while($meubel = mysqli_fetch_array($results)){
        $video = $meubel['naam'];
        $nmr = $meubel['artikelnr'];
        echo '<tr>';
        echo '<td>' . $artikel . '</td>';
        echo '<td> <img src="Meubels/'.$video.'.jpg" width="150" height="150" alt="Meubel"></td>';
        echo '<td> ' . $meubel['cnt'] . '</td>';
        echo '</tr>';
    }
}