Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to display checked/unchecked checkbox from mysql query array result.

Posted on 2006-04-22
7
Medium Priority
?
570 Views
Last Modified: 2013-12-12
I'm doing a query in the db and display the result in a form to update certain fields.

The query will return many rows.

I have no problem at this point to populate the form and to do a multiple update in the db. (They are all textbox).

1 of the field though hold a value of 0 or 1, I could display it in a textbox and to update it you just change the 0 to 1 or vice versa BUT what I would like to do is display it as a checkbox, checked or unchecked depending if the value is 1 or 0.

So I thought that an if statement would do the trick

if(".row['issues']." == '1') { <input type=\"checkbox\" name=\"issues[]\" value=\"1\" checked> } else { <input type=\"checkbox\" name=\"issues[]\" value=\"1\">

I cannot get it to work, there is no place in my code where I can run this code without creating an error, here is the code.

case "Edit":
                 
                    $values_to_detail = array(); $values_to_detail = $_POST['Record_id'];    
                   
                    $listvals = $_POST['Record_id']; $n=count($listvals);        


                  foreach($values_to_detail as $value) {
                 
         

                     $view = mysql_query("SELECT *
                                            FROM Peel_Region
                                           WHERE id = '$value'");
                                           
                           
               while($row = mysql_fetch_array($view))
                                 
            print "  <br>
                     <form action=\"edit.php\" method=\"POST\">
                     <table border=1 cellpadding=2 cellspacing=0 width=\"90%\" bgcolor=\"white\">
                     <input type=\"hidden\" name=\"count\" value=\"$n\">
                     <input type=\"hidden\" name=\"id[]\" value=\"$value\">
                     <tr><td bgcolor=\"#eeeecc\" width=\"20%\">ID#:</td><td>".$row['lfs_id']."</td>
<td>Issue:</td><td><input type=\"checkbox\" name=\"issues[]\" value=\"".$row['issues']."\"></td></tr>
                     <tr><td bgcolor=\"#eeeecc\">Address:</td><td width=\"20%\">".$row['address']."</td></tr>
                     <tr><td bgcolor=\"#eeeecc\">Street:</td><td>".$row['road']."</td></tr>
                     <tr><td bgcolor=\"#eeeecc\">City:</td><td>".$row['city']."</td></tr>
                                       
                      <tr><td bgcolor=\"#eeeecc\">C/O Installed:</td><td><input type=\"text\" name=\"co_installed[]\" value=\"".$row['co_installed']."\"></td></tr>

                      <tr><td bgcolor=\"#eeeecc\">Grouted:</td><td><input type=\"text\" name=\"grouted[]\" value=\"".$row['grouted']."\"></td></tr>

                      <tr><td bgcolor=\"#eeeecc\">Cored:</td><td><input type=\"text\" name=\"cored[]\" value=\"".$row['cored']."\"></td></tr>

                      <tr><td bgcolor=\"#eeeecc\">Preped:</td><td><input type=\"text\" name=\"preped[]\" value=\"".$row['preped']."\"></td></tr>

                      <tr><td bgcolor=\"#eeeecc\">Top Soil:</td><td><input type=\"text\" name=\"topsoil[]\" value=\"".$row['topsoil']."\"></td></tr>

                      <tr><td bgcolor=\"#eeeecc\">Measurement:</td><td><input type=\"text\" name=\"measurement[]\" value=\"".$row['measurement']."\"></td></tr>

                      <tr><td bgcolor=\"#eeeecc\">Pre-Video:</td><td><input type=\"text\" name=\"prevideo[]\" value=\"".$row['prevideo']."\"></td></tr>

                      <tr><td bgcolor=\"#eeeecc\">Liner Installed:</td><td><input type=\"text\" name=\"liner_installed[]\" value=\"".$row['liner_installed']."\"></td></tr>

                      <tr><td bgcolor=\"#eeeecc\">Final-Video:</td><td><input type=\"text\" name=\"finalvideo[]\" value=\"".$row['finalvideo']."\"></td></tr>

                      <tr><td bgcolor=\"#eeeecc\">Comments:</td><td><textarea name=\"comments[]\" rows=5 cols=30>".$row['comments']."</textarea></td></tr>
                     
                    </table>";
                                                   }
                                                   
                    echo "<input type=\"submit\" name=\"edit\" value=\"Edit\">";

                    break;

                }
               
        }else{ echo "<br><br><h3><font color=\"red\">Please go back and make a selection.</font></h3>"; }           exit;


if I run the if statement within the print statement it print the whole line of code if I stop the print statement  to run the code and then restart the print statement I will not get any info from the db in the following fields. after the first stop.

Anybody have an idea how it can be done.

Gamebits
0
Comment
Question by:gamebits
  • 3
  • 3
7 Comments
 
LVL 4

Expert Comment

by:AndyAelbrecht
ID: 16518600
change your print statement to this:

 print "  <br>
                     <form action=\"edit.php\" method=\"POST\">
                     <table border=1 cellpadding=2 cellspacing=0 width=\"90%\" bgcolor=\"white\">
                     <input type=\"hidden\" name=\"count\" value=\"$n\">
                     <input type=\"hidden\" name=\"id[]\" value=\"$value\">
                     <tr><td bgcolor=\"#eeeecc\" width=\"20%\">ID#:</td><td>".$row['lfs_id']."</td>
<td>Issue:</td><td><input type=\"checkbox\" name=\"issues[]\" value=\"1\" ".$issueschecked."></td></tr>
                     <tr><td bgcolor=\"#eeeecc\">Address:</td><td width=\"20%\">".$row['address']."</td></tr>
                     <tr><td bgcolor=\"#eeeecc\">Street:</td><td>".$row['road']."</td></tr>
                     <tr><td bgcolor=\"#eeeecc\">City:</td><td>".$row['city']."</td></tr>
                                       
                      <tr><td bgcolor=\"#eeeecc\">C/O Installed:</td><td><input type=\"text\" name=\"co_installed[]\" value=\"".$row['co_installed']."\"></td></tr>

                      <tr><td bgcolor=\"#eeeecc\">Grouted:</td><td><input type=\"text\" name=\"grouted[]\" value=\"".$row['grouted']."\"></td></tr>

                      <tr><td bgcolor=\"#eeeecc\">Cored:</td><td><input type=\"text\" name=\"cored[]\" value=\"".$row['cored']."\"></td></tr>

                      <tr><td bgcolor=\"#eeeecc\">Preped:</td><td><input type=\"text\" name=\"preped[]\" value=\"".$row['preped']."\"></td></tr>

                      <tr><td bgcolor=\"#eeeecc\">Top Soil:</td><td><input type=\"text\" name=\"topsoil[]\" value=\"".$row['topsoil']."\"></td></tr>

                      <tr><td bgcolor=\"#eeeecc\">Measurement:</td><td><input type=\"text\" name=\"measurement[]\" value=\"".$row['measurement']."\"></td></tr>

                      <tr><td bgcolor=\"#eeeecc\">Pre-Video:</td><td><input type=\"text\" name=\"prevideo[]\" value=\"".$row['prevideo']."\"></td></tr>

                      <tr><td bgcolor=\"#eeeecc\">Liner Installed:</td><td><input type=\"text\" name=\"liner_installed[]\" value=\"".$row['liner_installed']."\"></td></tr>

                      <tr><td bgcolor=\"#eeeecc\">Final-Video:</td><td><input type=\"text\" name=\"finalvideo[]\" value=\"".$row['finalvideo']."\"></td></tr>

                      <tr><td bgcolor=\"#eeeecc\">Comments:</td><td><textarea name=\"comments[]\" rows=5 cols=30>".$row['comments']."</textarea></td></tr>
                     
                    </table>";

and before the if statement, add:

if ($row["issues"] == 1){ $issueschecked = "CHECKED"; }

now, you will get a checkbox that's checked when the value in $row["issues"] is checked, else you get a nonchecked checkbox.
however, on the receiving end of the script, you will need to get the integer value of the $_POST["issues"] field because it will return an empty value when it is not checked.
you can do this: $issuesval = (int) $_POST["issues"]

alternatively, you could add some JavaScript to you form page, but that's another story and as usual, not that secure.

hope this helps

cheers,
Andy
0
 
LVL 28

Author Comment

by:gamebits
ID: 16518758
2 things, the value for the checkbox is not comming from a $_POST but from a db query so it will always have a 0 or a 1 second thing is, I cannot run the if statement anywhere.

before the query doesn't make sense of course.

before the while doesn't return anything.

after the while loop, the form displayed is empty

if I try to run the if statement inside the print then the whole statement is getting print and not executed anyway.

if I end the print statement on the line before the if statement
and then execute the if statement
and restart a print statement  after the if statement has been executed, the form is populated only in the first print statement everything else after that is empty.

Hope this help to clarify the situation I'm running into.

Thanks for your help.

Gamebits
0
 
LVL 9

Accepted Solution

by:
tolgaong earned 2000 total points
ID: 16518773
change the line

OLD CODE LINE
<td>Issue:</td><td><input type=\"checkbox\" name=\"issues[]\" value=\"".$row['issues']."\"></td></tr>

NEW CODE LINE
<td>Issue:</td><td><input type=\"checkbox\" name=\"issues[]\" value=\"".$row['issues']."\"".(($row['issues']=="1")?" checked":"")."></td></tr>

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 28

Author Comment

by:gamebits
ID: 16519021
I accepted your answer, cause it works but would you mind to explain to me what is happening so I can understand it.

Thanks

Gamebits
0
 
LVL 4

Expert Comment

by:AndyAelbrecht
ID: 16521117
the line
($row['issues']=="1")?" checked":""
is short for
if ($row['issues'] == "1"){
  echo " checked";
}else{
  echo "";
}

so if the issues is 1, the checkbox gets checked.

fyi: my code did the exact same thing, i just don't like to use the short form of the if statement ;-)

also, when i said this:
> now, you will get a checkbox that's checked when the value in $row["issues"] is checked, else you get a nonchecked checkbox.
> however, on the receiving end of the script, you will need to get the integer value of the $_POST["issues"][$i] field because it will return an empty value when it is not checked.
> you can do this: $issuesval = (int) $_POST["issues"][$i]

that still applies; you'll see what i mean once you start working on the code for the edit.php form.

cheers,
Andy
0
 
LVL 28

Author Comment

by:gamebits
ID: 16530514
Hi Andy

Very sorry if I upset you, and yes I understand the problem about an unchecked checkbox, it is just not sent with the rest of the data so when I do the loop to update every records I end up updating the wrong records.

Not sure if I understand what you suggest to fix that issue, if you dont mind to help me out with that issue (no punt intended) I will post another question to deal with that.

Thanks

Gamebits
0
 
LVL 4

Expert Comment

by:AndyAelbrecht
ID: 16532543
hey mate i'm far from upset, no idea where u got that ? :-D
i'm just stating the facts in the last post, i make no problem of not getting points or whatever, i'd like to see your problem fixed, mostly :-)

basically, what i suggest you try to do to cope with an unchecked checkbox is:

in the receiving script, you have your $_POST array, right ?
in that array, all elements from the form will be available; however, like you noticed, an unchecked checkbox, no matter what its value is, will give u "" (empty).
now, because this databasefield needs a 0 and not a "", you need to conver this.
hence, you should use something like:   $realissue = (int) $_POST["issues"][$i]   for every "issues" checkbox on the form.
alternatively, you can use the intval() function, which would look like this:    $realissue = intval($_POST["issues"][$i]

i hope this explains and helps ?

cheers,
Andy
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month20 days, 20 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question