0

I am building a cms I want to iplement the ability to hide pages.

I have a row in mysql named hidden which stores the values 1 and 0. 1 will allow the page to be shown and 0 will hide it. The defalut value is set as 1.

When the box is checked I want the database to be updated with the value 0 and when it is unchecked I want it to be updated with the value 1.

I am looping out the current data stored into the database which provides the page title and two checkboxes one to delete the page and other to hide the page.

I use a foreach loop to update the values in the database but I can't figure out how to update it with a value when the checkbox is not checked.

if (isset($_POST['delete'])) {
foreach ($_POST['checkbox'] as $val){
    mysql_query("DELETE FROM about WHERE id = ".$val."");
}
    }
if(isset($_POST['visible'])){
    foreach ($_POST['hidden'] as $hide){
        mysql_query("UPDATE about SET hidden = 0 WHERE id = ".$hide."");
    }
}      

$sql = mysql_query("SELECT * FROM about ORDER BY id ASC");
while($row = mysql_fetch_assoc($sql)){

    if($row['hidden'] ==) 0{
        $checked = "checked=\"checked\"";
    }else{
        $checked = null;
    }   
echo "<p>Title:" . $row['title'] . "
<label for=\"checkbox".$i."\"> Hidden: </label><input name=\"hidden[]\" type=\"checkbox\" id=\"checkbox". $i . "\" value=\"". $row['id'] ."\" $checked> 

<label for=\"checkbox".$ii."\"> Delete: </label><input name=\"checkbox[]\" type=\"checkbox\" id=\"checkbox". $ii . "\" value=\"". $row['id'] ."\"></p><hr>";
$i++;   
$ii++;
    }
        echo"<input type=\"submit\" name=\"delete\" value=\"Delete\" class=\"submit\">";
        echo"<input type=\"submit\" name=\"visible\" value=\"Hide\" class=\"submit\">";
3
Contributors
3
Replies
30
Views
4 Years
Discussion Span
Last Post by MattD00
0

Well, your checkboxes will not be in the $_POST['hidden'] array if they have not been checked. Therefore you cannot set them to 0 using a foreach() loop the way you are using it. You will somehow need to compare the checked checkboxes against the present checkboxes, and work with the difference. Or, you could create two checkboxes: one called "show" and one called "hide", and use those to determine the action of your query.

0

use a dropdown option instead of checkbox here is the html:

//the php code for the function you need
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST' && $_POST['form_name'] == 'showhide')
{  
$status = $_POST['status'];

try
{
$pdo = new PDO('mysql:host=localhost;dbname=yourdatabasename', 'username', '');
}
catch (PDOException $e)
{
$output = 'Unable to connect to the database server.';
echo $output;
exit();
}

try
{
$sql = //here put the sql query
$statement = $pdo->prepare($sql);
$statement->bindValue(//bind the value of the input);
$statement->execute();
}
catch (PDOException $e)
{
$output = 'Unable to connect to the database server.';
echo $output;
exit();
}

}

?>



//the html code
<form name="showhide" method="post" action="<?php echo basename(__FILE__); ?>">
<input type="hidden" name="form_name" value="showhide">
    <select name="status" style="height:25px;padding:2px; width:100px; border-radius:3px; border:1px solid #CCCCCC;">
      <option value="0">Hide</option>
      <option value="1">Show</option>
    </select>
    </form>

i am using pdo instead of mysql or mysqli, it is more safe than mysql, i suggest you also use pdo or mysqli, mysql will be discontinued in the near future.

0

I worked out a solution to my problem by using a hidden field with an identical name to the checkbox allowing a diffreent value to be insrted into the database when the box was unchecked.

<?php
if(isset($_POST['visible'])){
foreach ($_POST['id'] as $id){
    $value = mysqli_real_escape_string($con,($_POST['hide'][$id]));

mysqli_query($con,"UPDATE hidden SET hidden = '{$value}' WHERE id = $id");
    }
} 
$sql = mysqli_query($con,"SELECT * FROM hidden");
?>

<form method="post" action="">
<?php
$i=0;
while($row = mysqli_fetch_assoc($sql)){

     if($row['hidden'] == 0){
$checked = "checked=\"checked\"";
}else{
$checked = null;
} 

echo "<p>name:" . $row['name'] . "
<input type=\"hidden\" name=\"id[".$i."]\" value=\"" . $row["id"] . "\">
<input type =\"hidden\" name=\"hide[".$row['id']."]\" value=\"1\" $checked>
<label> Hidden: </label><input name=\"hide[".$row['id']."]\" type=\"checkbox\" value=\"0\" $checked></p>";
$i++;
}
?>
<input type="submit" name="visible" value = "show/hide">

</form>
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.