Explain this: Lose value in array during for loop

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

Post Reply
User avatar
dimitris
Forum Contributor
Posts: 110
Joined: Wed Jan 14, 2004 3:47 am
Location: Athens, Greece

Explain this: Lose value in array during for loop

Post by dimitris »

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!
User avatar
bmcewan
Forum Commoner
Posts: 55
Joined: Wed Jun 02, 2004 7:19 am
Location: West Yorkshire, UK.

Post by bmcewan »

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.
User avatar
dimitris
Forum Contributor
Posts: 110
Joined: Wed Jan 14, 2004 3:47 am
Location: Athens, Greece

Post by dimitris »

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!!!) :?
User avatar
bmcewan
Forum Commoner
Posts: 55
Joined: Wed Jun 02, 2004 7:19 am
Location: West Yorkshire, UK.

Post by bmcewan »

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>
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

I think you can just do

Code: Select all

date("Y"-"m"-"d");
If I remember right
hessodreamy
Forum Commoner
Posts: 58
Joined: Wed Apr 20, 2005 8:11 am

Post by hessodreamy »

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

Code: Select all

$showdate = (string) $i;
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.
Post Reply