-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.

2
  • when echoing, if you want quoatations in your echoed string, always use single quote. for eg. "bla bla 'bla' 'blabla'blip'blop' " Commented Jul 22, 2014 at 8: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. or look up "PHP Alternative syntax" if that question/answer gets banned. Commented Dec 16, 2021 at 7:42

3 Answers 3

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>";
Sign up to request clarification or add additional context in comments.

Comments

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!

Comments

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>";

}
?>

Comments

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.