Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.