Solved

Hide or Show Images using checkbox array in mysql database

Posted on 2014-01-18
11
253 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
ID: 39791140
Is it
visable if($row['visable']==1){
or
visible  $sql="UPDATE images SET visible = 0 WHERE images.id = $cid " ;
0
 

Author Comment

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

Expert Comment

by:Gary
ID: 39791165
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 31

Expert Comment

by:Marco Gasi
ID: 39791171
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39791179
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 31

Expert Comment

by:Marco Gasi
ID: 39791183
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
ID: 39791190
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
ID: 39791211
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
ID: 39791419
@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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Parts and Products table schema in mysql 6 43
MySQL  on Tomcat 8 27
How can I echo a PHP variable in AJAX function? 7 32
is this a cms? 8 34
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

911 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

23 Experts available now in Live!

Get 1:1 Help Now