vendredi 1 avril 2016

PHP array implode keys and values to function

I'm not too familiar with PHP arrays, I have the following code that generates query to output the results needed.

$allstore = $_POST['store'];


 function createSelect($allstore)
{
    if (empty($allstore))
        return "";

    $querySelect = "";
    $queryJoin = "";
    $baseTable = "";
    foreach ($allstore as $store => $value) {
        if (!$querySelect) {
            $baseTable = $store;
            $querySelect = "SELECT " . $store . ".item_no, " . $store . ".actual_price, " . $store . ".selling_price, " . $store . ".qty as " . $store;
        } else {
            $querySelect .= ", " . $store . ".qty as " . $store;
            $queryJoin .= "
             INNER JOIN " . $store . " ON " . $baseTable . ".item_no = " . $store . ".item_no";
        }
    }
    $querySelect .= " FROM " . $baseTable;
    $query = $querySelect . $queryJoin;

    return $query;
}

//Stores to be shown
$allstore = ['s_M9' =>0 , 's_M10' =>1];

$query = (createSelect($allstore));
$result = mysql_query($query);
//rest of code...

As you can see above, at the very top there is $allstore = $_POST['store']; Which collects values based from previous form POST method that has checkbox with the name=store[] .

Now According to the function shown, if I create my own keys and values like this

$allstore = ['s_M9' =>0 , 's_M10' =>1];

the output shows exactly what i'm looking for. But the problem goes on how to let $allstore implode those stores s_M9, s_M10 based on what the user has selected on the previous page ( checkbox )? I mean, the user can select either one of the stores or Both stores . How can I implode the checked results between those brackets without inserting them manually?

Thank You

Edit :

<?php
echo "<form action='somewhere.php' method='POST'>";

$query = "SELECT * from stores_list ORDER BY short Asc";
$result = mysql_query($query);
if(mysql_num_rows($result)>0){
    $num = mysql_num_rows($result);
    for($i=0;$i<$num;$i++){
    $row = mysql_fetch_assoc($result);
    echo "<input type=checkbox name=store[] value={$row['short']} style='width:20px; height:20px;'>{$row['short']}";
    }
}

else{
    //No Stores Available
    echo "No Stores Found !";
}


echo "</td><input type='submit' value='Search'/></form>";




Aucun commentaire:

Enregistrer un commentaire