0

I am trying to get multiple items from mysql database based on the checkboxes select.

Example:

1- user selects 3 checkboxes on the page and clicks on the submit button. 2- on the next page, those 3 products would show.

my current code is like this:

First Page:

<div align="center">
<input style="float:left;" type="checkbox" name="check_list[]" value="'.$product_name.'" />
<img width="67" src="../images/'.$id.'.jpg"  /><br />
'.$product_name.'
</div>

the code above is in a while loop and it works fine. I get all the products from the mysql database as it should.

second page:

<?php
if(isset($_POST['submit'])){//to run PHP script on submit
if(!empty($_POST['check_list'])){
// Loop to store and display values of individual checked checkbox.
foreach($_POST['check_list'] as $selected){
//echo $selected."</br>";



include "../config/connect.php";
// This block grabs the whole list for viewing
$products_list = "";
$sql = "SELECT * FROM products WHERE product_name='$selected'";
$query = mysqli_query($db_conx, $sql);
$productCount = mysqli_num_rows($query); // count the output amount
if ($productCount > 0) {
    while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){ 
             $id = $row["id"];
             $product_name = $row["product_name"];
             $gender = $row["gender"];
             $products_list .= '<div align="center">
<input style="float:left;" type="checkbox" name="check_list[]" value="'.$product_name.'" />
<img width="67" src="../images/'.$id.'.jpg"  /><br />
'.$product_name.'
</div>';
    }
} else {
    $products_list .= "You have nothing";
}
}
}
}
?>
<?php echo $products_list; ?>

the code on the second page only echo's the last checked item!

but i need to display all the items checked on the first page.

could someone please help me out with this?

Thanks.

2 Answers 2

2

Don't do

$products_list = "";

inside your loop. Move it prior to

foreach($_POST['check_list'] as $selected){
Sign up to request clarification or add additional context in comments.

Comments

1

Its printing the last checked item because you are resetting the string you are echoing every time the loop interates. Use this code.

<?php
if(isset($_POST['submit'])){//to run PHP script on submit
    if(!empty($_POST['check_list'])){
    // Loop to store and display values of individual checked checkbox.
    $products_list = "";
        foreach($_POST['check_list'] as $selected){
        //echo $selected."</br>";



        include "../config/connect.php";
        // This block grabs the whole list for viewing
        $sql = "SELECT * FROM products WHERE product_name='$selected'";
        $query = mysqli_query($db_conx, $sql);
        $productCount = mysqli_num_rows($query); // count the output amount
            if ($productCount > 0) {
                while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){ 
                    $id = $row["id"];
                    $product_name = $row["product_name"];
                    $gender = $row["gender"];
                    $products_list .= '<div align="center">
                    <input style="float:left;" type="checkbox" name="check_list[]" value="'.$product_name.'" />
                    <img width="67" src="../images/'.$id.'.jpg"  /><br />
                    '.$product_name.'
                    </div>';
                }
            } else {
                $products_list .= "You have nothing";
            }
        }
    }
}
?>
<?php echo $products_list; ?>

I moved $products_list outside the foreach loop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.