I am trying to write a script to display records from an SQL database, but based on three variables which are set by three dropdown boxes on the page which are auto-populated from the database.
This is for a Learning Management System I am working on to provide effective feedback from online learning tests.
The code I am currently working on is below, along with the pseudo code which I hope will explain my requirements. The problem I am having is I cannot get the dropdown boxes to populate based on the criteria I have set in my pseudo code.
Any help is very much appreciated,
Thank you
John
// I used this article for the structure of the following script:
http://forums.devarticles.com/mysql-development-50/drop-down-menu-populated-from-a-mysql-database-1811.html
// Dropdown Box 1 - Choose the course - Show entries from the column "Name" from table "mdl_scorm". Once an option has been selected set variable $coursechoice to the value in the "id" column of the "mdl_scorm" table
// Dropdown Box 2 - Choose the user - Show entries from the columns "firstname" + "lastname" from table "mdl_user" IF the number shown in the "id" column of table "mdl_user" is present in the "userid" column of table "mdl_scorm_scoes_track" AND IF $coursechoice is present in the "scormid" column of table "mdl_scorm_scoes_track". Once an option has been selected set variable $userchoice to the value in the "id" column of table "mdl_user"
// Dropdown Box 3 - Choose the attempt - Show entries from the column "attempt" from table "mdl_scorm_scoes_track" IF $coursechoice is present in the "scormid" column of the table "mdl_scorm_scoes_track" AND IF $userchoice is present in the "userid" column of table "mdl_scorm_scoes_track". Once an option has been selected set variable $attemptchoice to the value in the "attempt" column from table "mdl_scorm_scoes_track"
// Submit button displays the records from table "mdl_scorm_scoes_track" which have a value in the column "scormid" which matches $coursechoice AND have a value in the column "userid" which matches $userchoice AND have a value in the column "attempt" which matches $attemptchoice
$sql="SELECT name FROM mdl_scorm";
$result=mysql_query($sql);
$options="";
while ($row=mysql_fetch_array($result)) {
$id=$row["id"];
$thing=$row["name"];
$options.="<OPTION VALUE=\"$id\">".$thing;
}
?>
<SELECT NAME=course>
<OPTION VALUE=0>Choose the course
<?=$options?>
</SELECT>
<?php
$sql="SELECT username FROM mdl_user";
$result=mysql_query($sql);
$options="";
while ($row=mysql_fetch_array($result)) {
$id=$row["id"];
$thing=$row["name"];
$options.="<OPTION VALUE=\"$id\">".$thing;
}
?>
<SELECT NAME=user>
<OPTION VALUE=0>Choose the user
<?=$options?>
</SELECT>
<?php
$sql="SELECT attempt FROM mdl_scorm_scoes_track";
$result=mysql_query($sql);
$options="";
while ($row=mysql_fetch_array($result)) {
$id=$row["id"];
$thing=$row["name"];
$options.="<OPTION VALUE=\"$id\">".$thing;
}
?>
<SELECT NAME=attempt>
<OPTION VALUE=0>Choose the attempt
<?=$options?>
</SELECT>
<?php
$finalresult = SELECT element, value FROM mdl_scorm_scoes_track WHERE scormid=$coursechoice AND userid=$userchoice AND attempt=$attemptchoice
while ($testrows = mysqli_fetch_array($finalresult)){
echo $testrows['value'];
I think the recommendation below will get your dropdowns working... To actually pull information dynamically based on the dropdowns will require a form, a submit button and some parsing of the $_POST variable.
One step at a time, lets get your dropdowns working.
First, don't forget to close your options with </option> after .$thing:
$options.="<OPTION VALUE=\"$id\">".$thing."</OPTION>";
and
<OPTION VALUE=0>Choose the course</OPTION>
In addition, you are only selecting the name, if you want the id you need to select that too.
$sql="SELECT name, id FROM mdl_scorm";
and finally, to use the results of the query as an associative array you need mysql_fetch_assoc
while ($row=mysql_fetch_assoc($result)) {
That last was was probably what was tripping you up most.
Hope that helps.
Related
So, I created a bunch of drop down options like this:
And I have a table in my database called 'recipes' which will contain meal names and the number of the various ingredients required to make them.
When a user chooses inputs using the drop down boxes and submits it via the POST function, I want to be able to sum the total ingredients required for all the meals they have chosen and echo those values. How can I do this?
My code for the drop down boxes is here:
<label for="dinner7">S</label>
<select name="dinner7" id="mealSelect">
<option name=default id=defaultSelect>None</option>
<?php
$sqli = "SELECT * FROM recipes;";
$result = mysqli_query($connect, $sqli);
while ($row = mysqli_fetch_assoc($result)) {
$mealname = $row['mealname'];
echo "<option>$mealname</option>\n";
}
?>
</select>
Dearest genius coders,
I have a form in which users create a Team from a number of selections. These selections are created from a drop down menu that pulls from a MySQL DB.
<select name="A1">
<?php
$queryA1 = "select p_Num, p_Name from players where p_Class = 'A'";
$resultA1 = mysql_query($queryA1);
while ($lineA1 = mysql_fetch_array($resultA1, MYSQL_ASSOC)) {
?>
<option value="<?php echo $lineA1['p_Num'];?>"> <?php echo $lineA1['p_Name'];?> </option>
<?php } ?>
</select>
This displays as shown. The player name (p_Name) in the list, but the INSERT takes the player number (p_Num).
From there, I have an insert created that takes the players and puts them in a Team database, but uses the p_Num (player number / key) to reference in the Player database.
I'm not having issues with the insert, what I need is to display the results after submission to the user, but I want to display the player name (p_Name) not the player number (p_Num).
How would I go about this?
-Nick
I have a simple drop down menu.
<form method="post" action="index.php">
<select name="mountname">
<option value="white">white</option>
<option value="black">black</option>
<option value="yellow">yellow</option>
<option value="green">green</option>
</select>
<input type="submit" value="Submit Pick" />
to save what is selected I used.
if (!empty($_POST['color'])){
$id = $_SESSION['user_id'];
$color = $_POST['color'];
mysqli_query($mysqli,"UPDATE users SET home_color='".$color."' WHERE id='".$id."'")or die("error == ----> ".mysqli_error());
mysqli_close($mysqli);
header('Location: index.php');
}
saving the color to mysql is no problem.
//update//
the USER table is set up like this.
ID, username,password, first_name, last_name, email,home_color
When a user selects his home color, and then submits it it is saved to the db.
ie 1, Bob, MD5pass, Bob,Smith,Bob#bob.com, Black
2, Joe, MD5pass, Joe,Doe,joe#Doe.com, Green
now i have another table called mount.
mount has color info in it.
this table hold color name, and info.
ID, color_name, color_info
the ID is is an INT with A_I.
Bob Logs in and selects his home color saves it to his profile.
so now when a person goes to bobs profile the will see color info.
how do I make it where it reads profile info and displays info from another table.
something like the code below. I know the code is wrong, but only way i can explain it.
if (black){
mysqli_query($mysqli,"SELECT * FROM mount;
}else{
(green)
As you said, color is saved in user's table. Problem is how to fetch color's data from mount along with user's data..
On Profile Page, you can get details of user with color by ..
$id = $_SESSION['user_id'];
$res = mysqli_query($mysqli,"SELECT * FROM users u, mount c WHERE u.home_color=c.color_name AND u.id='".$id."'")or die("error == ----> ".mysqli_error());
$result = mysqli_fetch_array($res);
mysqli_close($mysqli);
Now, you can manipulate $result as it contains users detail as well as color detail! We are getting data from both tables users and mount by same keys comparision home_color of users and color of mount.
If you saved colors in mount like 'Black', I'm suggesting you to keep same keyword in <select> dropdown, as 'Black' is not equal to 'black'.
Another thing I want to suggest you is, Change your select dropdown to dynamic. Fetch color data from table mount and use it like..
<select name="mountname">
<?php
$res_colors = mysqli_query($mysqli, "SELECT * FROM mount");
$colors = mysqli_fetch_array($res_colors);
foreach($colors as $color){
?>
<option value="<?php echo $color['color']; ?>"><?php echo $color['color']; ?></option>
<?php } ?>
</select>
Best way to use primary key as foreign key, so use ID of mount instead of color for dropdown and saving it to user's table.
My database contains list of countries with ccode and country name. This code works here except that the list of countries are not displayed in dropdown list. but when i blindly select a random selection from drop down list, it is inserted into db.
Country
Select Country
<!-- PHP code to retreive drop down list from database countries -->
<?php
include('connection.php');
$sql = "SELECT country FROM countries ORDER BY ccode ASC";
$result = mysql_query($sql);
while($row = mysql_fetch_row($result))
{
echo '<option value="'.$row['country'].'">'.'</option>';
}
?>
</select>
Include the value inside the <option> tag.
echo '<option value="'.$row['country'].'">'.$row['country'].'</option>';
The value attribute is for specifying the value which is sent with the form, usually an id. You have to add the country names between the tags to display them.
echo '<option value="'.$row['country'].'">'.$row['country'].'</option>';
But when the two are the same you don't need to specify value:
echo '<option>'.$row['country'].'</option>';
I have some trouble with this.
I have one database with the following tables:
Countries -> All countries of the world are added
Cities -> The cities are also added
user_profile -> The profile of the user with the fields "country" & "city".
Everything works fine, even the populating of the boxes is working. But I don't know how to get the SELECTED value of the user for both country & city.
I have the following code:
Select Country: <br />
<select id="countries">
<option value="">--</option>
<?php
$sql = "SELECT country, title FROM countries ORDER BY title ASC";
$rs = mysql_query($sql);
while($row = mysql_fetch_array($rs))
{
echo "<option value=\"".$row['country']."\">".$row['title']."\n ";
}
?>
</select>
Select City: <br />
<select id="cities">
<option value="">Select one</option>
<?php
$sql = "SELECT country, title FROM cities ".
"ORDER BY title ASC";
$rs = mysql_query($sql);
while($row = mysql_fetch_array($rs))
{
echo "<option class=\"".$row['country']."\" value=\"".$row['title']."\">".$row['title']."\n ";
}
?>
</select>
Now I need to check in the table user_profile which country & city he chose and show it to him.
I also use jchained (jquery plugin) to make both select boxes working together.
How can I achieve this? If I need to post anything else, please let me know.
Thanks in advance.
You have a lot of missing code here but I will try to help.
I am going to start by assuming that you have already stored the users selections in the user_profile table and that they are foreign keys to to correct relations.
To get the selected id's for repopulating the select boxes with the selected flag use:
SELECT country, city FROM user_profile where id = $user_id;
To get the text values you would do something like:
SELECT country.title as country, city.title FROM user_profile LEFT JOIN countries ON user_profile.country = country.id LEFT JOIN cities ON user_profile.city = cities.id WHERE user_profile.id = $user_id;
If you are not storing them as foreign key relations but instead by string title in the user_profile table you will need to do this:
SELECT country.title as country, city.title FROM user_profile LEFT JOIN countries ON user_profile.country = country.title LEFT JOIN cities ON user_profile.city = cities.title WHERE user_profile.id = $user_id;
You can then use these results to either display to the user or set the "SELECTED" flag in the options box.
Do you just mean you don't know what to do after they select the values? If so then you would just use the form to post the values to the new page. On the receiving page you can run a query to Update the user's profile.
Sorry if my response isn't detailed enough ><
edit Oh, I may see what you mean now. You're able to store it correctly but you want the correct option to be flagged as selected if it's what the user has previously set? If that's it then you can query for the value, then inside of the while loop just add a check to see if $user_country == $row['country'] and if so echo ' SELECTED '. Hope that helps!