Forum Moderators: coopster
I tried many means to feed back the info into the select fields
and "rebuilt" the drop down box to display the selected option first
like if $cook_rating_post == $intermediate echo intermediate as first option.
But it either breaks the drop down box or even has no effect
I am clue less, what can be done?
I have seen here many posts with the same object
If this can work I will post the whole thing with comments for everyone benefits.
<<< // select/update
<?
print query_select("beginner, intermediate
from users where username = '$username' and id = '$new_content' LIMIT 1");
?>
>>>
the fist script calls the following function()
<<<
function query_select()
{ $conn = db_connect();
$sql= "select beginner, intermediate from users LIMIT 1";
$result = mysql_query($sql,$conn);
echo "<select name=\"cook_rating_post\">";
while(list($beginner, $intermediate)=mysql_fetch_array($result)){
$cook_rating_post = stripslashes($cook_rating_post);
echo "<option value=$beginner>$beginner </option><br>";
echo "<option value=$intermediate>$intermediate </option><br>";
}
}
?>
I need to query the DB get the value from cook_rating_post compare with every other options and then dynamically rebuilt the options upon what is selected.
Therefore the selected one will top the option range
I tried your suggestion but either I got a blank or I got duplicate option
I cannot figure how to get that value
Even tried to get it by a secondary query as follow
<<<
function query_select()
{ $conn = db_connect();
$sql= "select cook_rating_post from users LIMIT 1";
$result = mysql_query($sql,$conn);
while ($qry = mysql_fetch_array($result)) {
$selected=$qry[cook_rating_post];
}
$sql= "select beginner, intermediate from users LIMIT 1";
$result = mysql_query($sql,$conn);
echo "<select name=\"cook_rating_post\">";
echo "<option selected=$selected>$selected</option><br>";
while(list($beginner, $intermediate)=mysql_fetch_array($result)){
$cook_rating_post = stripslashes($cook_rating_post);
echo "<option value=$beginner>$beginner </option><br>";
echo "<option value=$intermediate>$intermediate </option><br>";
}
}
<Edit> del useless script</edit>
if ($cook_rating_post == $beginner) {$selected = " selected";} else {$selected = "";}
echo "<option value=\"$beginner\"$selected>$beginner </option>";
if ($cook_rating_post == $intermediate) {$selected = " selected";} else {$selected = "";}
echo "<option value=\"$intermediate\"$selected>$intermediate </option>";
Also remember that values you need inside a function, that are not set within the function needs to be set as function($need_this_variable) or as global.
Dont know if this is the case though.
Populate a Select Tag from MySQL with options [webmasterworld.com]
I made it working and am preparing to post the whole thing with comments so that it can be used again.
I checked many posts and quest similar to mine comes often.
My version uses a class which allows for loading many selects on one form in one shot and the function can be extended if needed.
I did not use my previous idea to work around cases or if and ==
I used the select option as suggested by hackre.
Will be back soon :)
Drop down box scripts.
Relies on DB and functions.
Using the function allows to add more drop down boxes if needed
by transforming the () in a class and allowing to extend the class if needed (only optional).
The form: As is offers to select a qualification to receive a tutorial based on the chosen option
The form relies on the three scripts served by: "include_fns.php"
Here is the include_fns:
<<<
<?
include_once("db_fns.php"); //the DB conn script- and two complementary functions
include_once("user_auth_fns.php"); //an authentication function (you may use your own)
include_once("select_fns.php"); //the drop down box function
?>
>>>
<<<
Here is the db_fns_function:
<?
function db_connect()
{
$result = @mysql_pconnect("HOST", "USER_NAME", "PWPWPW");
if (!isset($result) && empty($result))
{echo "can't connect!"; }
if (!@mysql_select_db("DB_NAME"))
return false;
return $result;
}
function get_writer_record($username)
{
$conn = db_connect();
$sql = "select * from A TABLE where username = '$username'";
$result = mysql_query($sql, $conn);
return(mysql_fetch_array($result));
}
$id=$_POST['id'];
$new_content=$id;
function get_new_content_record($new_content)
{
$conn = db_connect();
$sql = "select * from A TABLE where id = '$new_content'";
$result = mysql_query($sql, $conn);
return(mysql_fetch_array($result));
}
?>
>>>
Here is the select_fns function
<<<
<?php
function query_select()
{ $conn = db_connect();
// it first looks for the default DB preloaded options to build the drop box
// then it looks for the cook_rating_post value that is the result of the selection loaded in a col named cook_rating_post
$sql= "select beginner, intermediate, experienced, very_good, chef from A TABLE LIMIT 1";
$result = mysql_query($sql,$conn);
echo "<select name=\"cook_rating_post\">";
while(list($beginner, $intermediate, $experienced, $very_good, $chef)=mysql_fetch_array($result)){
$cook_rating_post = stripslashes($cook_rating_post);
$conn = db_connect();
$username=$_SESSION['username'];
$sql= "select cook_rating_post from A TABLE where username = '$username' ";
$result = mysql_query($sql,$conn);
while ($qry = mysql_fetch_array($result)) {
$cook_rating_post=$qry[cook_rating_post];
}
echo "<option selected=$cook_rating_post>$cook_rating_post</option><br>";
echo "<option value=$beginner>$beginner </option><br>";
echo "<option value=$intermediate>$intermediate </option><br>";
echo "<option value=$experienced>$experienced </option><br>";
echo "<option value=$very_good>$very_good </option><br>";
echo "<option value=$chef>$chef</option>";
}
}
?>
>>>
AND FINALLY THE FORM
<<<
session_start();
//error_reporting (E_ALL);
include "include_fns.php";
// includes the function: select_fns.php
//
$conn = db_connect();
$username=$_SESSION['username'];
$s = get_new_content_record($new_content);
?>
<tr>
<td align="left" valign="top">
<div class="form_subject"><b>Cooking Knowledge<br>
Which cook are you:</b><br>
<b>Select </b> </div>
<?
print query_select("beginner, intermediate, experienced, very_good, chef
from users where username = '$username' and id = '$new_content' LIMIT 1");
?>
</td></tr>
<td align="left" valign="top">
// to be on the safe side I echo again the chosen option
<b> You Selected --></b><!-- ## show selected value ## --><input type="text" size="10" name="" value="<? echo $s[cook_rating_post];?>">
//if you wonder about $s
//go back to the top of the form and look for:
// $s = get_new_content_record($new_content);
// then look for db_fns.php and review its last function
<br></td></tr>
<input type=SUBMIT VALUE="submit">
</form>