Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

jamesjones21
jamesjones21
9,260 Points

foreach loop with if statements

Hi,

I'm trying to establish an if statement within a foreach loop, which the output it to have the $value['name'] to be called out inbetween the HTML. But now it only provides me with 1 and the value within the value: My associative array as follows:

$options_format = [] []

    'name' => 'Audio',
    'type' => 'Audio',

];

$options_format = [] [

    'name' => 'EBook',
    'type' => 'EBook',
];

$options_format = [] [

    'name' => 'Hardcover',
    'type' => 'Hardcover',
];

$options_format = [] [

    'name' => 'Paperback',
    'type' => 'Paperback',
];
<optgroup label="Books">
                        <?php // replace back here
                            foreach ($options_format as $key => $value) {
                             echo '<option value="' . $value['name'] . '"' . '(' . $value['name'] && $value['name'].  '==' . $value['name'] . ')' . 'echo selected; }' . '"' . '>' ; 
                               echo  $value['name'];
                               echo '</option>'. "\n";
                            }
                        ?>
                        </optgroup>

the output after the loop is executed:

<optgroup label="Books">
                        1Audio</option>
1EBook</option>
1Hardcover</option>
1Paperback</option>

Any help would be appreciated, thanks.

James

4 Answers

Antonio De Rose
Antonio De Rose
20,885 Points

I am finding a little hard to understand on the little parts of the checking with == yet, I think, you can get some direction from the below

<?php

$options_format = array
  (
  array('name' => 'Hardcover'
        'type' => 'Hardcover'),
  array('name' => 'Paperback',
        'type' => 'Paperback'),
  array('name' => 'EBook',
        'type' => 'EBook'),
  array('name' => 'Audio',
        'type' => 'Audio')

  );

//echo "<pre>";
//print_r($options_format);
//echo "</pre>";

echo "<select>";
  echo "<optgroup label=\"Books\">";
    foreach ($options_format as $option){
        foreach($option as $key => $value){
        echo "<option value= ".$value['name'] .">$value</option>";
        }
    }
  echo "</optgroup>";
echo "</select>";


?>
jamesjones21
jamesjones21
9,260 Points

Hi Antonio,

I’m trying to a conditional so if the value is equal to the selection then it would be selected. It’s to do with form input.

Originally I was using a different variable to if $format to hold the value and compare it.

Antonio De Rose
Antonio De Rose
20,885 Points

I get now, however, I would like to know, what are you trying to check against what

you say if the value is equal to the selection

where the value coming from and what is the selection

jamesjones21
jamesjones21
9,260 Points

selection will be coming from another variable from within the same page, so $format will hold the variable against $value and will do the comparison.

Antonio De Rose
Antonio De Rose
20,885 Points

will this be helpful

<?php

$options_format = array
  (
  array('name' => 'Hardcover',
        'type' => 'Hardcover'),
  array('name' => 'Paperback',
        'type' => 'Paperback'),
  array('name' => 'EBook',
        'type' => 'EBook'),
  array('name' => 'Audio',
        'type' => 'Audio')

  );


$format = "A";

echo "<select>";
  echo "<optgroup label=\"Books\">";
    foreach ($options_format as $option){
        foreach($option as $key => $value){
            echo "<option value= ". $value . " <?php echo $format == \"A\" ? \"selected\"=\"selected\" : ''; ?>{$value}</option>";
        }
    }
  echo "</optgroup>";
echo "</select>";

?>
jamesjones21
jamesjones21
9,260 Points

this finally worked, used a ternary operator: this outputs the correct format and also has the selected attribute :)

foreach ($options_format as $key => $value) {
       echo '<option value="' . $value['name'] . '"' . ((isset($format) && $format == $value['name']) ? "$select" : "");
       echo ">" . $value['name'];
       echo '</option>' . "\n";
 }

Thanks very much for the help also.

(Y)

James