0

I have been trying to decipher the option part of this: echo "<option value='" . $row['PcID'] . "'>" . $row['PcID'] . "</option>"; taken from Populate a Drop down box.....

can someone kindly explain what the periods do? and why when i do this: print "<option value='".$wrow['week_num']."'>".$wrow['week_name']."</option><br>\n"; I only get only the week name in the list.

$wquery="select week_num,week_name from stats_week";
$wresult=mysql_query($wquery);
print "Select Week:<select name=Week_select><br>\n";
while ($wrow=mysql_fetch_assoc($wresult)){
    print "<option value='".$wrow['week_num']."'>".$wrow['week_name']."</option><br>\n";    
}
print "</select>";
Community
  • 1
  • 1
Bill Flippen
  • 453
  • 1
  • 4
  • 19

3 Answers3

1

In PHP, the period is the concatentation operator. Putting the periods in tells PHP to concatenate strings, See this page:

http://www.php.net/manual/en/language.operators.string.php

TheEwook
  • 11,037
  • 6
  • 36
  • 55
1

The periods "break up" the string, and allow you to run PHP code (mainly variables/ternary operators/functions) in it's place, between them.

It's also a method of joining strings. This is called Concatenation.

See String Operators


Also, you only see the week name in the list because that's the only output you've got between your <option></option> tags.

Does this help,

$wquery="select week_num,week_name from stats_week";
$wresult=mysql_query($wquery);
print "Select Week:<select name=Week_select>";
while ($wrow=mysql_fetch_assoc($wresult)){
    print "<option value='".$wrow['week_num']."'>".$wrow['week_num']." - ".$wrow['week_name']."</option>\n";    
}
print "</select>";

This will display the week number and name in the list. I've also removed the <br /> tags after your select/options.

Please familiarise yourself with HTML Selects/Options.

Adrian
  • 1,046
  • 7
  • 12
  • Thanks, couldn't use & could they? I finally got it to work with print '
    \n';
    – Bill Flippen Mar 30 '13 at 18:05
  • olso i have gone through the w3 site (and others) but couldn't see how to set up multiple names and the the periods were throwing me. Now it is almost clear as mud :) Thanks everyone – Bill Flippen Mar 30 '13 at 18:18
-1

Replace with;

print "<option value='{$wrow['week_num']}'>{$wrow['week_name']}</option><br>\n";  

This will print the correct values and not return the php in html

You will also want to look at concatenation on the PHP documentation

Harry Beasant
  • 1,014
  • 8
  • 18
  • So can you explain the use of the { does/can it replace a double/single quote? I may want to start using them in the future as the quotes are driving me nuts :) – Bill Flippen Mar 30 '13 at 18:14