Jump to content

Problem with for loop value retrieval


geroido

Recommended Posts

Hi

I've included just a snippet of the code below to show the problem. I'm creating a drop down select list populated by a for loop. The drop down list works and contains values 1-31 which is what I want. However, when I try to get that value and store it in $_SESSION['day'], the variable is empty. I think I have some small problem with the loop syntax possibly in the 'value=<?$d?>' part but can't find it. Any ideas?

 

 

$_SESSION['day'] = $_REQUEST['day'];
echo $_SESSION['day'];

<TD><select name="day">
    <option value="" selected="selected">--</option>
      <?for ($d=1;$d<=31;$d++){?>
    <option value="<?$d?>" ><?echo $d?></option>
      <?}?>
      </select></td> 

Link to comment
https://forums.phpfreaks.com/topic/121671-problem-with-for-loop-value-retrieval/
Share on other sites

Make this:

<?for ($d=1;$d<=31;$d++){?>
    <option value="<?$d?>" ><?echo $d?></option>
      <?}?>

 

This:

<?php
for ($d=1;$d<=31;$d++){
?>
<option value="<?php echo $d; ?>" ><?php echo $d; ?></option>
<?php } ?>

 

I wouldn't recommend short tags, and you weren't echoing $d.

Make this:

<?for ($d=1;$d<=31;$d++){?>
   <option value="<?$d?>" ><?echo $d?></option>
     <?}?>

 

This:

<?php
for ($d=1;$d<=31;$d++){
?>
<option value="<?php echo $d; ?>" ><?php echo $d; ?></option>
<?php } ?>

 

I wouldn't recommend short tags, and you weren't echoing $d.

 

While short tags aren't recommended, they may be needed. And that does echo out $d, (it's like the concatenated version for a conditional, just because it doesn't say "echo" doesn't mean it won't echo).

 

 

Edit, I know why it won't work. Yes, you're not echoing it, but this will work for echoing it out:

 

<?=$d?>
//rather than
<?$d?>

 

See the diff?

Haha fair enough. As you can see I have never used short-tags, but I do recall seeing lots of people using them and they use the =.

 

To me they look ugly, and from what I've heard there are hosts who have them turned off. I don't see the harm in writing a few more characters.

Generally, the universal standard in writing php code is the following:

 

<?php

//blah
//blah
//some code here

?>

 

A short tag is used mainly as a "quickie" either to save room, or just to test something really quickly:

 

<? //blah ?>

 

They are usually one line.

 

When you echo something, you can do:

 

<?php
echo $name;
?>
//or
<?=$name?>

 

Both will have the same result, but the former is preferred over the latter as far as readability goes.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.