Page 1 of 1
<select> boxes !!!
Posted: Wed Mar 24, 2004 11:09 pm
by Trippin
Hi
l was digging everywhere but can't find usefull and simple code that shows me how to populate select box via php and Mysql DB. Basically I am making a small upload console for users so they have a select box that pulls out of the database the list of the folders user has created on the server .... there he picks which pics to upload to that folder and submits ... simple as that .. so first how it works once user logs in (the default upload places files in the root of their personal directory but.. I want to get user to use that select box to view all subfolders inside their own root and user can then upload images to each one depending what selection in box is made instead of uploading to their root. At the moment I have a DB table called 'folders' and php needs to pull that data matching all folders under currently logged user ID and put them in a select box. Hope someone can help because I know how it should work in theory but I can't get the code right.
Cheers
Posted: Thu Mar 25, 2004 2:11 pm
by Farrier
I've no idea if this will help you, or weird you out, but here's my library function for creating select boxes.
Code: Select all
# Display a <select> tag.
# If more than one value is provided in fieldlist, it will be split
# on the commas, remove any that match the id field, then remove anything
# in each field before the last space, to get any aliases.
# That means this fn will NOT work with fieldnames that have spaces in.
# It also means that if you want the id field to appear in your results, give it an alias.
function print_html_select($name, $fieldlist=null, $order=null, $selected=null, $db=null, $table=null, $desc=null, $idfield=null, $select_params=null) {
global $PARAM_ERROR, $HTTP_POST_VARS, $DB_NAME_DEFAULT;
if ($fieldlist === null) { $fieldlist = 'id'; }
if ($desc === null) { $desc = $name; }
if ($table === null) { $table = $name; }
if ($db === null) { $db = $DB_NAME_DEFAULT; }
if ($idfield === null) { $idfield = 'id'; }
if ($order === null) { $order = $fieldlist; }
print(" <SELECT NAME="$name" $select_params>\n");
$fields = preg_split('/,\s*/', $order);
$fields = preg_replace('/\w+\s+/', '', $fields); # Leave only the aliases.
if ($idindex=array_search($idfield, $fields)) {
unset($fields[$idindex]);
}
$query="SELECT $fieldlist FROM $db.$table ORDER BY $order";
$results=sql_get_results($query, "$desc");
foreach ($results as $result) {
if (!isset($result[$idfield])) { continue; }
if ($result[$idfield] == $selected) {
print (' <OPTION SELECTED VALUE="'.$result[$idfield].'">');
}
else {
print (' <OPTION VALUE="'.$result[$idfield].'">');
}
foreach ($fields as $field) {
if (isset($result[$field])) {print($result[$field].' ');}
}
print (" </OPTION>\n");
}
print (" </SELECT>\n");
#print("[$query]<br>\n");
#print_r($results);
}
Posted: Thu Mar 25, 2004 3:15 pm
by penguinboy
Just typed, probably a parse error or two.
Code: Select all
<?php
$options = '';
$query= mysql_query("SELECT `id`,`name` FROM `folders` WHERE `userid`='\*curent userid*''";
while($results=mysql_fetch_array($query))
{
$options .= '<option value="'.$results['id'].'">'.$results['name'].'</option>';
}
?>
<select name="folders">
<?=$options;?>
</select>
umm problem
Posted: Thu Mar 25, 2004 7:23 pm
by Trippin
hi
I always have warning with this line
-->>> while($results=mysql_fetch_array($query))
I aleady had similar code but it always gives
$query= mysql_query("SELECT foldername FROM folders WHERE userid ='$user_name'");
-->> Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in
I can't get past this problem and all similar examples i tried do the same thing they are full of warnings ... please help
another version
Posted: Thu Mar 25, 2004 9:31 pm
by Trippin
This is another version of code that I can't make to work , no warnings in this one but my select box i just simply empty =(
<?php
echo" <select name=\"category\"> "; //start the select box
$results= mysql_query("SELECT userid,foldername FROM folder WHERE userid='$user_name'"); //user_name is global variable of user loged in
$userid = "userid";
$foldername = "foldername";
echo mysql_error();
if (mysql_Numrows($results)>0) //if there are records in the fields
{
$numrows=mysql_NumRows($results); //count them
$x=0;
while ($x<$numrows){ //loop through the records
$theId=mysql_result($results,$x,$userid); //place each record in the variable everytime we loop
$theName=mysql_result($results,$x,$foldername);
echo "<option value=\"$theId\">$theName</option>\n"; //and place it in the select
$x++;
}
}
echo "</select>"; //close the select
?>
Posted: Thu Mar 25, 2004 10:07 pm
by McGruff
It's best not mix up models (where you define unformatted vars) and views (where you apply formatting to bare vars and print the page).
So, if your script was rewritten to define an array of options with keys for submitted and displayed values:
Code: Select all
<?php
$options = array();
$sql = "SELECT userid, foldername FROM folder WHERE userid='$user_name'";
$query = mysql_query($sql) or die(mysql_error());
if (mysql_num_rows($query) > 0)
{
$i = 0;
while($result = mysql_fetch_assoc($query))
{
$options[$i]['submitted'] = $result['userid']; // replace with col name
$options[$i]['displayed'] = $result['foldername']; // replace with col name
$i++;
}
}
?>
That's the model bit. In the view, loop through the array and print options.
its ok
Posted: Thu Mar 25, 2004 10:23 pm
by Trippin
It's ok I worked it out it was more like this =)))
<?php
echo" <select name=\"category\"> "; //start the select box
$results= mysql_query("SELECT folder.foldername FROM folder, client WHERE client.id=folder.userid AND client.userid='$user_name'");
echo mysql_error();
while (list($theName)=mysql_fetch_row($results))
{ //loop through the records
echo "<option value=\"$user_name\">$theName</option>"; //and place it in the select
}
echo "</select>"; //close the select
?>
done