Reputation: 591
i'm sorry for asking this question because i think it has a really simple answer, but i really don't know php at all. I want to install bandwidthd for network monitoring, and i wanted the option which lets you run the bandwidthd daemon on the gateway and all the info is exported to a postgres database on another machine. Also on that machine, the one with the database i've set up an apache server for displaying the webapp of bandwidthd. The web up is a bunch of php files which don't seem to work at all so i'm just spending my time corecting them. I've been able to get a few of the right but now i'm stuck. I have the following code:
<td><SELECT name="sensor_name">
<OPTION value="none">--Select A Sensor--
<?php
$dbh = pg_pconnect("host=localhost dbname=BandwidthD user=postgres");
$sql = "SELECT sensor_name from sensors order by sensor_name;";
$result = pg_query($dbh, $sql);
while ($r = pg_fetch_array($result))
echo "<option value=\"".$r[0]."\" ".($sensor_name==$r[0]?"SELECTED":"").">".$r[0]."\n";
?>
And when the page executes it is saying that sensor_name is undefined, but from what i see, it looks to me that the variable should be initialized there right at that line. If you have any suggestions please let me know but bear in mind that i don't know any php, i'm just venturing guesses because it's a programming language and it's supposed to be logical.
I was thinking that $sensor_name would be initialized in that peace of code because i also have this code:
<td><SELECT name="limit">
<OPTION value="none">--How Many Results--
<OPTION value=20 <?php=$limit==20?"SELECTED":""?>>20
<OPTION value=50 <?php=$limit==50?"SELECTED":""?>>50
<OPTION value=100 <?php=$limit==100?"SELECTED":""?>>100
<OPTION value=all <?php=$limit=="all"?"SELECTED":""?>>All
</select>
and limit is not declared anywhere else and doesn't give an error.
Upvotes: 0
Views: 1194
Reputation: 8669
It seems to me that $sensor_name does not exist. You fetch $results from the database which is an enumeration of database results in which you have an index of 'sensor_name'.
Thus, I think you should replace this line (not tested):
echo "<option value=\"".$r[0]."\" ".($sensor_name==$r[0]?"SELECTED":"").">".$r[0]."\n";
With this
echo "<option value=\"".$r[0]."\" ".($result['sensor_name']==$r[0]?"SELECTED":"").">".$r[0]."\n";
Upvotes: 2
Reputation: 623
The code, $sensor_name == $r[0]
does not assign a value to $sensor_name
, but compares it for equality with $r[0]
.
The code $sensor_name==$r[0]?"SELECTED":""
means that if $sensor_name
is equal to $r[0]
(the sensor_name field in the row), then evaluate the operation to SELECTED
, otherwise evaluate to ''
.
$sensor_name
isn't defined anywhere in your code, which is why you're getting the notice.
Upvotes: 0