2

I want to use for loop inside echo statement and I have used following code but its getting error can anyone please help me?

$a = "<select class='formfield_select'   name='return_pickuptime_hh' id='rhh' disabled='disabled'>
                      <option value='' selected='selected' disabled='disabled'>Hour</option>
                      ".for($i=$data['timeh'];$i <= 24;$i++) {."
                      <option value='".$i."'>".$i."</option>
                      ". } ."
                    </select>"; 

This code is in ajax part so i need this whole html as one variable so that i can replace this part The error is like this syntax error, unexpected T_FOR in CI/application/controllers/lha.php on line 748

chirag
  • 105
  • 2
  • 3
  • 10

2 Answers2

11

id recommend either building your options first then appending them to your echo string like so:

$optionString = '';
for ( $i = $data['timeh']; $i <=24; $i++ ){
 $optionString .= "<option ...>".$i."</option>";
}

or making 2 echos to open and close the select and a loop with the option echo in the middle eg

echo "<select ...>";

for ( $i = $data['timeh']; $i <=24; $i++ ){
 echo "<option ...>".$i."</option>";
}

echo "</select>"; 
Joe
  • 2,085
  • 1
  • 18
  • 28
  • sorry but i need this html part in one variable – chirag Nov 09 '13 at 06:22
  • then i would set a variable like `$htmlString`... set it to your opening select string. use the above for loop to append the options. after the loop append the closing select tag - and echo it all out at once – Joe Nov 09 '13 at 06:25
  • 1
    @chirag Then replace the first `echo` with `$string=`, and the remaining ones with `$string.=`. Then `$string` will contain everything in one variable, which can be `echo`'d. – Willem Renzema Nov 09 '13 at 06:25
0

try this..

echo "<select class='formfield_select'   name='return_pickuptime_hh' id='rhh' >
                  <option value='' selected='selected' >Hour</option>
                  ";


                  for($i=0;$i<=24;$i++) 
                  {

                  echo "<option value=$i>$i</option>";

                   } 

                echo "</select>"; 
Anshul Parashar
  • 3,096
  • 4
  • 30
  • 56