Solved

Hide or Show Images using checkbox array in mysql database

Posted on 2014-01-18
11
236 Views
Last Modified: 2016-05-17
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
0
Comment
Question by:mrwells
  • 3
  • 3
  • 2
  • +1
11 Comments
 
LVL 58

Expert Comment

by:Gary
Comment Utility
Is it
visable if($row['visable']==1){
or
visible  $sql="UPDATE images SET visible = 0 WHERE images.id = $cid " ;
0
 

Author Comment

by:mrwells
Comment Utility
sorry type error in the question. in all casses it is 'visible'
0
 
LVL 58

Expert Comment

by:Gary
Comment Utility
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.
0
 
LVL 30

Expert Comment

by:Marco Gasi
Comment Utility
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

0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 30

Expert Comment

by:Marco Gasi
Comment Utility
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)
0
 
LVL 30

Expert Comment

by:Marco Gasi
Comment Utility
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.
0
 

Author Comment

by:mrwells
Comment Utility
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);
0
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
Comment Utility
First off a checkbox doesn't send a value if it is unchecked - so you cannot check this with the posted values.  The only checkboxes you will get posted are the ones that are checked.

Depending on if this page shows all the images, then you could initially set them all to invisible and then to set the ones that should be visible

foreach($_POST['imageCheck'] as $id){
     $sql2="UPDATE images SET visible = 1 WHERE images.id = $id" ;
     mysqli_query ($mysqli, $sql2);
}
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
@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.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_5450-Common-Sense-Examples-Using-Checkboxes-with-HTML-JavaScript-and-PHP.html
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now