Echo in for loop - php

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.

Related

get current year and five years before

I need a select tag with current year and five years before
<select class='selrange' id='rangeyeara'>
<?php
for($y=date("Y")-5; $y=date("Y"); $y++){
echo "<option>" . $y . "</option>";
}
?>
</select>
current year is there but there is no any other one.
The second statement in your for loop need to be the "end" condition.
Change the operator = (assignation) to <= (comparison):
<select class='selrange' id='rangeyeara'>
<?php
for($y=date("Y")-5; $y<=date("Y"); $y++){
echo "<option>" . $y . "</option>";
}
?>
</select>
Getting the year is quite easy in PHP.
<select class='selrange' id='rangeyeara'>
<?php $currentYear = date('Y'); ?>
<?php for ($year = $currentYear - 5; $year <= $currentYear; $year++): ?>
<option value="<?= $year; ?>"><?= $year; ?></option>
<?php endfor; ?>
</select>

Display Only Values not retrieved by array

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

increament by 5 for loop in php

I want to display some number in a dropdown options with the increment 5 which will be started to 5 and ended at 1005. The result should be 5, 10, 15, .... 1000, 1005.
So I wrote the loop as
<?php for ($i=5; $i < 1005; $i++) { ?>
<option value = "<?php echo $i; ?>"><?php echo $i; ?></option>
<?php } ?>
But it gives me as 5,6,7,.... . How can I resolve it?
<?php for ($i=5; $i < 1005; $i+=5) { ?>
<option value = "<?php echo $i; ?>"><?php echo $i; ?></option>
<?php } ?>
$i+=5 <-- This is the only change.
How about this?

Display 24 hours in a dropdown list using php

I am trying to create a dropdown list for display time. My Option tag should be something like this
<option value="1">01.00AM</option>
<option value="2">02.00AM</option>
<option value="3">03.00AM</option> and so on
So Can I know is there a quick way to create the array instead of typing each hours in option tag?
NOTE: AM and PM should be display according to the time.
I tried it with this code, but it doesn't work for me..
<select>
<?php for($i = 0; $i < 24; $i++): ?>
<option value="<?= $i; ?>"><?= $i % 12 ? $i % 12 : 12 ?>:00 <?= $i >= 12 ? 'pm' : 'am' ?></option>
<?php endfor ?>
</select>
A slight variant on Fresh Prince's deleted answer
I give Fresh Prince of SO most of the credit for this one, but since he deleted his original answer I'm posting a variant of it without the mixed echo tags and weird concatenations.
<select id='time'>
<?php for($i = 1; $i <= 24; $i++): ?>
<option value="<?= $i; ?>"><?= date("h.iA", strtotime("$i:00")); ?></option>
<?php endfor; ?>
</select>
Note: I am using short echo tags <?= because the original post used them. I'd recommend replacing these with <?php echo if you're writing portable code that needs to support older versions of PHP.
Output format
The output format from this script is:
<select id='time'>
<option value="1">01.00AM</option>
<option value="2">02.00AM</option>
...
<option value="23">11.00PM</option>
<option value="24">12.00AM</option>
</select>
This is a live demo.
Why not just use strtotime and date?
date("h.iA", strtotime($i . ":00"))
See a demo
Working it into your example,
<?php
for($i = 0; $i < 24; $i++):
echo "<option value=\"$i\">" . date("h.iA", strtotime($i . ":00")) . "</option>\n";
endfor;
?>
Alternatively,
<?php
for($i = 0; $i < 24; $i++, $d = date("h.iA", strtotime($i . ":00:00"))) {
echo "<option value=\"$i\">$d</option>\n";
}
?>
Lastly,
for($i = 0; $i < 24; print "<option value=\"$i\">" . date("h.iA", strtotime($i . ":00:00")) . "</option>\n", $i++);
See a demo
Checkout the following demo: http://phpfiddle.org/main/code/8y7-hut In-which sprintf formating features are used to fill preleading 0.
The following is the code used:
<select>
<?php for($i = 0; $i < 24; $i++): ?>
<option value="<?php echo $i+1; ?>"><?php printf('%1$02d.00',(($i+1) > 12)? ($i+1)-12 : $i+1)?><?php echo (($i < 12)? 'AM' : 'PM'); ?></option>
<?php endfor ?>
</select>
No need for all those messy php tags.
for ($i=0; $i<24; ++$i) {
$t = date("H.iA", strtotime($i.":00:00"));
echo '<option value="'.$i.'">'.$t.'</option>';
}
Try this:
$start = '12:00AM';
$end = '11:59PM';
$interval = '+1 hour';
// $interval = '+30 minutes';
// $interval = '+15 minutes';
$start_str = strtotime($start);
$end_str = strtotime($end);
$now_str = $start_str;
echo '<select>';
while($now_str <= $end_str){
echo '<option value="' . date('h:i A', $now_str) . '">' . date('h:i A', $now_str) . '</option>';
$now_str = strtotime($interval, $now_str);
}
echo '</select>';
Here's a working example.

Change currency icon block to drop-down list

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!

Categories