Jump to content

Difficult: giving parameters start value to check boxes


SanderK

Recommended Posts

Hi There!

 

I am quite far with this script I'm creating, but there is one thing I am not getting straight.

 

The script shows and hides information from a database by changing checkboxes. This can be filtered by a form.

 

Now my final problem: the checkboxes should start CHECKED instead of unchecked as they start now. It could be done by giving the $film or the $a attribute a start value I guess, but I don't know how.

 

I tried to put

$a = "checked=\"checked\"";

$film_query = "film='1'";

on several places in the beginning of the script, but it doesn't seem to affect anything. It only works if I put it right before the form, but then it checks the checkboxes every time after the form has been submitted (making it impossible to uncheck it). Not good :)

 

Could you give me a little help maybe?

 

Thanks a LOT! :D

Link to comment
Share on other sites

Oops, forgotten the code  :P

 

Here it is:

 

<head>
</head>

<?php
$host="XXX"; // Host name 
$username="XXX"; // Mysql username 
$password="XXX"; // Mysql password 
$db_name="XXX"; // Database name 
$tbl_name="XXX"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("DB bestaat niet");




$selected = array();


$orderby = mysql_real_escape_string($_GET[orderby]);
// Standaartwaarde is sorteren op waarde hieronder:
if(!$orderby) { $orderby = 'id_nr'; }

if($orderby == 'titel_asc') 
{
    $orderby_query = "ORDER BY titel asc";
$titel_asc = "selected";
}
else if($orderby == 'titel_desc')
{
$orderby_query = "ORDER BY titel desc";
$titel_desc = "selected";
}
else if($orderby == 'jaar')
{
$orderby_query = "ORDER BY jaar desc";
$jaar = "selected";
}
else if($orderby == 'id_nr')
{
$orderby_query = "ORDER BY id_nr";
$id_nr = "selected";
}
else { unset($orderby); }




//Check checkboxes an pas op basis daarvan variabelen aan om te filteren
$film = mysql_real_escape_string($_GET[film]);

if($film == 'true') {
$a = "checked=\"checked\"";
$film_query = "film='1'";
}
else{
$a = "";
$film_query = "(film='1' AND film='0')";
}

$tv = mysql_real_escape_string($_GET[tv]);
if($tv == 'true') {
$b = "checked=\"checked\"";
$tv_query = "tv='1'";
}
else{
$b = "";
$tv_query = "(tv='1' AND tv='0')";
}
$comm = mysql_real_escape_string($_GET[comm]);
if($comm == 'true') {
$c = "checked=\"checked\"";
$comm_query = "commercial='1'";
}
else{
$c = "";
$comm_query = "(commercial='1' AND commercial='0')";
}
$animatie = mysql_real_escape_string($_GET[animatie]);
if($animatie == 'true') {
$d = "checked=\"checked\"";
$animatie_query = "animatie='1'";
}
else{
$d = "";
$animatie_query = "(animatie='1' AND animatie='0')";
}
$stereo = mysql_real_escape_string($_GET[stereo]);
if($stereo == 'true') {
$e = "checked=\"checked\"";
$stereo_query = "stereo_3d='1'";
}
else{
$e = "";
$stereo_query = "(stereo_3d='1' AND stereo_3d='0')";
}
$opdr = mysql_real_escape_string($_GET[opdr]);
if($opdr == 'true') {
$f = "checked=\"checked\"";
$opdr_query = "in_opdracht='1'";
}
else{
$f = "";
$opdr_query = "(in_opdracht='1' AND in_opdracht='0')";
}
$fictie = mysql_real_escape_string($_GET[fictie]);
if($fictie == 'true') {
$g = "checked=\"checked\"";
$fictie_query = "fictie='1'";
}
else{
$g = "";
$fictie_query = "(fictie='1' AND fictie='0')";
}
$reality = mysql_real_escape_string($_GET[reality]);
if($reality == 'true') {
$h = "checked=\"checked\"";
$reality_query = "reality_docu='1'";
}
else{
$h = "";
$reality_query = "(reality_docu='1' AND reality_docu='0')";
}
if(($film != 'true') && ($tv != 'true') && ($comm != 'true') && ($animatie != 'true') && ($stereo != 'true') && ($opdr != 'true') && ($fictie != 'true') && ($reality != 'true')) {
$nodata = "Er zijn geen projecten gevonden die aan de door u gestelde criteria voldoen. Wijzig uw selectie.";
}



// Now run your SQL query with the $orderby_query variable.  Ex:
// Retrieve data from database 
$sql="SELECT * FROM $tbl_name WHERE (
$film_query OR $tv_query OR $comm_query OR $animatie_query OR $stereo_query OR $opdr_query OR $reality_query OR $fictie_query OR $reality_query
) $orderby_query";
$result=mysql_query($sql);
// SQL code goes here..


// Start looping rows in mysql database.
while($rows=mysql_fetch_array($result)){
?>
<table width="" border="1" cellspacing="0" cellpadding="3">
<tr>
<td width=""><? echo $rows['id_nr']; ?></td>
<td width=""><? echo $rows['titel']; ?></td>
<td width=""><? echo $rows['jaar']; ?></td>
<td width=""><? echo $rows['genre']; ?></td>
<td width=""><? echo $rows['korte_omsch']; ?></td>
<td width=""><? echo $rows['film']; ?></td>
<td width=""><? echo $rows['tv']; ?></td>
<td width=""><? echo $rows['fiction']; ?></td>
</tr>
</table>
<?
// close while loop 
}

// close connection 
mysql_close();
?>


<div class="nodata"><? print $nodata; ?></div>
<BR /><BR />

Sort by
<form method=get style="display: inline;" name='orderby_form'>
<select name=orderby onChange="orderby_form.submit();">
<option value='id_nr' <?php print $id_nr; ?>>Relevantie</option>
<option value='jaar' <?php print $jaar; ?>>Jaar</option>
<option value='titel_asc' <?php print $titel_asc; ?>>Titel (A - Z)</option>
<option value='titel_desc' <?php print $titel_desc; ?>>Titel (Z - A)</option>
</select>
Toon:
<input type="checkbox" onClick="orderby_form.submit();" name="film" value="true" <?php print $a; ?>/>Film
<input type="checkbox" onClick="orderby_form.submit();" name="tv" value="true" <?php print $b; ?>/>TV
<input type="checkbox" onClick="orderby_form.submit();" name="comm" value="true" <?php print $c; ?>/>Commercial
<input type="checkbox" onClick="orderby_form.submit();" name="animatie" value="true" <?php print $d; ?>/>Animatie
<input type="checkbox" onClick="orderby_form.submit();" name="stereo" value="true" <?php print $e; ?>/>Stereo 3D
<input type="checkbox" onClick="orderby_form.submit();" name="opdr" value="true" <?php print $f; ?>/>In Opdracht
<input type="checkbox" onClick="orderby_form.submit();" name="fictie" value="true" <?php print $g; ?>/>Fictie
<input type="checkbox" onClick="orderby_form.submit();" name="reality" value="true" <?php print $h; ?>/>Documentaire/reality
</form>


</body>
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.