so as the title states, using the following code I have got it populating the dropbox with a single result from the query, that result being the latest added in the table.
here is my code:
<?php
$query = "SELECT * FROM units_tb WHERE user_id='$userid'";
$result = mysql_query($query) or die (mysql_error());
while($row = mysql_fetch_assoc($result)){
$aa = "<option value='{$row['unit_id']}'>{$row['unit_code']}</option>";
}
?>
<select name="t_unit"><? echo $aa; ?></select>
The odd thing is, I use this same code for another field, and it works, populating the dropdown with all the results, however in this case it only fills in the last unit code in the table and not all of which are attached to the particular user id.
I would appreciate anyones thoughts :D
thanks
$aa .= "<option value='{$row['unit_id']}'>{$row['unit_code']}</option>";
add . before = and initiate $aa = ''; before while loop
<?php
$query = "SELECT * FROM units_tb WHERE user_id='$userid'";
$result = mysql_query($query) or die (mysql_error());
$options = "";
while($row = mysql_fetch_assoc($result)){
$options .= "<option value='{$row['unit_id']}'>{$row['unit_code']}</option>";
}
?>
<select name="t_unit"><? echo $options; ?></select>
should work. You forgot a . in your while loop
Related
I'm a complete newbie with PHP/html stuff so please bear with me.
I've been trying to populate a select box using data from a myslq database and I can't get it to work, all I have is just a blank textBox.
This is what I have for now:
<select name="cargo">
<?php
require("conectadb.php");
$ok = conecta_db() or die ("Failure");
$sql = mysqli_query($ok, "SELECT descCargo FROM tbcargo");
while ($row = mysqli_fetch_array($sql)){
$c = $row['descCargo'];
echo("<option value=\"$c\">$c</option>");
}
?>
</select>
This is my database structure for now, with all the relevant rows:
Database name: tbcargo
PkCodCargo (primary key, AUTO_INCREMENT)
descCargo (what I want to fill the dropdown with)
I've tried everything I could think of to no avail, unfortunately.
Can someone help to point out exactly what am I doing wrong here?
Thanks in advance!
Update the part of your code to:
<select name= 'cargo'>
<?php
require("conectadb.php");
$ok = conecta_db() or die ("Failure");
$sql = mysqli_query($ok, "SELECT descCargo FROM tbcargo");
while ($rows = $sql->fetch_assoc())
{
echo '<option value="'.$rows['descCargo'].'">'.$rows['descCargo'].'</option>';
}
?>
</select>
I've finally found out the issue.
My file was named .html instead of .php and that's why nothing ever worked.
Hope this is able to help another newbie like myself out there
Cheers!
Here is the solution.
# here database details
mysql_connect('hostname', 'username', 'password');
mysql_select_db('database-name');
$sql = "SELECT descCargo FROM tbcargo";
$result = mysql_query($sql);
echo "<select name='cargo'>";
while ($row = mysql_fetch_array($result)) {
echo '<option value="'. $row['descCargo'] .'">'.$row['descCargo'] .'</option>';
}
echo "</select>";
I have a HTML etc.. tags now what I want to achieve is upon a selection of ie. i want to load the related info from database to in a new tag with as many tags.
I am using PHP to do achieve this now at this point if for example i choose option1 then the query behind it retrieves relevant information and stores it in a array, and if I select option2 exactly the same is done.
The next step I made is to create a loop to display the results from array() but I am struggling to come up with the right solution to echo retrieved data into etc. As its not my strongest side.
Hope you understand what I am trying to achieve the below code will clear thing out.
HTML:
<select id="workshop" name="workshop" onchange="return test();">
<option value="">Please select a Workshop</option>
<option value="Forex">Forex</option>
<option value="BinaryOptions">Binary Options</option>
</select>
PHP:
$form = Array();
if(isset($_POST['workshop'])){
$form['workshop'] = $_POST['workshop'];
$form['forex'] = $_POST['Forex'];
$form['binary'] = $_POST['Binary'];
//Retrieve Binary Workshops
if($form['workshop'] == 'Forex'){
$sql2 = "SELECT id, course, location FROM courses WHERE course LIKE '%Forex%' OR course LIKE '&forex%'";
$query2 = mysqli_query($link, $sql2);
while($result2 = mysqli_fetch_assoc($query2)){
//The problem I am having is here :/
echo "<select id='Forex' name='Forex' style='display: none'>";
echo "<option value='oko'>.$result[1].</option>";
echo "</select>";
print_r($result2);echo '</br>';
}
}else{
$sql = "SELECT id, course, location FROM courses WHERE course LIKE '%Binary%' OR course LIKE '%binary%'";
$query = mysqli_query($link, $sql);
while($result = mysqli_fetch_assoc($query)){
print_r($result);echo '</br>';
}
}
}
Try this code:
$query2 = mysqli_query($link, $sql2);
echo "<select id='Forex' name='Forex' style='display: none'>";
while($result2 = mysqli_fetch_assoc($query2)){
echo "<option value='oko'>{$result['course']}</option>";
}
echo "</select>";
echo '</br>';
From the top in your php:
// not seeing uses of the $form I removed it from my answer
if(isset($_POST['workshop'])){
$workshop = $_POST['workshop'];
$lowerWorkshop = strtolower($workshop);
// neither of $_POST['Forex'] nor $_POST['Binary'] are defined in your POST. you could as well remove those lines?
//Retrieve Binary Workshops HERE we can define the sql in a single line:
$sql = "SELECT id, course, location FROM courses WHERE course LIKE '%$workshop%' OR course LIKE '&$lowerWorkhop%'";
$query = mysqli_query($link, $sql); // here no need to have two results
// Here lets build our select first, we'll echo it later.
$select = '<select id="$workshop" name="$workshop" style="display: none">';
while($result = mysqli_fetch_assoc($query)){
$select.= '<option value="' . $result['id'] . '">' . $result['course'] . '</option>';
// here I replaced the outer containing quotes around the html by single quotes.
// since you use _fetch_assoc the resulting array will have stroing keys.
// to retrieve them, you have to use quotes around the key, hence the change
}
$select.= '</select>';
}
echo $vSelect;
this will output a select containing one option for each row returned by either of the queries. by the way this particular exemple won't echo anything on screen (since your select display's set to none). but see the source code to retrieve the exemple.
I'm attempting to populate an HTML dropdown menu with the results of a MySQL query. The query is running fine and not throwing any errors, but for some odd reason the results won't display correctly.
<?php
$query = "SELECT * FROM parts WHERE itemName LIKE 'Processors:%'";
$result = mysql_query($query) or die("Unable to query CPU parts");
while($row=mysql_fetch_array($result)) {
$option .= "<option value='{$row['itemName']}'></option>";
}
?>
<select name="cpu"><? echo $option; ?></select>
I'm pretty sure it lies somewhere in the $option .= ... ; but it I can't seem to figure it out.
You are not printing the value to be shown in dropdown
try this
while($row=mysql_fetch_array($result)) {
$option .= "<option value='{$row['itemName']}'>{$row['itemName']}</option>";
}
I saw that you are using short tags here
<select name="cpu"><? echo $option; ?></select>
please make sure you have short tag enabled in php.ini
Two things:
You need to initialize $option and print the value to be shown in the dropdown
<?php
$query = "SELECT * FROM parts WHERE itemName LIKE 'Processors:%'";
$result = mysql_query($query) or die("Unable to query CPU parts");
$option = "";
while($row=mysql_fetch_array($result)) {
$option .= "<option value='{$row['itemName']}'>{$row['itemName']}</option>";
}
?>
<select name="cpu"><? echo $option; ?></select>
Seems you forget to add a value inside <option></option> field you only set value for fetching $_POST[] but not for viewing. :D
Also seeing $option variable to be concatenated seems that you already declared it at first
if not you need to probably declared it first.cause this may result to error undefined variable.
<?php
$query = "SELECT * FROM parts WHERE itemName LIKE 'Processors:%'";
$result = mysql_query($query) or die("Unable to query CPU parts");
$option = '';
while($row=mysql_fetch_array($result)) {
$option .= '<option value='.$row['itemName'].'>'. $row['itemName'].'</option>';
}
?>
<select name="cpu"><?php echo $option; ?></select>
Forgot the simplest part:
$aa .= "{$row['itemName']}";
I needed to add the actual label to be displayed between the option tags :)
I am trying to get the names of the events from a table in a database, i need to insert this data in a dropdown list, so then someone clicks them and then the information for this specific event displays...
Here is my code up to now..
<?php
require "config.php"; // Your Database details
?>
<?PHP
$SQL = "SELECT title_en_US FROM civicrm_event";
$result = mysql_query($SQL);
// Write out our query.
$query = "SELECT title_en_US FROM civicrm_event";
// Execute it, or return the error message if there's a problem.
$result = mysql_query($SQL);
while ($db_field = mysql_fetch_assoc($result)) {
print $db_field['title_en_US'] . "<BR>";
}
?>
Can you tell me how to put first the event names into a dropdown list?
Thanks!
Please use the following code
$options='';
while($db_field = mysql_fetch_assoc($result))
{
$options .= "<option>".$db_field['title_en_US']."</option>";
}
For the html part,
<select name="titles"><? echo $options; ?></select>
I have looked through similar problems and solution but somehow only half way help me with my problem. I'm trying to make a form to checked more than one record from MySQL database and display the checked record to another page. Somehow I managed to do the page with check boxes but I don't know how to display the record checked. It can only display the first row of the record or all the records regardless which box are checked.
This is checkbox page
$columns = count($fieldarray);
//run the query
$result = mysql_query(
"SELECT * FROM request_item
ORDER BY request_item.IllNo DESC LIMIT 0, 6") or die(mysql_error());
$row = mysql_num_rows($result);
while($row=mysql_fetch_array($result))
{
{
$rows[] = $row['IllNo'];
}
foreach($rows as $value);
echo "";
echo " ";
echo $row['IllNo'];
echo "";
}
echo "";
?>
This is display record checked
$columns = count($fieldarray);
//run the query
$result = mysql_query(
"SELECT * FROM request_item
ORDER BY request_item.IllNo DESC LIMIT 0, 6") or die(mysql_error());
$row = mysql_num_rows($result);
while($row=mysql_fetch_array($result))
{
$rows[]=$row['IllNo'];
foreach($rows as $value);
if ($rows= 'checked') {
echo "";
echo $value;
}
Any help are welcome. Thank you.
There's actually a lot of problems with that script including syntax errors, calling the wrong variable name, form not opening where it should, invoking PHP after you already have, etc...
To get a good answer to you, you should share what make $row['IllNo'] should equal to indicate if it should be checked or not.
I reformatted it a bit and this may give you a good start.
<form NAME ="form1" METHOD ="POST" ACTION ="dari.php">
<table>
<?php
$columns = count($fieldarray);
//run the query
$result = mysql_query("SELECT * FROM request_item ORDER BY request_item.IllNo DESC LIMIT 0, 6") or die(mysql_error()) ;
$row = mysql_num_rows($result);
while($row=mysql_fetch_array($result)) {
echo "<tr><td>";
echo "<Input type = 'Checkbox' Name ='ch1' value ='ch1'";
// check checked if it is. this will be checked if $row['IllNo'] has a value
// if there were a condition to make it checked, you would put the condition
// before the ?
echo $row['IllNo'] ? ' checked' : '';
echo ' />';
echo $row['IllNo'];
echo "</td></tr>";
}
?>
</table>
<INPUT TYPE = "Submit" Name = "Submit1" VALUE = "Choose your books">
</FORM>