简体   繁体   中英

Check if mysql row exists against array

I have an array of Store Numbers. I have a mysql db where those stores submit weekly reports. I am trying to set up a simple table that lists all the stores on the left, then puts a check mark or x depending on if that stores has submitted the weekly report or not.

I can make the StoreNumber array fit into the table column as it should but have a hard time checking to see if the num_row for the store_number exists. I think I am close, but maybe not. Thanks in advance.

Code is below:

<?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>";
            }
        }
    }
?>

You can simply use mysql IN() clause

$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

EDIT

If it is string you can implode with ','

$StoreNumbers = "'".implode("','",$StoreNumber)."'";

use IN operator it's work like OR

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM