geroido Posted August 28, 2008 Share Posted August 28, 2008 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> Quote Link to comment https://forums.phpfreaks.com/topic/121671-problem-with-for-loop-value-retrieval/ Share on other sites More sharing options...
kratsg Posted August 28, 2008 Share Posted August 28, 2008 ... is there a form? Can you show me the source code of the page as viewed in the browser? We'll work from there. Quote Link to comment https://forums.phpfreaks.com/topic/121671-problem-with-for-loop-value-retrieval/#findComment-627670 Share on other sites More sharing options...
JasonLewis Posted August 28, 2008 Share Posted August 28, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/121671-problem-with-for-loop-value-retrieval/#findComment-627672 Share on other sites More sharing options...
kratsg Posted August 28, 2008 Share Posted August 28, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/121671-problem-with-for-loop-value-retrieval/#findComment-627674 Share on other sites More sharing options...
JasonLewis Posted August 28, 2008 Share Posted August 28, 2008 I thought to echo it out you needed the = <?=$d;?> I still wouldn't consider using short-tags. Quote Link to comment https://forums.phpfreaks.com/topic/121671-problem-with-for-loop-value-retrieval/#findComment-627677 Share on other sites More sharing options...
kratsg Posted August 28, 2008 Share Posted August 28, 2008 I thought to echo it out you needed the = <?=$d;?> I still wouldn't consider using short-tags. No semi-colon, but just the "=" (it's 5am-ish here, I don't see all that well :-P) Quote Link to comment https://forums.phpfreaks.com/topic/121671-problem-with-for-loop-value-retrieval/#findComment-627678 Share on other sites More sharing options...
JasonLewis Posted August 28, 2008 Share Posted August 28, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/121671-problem-with-for-loop-value-retrieval/#findComment-627680 Share on other sites More sharing options...
geroido Posted August 28, 2008 Author Share Posted August 28, 2008 Hi all Thanks, I'm going to test that now. What do you mean by short tags? I havn't heard that before. Quote Link to comment https://forums.phpfreaks.com/topic/121671-problem-with-for-loop-value-retrieval/#findComment-627681 Share on other sites More sharing options...
kratsg Posted August 28, 2008 Share Posted August 28, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/121671-problem-with-for-loop-value-retrieval/#findComment-627683 Share on other sites More sharing options...
JasonLewis Posted August 28, 2008 Share Posted August 28, 2008 And compatibility, as some hosts have short-tags disabled by default. Quote Link to comment https://forums.phpfreaks.com/topic/121671-problem-with-for-loop-value-retrieval/#findComment-627684 Share on other sites More sharing options...
geroido Posted August 28, 2008 Author Share Posted August 28, 2008 Ok that's bye bye to short tags from now on. I tested that code and all works now. Thanks a lot. Quote Link to comment https://forums.phpfreaks.com/topic/121671-problem-with-for-loop-value-retrieval/#findComment-627689 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.