Fixing PHP code
A a is an intern whom I am supposed to assist to fix her PHP code. The following case seems obvious; it’s nevertheless tricky.
- A a: I have several checkboxes whose the number depends on the database rows. Once a user submits, checkboxes are displayed again, but the ones already checked must appear checked too.
Initial statement:
<?php
$query = mysql_query("SELECT * FROM sector");
while $row = mysql_fetch_row($query) {
?>
<input type="checkbox" name="var[]" value="<?=$row[1]?>" /><?=$row[1]?> <br />\n";
<?php
}
?>
<input type="submit" name="sub" />
- Swobodin: First, you have to change the value by the ID, instead of the sector name.
- A a: But I have already proceeded using the ‘name’ column!
- Swobodin: Then you change everything! Using the sector name as checkbox value would mess your HTML up: spaces, special characters and so are not suitable. Thus:
<input type="checkbox" name="var[]" value="<?=$row[0]?>" /><?=$row[1]?> <br />\n";
- A a: Ok, keep working.
- Swobodin: Hey! It’s me the boss here, so I order and you have to respect the hierarchy!
- A a: Don’t foolish around! You don’t behave like a boss anyway, what’s next?
- Swobodin: Well, you’re right. Next, we assign a variable $var ; for example
<?php
$var = $_POST['var'];
while $row = mysql_fetch_row($query) {
?>
<input type=”checkbox” name=”var[]” value=”<?=$row[1]?>” <?php if (isset($var[$row[0]])) print ‘checked=”checked”‘; ?> /><?=$row[1]?> <br />\n”;
<?php
}
?>
- A a: Doesn’t work!
- Swobodin: Yes, I see; how about
<?php if ($var[] == $row[0]) print ‘checked=”checked”‘; ?>
- A a: Even without trying that won’t work. I tried everything, and I think it’s impossible.
- Swobodin: What was the last word?
- A a: Yes, impossible. I give up.
- Swobodin: How much do I have in my pocket? Only a 5-dinar coin. And you?
- A a: What? Do you want to bet?
- Swobodin: Guess that I don’t have too much to give a charity. Of course I want to bet.
- A a: I don’t have money.
- Swobodin: I understand. You have a 64-bit 3Ghz Dell Box, right? Ok, you won’t have to pay me if you lose.
- A a: Yeah, that’s it.
- Swobodin: Let’s use the array method:
<?php if (in_array($row[0], $var)) print 'checked="checked"'; ?>
- A a: Oh, It works! How did you do that?
- Swobodin: $var is an array whose the keys are 0, 1, 2, … and the values are the extracted data (ID’s) from the table.
- A a (After reinitializing the page): There’s a warning error: wrong count for in_array . I think I will get your coin.
- Swobodin: Because $var is considered as NULL if the form is not submitted. Here’s the final statement
<?php
$query = mysql_query("SELECT * FROM sector");
if (isset($_POST['var']))
$var = $_POST['var'];
else
$var = array();
while $row = mysql_fetch_row($query) {
?>
<input type="checkbox" name="var[]" value="<?=$row[0]?>" <?php if (in_array($row[0], $var)) print 'checked="checked"'; ?> /><?=$row[1]?> <br />\n";
<?php
}
?>
Now, may I blog or you still have questions?
- A a: No, thanks, it’s fixed now.

April 14th, 2006 at 9:23 pm
:- Yes, impossible. I give up.
:rolleyes: