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>";
}
?>
Related
This is my code:
<?php
$date = 2015-02-30;
$year = substr($date, 0, 4);
$month = substr($date, 5, 2);
$day = substr($date, 8, 2);
?>
<select>
<?php
for ($i=1; $i < 31; $i++) { ?>
<option value="<?php echo $i; ?>" <?php if($day === $i){ echo "selected"; }; ?>><?php echo $i; ?></option>
<?php } ?>
</select>
For the option, the number 4 should be selected. Why doesn't it work? Thanks
Sorry, I already had this in a select statement
EDIT: See the code edit above. Maybe because the
You need to wrap your code in a select statement!
An option statement wont work without the select tag around it:
<html>
<body>
<select> <!-- Start the select statement -->
<!-- Your Code -->
<?php
$num = 4;
for ($i=1; $i < 10; $i++)
{
?>
<option value="<?php echo $i; ?>" <?php if($num === $i){ echo "selected"; }; ?>><?php echo $i; ?></option>
<?php
}
?>
<!-- End your code -->
</select> <!-- End the select statement -->
</body>
</html>
After your edit, it looks like this is your main problem:
$date = 2015-02-30;
This is not what you think it is. It should be quoted like this:
$date = '2015-02-30';
Otherwise, $date is a not a string, it's a math expression that evaluates to (int) 1983, so substr($date, 8, 2); will evaluate to false, not 30, and then obviously your option won't be selected.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<select>
<?php
include 'connection.php';
$q = "SELECT * FROM rooms WHERE duration='first lecture' and day='Sunday'";
$r = mysql_query($q);
$ro = mysql_num_rows($r);
while($row = mysql_fetch_array($r)){
for ($i=1; $i<=14; $i++)
{
$exclude = array($row['name']);
if(in_array($i, $exclude)) continue;
?>
<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php
}
}
?>
</select>
</body>
</html>
I want to use a FOR LOOP to eliminate elements retrieved by an array in one loop. Suppose $row retrieved some values , what I want with that FOR LOOP is to display numbers between 1 to 14 except the retrieved values .
I tried it many times and I succeed in that, but the loop in first time eliminate just first retrieved and in second time eliminate just second retrieved value.
Is there's a way to eliminate both at one time ?
Considering $row['name'] is a int and use the below code
while($row = mysql_fetch_array($r)){
$exclude[] = $row['name'];
}
for ($i=1; $i<=14; $i++)
{
if(in_array($i, $exclude)) continue;
?>
<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php } ?>
Start using mysqli_* function. mysql_* functions are depreciated.
First off you should be using PHP PDO for db interactions, mysql_fetch_array has been deprecated. Here is a link to the PDO's manual http://php.net/manual/en/book.pdo.php
See answer bellow:
<?php
$q = "SELECT * FROM rooms WHERE duration='first lecture' and day='Sunday'";
$stmt = $db_pdo->prepare($sql);
$stmt->execute();
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<select name="" id="">
<?php
for ($i = 1; $i <= 14; $i++):?>
<?php $good_to_go = true; ?>
<?php foreach ($data as $exclude): ?>
<?php if ($exclude['name'] == $i): ?>
<?php $good_to_go = false;?>
<?php endif; ?>
<?php endforeach; ?>
<?php if ($good_to_go): ?>
<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php endif; ?>
<?php endfor; ?>
</select>
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; ?>
My client would prefer a currency drop-down list to the currency icon block installed on a 1.5.1 OpenCart theme. I've tried coding it but get the following error:
Parse error: syntax error, unexpected T_STRING, expecting ';' in /...file path......
I've pasted the code and used before and after the offending line.
<?php
$a = 0;
foreach ($currencies as $currency) {
$thisCurTitle[$a] = $currency['title'];
$thisCurCode[$a] = $currency['code'];
if ($currency['symbol_left']) {
$thisCurSymb[$a] = $currency['symbol_left'];
} else {
$thisCurSymb[$a] = $currency['symbol_right'];
}
$a++;
}
?>
<select name=”curselect” onchange=”$(‘input[name=\'currency_code\']‘).attr(‘value’, this.options[this.selectedIndex].value).submit(); $(this).parent().parent().submit();”>
***<?php for ($z = 0; $z <= $a – 1; $z++) { ?>***
<?php if ($thisCurCode[$z] == $currency_code) { ?>
<option value=”<?php echo $thisCurCode[$z]; ?>” selected><?php echo $thisCurTitle[$z]; ?> <?php echo $thisCurSymb[$z]; ?></option>
<?php } else { ?>
<option value=”<?php echo $thisCurCode[$z]; ?>”><?php echo $thisCurTitle[$z]; ?> <?php echo $thisCurSymb[$z]; ?></option>
<?php } ?>
<?php } ?>
Any help would be most appreciated.
$a – 1 contains something like an m-dash, or whatever it's called. Not a - minus sign. This most likely happened because you copy-pasted code that had been through an auto-formatter like some mail programs or word processors.
after 2 hours of work here is my solution
<form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data" name="formcur" id="formcur">
<div id="currency">
<?php echo $text_currency; ?>
<?php
$a = 0;
foreach ($currencies as $currency) {
$thisCurTitle[$a] = $currency['title'];
$thisCurCode[$a] = $currency['code'];
if ($currency['symbol_left']) {
$thisCurSymb[$a] = $currency['symbol_left'];
} else {
$thisCurSymb[$a] = $currency['symbol_right'];
}
$a++;
}
?>
<select name="curselect" id="curselect" onchange="$('input[name=\'currency_code\']').attr('value', document.getElementById('curselect').value); document.forms['formcur'].submit();">
<?php
for ($z = 0; $z <= $a - 1; $z++) {
if ($thisCurCode[$z] == $currency_code) { ?>
<option value="<?php echo $thisCurCode[$z]; ?>" selected><?php echo $thisCurTitle[$z]; ?> <?php echo $thisCurSymb[$z]; ?></option>
<?php
} else {
?>
<option value="<?php echo $thisCurCode[$z]; ?>"><?php echo $thisCurTitle[$z]; ?> <?php echo $thisCurSymb[$z]; ?></option>
<?php } ?>
<?php } ?>
</select>
<input type="hidden" name="currency_code" value="" />
<input type="hidden" name="redirect" value="<?php echo $redirect; ?>" />
</div>
</form>
I think your problem is that the value returned in your if block is a String?
if ($currency['symbol_left']) {
Had to clean up your code to be able to make any sense of it. You had a load of odd ` chars stick to " and ' and try to keep inside PHP as much as possible.... this is not an answer yet.... I suggest you upload this and identify to us exaclty which line is at fault, I suspect its in the first echo
<?php
$a = 0;
foreach ($currencies as $currency)
{
$thisCurTitle[$a] = $currency['title'];
$thisCurCode[$a] = $currency['code'];
if ($currency['symbol_left'])
{
$thisCurSymb[$a] = $currency['symbol_left'];
}
else
{
$thisCurSymb[$a] = $currency['symbol_right'];
}
$a++;
}
echo "<select name='curselect' onchange='$('input[name=\'currency_code\']').attr('value’, this.options[this.selectedIndex].value).submit(); $(this).parent().parent().submit();'>";
for ($z = 0; $z <= $a – 1; $z++)
{
if ($thisCurCode[$z] == $currency_code)
{
echo "<option value='".$thisCurCode[$z]."' selected>".$thisCurTitle[$z]." ".$thisCurSymb[$z]."</option>";
}
else
{
echo "<option value='".$thisCurCode[$z]."'>".$thisCurTitle[$z]." & nbsp;".$thisCurSymb[$z]."</option>";
}
}
?>
found the problem.....
You have placed a calculation inside the for() statement...
$a =10; // this is to mimic your counter
$a=$a-1; // do you subtract here
//for ($z = 0; $z <= $a – 1; $z++) // this is the issue.
//for ($z = 0; $z <= ($a – 1); $z++) // doesn't work either.
for($z=0; $z <= $a; $z++)
{
echo $z." hello</br />";
}
http://php.net/manual/en/control-structures.for.php
cant see any reason why you wouldnt be able to do it the way you tried, my PHP errors in the same way, even encapsulating the subtract in ()... fixes the issue but still bothers me. Rarely use for()...
I hope that fixes your trouble!
I use brackets when using foreach loops. What is endforeach for?
It's mainly so you can make start and end statements clearer when creating HTML in loops:
<table>
<? while ($record = mysql_fetch_assoc($rs)): ?>
<? if (!$record['deleted']): ?>
<tr>
<? foreach ($display_fields as $field): ?>
<td><?= $record[$field] ?></td>
<? endforeach; ?>
<td>
<select name="action" onChange="submit">
<? foreach ($actions as $action): ?>
<option value="<?= $action ?>"><?= $action ?>
<? endforeach; ?>
</td>
</tr>
<? else: ?>
<tr><td colspan="<?= array_count($display_fields) ?>"><i>record <?= $record['id'] ?> has been deleted</i></td></tr>
<? endif; ?>
<? endwhile; ?>
</table>
versus
<table>
<? while ($record = mysql_fetch_assoc($rs)) { ?>
<? if (!$record['deleted']) { ?>
<tr>
<? foreach ($display_fields as $field) { ?>
<td><?= $record[$field] ?></td>
<? } ?>
<td>
<select name="action" onChange="submit">
<? foreach ($actions as $action) { ?>
<option value="<?= $action ?>"><?= action ?>
<? } ?>
</td>
</tr>
<? } else { ?>
<tr><td colspan="<?= array_count($display_fields) ?>"><i>record <?= $record['id'] ?> has been deleted</i></td></tr>
<? } ?>
<? } ?>
</table>
Hopefully my example is sufficient to demonstrate that once you have several layers of nested loops, and the indenting is thrown off by all the PHP open/close tags and the contained HTML (and maybe you have to indent the HTML a certain way to get your page the way you want), the alternate syntax (endforeach) form can make things easier for your brain to parse. With the normal style, the closing } can be left on their own and make it hard to tell what they're actually closing.
It's the end statement for the alternative syntax:
foreach ($foo as $bar) :
...
endforeach;
Useful to make code more readable if you're breaking out of PHP:
<?php foreach ($foo as $bar) : ?>
<div ...>
...
</div>
<?php endforeach; ?>
as an alternative syntax you can write foreach loops like so
foreach($arr as $item):
//do stuff
endforeach;
This type of syntax is typically used when php is being used as a templating language as such
<?php foreach($arr as $item):?>
<!--do stuff -->
<?php endforeach; ?>
It's just a different syntax. Instead of
foreach ($a as $v) {
# ...
}
You could write this:
foreach ($a as $v):
# ...
endforeach;
They will function exactly the same; it's just a matter of style. (Personally I have never seen anyone use the second form.)
How about this?
<ul>
<?php while ($items = array_pop($lists)) { ?>
<ul>
<?php foreach ($items as $item) { ?>
<li><?= $item ?></li>
<?php
}//foreach
}//while ?>
We can still use the more widely-used braces and, at the same time, increase readability.
Using foreach: ... endforeach; does not only make things readable, it also makes least load for memory as introduced in PHP docs
So for big apps, receiving many users this would be the best solution
How about that?
<?php
while($items = array_pop($lists)){
echo "<ul>";
foreach($items as $item){
echo "<li>$item</li>";
}
echo "</ul>";
}
?>