PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
dimitris
Forum Contributor
Posts: 110 Joined: Wed Jan 14, 2004 3:47 am
Location: Athens, Greece
Post
by dimitris » Fri Jun 16, 2006 5:29 am
This is the code
Code: Select all
<select name="datestart" id="datestart" style="font-size:12px;border:1px solid #6393DF;">
<option value="0">Choose Date</option>
$first=$row['registerdate'];//lets say 20060515120000
$today=date('YmdHis');
/*echo '<br>First:'.$first;
echo '<br>today:'.$today;*/
$showdate='';
for($i=$today;$i>=$first;$i-=1000000){
$showdate=$i;
?>
<option value="<?php echo $showdate; ?>">
<?php
echo (string)($showdate[0].$showdate[1].$showdate[2].$showdate[3]).'-'.(string)($showdate[4].$showdate[5]).'-'.(string)($showdate[6].$showdate[7]);
?>
</option>
<?php
}//end for($i=$first;$i<$today;$i++){
?>
</select>
What i get is :
Code: Select all
<option value="20060616132738">
2006-06-16 </option>
<option value="20060615132738">
-- </option>
<option value="20060614132738">
-- </option>
<option value="20060613132738">
-- </option>
<option value="20060612132738">
-- </option>
<option value="20060611132738">
-- </option>
<option value="20060610132738">
-- </option>
I don't get what is going wrong!
bmcewan
Forum Commoner
Posts: 55 Joined: Wed Jun 02, 2004 7:19 am
Location: West Yorkshire, UK.
Post
by bmcewan » Fri Jun 16, 2006 5:44 am
Hi there,
Not sure exactly what you want to display, let us know what output you are expecting and we may be able to point you in the right direction.
dimitris
Forum Contributor
Posts: 110 Joined: Wed Jan 14, 2004 3:47 am
Location: Athens, Greece
Post
by dimitris » Fri Jun 16, 2006 6:40 am
I think it is obvious i want to have something like this:
Code: Select all
<select name="myvariable">
<option value="20060616132738">
2006-06-16 </option>
<option value="20060615132738">
2006-06-15 </option>
<option value="20060614132738">
2006-06-14 </option>
...
</select>
It seems that it is losing the value when i call each character separately (but this doesn't happen in the first row that it is returned!!!)
bmcewan
Forum Commoner
Posts: 55 Joined: Wed Jun 02, 2004 7:19 am
Location: West Yorkshire, UK.
Post
by bmcewan » Fri Jun 16, 2006 7:28 am
Ok, small change that should give you the correct display.
change your line
Code: Select all
echo (string)($showdate[0].$showdate[1].$showdate[2].$showdate[3]).'-'.(string)($showdate[4].$showdate[5]).'-'.(string)($showdate[6].$showdate[7]);
to
Code: Select all
echo substr($showdate, 0, 4).'-'.substr($showdate,4,2).'-'.substr($showdate,6,2);
Be careful though as you have an infinite loop as the statement
$i>=$first will never resolve.
$i is moving away from
$first not toward it.
Code: Select all
<select name="datestart" id="datestart" style="font-size:12px;border:1px solid #6393DF;">
<option value="0">Choose Date</option>
<?php
$first=$row['registerdate'];//lets say 20060515120000
$today=date('YmdHis');
$showdate='';
for($i=$today;$i>=$first;$i-=1000000){ // this is an infinite loop $i will never reach $first as $i is decreasing away from it with each iteration
$showdate=$i;
?>
<option value="<?php echo $showdate; ?>">
<?php
echo substr($showdate, 0, 4).'-'.substr($showdate,4,2).'-'.substr($showdate,6,2);
?>
</option>
<?php
}
?>
</select>
tecktalkcm0391
DevNet Resident
Posts: 1030 Joined: Fri May 26, 2006 9:25 am
Location: Florida
Post
by tecktalkcm0391 » Fri Jun 16, 2006 7:38 am
I think you can just do
If I remember right
hessodreamy
Forum Commoner
Posts: 58 Joined: Wed Apr 20, 2005 8:11 am
Post
by hessodreamy » Fri Jun 16, 2006 7:46 am
To address where your code was going wrong in the first place, referencing a character of a string using [] works on strings, however, you are trying to apply it to a number, so it won't work.
Casting the number to a string like
Will get you on the right track.
However, using substring to get ranges, or using the date function, would seem to be a better idea.