检查数组是否存在mysql行

时间:2017-08-06 06:33:05

标签: php mysql

我有一系列商店号码。我有一个mysql数据库,这些商店提交每周报告。我正在尝试设置一个简单的表格,列出左边的所有商店,然后根据商店是否提交了每周报告来设置复选标记或x。

我可以使StoreNumber数组适应表列,但是很难检查store_number的num_row是否存在。我想我很亲密,但也许不是。提前谢谢。

代码如下:

<?php
//set var store as store number
//check if var store has kir submitted
//if so, put check.
//if not, put x.
//check next store number

    //connect to the database
    require("../dbfiles/connect.php");

    // Date Formatting for MYSQL
    if(!empty($_POST['WeekEnding'])){
        $WeekEnding = $_POST['WeekEnding'];

        $WeekEnding = strtotime($WeekEnding);
        $WeekEnding = date('Y-m-d', $WeekEnding);
}
//Array of all stores
$StoreNumber = array(586,
7695,
12510, 
24022,
24707,
28373,
30367,
30819,
31396,
31544,
33663,
33475,
39245,
47140,
49647,
49648,
51833,
51833-1,
57107,
60641,
62802,
297,
13778,
26519,
27055,
28311,
31296,
36438,
38156,
48925,
59288);

while($StoreNumber[$x] = 0){
//query to check for info in table
    $query = "SELECT * FROM kir1 WHERE WeekEnding = '$WeekEnding' AND storenumber = ' . $StoreNumber[$x] . '";  
    $response = @mysqli_query($dbc, $query);  
        echo '<style>
            table {
                border-collapse: collapse;
                width: 100%;
            }

            th, td {
                text-align: left;
                padding: 8px;
            }

            tr:nth-child(even){background-color: #f2f2f2}

            th {
                background-color: #4CAF50;
                color: white;
            }
            </style>';
        echo '<table>

        <tr><th><b>Store Number</b></th>
        <th><b>KIR</b></th></tr>';

//If connected, build table, then row out each response
    if($response) {
        $count = mysqli_num_row($response);
        if($count > 0){
            echo "<tr><td>" . $StoreNumber[$x] . "</td><td> &#x2714; </td></tr>";
            } else {
            echo "<tr><td>" . $StoreNumber[$x] . "</td><td> &#x2717; </td></tr>";
            }
        }
    }
?>

2 个答案:

答案 0 :(得分:0)

您可以简单地使用mysql IN()子句

$StoreNumber = array(586,
7695,
12510, 
24022,
24707,
28373,
30367,
30819,
31396,
31544,
33663,
33475,
39245,
47140,
49647,
49648,
51833,
51833-1,
57107,
60641,
62802,
297,
13778,
26519,
27055,
28311,
31296,
36438,
38156,
48925,
59288);
$StoreNumbers = implode(",", $StoreNumber);
 $query = "SELECT * FROM kir1 WHERE WeekEnding = '$WeekEnding' AND storenumber IN ($StoreNumbers)"; 
//do further operation

修改

如果是字符串,则可以使用','

进行内爆
$StoreNumbers = "'".implode("','",$StoreNumber)."'";

答案 1 :(得分:0)

使用IN运算符,它的工作方式与OR

相同
$storeNumbers = array(...);
$query = "SELECT * FROM kir1 WHERE WeekEnding = '$WeekEnding' AND storenumber IN ($storeNumbers)";