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>";
4

3 回答 3

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

于 2013-03-30T17:19:39.313 回答
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.

于 2013-03-30T17:19:56.147 回答
-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

于 2013-03-30T17:20:12.550 回答