Link to home
Start Free TrialLog in
Avatar of mrwells
mrwells

asked on

Hide or Show Images using checkbox array in mysql database

Hi, I have a group of images that I want to show (make visible) or hide using checkbox array in php/mysql (see screen1).

The database:

visible bit(1) =  show images
visible bit(0) =  hide images

PHP code:
  if($row['visable']==1){
                echo'<td align="center"><input type="checkbox" name="imageCheck[]" id="imageCheck[]" checked='.$chk.'  value="'.$row['id'].'"><br />'.$chk.'</td>';
            }else{
                echo'<td align="center">                   
                    <input type="checkbox" name="unimageCheck[]" id="unimageCheck[]" value="'.$row['id'].'"><br />'.$chk.'</td>';
            }

Open in new window

using a submit button:

          <input name="check" type="submit" id="check" value="Update"></td>



Code for updating database:

if (isset($_POST['check'])){ 
               
 // hide images
        $imageCheck = isset($_POST['imageCheck']);
        $checkbox = $_POST['imageCheck']; //from name="checkbox[]"
        $countCheck = count($_POST['imageCheck']);
        for($i=0;$i<$countCheck;$i++) {
        $cid  = $checkbox[$i];    

// Create Query: if checkbox is unchecked make image visible=0
            $sql="UPDATE images SET visible = 0 WHERE images.id = $cid " ;
            $row = @mysqli_query ($mysqli, $sql);
            echo '<meta http-equiv="refresh" content="3;url=../admin/admin_edit.php?id='.$id.'">';
        }

//show test results
        echo 'hide';
        echo '<pre>';
        print_r($_POST['imageCheck']);
        echo '</pre>';    
  
 
//show images
        $unimageCheck = isset($_POST['unimageCheck']);
        $uncheckbox = $_POST['unimageCheck']; //from name="checkbox[]"
        $countCheck = count($_POST['unimageCheck']);
        for($i=0;$i<$countCheck;$i++) {
        $uncid  = $uncheckbox[$i];

// Create Query: if checkbox is checked make image visible=1
            $sql2="UPDATE images SET visible = 1 WHERE images.id = $uncid " ;
            $row = @mysqli_query ($mysqli, $sql2);
            echo '<meta http-equiv="refresh" content="3;url=../admin/admin_edit.php?id='.$id.'">';
        }

//show test results
        echo 'show';
        echo '<pre>';
        print_r($_POST['unimageCheck']);
        echo '</pre>';    
}

?>

Open in new window

I can get the code to work independently but cant get them to work together. I’m really out of the depth here and been working on this for the past 2 months. I have tried many different 'if' statements but still no joy. Need help.
screen1.JPG
screen2.JPG
Avatar of Gary
Gary
Flag of Ireland image

Is it
visable if($row['visable']==1){
or
visible  $sql="UPDATE images SET visible = 0 WHERE images.id = $cid " ;
Avatar of mrwells
mrwells

ASKER

sorry type error in the question. in all casses it is 'visible'
Confused about what you want.
Do you want the images to be hidden on the screenshots you gave if the Visible checkbox is not checked.
Else explain more clearly what/where you are going wrong.
Please, avoid to use @ before statements: that sign suppresses any error message and while debugging it's the most bad thing you can do. Place instead at the top of your scripts

error_reporting(E_ALL)

so you'll be notified about any small problem in your code.

Secondly, you should use mysqli_error() function:

    $sql2="UPDATE images SET visible = 1 WHERE images.id = $uncid " ;
    if (!mysqli_query ($mysqli, $sql2))
    {
        printf("Error message: %s\n", $mysqli->error);
    }

Open in new window

You don't need to write

$row = mysqli_query....

since you're updating the table and not selecting records.

You should also post here the whole code and the error messages you get once you have fixed the script as I said above (error_reporting and suppressing @ sign)
We also need to see the code where you select check values from the database and build your form with checkboxes check or unchecked accordingly to the query results.
Avatar of mrwells

ASKER

I'd like the user to check the ckeckbox if they want the image 'visible' or 'hidden' on the website;

if the images checkbox is checked then make the image 'visible' (visible=1):

<input type="checkbox" name="imageCheck[]" id="imageCheck[]" checked='.$chk.' value="'.$row['id'].'"><br />'.$chk.'</td>

$sql2="UPDATE images SET visible = 1 WHERE images.id = $uncid " ;
$row = @mysqli_query ($mysqli, $sql2);

if the images checkbox is unchecked then 'hide' the image (visable=0):

<input type="checkbox" name="unimageCheck[]" id="unimageCheck[]" value="'.$row['id'].'"><br />'.$chk.'</td>

 $sql="UPDATE images SET visible = 0 WHERE images.id = $cid " ;
 $row = @mysqli_query ($mysqli, $sql);
ASKER CERTIFIED SOLUTION
Avatar of Gary
Gary
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@Gary: One thing you can do to identify unchecked checkboxes is have a hidden input control of the same name in the HTML form before the checkbox.  The way PHP constructs the $_REQUEST-type arrays from the raw request data is always from top-to-bottom, so it will set the hidden value first, then if the checkbox is fired, it will overwrite the hidden value with the checkbox value.  This article shows how to do it.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_5450-Common-Sense-Examples-Using-Checkboxes-with-HTML-JavaScript-and-PHP.html