1

I have a chunk of code that is as follows:

<td><?
    $days = array("Sun","Mon","Tue","Wed","Thur","Fri","Sat");
    for($i=1;$i<8;$i++)
    {
        echo $days[$i-1]?><input type="checkbox" name="labDays[]" value="<?=$days[$i]?>" checked="checked"><?
    }?>
</td>

However when I run print_r($_REQUEST['labDays']);

I get Array ( [0] => [1] => [2] => [3] => [4] => [5] => [6] => )

I expected Array ( [0] => Sun[1] => Mon[2] => Tue[3] => Wed[4] => Thur[5] => Fri[6] => Sat)

I'm sure it's something silly but I'm not sure what I am missing...

print_r($_REQUEST) pukes: Array ( [pg] => 12 [msg] => Facility Already Exist [facilityname] => kjgkjhgkjhg [facorgtype] => [facilitytype] => [administratorsname] => [divisionname] => [streetaddress1] => [streetaddress2] => [city] => [state] => [zipcode] => [phonenumber] => [faxnumber] => [email] => [mainstate] => [labDays] => Array ( [0] => [1] => [2] => [3] => [4] => [5] => [6] => ) [facBillingContact] => [facBillingPhone] => [facBillingFax] => [facBillingRep] => [facBillingAccNum] => [facNotes] => [submit] => Create )

var_dump($_REQUEST['labDays']):

array
  0 => string 'Sun' (length=3)
  1 => string 'Mon' (length=3)
  2 => string 'Tue' (length=3)
  3 => string 'Wed' (length=3)
  4 => string 'Thur' (length=4)
  5 => string 'Fri' (length=3)
  6 => string 'Sat' (length=3)

3 Answers 3

2

I believe this is what you're looking for:

    <?php
    $days = array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
    foreach ($days as $day) {
        echo <<<HTML
    <label>$day
        <input type="checkbox" name="labDays[]" value="$day" checked>
    </label>
HTML;

    }
    ?>

Few notes:

PHP

  • The use of the shorthand syntax (<? ?> and especially <?= ?>) is not recommended.
  • For iterating an array, foreach is more suited than a for.
  • I use PHP's heredoc syntax for strings. This way I avoid the quotes confusion.
  • Use $_POST or $_GET rather than $_REQUEST, it's considered more secure that way.

HTML

  • Use labels for form elements in HTML, this associates the text with the form control, plus when you click on the text, it will mark the appropriate checkbox too!
  • The checked attribute needs no value. It's enough that it's there.
Sign up to request clarification or add additional context in comments.

8 Comments

what is echo <<<HTML?! i have never seen this before
@Truth, so for every single HTML tag looping you write heredoc?
@Truth, OP is already confused, you are confusing him more :)
@Bill.Caffery: I see the intended result. Check the result of the entire $_POST variable (not just what you think you should see).
@KalpeshMehta: I'm not confusing him, I'm pointing him at the right direction, with good programming practices. Heredoc is far superior to all other forms of strings when writing HTML blocks (not one-liners) because it's easily visible, evaluates variables while not messing with quotes. So yes, I will use it for every single HTML tag I'm looping. In more complex cases, I would even use DOM.
|
1

If you want to loop through array, foreach is best option

$days = array("Sun","Mon","Tue","Wed","Thur","Fri","Sat");
foreach($days as $k=>$v)
{
    echo $k?><input type="checkbox" name="labDays[]" value="<?=$v?>" checked="checked"><?
}?>

Using for, you can do like this:

$days = array("Sun","Mon","Tue","Wed","Thur","Fri","Sat");
for($i=0;$i<=6;$i++)
{
    echo $i;?><input type="checkbox" name="labDays[]" value="<?=$days[$i]?>" checked="checked"><?
}?>

RECOMMENDED WAY..

$days = array("Sun","Mon","Tue","Wed","Thur","Fri","Sat");
foreach($days as $k=>$v)
{
    echo $k . ' <input type="checkbox" name="labDays[]" value="'.$v.'" checked="checked">'; //you can give your styles and css ofcourse to make it better..
}

5 Comments

This sort of syntax is not recommended.
@Truth, yes I know, just letting the OP find the mistake.
i dont want it to say 1[] 2[] 3[] i want mon[] tue[] wed[]. but i see the concatenation you did with .$v. so ty i will try that
if you want like: mon [checkbox], tue [checkbox], just replace $k with $v that's it..
can you check the html source and confirm if the value field is getting correct values? (mon, sun, etc..)
-1

Maybe with echo to print the value:

<input type="checkbox" name="labDays[]" value="<?php echo $days[$i]; ?>" checked="checked">

5 Comments

This makes no difference. He uses the shorthand syntax <?= $stuff ?> which is equivalent to <?php echo $stuff; ?>.
Yes but it requires to be configured in php.ini: php.net/manual/en/ini.core.php#ini.short-open-tag
Since PHP 5.4.0, <?= is always available. from the same source you've linked.
I'm not side by side with Bill, I don't know which version he is using. Maybe is written somewhere but I didn't see it. Or he is using an old php.ini configuration ... how can I know?
ive enable short tags enough squabbling ty

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.