Jump to content

looping through checkboxes - please help


jarv

Recommended Posts

hi, in my database I have  booking_id = 22

then I have 3 records for booking_id=22

 

start_time = 07:00:00 - booking_id=22

start_time = 10:00:00 - booking_id=22

start_time = 13:00:00 - booking_id=22

 

I would like to loop through these as checkboxes adding an additional one eg

 

checkbox1 = start_time = 07:00:00 - checked

checkbox2 = start_time = 10:00:00 - checked

checkbox3 = start_time = 13:00:00 - checked

checkbox4 = start_time = 16:00:00 - unchecked

 

so far, I have:

 

$query1 = mysql_query("SELECT * FROM aarbookts_booking_bookings_slots WHERE booking_id = '$booking_id'");
$row1 = mysql_fetch_array($query1);


<input type="checkbox" name="Times" value="07:00:00" <?php if($row1['$start_time']=="07:00:00"){ echo 'checked';} ?>> 07:00 - 10:00<br />
								<input type="checkbox" name="Times" value="10:00:00" <?php if($row1['$start_time']=="10:00:00"){ echo 'checked';} ?>> 10:00 - 13:00<br />
								<input type="checkbox" name="Times" value="13:00:00" <?php if($row1['$start_time']=="13:00:00"){ echo 'checked';} ?>> 13:00 - 16:00<br />
								<input type="checkbox" name="Times" value="16:00:00" <?php if($row1['$start_time']=="16:00:00"){ echo 'checked';} ?>> 16:00 - 19:00<br />

 

then I only want to either INSERT INTO or DELETE depending on which is checked

 

Please help!

Link to comment
https://forums.phpfreaks.com/topic/242573-looping-through-checkboxes-please-help/
Share on other sites

okay so you would do something like this i believe..

 

$checkbox = $_POST['checkbox']; //depends on what method you used..$_GET or $_POST
foreach($checkbox as $value){
$sql = "INSERT INTO tbl_name VALUES('$value')"; //insert correct values..
$query = mysql_query($sql) or trigger_error("Mysql Error:".mysql_error(),E_USER_ERROR);
}

I don't get it, I have

 

<input type="checkbox" name="cbox[]" value="07:00:00" <?php if($row1['start_time']=="07:00:00"){ echo 'checked';} ?>> 07:00 - 10:00<br />
								<input type="checkbox" name="cbox[]" value="10:00:00" <?php if($row1['start_time']=="10:00:00"){ echo 'checked';} ?>> 10:00 - 13:00<br />
								<input type="checkbox" name="cbox[]" value="13:00:00" <?php if($row1['start_time']=="13:00:00"){ echo 'checked';} ?>> 13:00 - 16:00<br />
								<input type="checkbox" name="cbox[]" value="16:00:00" <?php if($row1['start_time']=="16:00:00"){ echo 'checked';} ?>> 16:00 - 19:00<br />

 

but only the 1st one is checked, first 3 out of the 4 should be checked?!

You're referencing the same variable all three times. You need to check it against all results in the database.

 

You could try something like this:

 

SQL:

SELECT GROUP_CONCAT(start_time) FROM aarbookts_booking_bookings_slots WHERE booking_id = '$booking_id'

 

Then in your code do:

$row1 = mysql_fetch_array($query1);
$times=explode(",", $row1);
								<input type="checkbox" name="Times" value="07:00:00" <?php if(in_array("07:00:00",$times){ echo 'checked';} ?>> 07:00 - 10:00<br />
								<input type="checkbox" name="Times" value="10:00:00" <?php if(in_array("10:00:00",$times){ echo 'checked';} ?>> 10:00 - 13:00<br />
								<input type="checkbox" name="Times" value="13:00:00" <?php if(in_array("13:00:00",$times){ echo 'checked';} ?>> 13:00 - 16:00<br />
								<input type="checkbox" name="Times" value="16:00:00" <?php if(in_array("16:00:00",$times){ echo 'checked';} ?>> 16:00 - 19:00<br />

 

^^ Not tested

I'm still really stuck here:

 

so far I have:

 

page1

$query1 = mysql_query("SELECT * FROM aarbookts_booking_bookings_slots WHERE booking_id = '$booking_id'");
$row1 = mysql_fetch_array($query1);

<input type="checkbox" name="cbox[]" value="07:00:00" <?php if($row1['start_time']=="07:00:00"){ echo 'checked';} ?>> 07:00 - 10:00<br />
								<input type="checkbox" name="cbox[]" value="10:00:00" <?php if($row1['start_time']=="10:00:00"){ echo 'checked';} ?>> 10:00 - 13:00<br />
								<input type="checkbox" name="cbox[]" value="13:00:00" <?php if($row1['start_time']=="13:00:00"){ echo 'checked';} ?>> 13:00 - 16:00<br />
								<input type="checkbox" name="cbox[]" value="16:00:00" <?php if($row1['start_time']=="16:00:00"){ echo 'checked';} ?>> 16:00 - 19:00<br />

This only shows one checkbox checked?!

 

2nd page


$booking_id = $_REQUEST['booking_id'];


if(sizeof($_POST['cbox'])) {

//means if at least one check box is selected

foreach($_POST['cbox'] AS $id) {

$sql "UPDATE aarbookts_booking_bookings_slots SET start_time='$start_time' WHERE booking_id=$booking_id");
} //end foreach

} //end IF


echo $sql;

 

this shows a blank page?!

I have just put my checkboxes into a loop:

 

<?php while($row1 = mysql_fetch_array($result))
								{
								echo '<input type="checkbox" name="cbox[]" value="07:00:00"'.if($row1['start_time']=="07:00:00"){ echo "checked";}.'> 07:00 - 10:00<br />';
								echo '<input type="checkbox" name="cbox[]" value="10:00:00"'.if($row1['start_time']=="10:00:00"){ echo "checked";}.'> 10:00 - 13:00<br />';
								echo '<input type="checkbox" name="cbox[]" value="13:00:00"'.if($row1['start_time']=="13:00:00"){ echo "checked";}.'> 13:00 - 16:00<br />';
								echo '<input type="checkbox" name="cbox[]" value="16:00:00"'.if($row1['start_time']=="16:00:00"){ echo "checked";}.'> 16:00 - 19:00<br />';
								}?>

 

my page is now blank?!

I already explained why your code doesn't work. I also said that my code wasn't tested. It was meant as an example for how to do it.

 

You get a white page because you have PHP Error reporting turned off.

 

ini_set('display_errors', 1);
ini_set('log_errors', 1);
ini_set('error_log', dirname(__FILE__) . '/error_log.txt');
error_reporting(E_ALL);

 

Your code doesn't work because you are essentially doing this:

 

$row1['start_time']="07:00:00";

if ($row1['start_time']=="07:00:00") echo "echecked";  // Evaluates as true 07:00:00 == 07:00:00
if ($row1['start_time']=="10:00:00") echo "echecked";  // Evaluates as false 07:00:00 != 10:00:00

 

If you would do a little debugging on the code I posted, you would probably see that it does work.

 

<?php
$query1 = mysql_query("SELECT GROUP_CONCAT(start_time) FROM aarbookts_booking_bookings_slots WHERE booking_id = '$booking_id'");
$row1 = mysql_fetch_array($query1);
$times=explode(",", $row1);
?>
<input type="checkbox" name="Times" value="07:00:00" <?php if(in_array("07:00:00",$times){ echo 'checked';} ?>> 07:00 - 10:00<br />
<input type="checkbox" name="Times" value="10:00:00" <?php if(in_array("10:00:00",$times){ echo 'checked';} ?>> 10:00 - 13:00<br />
<input type="checkbox" name="Times" value="13:00:00" <?php if(in_array("13:00:00",$times){ echo 'checked';} ?>> 13:00 - 16:00<br />
<input type="checkbox" name="Times" value="16:00:00" <?php if(in_array("16:00:00",$times){ echo 'checked';} ?>> 16:00 - 19:00<br />

  Quote

Thankyou Thomas but I added

 

ini_set('display_errors', 1);
ini_set('log_errors', 1);
ini_set('error_log', dirname(__FILE__) . '/error_log.txt');
error_reporting(E_ALL);

 

to try and debug your code and my page is still blank?!

if your page is still blank, paste your current code

<?php
session_start();
ini_set('display_errors', 1);
ini_set('log_errors', 1);
ini_set('error_log', dirname(__FILE__) . '/error_log.txt');
error_reporting(E_ALL);
include_once("config.php");
if (!isset($_SESSION['rsUser'])) {
$msg = "Username and/or Password incorrect!";
header('Location: index.php?msg='.$msg.'');
}
if (!isset($_REQUEST['msg']))
{
$_REQUEST['msg'] = "nothing";
}
$booking_id = $_REQUEST['booking_id'];

$query1="SELECT * FROM aarbookts_booking_bookings_slots WHERE booking_id = '$booking_id'";
$result = mysql_query($query1);

?>



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

	<title>Affordable Appliance Repairs Admin - Edit Booking Date/Time</title>

	<!--                       CSS                       -->

	<!-- Reset Stylesheet -->
	<link rel="stylesheet" href="resources/css/reset.css" type="text/css" media="screen" />
  
	<!-- Main Stylesheet -->
	<link rel="stylesheet" href="resources/css/style.css" type="text/css" media="screen" />
	<link rel="stylesheet" href="resources/css/blue.css" type="text/css" media="screen" />
	<!-- Invalid Stylesheet. This makes stuff look pretty. Remove it if you want the CSS completely valid -->
	<!--<link rel="stylesheet" href="resources/css/invalid.css" type="text/css" media="screen" />-->	

	<!-- Colour Schemes

	Default colour scheme is green. Uncomment prefered stylesheet to use it.

	<link rel="stylesheet" href="resources/css/blue.css" type="text/css" media="screen" />

	<link rel="stylesheet" href="resources/css/red.css" type="text/css" media="screen" />

	-->

	<!-- Internet Explorer Fixes Stylesheet -->

	<!--[if lte IE 7]>
		<link rel="stylesheet" href="resources/css/ie.css" type="text/css" media="screen" />
	<![endif]-->

	<!--                       Javascripts                       -->

	<!-- jQuery -->

	<script type="text/javascript" src="http://code.jquery.com/jquery-1.5.2.min.js"></script>

	<!-- jQuery Configuration -->
	<script type="text/javascript" src="resources/scripts/simpla.jquery.configuration.js"></script>

	<!-- Facebox jQuery Plugin -->
	<script type="text/javascript" src="resources/scripts/facebox.js"></script>

	<!-- jQuery WYSIWYG Plugin -->
	<script type="text/javascript" src="resources/scripts/jquery.wysiwyg.js"></script>

	<!-- Internet Explorer .png-fix -->

	<link type="text/css" rel="stylesheet" href="http://79.170.40.235/affordableappliancerepairs.co.uk/bookings/core/libs/jquery/ui/css/smoothness/jquery.ui.core.css" />
	<link type="text/css" rel="stylesheet" href="http://79.170.40.235/affordableappliancerepairs.co.uk/bookings/core/libs/jquery/ui/css/smoothness/jquery.ui.theme.css" />
	<link type="text/css" rel="stylesheet" href="http://79.170.40.235/affordableappliancerepairs.co.uk/bookings/core/libs/jquery/ui/css/smoothness/jquery.ui.tabs.css" />

	<link type="text/css" rel="stylesheet" href="http://79.170.40.235/affordableappliancerepairs.co.uk/bookings/index.php?controller=AdminCalendars&action=css&cid=1" />
	<link type="text/css" rel="stylesheet" href="http://79.170.40.235/affordableappliancerepairs.co.uk/bookings/core/libs/jquery/ui/css/smoothness/jquery.ui.button.css" />
	<link type="text/css" rel="stylesheet" href="http://79.170.40.235/affordableappliancerepairs.co.uk/bookings/core/libs/jquery/ui/css/smoothness/jquery.ui.dialog.css" />
	<link type="text/css" rel="stylesheet" href="http://79.170.40.235/affordableappliancerepairs.co.uk/bookings/core/libs/jquery/ui/css/smoothness/jquery.ui.datepicker.css" />
	<script type="text/javascript" src="http://79.170.40.235/affordableappliancerepairs.co.uk/bookings/core/libs/jquery/jquery-1.5.2.min.js"></script>
	<script type="text/javascript" src="http://79.170.40.235/affordableappliancerepairs.co.uk/bookings/app/web/js/admin-core.js"></script>
	<script type="text/javascript" src="http://79.170.40.235/affordableappliancerepairs.co.uk/bookings/core/libs/jquery/ui/js/jquery.ui.core.min.js"></script>
	<script type="text/javascript" src="http://79.170.40.235/affordableappliancerepairs.co.uk/bookings/core/libs/jquery/ui/js/jquery.ui.widget.min.js"></script>
	<script type="text/javascript" src="http://79.170.40.235/affordableappliancerepairs.co.uk/bookings/core/libs/jquery/ui/js/jquery.ui.tabs.min.js"></script>
	<script type="text/javascript" src="http://79.170.40.235/affordableappliancerepairs.co.uk/bookings/core/libs/jquery/ui/js/jquery.ui.button.min.js"></script>
	<script type="text/javascript" src="http://79.170.40.235/affordableappliancerepairs.co.uk/bookings/core/libs/jquery/ui/js/jquery.ui.position.min.js"></script>
	<script type="text/javascript" src="http://79.170.40.235/affordableappliancerepairs.co.uk/bookings/core/libs/jquery/ui/js/jquery.ui.dialog.min.js"></script>
	<script type="text/javascript" src="http://79.170.40.235/affordableappliancerepairs.co.uk/bookings/core/libs/jquery/ui/js/jquery.effects.core.min.js"></script>
	<script type="text/javascript" src="http://79.170.40.235/affordableappliancerepairs.co.uk/bookings/core/libs/jquery/ui/js/jquery.ui.datepicker.min.js"></script>
	<script type="text/javascript" src="http://79.170.40.235/affordableappliancerepairs.co.uk/bookings/core/libs/jquery/plugins/validate/js/jquery.validate.min.js"></script>
	<!--[if IE 6]>

		<script type="text/javascript" src="resources/scripts/DD_belatedPNG_0.0.7a.js"></script>
		<script type="text/javascript">
			DD_belatedPNG.fix('.png_bg, img, li');
		</script>
	<![endif]-->

<script type="text/javascript">
      $(document).ready(function(){
            $("fieldset input[type=text]").focus(function(){
                $(this).parent().find(".input-notification").css("visibility", "visible");
            }).blur(function(){
                $(this).parent().find(".input-notification").css("visibility", "hidden");
            });
        });
</script>
<script>
$(function() {
	$( "#datepicker" ).datepicker();
});

</script>
</head>

<body><div id="body-wrapper"> <!-- Wrapper for the radial gradient background -->

	<?
$menuitem=4;
$submenuitem=1;
include("menu.php");
?>

	<div id="main-content"> <!-- Main Content Section with everything -->

		<noscript> <!-- Show a notification if the user has disabled javascript -->
			<div class="notification error png_bg">
				<div>
					Javascript is disabled or is not supported by your browser. Please <a href="http://browsehappy.com/" title="Upgrade to a better browser">upgrade</a> your browser or <a href="http://www.google.com/support/bin/answer.py?answer=23852" title="Enable Javascript in your browser">enable</a> Javascript to navigate the interface properly.
				</div>
			</div>
		</noscript>

		<!-- Page Head -->
		<?php include_once("welcome.php");?>

		<div class="clear"></div> <!-- End .clear -->

		<div class="content-box"><!-- Start Content Box -->

			<div class="content-box-header">

				<h3>Bookings</h3>

				<ul class="content-box-tabs">
					<li><a href="#tab1" class="default-tab">Edit Booking Date/Time</a></li>
				</ul>

				<div class="clear"></div>

			</div> <!-- End .content-box-header -->

			<div class="content-box-content">

				<div class="tab-content default-tab" id="tab1">

					<form action="edit-bookingdate-script.php?booking_id=<?php echo $row1['booking_id']; ?>" method="post">

						<fieldset> <!-- Set class to "column-left" or "column-right" on fieldsets to divide the form into columns -->

							<p>
								<label>Date</label>
								<input class="text-input small-input" type="text" id="datepicker" name="booking_date" value="<?php echo $row5['booking_date']; ?>" /> 		
							</p>

							<p>
								<label>Booking Status</label>
								<?php $row1 = mysql_fetch_array($result);
									foreach($row1 as $row){								
										$chk1="";
										if($row['start_time']=="07:00:00")
										$chk1="checked='yes'";
										}
										$chk2="";
										if($row['start_time']=="10:00:00")
										$chk2="checked='yes'";
										}
										$chk3="";
										if($row['start_time']=="13:00:00")
										$chk3="checked='yes'";
										}
										$chk4="";
										if($row['start_time']=="16:00:00")
										$chk4="checked='yes'";
										}
									echo "
									<input type='checkbox' name='cbox[]' value='07:00:00' $chk1> 07:00 - 10:00<br />
									<input type='checkbox' name='cbox[]' value='10:00:00' $chk2> 10:00 - 13:00<br />
									<input type='checkbox' name='cbox[]' value='13:00:00' $chk3> 13:00 - 16:00<br />
									<input type='checkbox' name='cbox[]' value='16:00:00' $chk4> 16:00 - 19:00<br />
									<br />
									";
									}
								?>
							</p>

							<p>
								<input class="button" type="submit" value="Edit Date/Time" />
							</p>

						</fieldset>

						<div class="clear"></div><!-- End .clear -->

					</form>

				</div> <!-- End #tab2 -->


			</div> <!-- End .content-box-content -->


		</div> <!-- End .content-box -->

		<?php include_once("footer.php");?><!-- End #footer -->

	</div> <!-- End #main-content -->

</div>
</body>

</html>

so you don't receive any errors?

do you ave short tags enabled in your php.ini, you use them here

<?
$menuitem=4;
$submenuitem=1;
include("menu.php");
?>

3. don't reaqlly need to concatenate your header, this will work.

header("Location: index.php?msg=$msg";

 

hope you didn't forget the ending parenthesis like i did should be

 

header("Location: index.php?msg=$msg");

 

to check if short tags are enabled, insert this in your code somewhere

 

print "short_tags=" . ini_get("short_open_tag");

The code you posted is not the code I showed you and will not work right. Also, you have a syntax error here:

 

if($row['start_time']=="07:00:00")

$chk1="checked='yes'";

}

$chk2="";

if($row['start_time']=="10:00:00")

$chk2="checked='yes'";

}

$chk3="";

if($row['start_time']=="13:00:00")

$chk3="checked='yes'";

}

$chk4="";

if($row['start_time']=="16:00:00")

$chk4="checked='yes'";

}

 

You are ending each if with a } but you don't start any of them with a {

ok Teynon, using the code you used previously, .. and now getting error messages as it's now turned on on the server I currently get:

 

<b>Notice</b>:  Array to string conversion in <b>/home/sites/affordableappliancerepairs.co.uk/public_html/admin/edit-bookingdate.php</b> on line <b>14</b><br />

 

here is my code:

 

$query1 = mysql_query("SELECT GROUP_CONCAT(start_time) FROM aarbookts_booking_bookings_slots WHERE booking_id = '$booking_id'");

$row1 = mysql_fetch_array($query1);
$times=explode(",", $row1);

<input type="checkbox" name="Times" value="07:00:00" <?php if(in_array("07:00:00",$times)){ echo 'checked';} ?>> 07:00 - 10:00<br />
								<input type="checkbox" name="Times" value="10:00:00" <?php if(in_array("10:00:00",$times)){ echo 'checked';} ?>> 10:00 - 13:00<br />
								<input type="checkbox" name="Times" value="13:00:00" <?php if(in_array("13:00:00",$times)){ echo 'checked';} ?>> 13:00 - 16:00<br />
								<input type="checkbox" name="Times" value="16:00:00" <?php if(in_array("16:00:00",$times)){ echo 'checked';} ?>> 16:00 - 19:00<br />

Thanks for your help so far Teynon!

I'm a little stuck on my action page:

 

$booking_id = $_REQUEST['booking_id'];
$booking_date = $_REQUEST['booking_date'];

if(sizeof($_POST['Times'])) {

//means if at least one check box is selected

foreach($_POST['Times'] AS $start_time){
if($start_time == "07:00:00"){
	$end_time = "10:00:00";
} else if($start_time == "10:00:00"){
	$end_time = "13:00:00";
} else if($start_time == "13:00:00"){
	$end_time = "16:00:00";
} else if($start_time == "16:00:00"){
	$end_time = "19:00:00";
}
$sql = "DELETE * FROM aarbookts_booking_bookings_slots WHERE booking_id=$booking_id";
	$sql1 = "INSERT INTO aarbookts_booking_bookings_slots SET start_time='$start_time',end_time='$end_time',booking_date='$booking_date' WHERE booking_id=$booking_id";
} //end foreach

} //end IF


echo $sql;
echo $sql1;

 

this only shows one SQL DELETE and one SQL INSERT INTO

I thought surely these should be in the for each loop and INSERT EACH checkbox?!

 

your insert syntax is not correct anyway...

 

heres the correct syntax to use  http://dev.mysql.com/doc/refman/5.5/en/insert.html

 

in order to get more than one echo here. you will need to place them inside of the foreach loop..

 

foreach($_POST['Times'] AS $start_time){
if($start_time == "07:00:00"){
	$end_time = "10:00:00";
} else if($start_time == "10:00:00"){
	$end_time = "13:00:00";
} else if($start_time == "13:00:00"){
	$end_time = "16:00:00";
} else if($start_time == "16:00:00"){
	$end_time = "19:00:00";
}
$sql = "DELETE * FROM aarbookts_booking_bookings_slots WHERE booking_id=$booking_id";
	$sql1 = "INSERT INTO aarbookts_booking_bookings_slots SET start_time='$start_time',end_time='$end_time',booking_date='$booking_date' WHERE booking_id=$booking_id";

echo $sql;
echo $sql1;
} //end foreach

} //end IF

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.