Forum Moderators: coopster

Message Too Old, No Replies

Need help with my checkbox

         

Portnumbay

4:54 am on Jul 9, 2010 (gmt 0)

10+ Year Member



Hi masters...
I want to create a searching using checkboxes as the category for "where" condition.. I done it.. and now i confuse.. The thing is, I want the checkbox was checked keep checking.. I dont know what to do..
here is my code

<?php require_once('Connections/koneksi.php'); ?>

<style type="text/css">
<!--
.check {
font-family: Helvetica, Arial, sans-serif;
color: #58595b;
font-size: 12px;
}
-->
</style>
<form action="" method="post" target="_self" >
<p><span class="check">Sort by projects type :
<input type="checkbox" name="cat1" value="1" />
interactive
<input type="checkbox" name="cat2" value="1" />
entertainmentl
<input type="checkbox" name="cat3" value="1" />
print
<input type="checkbox" name="cat4" value="1" />
packaging
<input type="checkbox" name="cat5" value="1" />
advertising
<input name="Submit" type="Submit" id="Submit" value="Sorting" />
</span> </p>
</form>

<div style="width: 750px; height: 500px; overflow: auto; padding: 5px"><?
include "includefile/conn.php";
if (isset($_POST['Submit'])) {
echo "<morning>
<type name=\"portfolio\">
";
$cat1 = $_POST['cat1'];
$cat2 = $_POST['cat2'];
$cat3 = $_POST['cat3'];
$cat4 = $_POST['cat4'];
$cat5 = $_POST['cat5'];

if($cat1=="1"){

if($query==""){
$query=$query."int_portfolio1 like '1'";
}else{
$query=$query." or int_portfolio1 like '1'";
}
}

if($cat2=="1"){
if($query==""){
$query=$query."int_portfolio2 like '1'";
}else{
$query=$query." or int_portfolio2 like '1'";
}
}

if($cat3=="1"){
if($query==""){
$query=$query."int_portfolio3 like '1'";
}else{
$query=$query." or int_portfolio3 like '1'";
}
}

if($cat4=="1"){
if($query==""){
$query=$query."int_portfolio4 like '1'";
}else{
$query=$query." or int_portfolio4 like '1'";
}
}

if($cat5=="1"){
if($query==""){
$query=$query."int_portfolio5 like '1'";
}else{
$query=$query." or int_portfolio5 like '1'";
}
}
if($cat1=="" && $cat2=="" && $cat3=="" && $cat4=="" && $cat5==""){
$query=" int_portfolio1 like '0' and int_portfolio2 like '0' and int_portfolio3 like '0' and int_portfolio4 like '0' and int_portfolio5 like '0' ";
}

mysql_select_db($database_koneksi, $koneksi);
$query_Recordset1 = "SELECT * FROM tbl_portfolio where $query ";
$Recordset1 = mysql_query($query_Recordset1, $koneksi) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>

<?php do { ?>
<table width="157" border="0" align="left" id="content">
<tr>
<td class="borderfoto"> <a class="" href="galeri.php?id=<? echo $row_Recordset1['vch_portfolioId']; ?>" title="<?php echo $row_Recordset1['vch_portfolioClient']; ?>"> <img src="uploaded/portfolio/thumb/<?php echo $row_Recordset1['vch_portfolioImage']; ?>" border="0" />
</a></td>
</tr>
<?php $judul = $row_Recordset1['vch_portfolioClient']; ?>
<tr>
<td><?php echo substr($judul, 0, 20); ?></td>
</tr><?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
</table><?
}
else
{
mysql_select_db($database_koneksi, $koneksi);
$query_Recordset1 = "SELECT * FROM tbl_portfolio ";
$Recordset1 = mysql_query($query_Recordset1, $koneksi) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>

<?php do { ?>
<table width="157" border="0" align="left" id="content">
<tr>
<td class="borderfoto"> <a class="" href="galeri.php?id=<? echo $row_Recordset1['vch_portfolioId']; ?>" title="<?php echo $row_Recordset1['vch_portfolioClient']; ?>"> <img src="uploaded/portfolio/thumb/<?php echo $row_Recordset1['vch_portfolioImage']; ?>" border="0" />
</a></td>
</tr>
<?php $judul = $row_Recordset1['vch_portfolioClient']; ?>
<tr>
<td><?php echo substr($judul, 0, 20); ?></td>
</tr><?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
</table>
<?php
mysql_free_result($Recordset1);
}?></div>




its gonna be like this :
http://i153.photobucket.com/albums/s220/marchel_hogwarts/Untitled-1-1.jpg

The code is working good just like what i want.. When user check movie and music, the script only load data where the categories are movie and music.. The problem is after it load the data and display it, my checkboxes for movie and music that i checked before is cleared.. I want it stay checked so user know about their sorting..

Thanks all.. I'd really appreciate your help

enigma1

11:23 am on Jul 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Portnumbay, welcome to the forums.

You need to setup the value of the checkboxes according to the results of the operation. So when the form is posted, you can have for instance:

$cat_check = isset($_POST['cat1'])?' checked="checked"':'';
echo '<input type="checkbox" name="cat1" value="1"' . $cat_check. />';