getting values in array to populate corresponding Xboxes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
glennnphp
Forum Newbie
Posts: 16
Joined: Thu Aug 28, 2008 10:13 pm

getting values in array to populate corresponding Xboxes

Post by glennnphp »

i'm having trouble getting this array to populate the selected checkboxes (this almost works, but it only gets the last value, 07 ... ).

Code: Select all

 
$offering = $_POST['highest_deg'];   //returns the selected values, "03,07"
    
        foreach($offering as $x) {
        $x... ;    
        } 
 
<input type="checkbox" name="highest_deg[]" value="03"<?php echo ($x == 03 ? " CHECKED" : ""); ?>
 
<input type="checkbox" name="highest_deg[]" value="04"<?php echo ($x == 04 ? " CHECKED" : ""); ?>
 
<input type="checkbox" name="highest_deg[]" value="05"<?php echo ($x == 05 ? " CHECKED" : ""); ?>
 
<input type="checkbox" name="highest_deg[]" value="04"<?php echo ($x == 06 ? " CHECKED" : ""); ?>
 
<input type="checkbox" name="highest_deg[]" value="05"<?php echo ($x == 07 ? " CHECKED" : ""); ?>
 

could someone kindly show me how to populate a group of checkboxes from the array i have...?

i appreciate it much.
GN
marcth
Forum Contributor
Posts: 142
Joined: Mon Aug 25, 2008 8:16 am

Re: getting values in array to populate corresponding Xboxes

Post by marcth »

Code: Select all

<?php
if(!isset($_POST['highest_deg'])) {
  $_POST['highest_deg'] = array();
}
?>
 

Code: Select all

 
<input type="checkbox" name="highest_deg[]" value="03"<?php echo in_array('03', $_POST['highest_deg']) ? ' checked="checked"' : ''); ?> />
<input type="checkbox" name="highest_deg[]" value="04"<?php echo in_array('04', $_POST['highest_deg']) ? ' checked="checked"' : ''); ?> />
<input type="checkbox" name="highest_deg[]" value="05"<?php echo in_array('05', $_POST['highest_deg']) ? ' checked="checked"' : ''); ?> />
<input type="checkbox" name="highest_deg[]" value="06"<?php echo in_array('06', $_POST['highest_deg']) ? ' checked="checked"' : ''); ?> />
 
glennnphp
Forum Newbie
Posts: 16
Joined: Thu Aug 28, 2008 10:13 pm

Re: getting values in array to populate corresponding Xboxes

Post by glennnphp »

awesome, thanks much
glennnphp
Forum Newbie
Posts: 16
Joined: Thu Aug 28, 2008 10:13 pm

Re: getting values in array to populate corresponding Xboxes

Post by glennnphp »

"Parse error: syntax error, unexpected ')', expecting ',' or ';'... " - can't find any bad syntax in this first input...
marcth wrote:

Code: Select all

<?php
if(!isset($_POST['highest_deg'])) {
  $_POST['highest_deg'] = array();
}
?>
 

Code: Select all

 
<input type="checkbox" name="highest_deg[]" value="03"<?php echo in_array('03', $_POST['highest_deg']) ? ' checked="checked"' : ''); ?> />
 Parse error: syntax error, unexpected ')', expecting ',' or ';' 
 
<input type="checkbox" name="highest_deg[]" value="04"<?php echo in_array('04', $_POST['highest_deg']) ? ' checked="checked"' : ''); ?> />
<input type="checkbox" name="highest_deg[]" value="05"<?php echo in_array('05', $_POST['highest_deg']) ? ' checked="checked"' : ''); ?> />
<input type="checkbox" name="highest_deg[]" value="06"<?php echo in_array('06', $_POST['highest_deg']) ? ' checked="checked"' : ''); ?> />
 
marcth
Forum Contributor
Posts: 142
Joined: Mon Aug 25, 2008 8:16 am

Re: getting values in array to populate corresponding Xboxes

Post by marcth »

There you go.

Code: Select all

<?php
if(!isset($_POST['highest_deg'])) {
  $_POST['highest_deg'] = array();
}
?>
 

Code: Select all

 
<input type="checkbox" name="highest_deg[]" value="03"<?php echo in_array('03', $_POST['highest_deg']) ? ' checked="checked"' : ''; ?> />
<input type="checkbox" name="highest_deg[]" value="04"<?php echo in_array('04', $_POST['highest_deg']) ? ' checked="checked"' : ''; ?> />
<input type="checkbox" name="highest_deg[]" value="05"<?php echo in_array('05', $_POST['highest_deg']) ? ' checked="checked"' : ''; ?> />
<input type="checkbox" name="highest_deg[]" value="06"<?php echo in_array('06', $_POST['highest_deg']) ? ' checked="checked"' : ''; ?> />
 
glennnphp
Forum Newbie
Posts: 16
Joined: Thu Aug 28, 2008 10:13 pm

Re: getting values in array to populate corresponding Xboxes

Post by glennnphp »

beautiful thanks again
marcth wrote:There you go.

Code: Select all

<?php
if(!isset($_POST['highest_deg'])) {
  $_POST['highest_deg'] = array();
}
?>
 

Code: Select all

 
<input type="checkbox" name="highest_deg[]" value="03"<?php echo in_array('03', $_POST['highest_deg']) ? ' checked="checked"' : ''; ?> />
<input type="checkbox" name="highest_deg[]" value="04"<?php echo in_array('04', $_POST['highest_deg']) ? ' checked="checked"' : ''; ?> />
<input type="checkbox" name="highest_deg[]" value="05"<?php echo in_array('05', $_POST['highest_deg']) ? ' checked="checked"' : ''; ?> />
<input type="checkbox" name="highest_deg[]" value="06"<?php echo in_array('06', $_POST['highest_deg']) ? ' checked="checked"' : ''; ?> />
 
Post Reply