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

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
LVL 28
gamebitsAsked:
Who is Participating?
 
tolgaongCommented:
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
 
AndyAelbrechtCommented:
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
 
gamebitsAuthor Commented:
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
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
gamebitsAuthor Commented:
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
 
AndyAelbrechtCommented:
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
 
gamebitsAuthor Commented:
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
 
AndyAelbrechtCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.