I have a problem to show my database values into a select box.
Here is my code
<select name="bugsolver">
<?php
if(count($yourBugs) > 0)
{
foreach( $emails as $key=> $singleEmail)
{ ?>
<option value="<?=$singleEmail['email']?>" selected='selected'> <?php echo $singleEmail['email']?></option>";
<?php }
} ?>
</select>
Your code should look like this:
<select name="bugsolver">
<?php
if(count($yourBugs) > 0):
foreach( $emails as $key=> $singleEmail ):
?>
<option value="<?php echo $singleEmail['email']; ?>">
<?php echo $singleEmail['email']; ?>
</option>
<?php
endforeach;
endif;
?>
</select>
When your code is easy to read (at least for you) it's easier to find bugs. If above code still doesn't work, do var_dump($yourBugs) and var_dump($emails) to check if these values are properly set. You can have disabled short tags (<?=) or variables are just empty.
Your code seems be OK. Try to test for $emails instead $yourBugs to get more info:
<?php if (count($emails)) : // test if $emails have values ?>
<select name="bugsolver">
foreach( $emails as $key=> $singleEmail) { /* ... */ }
</select>
<?php else : ?>
There are no emails to select ($emails is empty)
<?php endif; ?>
Related
I have an if statement that only has to show some code if the value of $result37 = ncrteam... but how can I echo that HTML and PHP code? echo" "; is not working and echo ' '; Is also not working.
This is my code:
<?php
if ($result37 === "ncrteam") {
?>
Status:<br>
<select class="form-control" name="status" style="width: 300px">
<?php
while ($row15 = mysqli_fetch_assoc($result15)):; ?>
<option selected value=\"<?php echo $row15['status'];?>\"><?php echo $row15['status'];?></option>
<?php endwhile;?>
<?php while ($row16 = mysqli_fetch_assoc($result16)):; ?>
<option value=\"<?php echo $row16['statusname'];?>\"><?php echo $row16['statusname'];?></option>
<?php endwhile;?>
</select>
<?php
}
?>
This is not a complete answer, because it is unclear what exactly you are trying to do, but your question looks something like How do I build html based on values of my php variables and I will try to answer it as best I can
With the limited information I have from your question, I think this is the kind of thing you are trying to do:
<?php
...
$html = "";
if ($result37 === "ncrteam") {
while ($row15 = mysqli_fetch_assoc($result15))
{
$html .= "<option value=".$row15['status'].">";
}
}
?>
<html>
<body>
...
<select>
<?php echo $html; ?>
</select>
...
</body>
</html>
I've been struggling with this for at least an hour to no avail. I'm sure the answer is simple, but I can't figure it out. The foreach loop is what I'm trying to accomplish, but it doesn't work. The for loop works and renders fine. Please help! - Jon
<div class="td"><select name="vehYear">
<?php
foreach ($veh_year_array as $item)
{?>
<option value="<?php echo $item;?>"><?php echo $item;?></option>
<?php}
?>
<?php
for ($i = date('Y'); $i > 1950; $i--) {
echo "<option>$i</option>";
}
?>
</select></div>
I tested your code and it seems your error is the curly bracket }. In my environment I received an
invalid argument error
when pasting your original code. You want to be very careful in PHP since it is a sensitive language.
So exchange this
<?php}?>
to this
<?php }?>
Your code should then execute
<?php
$veh_year_array =array("Tomato", "Apple", "Orange");
foreach ($veh_year_array as $item)
{?>
<option value="<?php echo $item;?>"><?php echo $item;?></option>
<?php }?>
<?php
for ($i = date('Y'); $i > 1950; $i--) {
echo "<option>$i</option>";
}
?>
I'm trying to create something like part finder (product finder).
I was thinking to edit layered navigation.
So now I have drop-boxes instead of list using this code for
app/design/frontend/default/carpoint/template/catalog/layer/filter.phtml
<select onchange="setLocation(this.value)">
<?php $count = 0; ?>
<?php foreach ($this->getItems() as $_item): ?>
<?php $count++; ?>
<?php if($count == 1): ?>
<option value='' disabled selected style='display:none;'>Choose <?php echo $attribute_code = $_item->getFilter()->getName();?> </option>
<?php endif; ?>
<option <?php if ($_item->getCount() > 0): ?> value="<?php echo $this->urlEscape($_item->getUrl()) ?>">
<?php echo $_item->getLabel() ?> <?php else: echo '>' . $_item->getLabel() ?> <?php endif; ?> (<?php echo $_item->getCount() ?>) </option>
<?php endforeach; ?>
</select>
How can I edit custom attribute eg "Type" so he is associated with parent attribute eg "Brand".
I something like this:
I am using a script that runs on the YII framework...
I have this select box that lets a user view content based on their country. But right now, when a user selects their country, after the selection, it just shows the country name...
And what I want is, after a user selected their country... it must still show the select box, but have the selected country at the top of the list.
I have tried so many things but I'm still new to this this.
<?php if ( $_country == '' ) { ?>
<select onchange="changeCountry(this)" name="country" id="country">
<?php if ( count ($c) != 0 ) { ?>
<?php if ( $_country == NULL ) ?>
<?php foreach ($c as $id => $val) { ?>
<option<?php if ($id == $countryId) {?> selected="selected"<?php } ?> value="<?php echo mbCoreFunctions::to_seo_ad_name ($val, $id) ?>"><?php echo $val; ?></option>
<?php } ?>
<?php } ?>
</select>
<?php } else { ?>
<span class="countryName"><?php echo $countryName; ?></span>
<?php } ?>
I didn't try but following should work
<select onchange="changeCountry(this)" name="country" id="country">
<?php foreach ($c as $id => $val) { ?>
<option<?php if ($id == $countryId) {?> selected="selected"<?php } ?> value="<?php echo mbCoreFunctions::to_seo_ad_name ($val, $id) ?>"><?php echo $val; ?></option>
<?php } ?>
</select>
So what I have is an array of things. The items in the array are put into a drop down with the tag, nothing new or complicated about that but here's the (current working) code anyway:
<?php foreach($roles as $role): ?>
<option value="<?php echo $role['id']; ?>"><?php echo $role['name']; ?></option>
<?php endforeach ?>
Now what needs to happen, is that if the $role['name'] is "Basic", it should not be displayed. I have been trying (and googling) for this, and I have been unsuccessful. I didn't think it was that big of a deal.
Here's what I'm trying:
<?php foreach($roles as $role): ?>
<?php if(!$role['name'] == "Basic") { ?>
<option value="<?php echo $role['id']; ?>"><?php echo $role['name']; ?></option>
<?php } ?>
<?php endforeach ?>
When I try that it doesn't add any fields to the drop down at all, so I'm obviously missing something here. Any tips would be appreciated, thanks!
Your problem is with operator precedence [docs]:
!role['name'] == "Basic"
Assuming that role['name'] is never empty or contains the string "0" (see converting to boolean), this will be evaluated as
false == "Basic"
which is always false. Use != instead or write !(role['name'] == "Basic").
You can use the alternative style for the if statement too:
<?php foreach($roles as $role): ?>
<?php if($role['name'] != "Basic"): ?>
...
<?php endif ?>
<?php endforeach ?>
<?php
foreach($roles as $role)
{
if($role['name'] != "Basic")
{
echo '<option value="'.$role['id'].'">'.$role['name'].'</option>';
}
}
?>
use
<?php if($role['name'] != "Basic") { ?>
instead of
<?php if(!$role['name'] == "Basic") { ?>
Your if() is incorrect:
if(!$role['name'] == "Basic")
What you intended was probably this:
if($role['name'] != "Basic")