Too Cool for Internet Explorer


Swobodin's Profile | Swobodin's Blog | Back to Fedora Tunisia

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.

Related posts

(sometimes, the plugin foolishes)
Stallman: I’m against fascism
Snipper
My blog traffic: analysis
Googlism
The Insider

Technorati tags

One Response to “Fixing PHP code”

  1. /etc/ Says:

    :- Yes, impossible. I give up.
    :rolleyes:

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>

 
 

This is a captcha-picture. It is used to prevent mass-access by robots. (see: www.captcha.net)

You must read and type the 5 chars within 0..9 and A..F, and submit the form.

  

Oh no, I cannot read this. Please, generate a


Top Tunisie Blogs