Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hello, How can display the list

Status
Not open for further replies.

queryguy

Programmer
Oct 10, 2003
23
SG
Hello,

How can display the list of checked checkbox according to database value? So when the uses want to edit the info, he/she knows that which are the one already being check.

ok this is my form

<form action=&quot;submit.php&quot; method=&quot;post&quot;>

<input type=&quot;checkbox&quot; name=&quot;book[]&quot; value=&quot;love&quot;>
<input type=&quot;checkbox&quot; name=&quot;book[]&quot; value=&quot;business&quot;>
<input type=&quot;checkbox&quot; name=&quot;book[]&quot; value=&quot;computer&quot;>
<input type=&quot;checkbox&quot; name=&quot;book[]&quot; value=&quot;arts&quot;>
<input type=&quot;checkbox&quot; name=&quot;book[]&quot; value=&quot;sports&quot;>
<input type=&quot;checkbox&quot; name=&quot;book[]&quot; value=&quot;gardening&quot;>
<input type=&quot;checkbox&quot; name=&quot;book[]&quot; value=&quot;fishing&quot;>
<input type=&quot;checkbox&quot; name=&quot;book[]&quot; value=&quot;marketing&quot;>

<input type=&quot;submit&quot; value-&quot;submit&quot;>
-------------------------------------------------------
This will than send to a submit.php with implode

$bookreceived = implode(&quot;-&quot;, $book);

$book_query=&quot;UPDATE bookstore SET book='$bookreceived' WHERE nameid='$nameid' ;
$book_result = mysql_query($book_query) ;
-------------------------------------------------

Now, the user going to edit this book selections, I want to let him know which are the book he have checked. So.. how to apply your code over to my?

thanks alot.
 
here is an example that I use myself for a similar problem

Code:
$query=&quot;SELECT * from clienttypetable order by cltdescr&quot;;


$rsclt=mysql_query($query,$conn);
$list = mysql_num_rows($rsclt); 
while($i < $list)	{
$row = mysql_fetch_array($rsclt); 
$cltid=$row[&quot;cltid&quot;];
$cltdescr=$row[&quot;cltdescr&quot;];
   echo &quot;<tr><td>$cltdescr</td><td><INPUT TYPE=checkbox name=cltype[$i] value=\&quot;$cltdescr\&quot;&quot;;
	if (stristr($cltype,$cltdescr)) {echo &quot; CHECKED &quot;;}
   echo &quot;></td></tr>&quot;;
$i++;
}

[code]

I have made a table with all the possible values (in your case: love,business,computer etc) which it loops through and looks if the value is in the string saved with the record. so if 3 values are checked and saved like in your example. 3 values will be shown when you run this code.

you can also put your values in an array but it won't be so flexible if someone wants to add a new category (unless you want to keep your job secure ;))
 
thanks for the reply but on the form field should I change anything? or remain the same?

<input type=&quot;checkbox&quot; name=&quot;book[]&quot; value=&quot;love&quot;>
<input type=&quot;checkbox&quot; name=&quot;book[]&quot; value=&quot;business&quot;>
<input type=&quot;checkbox&quot; name=&quot;book[]&quot; value=&quot;computer&quot;>
<input type=&quot;checkbox&quot; name=&quot;book[]&quot; value=&quot;arts&quot;>
<input type=&quot;checkbox&quot; name=&quot;book[]&quot; value=&quot;sports&quot;>
<input type=&quot;checkbox&quot; name=&quot;book[]&quot; value=&quot;gardening&quot;>
<input type=&quot;checkbox&quot; name=&quot;book[]&quot; value=&quot;fishing&quot;>
<input type=&quot;checkbox&quot; name=&quot;book[]&quot; value=&quot;marketing&quot;>
 
=ohh.. so I have to insert the line?

$query=&quot;SELECT * from clienttypetable order by cltdescr&quot;;


$rsclt=mysql_query($query,$conn);
$list = mysql_num_rows($rsclt);
while($i < $list) {
$row = mysql_fetch_array($rsclt);
$cltid=$row[&quot;cltid&quot;];
$cltdescr=$row[&quot;cltdescr&quot;];
echo &quot;<tr><td>$cltdescr</td><td><INPUT TYPE=checkbox name=cltype1[$i] value=\&quot;$cltdescr\&quot;&quot;;
if (stristr($cltype,$cltdescr)) {echo &quot; CHECKED &quot;;}
echo &quot;>
echo &quot;<tr><td>$cltdescr</td><td><INPUT TYPE=checkbox name=cltype2[$i] value=\&quot;$cltdescr\&quot;&quot;;
if (stristr($cltype,$cltdescr)) {echo &quot; CHECKED &quot;;}
echo &quot;>
echo &quot;<tr><td>$cltdescr</td><td><INPUT TYPE=checkbox name=cltype3[$i] value=\&quot;$cltdescr\&quot;&quot;;
if (stristr($cltype,$cltdescr)) {echo &quot; CHECKED &quot;;}
echo &quot;>
echo &quot;<tr><td>$cltdescr</td><td><INPUT TYPE=checkbox name=cltype4[$i] value=\&quot;$cltdescr\&quot;&quot;;
if (stristr($cltype,$cltdescr)) {echo &quot; CHECKED &quot;;}
echo &quot;>
echo &quot;<tr><td>$cltdescr</td><td><INPUT TYPE=checkbox name=cltype5[$i] value=\&quot;$cltdescr\&quot;&quot;;
if (stristr($cltype,$cltdescr)) {echo &quot; CHECKED &quot;;}
echo &quot;>


</td></tr>&quot;;
$i++;
}

 
you can completely ignore youre code since all the checkbox thingies will be printed in the loop. if no values are present in your database everything will be unchecked.

and ps my code is an example of my database. you have to rewrite it to yours ofcourse ;)
 
I don't understand what is this?

$cltdescr=$row&quot;cltdescr&quot;;

What is $cltdescr you are referring to?
 
that are my values which are for you the booktypes, I look if they are in the total string which is in my case $cltype and in you case the field book
 
queryguy:

hos2 neglected to post his code inside [ignore]
Code:
...
[/ignore] tags. Because of this anything that is a double-quoted associative array reference, such as:

Code:
$cltdescr=$row[&quot;cltdescr&quot;];

gets turned into:

$cltdescr=$row[&quot;cltdescr&quot;];

Want the best answers? Ask the best questions: TANSTAAFL!!
 
oops sorry, since I know the code tages I use them wherever possible ;)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top