Solved

Hide or Show Images using checkbox array in mysql database

Posted on 2014-01-18
11
265 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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
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
 
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 109

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
Display Calender only  When events is scheduled in Worpdress? 1 23
php simple error message 4 23
ebay devID, appID, certID, userToken 2 27
google analytics in wordpress 4 35
This article discusses four methods for overlaying images in a container on a web page
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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:
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

777 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