-1

I am trying to loop through an array and print HTML.

<?php
$categories = array('Hardware', 'Software', 'Game Items', 'Game Accounts');
?>

Some HTML goes here. I just wanted the array at the top of the page for easy edit.

<?php
foreach($categories as $category){

    echo "<option value=\"Hardware\"{$GLOBALS['filters_set']['trdfcat']['selected']['\" . $category . \"']}>\" . $category . \"</option>\""

}
?>

I am having a hard time trying to get the syntax right on this echo part. Some reason wont echo right which I think it is because of the parentheses.

The original HTML code is

<option value="Hardware"{$GLOBALS['filters_set']['trdfcat']['selected']['Hardware']}>Hardware</option>

It has to echo just like this.

user3858124
  • 15
  • 1
  • 6
  • when echoing, if you want quoatations in your echoed string, always use single quote. for eg. "_bla bla 'bla' 'blabla'blip'blop'_ " – epipav Jul 22 '14 at 08:10
  • This seems to be a sensitive question for some reason. Downvote here, and following duplicate is closed altogether. But not before someone smuggled in one answer. [Read at your own discretion.](https://stackoverflow.com/a/10258371/3273963) or look up "PHP Alternative syntax" if that question/answer gets banned. – papo Dec 16 '21 at 07:42

3 Answers3

3

It looks like you have some syntax errors with the quotation marks. Try concatenating the variables:

echo "<option value='Hardware" . $GLOBALS['filters_set']['trdfcat']['selected'][$category] . "'>$category</option>";

If you really want to interpolate:

echo "<option value='Hardware{$GLOBALS['filters_set']['trdfcat']['selected'][$category]}'>$category</option>";

You can also split things up if it makes it easier:

echo "<option value='Hardware";
echo $GLOBALS['filters_set']['trdfcat']['selected'][$category];
echo "'>$category</option>";
rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
2

Theres an alternative Syntax for foreach (as well for while etc.) you could use.

Alternative Syntax for foreach loops and echo

<?php foreach($iterable as $item): ?>
<span><?=$item?></span>
<?php endforeach;?>

I like to use this Syntax because it looks more clean to me. Your code adapted to my suggest would be:

<?php
$categories = array('Hardware', 'Software', 'Game Items', 'Game Accounts');
?>
//Some html...
<?php foreach($categories as $category): ?>
<option value="Hardware" <?=$GLOBALS['filters_set']['trdfcat']['selected']['Hardware'];?>><?=$category;?></option>
<?php endforeach;?>

I dont know why you would put "Hardware on the beginning of every value if there are multiple categories and also put some of your output in a non attribute part of the tag, maybe you mean something like:

<?php
$categories = array('Hardware', 'Software', 'Game Items', 'Game Accounts');
?>
//Some html...
<?php foreach($categories as $category): ?>
<option value="<?=$GLOBALS['filters_set']['trdfcat']['selected'][$category];?>"><?=$category;?></option>
<?php endforeach;?>

Should be okay, but not tested!

kevdev
  • 1,054
  • 8
  • 9
1

I tend to split stuff like this up in seperate lines for better readability:

<?php
foreach($categories as $category) {

    echo "<option value=\"Hardware\"";
    echo $GLOBALS['filters_set']['trdfcat']['selected'][$category];
    echo ">" . $category . "</option>";

}
?>
Maximilian Riegler
  • 22,720
  • 4
  • 62
  • 71