Page 1 of 1

Explain this: Lose value in array during for loop

Posted: Fri Jun 16, 2006 5:29 am
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!

Posted: Fri Jun 16, 2006 5:44 am
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.

Posted: Fri Jun 16, 2006 6:40 am
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!!!) :?

Posted: Fri Jun 16, 2006 7:28 am
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>

Posted: Fri Jun 16, 2006 7:38 am
by tecktalkcm0391
I think you can just do

Code: Select all

date("Y"-"m"-"d");
If I remember right

Posted: Fri Jun 16, 2006 7:46 am
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.