I'm trying to write a script to create a "select" menu based on a database. I have the script working, but I think I'm doing things the hard way. The database if very simple, it only has 3 rows and 2 columns:
CREATE TABLE ownersPriv (
privIndex int unsigned not null auto_increment primary key,
privLevel varchar(15)
);
When loaded it looks like this:
<table border="1"><tr><td>1</td><td>Administrator</td></tr>
<tr><td>2</td><td>Member</td></tr>
<tr><td>3</td><td>Debug</td></tr></table></html>
I want to read the different "privLevels" and create a drop-down menu with eash one listed. The $val variable is initialized earlier in the code and holds the value of the "privIndex", and I use that to determine which option is "selected="selected". Here is the main code:
$sql2 = "SELECT * from ownersPriv";
$result2 = mysql_query($sql2,$conn);
$numChoices = mysql_num_rows($result2);
$i=0;
$j=0;
while($row2=mysql_fetch_assoc($result2)) {
foreach($row2 as $index=>$value) {
$temp2dArray[$i][$j]=$value;
$j++;
}
$i++;$j=0;
}
print "<br><br><td><select name='privledges' selected='1'>";
for($i=0;$i<$numChoices;$i++) {
print "val:$val temp2dArray:".$temp2dArray[$i][0]."<br>";
if($val==$temp2dArray[$i][0]) {
print "setting selected<br>";
$selected="selected='selected'";
}
else {
$selected=NULL;
}
print "<option value=".$temp2dArray[$i][0]." ".$selected." >".$temp2dArray[$i][1]."</option><br>";
}
print "</select></td>";;
I'm trying to figure out a way to accomplish the same thing without using a temporary array. I think my problem is I don't now how to directly access the data members of "$row2" once I call mysql_get_assoc. Is there a way to reference these fields without going through the foreach loop and re-assigning the values?There's nothing wrong with a temporary array, just unset() it at the end. But if you want to avoid it altogether, try this:
$sql2 = "SELECT * from ownersPriv";
$result2 = mysql_query($sql2,$conn);
$numChoices = mysql_num_rows($result2);
$row2=mysql_fetch_assoc($result2);
print "<br><br><td><select name='priveliges' selected='1'>";
for($i=0;$i<$numChoices;$i++) {
print "val:$val row2:".$row2['privIndex']."<br>"; //I'm guessing this is a debug line...
if($val==$row2['privIndex']) {
print "setting selected<br>";
$selected="selected='selected'";
}
else {
$selected=NULL;
}
print "<option value=".$row2['privIndex]." ".$selected." >".$row2['privLevel']."</option><br>";
}
print "</select></td>";Ok so you access the 'privIndex' field by using $row2['privIndex']. But how do you go through eash privIndex in the table? The loop you have it inside of uses $i as a counter, but never uses it. Won't $row2['privIndex'] point to the same index each time, the first one?Yeah, it will But if you put mysql_fetch_assoc() inside the for loop, everything should work.
$sql2 = "SELECT * from ownersPriv";
$result2 = mysql_query($sql2,$conn);
$numChoices = mysql_num_rows($result2);
print "<br><br><td><select name='priveliges' selected='1'>";
for($i=0;$i<$numChoices;$i++) {
$row2=mysql_fetch_assoc($result2);
print "val:$val row2:".$row2['privIndex']."<br>"; //I'm guessing this is a debug line...
if($val==$row2['privIndex']) {
print "setting selected<br>";
$selected="selected='selected'";
}
else {
$selected=NULL;
}
print "<option value=".$row2['privIndex]." ".$selected." >".$row2['privLevel']."</option><br>";
}
print "</select></td>";Ok I gotcha now. That is what I was looking for, much cleaner. Thanks!loose the for loop altogether. and you have some other things that are wrong.
$sql2 = "SELECT * from ownersPriv";
$result2 = mysql_query($sql2,$conn);
$numChoices = mysql_num_rows($result2);
print "<br><br><td><select name='priveliges'>";
while($row2=mysql_fetch_assoc($result2)){
print "val:$val row2:".$row2['privIndex']."<br>"; //I'm guessing this is a debug line...
if ($val==$row2['privIndex']) {
print "setting selected<br>";
$selected="selected='selected'";
} else {
$selected='';
}
print "<option value=".$row2['privIndex]." ".$selected." >".$row2['privLevel']."</option><br>";
}
print "</select></td>";
CREATE TABLE ownersPriv (
privIndex int unsigned not null auto_increment primary key,
privLevel varchar(15)
);
When loaded it looks like this:
<table border="1"><tr><td>1</td><td>Administrator</td></tr>
<tr><td>2</td><td>Member</td></tr>
<tr><td>3</td><td>Debug</td></tr></table></html>
I want to read the different "privLevels" and create a drop-down menu with eash one listed. The $val variable is initialized earlier in the code and holds the value of the "privIndex", and I use that to determine which option is "selected="selected". Here is the main code:
$sql2 = "SELECT * from ownersPriv";
$result2 = mysql_query($sql2,$conn);
$numChoices = mysql_num_rows($result2);
$i=0;
$j=0;
while($row2=mysql_fetch_assoc($result2)) {
foreach($row2 as $index=>$value) {
$temp2dArray[$i][$j]=$value;
$j++;
}
$i++;$j=0;
}
print "<br><br><td><select name='privledges' selected='1'>";
for($i=0;$i<$numChoices;$i++) {
print "val:$val temp2dArray:".$temp2dArray[$i][0]."<br>";
if($val==$temp2dArray[$i][0]) {
print "setting selected<br>";
$selected="selected='selected'";
}
else {
$selected=NULL;
}
print "<option value=".$temp2dArray[$i][0]." ".$selected." >".$temp2dArray[$i][1]."</option><br>";
}
print "</select></td>";;
I'm trying to figure out a way to accomplish the same thing without using a temporary array. I think my problem is I don't now how to directly access the data members of "$row2" once I call mysql_get_assoc. Is there a way to reference these fields without going through the foreach loop and re-assigning the values?There's nothing wrong with a temporary array, just unset() it at the end. But if you want to avoid it altogether, try this:
$sql2 = "SELECT * from ownersPriv";
$result2 = mysql_query($sql2,$conn);
$numChoices = mysql_num_rows($result2);
$row2=mysql_fetch_assoc($result2);
print "<br><br><td><select name='priveliges' selected='1'>";
for($i=0;$i<$numChoices;$i++) {
print "val:$val row2:".$row2['privIndex']."<br>"; //I'm guessing this is a debug line...
if($val==$row2['privIndex']) {
print "setting selected<br>";
$selected="selected='selected'";
}
else {
$selected=NULL;
}
print "<option value=".$row2['privIndex]." ".$selected." >".$row2['privLevel']."</option><br>";
}
print "</select></td>";Ok so you access the 'privIndex' field by using $row2['privIndex']. But how do you go through eash privIndex in the table? The loop you have it inside of uses $i as a counter, but never uses it. Won't $row2['privIndex'] point to the same index each time, the first one?Yeah, it will But if you put mysql_fetch_assoc() inside the for loop, everything should work.
$sql2 = "SELECT * from ownersPriv";
$result2 = mysql_query($sql2,$conn);
$numChoices = mysql_num_rows($result2);
print "<br><br><td><select name='priveliges' selected='1'>";
for($i=0;$i<$numChoices;$i++) {
$row2=mysql_fetch_assoc($result2);
print "val:$val row2:".$row2['privIndex']."<br>"; //I'm guessing this is a debug line...
if($val==$row2['privIndex']) {
print "setting selected<br>";
$selected="selected='selected'";
}
else {
$selected=NULL;
}
print "<option value=".$row2['privIndex]." ".$selected." >".$row2['privLevel']."</option><br>";
}
print "</select></td>";Ok I gotcha now. That is what I was looking for, much cleaner. Thanks!loose the for loop altogether. and you have some other things that are wrong.
$sql2 = "SELECT * from ownersPriv";
$result2 = mysql_query($sql2,$conn);
$numChoices = mysql_num_rows($result2);
print "<br><br><td><select name='priveliges'>";
while($row2=mysql_fetch_assoc($result2)){
print "val:$val row2:".$row2['privIndex']."<br>"; //I'm guessing this is a debug line...
if ($val==$row2['privIndex']) {
print "setting selected<br>";
$selected="selected='selected'";
} else {
$selected='';
}
print "<option value=".$row2['privIndex]." ".$selected." >".$row2['privLevel']."</option><br>";
}
print "</select></td>";