Can't fill ComboBox option - php

$i = 1;
$tbl = "";
$field = "";
if($_POST['jenis']=="Bahan Baku")
{
$tbl = "bahanbaku";
$field = "bb_";
}
else if($_POST['jenis']=="Bahan Penunjang")
{
$tbl = "bahanpenunjang";
$field = "bp_";
}
<select class="span10" name="jenis" class="add-on">
<?php
$opt = "SELECT bb_nama FROM ".$tbl."";
$result = mysql_query($opt);
if($row = mysql_fetch_array($result))
{
echo "<option>". $row[$field.'nama'] ."</option>";
}
?>
</select>
The result is none of the selected rows displayed. which line is wrong?

while ($row = mysql_fetch_array($result))
{
echo "<option>". $row[$field.'nama'] ."</option>";
}

Change
if($row = mysql_fetch_array($result))
for
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
in order to iterate over the entire resultset

$opt = "SELECT bb_nama FROM ".$tbl."";
you are selecting the field bb_nama, but when you retrieving time you are doing $row[$field.'nama']. the field is mismatch while the table value is changing.
in other case, query should be
$opt = "SELECT ".$field."_nama FROM ".$tbl."";

Related

Populate dropdown from database and set default value

Right now I have a working solution for populating an HTML <select>/<option>-dropdown with the content through PHP/MYSQLI from my database: listoption.
The database:
DATABASE NAME:
# listoption
TABLES:
# ID INT(11) *Primary AI
# listoption_item VARCHAR(255)
Here's the other code (not the mysqli connect but everything afterwards..)
<?php
$result = $mysqli->query("select * from listoption");
echo "<select id='list' name='list'>";
while ($row = $result->fetch_assoc()) {
$listoption_item = $row['listoption_item'];
echo '<option value="'.$listoption_item.'">'.$listoption_item.'</option>';
}
echo "</select>";
?>
But the problem is now that I want to have one of these options that are populated through that query to be selected. And the option to be selected should be determed by a parameter in the URL, for example: index.php?id=1.
So now I need to somehow add a IF/ELSE and a $_GET['id']; into the code to make it identify if the ID from the database is the same as the populated item and then set it to selected.
Any idéas? Thanks!
You can do that like given below:
<?php
$result = $mysqli->query("select * from listoption");
$id = ($_GET['id'])? $_GET['id'] : '';
echo "<select id='list' name='list'>";
while ($row = $result->fetch_assoc()) {
$listoption_item = $row['listoption_item'];
$sel = ($id == $row['id'])? 'selected="selected"':'';
echo '<option value="'.$listoption_item.'" '.$sel.'>'.$listoption_item.'</option>'; // $sel will deside when to set `selected`
}
echo "</select>";
?>
You can rewrite the code as follows:
<?php
$id = $_GET['id'];
$select = "";
$result = $mysqli->query("select * from listoption");
echo "<select id='list' name='list'>";
while ($row = $result->fetch_assoc()) {
$row_id = $row['ID'];
if($row_id == $id){
$select = "selected";
}
$listoption_item = $row['listoption_item'];
echo '<option value="'.$listoption_item.'" selected="'.$select.'">'.$listoption_item.'</option>';
}
echo "</select>";
?>
Use the following code:-
<?php
$selectedId = isset($_GET['id'])?$_GET['id']:0;
$result = $mysqli->query("select * from listoption");
echo "<select id='list' name='list'>";
while ($row = $result->fetch_assoc()) {
$listoption_item = $row['listoption_item'];
echo '<option value="'.$listoption_item.' .(($selectedId>0)?:" selected ":"").'">'.$listoption_item.'</option>';
}
echo "</select>";
?>

how to get value from database and show in dropdown list

I already try many ways but the value didn't show in dropdown list
Here, this is my code. can you suggest me anything that i was wrong
<?php
$result = mysqli_query($con,"SELECT * FROM project");
if( mysqli_num_rows( $result )==0){
echo "<tr><td>No Rows Returned</td></tr>";
}else{
$row = mysqli_fetch_assoc( $result );
$pos = 0;
echo "<select name=Pname >";
while($pos <= count ($row)){
echo "<option value="$row["project_no"]">"$row["project_name"]"</option>";
$pos++;
}
echo "</select>";?>
And i write as .php file. Thanks for your help.
Try this out:
$output = '';
if(mysqli_num_rows($result) == 0){
// echo error;
} else {
while($row = mysqli_fetch_assoc($result)){
$project_no = $row['project_no'];
$project_name = $row['project_name'];
$output .= '<option value="' . $project_no . '">' . $project_name . '</option>";
}
}
Then inside of your HTML, print your $output variable inside of your <select> element:
<select>
<?php
print("$output");
?>
</select>
It should print all options for every row that you have requested from the database.
Hope this helps :)
Try this:
$result = mysqli_query($con,"SELECT * FROM project");
if( mysqli_num_rows( $result )==0){
echo "<tr><td>No Rows Returned</td></tr>";
}else{
echo "<select name=Pname >";
while ($row = mysqli_fetch_assoc($result)) {
echo "<option value="$row["project_no"]">"$row["project_name"]"</option>";
}
echo "</select>";
}
This is the result code that i can run it. I put this code in a form code of html
$result = mysqli_query($con,"SELECT * FROM project"); ?>
<?php
$output = '';
if(mysqli_num_rows($result) == 0){
// echo error;
} else {
echo " <select name = Pname>";
while($row = mysqli_fetch_assoc($result)){
$project_no = $row['project_no'];
$project_name = $row['project_name'];
$output = "<option value=" . $project_no . "> ". $project_name ." </option>";
print("$output");
}
echo " </select>";
}
?>
Thank you every one for helping me ^^

How can i get value from other db table who releted with array value?

In the code below I store one array in customer table. Now I want data from agent table but who related with array value.
<?php
$query = "SELECT * FROM customer";
$no1=1;
$result = mysql_query($query);
while($values = mysql_fetch_array($result))
{
$values= unserialize($values['part_no']);
foreach($values as $value)
{
echo $value."<br>";
$query2 = "SELECT * FROM agent WHERE number=$value";
$result2 = mysql_query($query2);
while($values2 = mysql_fetch_array($result2))
{
echo '' . $row['name'] . ''; // get value from agent table.
$no1++;
// echo $no1."<br>";
}
}
}
?>
Try this
<?php
$query = "SELECT * FROM customer";
$no1=1;
$result = mysql_query($query);
while($values = mysql_fetch_array($result))
{
$values= unserialize($values['part_no']);
foreach($values as $value)
{
$search_value = "'".implode("','", $value)."'";
echo $value."<br>";
$query2 = "SELECT * FROM agent WHERE number IN ($search_value)";
$result2 = mysql_query($query2);
while($values2 = mysql_fetch_array($result2))
{
echo '' . $values2['name'] . ''; // get value from agent table.
$no1++;
// echo $no1."<br>";
}
}
}
?>
Better you should use implode.
while($values = mysql_fetch_array($result))
{
$val= unserialize($values['part_no']);
$query2 = "SELECT * FROM agent WHERE number IN (".implode(',',$val).")";
$result2 = mysql_query($query2);
while($values2 = mysql_fetch_array($result2))
{
echo '' . $row['name'] . ''; // get value from agent table.
$no1++;
// echo $no1."<br>";
}
}

Populating dropdown with query results in PHP

The code should open all the rows of gamename column of games table and put 1700 rows into drop down menu, but it only displays a blank dropdown with 1700 rows.
// Connect to server and select database.
mysql_connect("$host", "$username", "$password") or die(mysql_error());
mysql_select_db("$db_name") or die(mysql_error());
$i=0;
$result = mysql_query("SELECT gamename FROM games");
$storeArray = Array();
echo '<select name="game" style="width: 400px">';
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$storeArray[] = $row[i];
echo "<option>".$storeArray[i]."</option>";
$i= $i+1;
}
?>
</select>
You should try it like this:
<?php
mysql_select_db("$db_name") or die(mysql_error());
$sql = "SELECT gamename FROM games";
$query = mysql_query($sql);
echo '<select name="game" style="width: 400px">';
while ($row = mysql_fetch_assoc($query)) {
echo '<option>'.$row['gamename'].'</option>';
}
echo '</select>';
?>
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$storeArray[] = $row[i];
echo "<option>".$storeArray[i]."</option>";
$i= $i+1;
}
For one thing, you're using i and $i interchangeably here; this may or may not cause an issue. You're assigning the ith member of $row into $storeArray, and that's not going to work after the first row, as there's only one item in your SELECT.
Why not just do:
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "<option>".$row['gamename']."</option>";
}
mysql_select_db("$db_name") or die(mysql_error());
$i=0;
$result = mysql_query("SELECT gamename FROM games");
$storeArray = Array();
echo '<select name="game" style="width: 400px">';
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$storeArray[] = $row['gamename'];
echo "<option>".$storeArray['gamename']."</option>";
$i= $i+1;
}
You have more than one issue here... But give this a try. (Also I would start to use PDO or mysqli if I were you...)
$result = mysql_query("SELECT gameid, gamename FROM games");
//$storeArray = Array();
echo '<select name="game" style="width: 400px">';
while ($row = mysql_fetch_assoc($result)) {
$gamename = $row['gamename'];
$gameid = $row['gameid'];
echo "<option'".$gameID."'>".$gamename."</option>";
}
echo '</select>';
This is assuming you have an ID field in your games table. You weren't assigning the options any value. Which won't be useful. Also you weren't pulling the data in the way you had it.

Why does this query show only one result?

The query I have below will only show me one result even if there are multiple matching entries (completely or partially matching). How do I fix it so it will return all matching entries:
//$allowed is a variable from database.
$sql = "SELECT `users`.`full_name`, `taglines`.`name`, `users`.`user_id` FROM
`users` LEFT JOIN `taglines` ON `users`.`user_id` = `taglines`.`person_id`
WHERE ( `users`.`user_settings` = '$allowed' ) and ( `users`.`full_name`
LIKE '%$q%' ) LIMIT $startrow, 15";
$result = mysql_query($sql);
$query = mysql_query($sql) or die ("Error: ".mysql_error());
$num_rows1 = mysql_num_rows($result);
if ($result == "")
{
echo "";
}
echo "";
$rows = mysql_num_rows($result);
if($rows == 0)
{
}
elseif($rows > 0)
{
while($row = mysql_fetch_array($query))
{
$person = htmlspecialchars($row['full_name']);
}
}
}
print $person;
Because your overwriting $person on each iteration.
Hold it in a $person[] array if your expecting more then one. Then loop through it with a foreach loop when you intend to output.
Not related but your also querying twice, you only need 1 $result = mysql_query($sql);
Update (Simple Outputting Example):
<?php
$person=array();
while($row = mysql_fetch_array($query)){
$person[] = array('full_name'=>$row['full_name'],
'email'=>$row['email'],
'somthing_else1'=>$row['some_other_column']);
}
//Then when you want to output:
foreach($person as $value){
echo '<p>Name:'.htmlentities($value['full_name']).'</p>';
echo '<p>Eamil:'.htmlentities($value['email']).'</p>';
echo '<p>FooBar:'.htmlentities($value['somthing_else1']).'</p>';
}
?>
Or an alternative way to is to build your output within the loop using concatenation.
<?php
$person='';
while($row = mysql_fetch_array($query)){
$person .= '<p>Name:'.$row['full_name'].'</p>';
$person .= '<p>Email:'.$row['email'].'</p>';
}
echo $person;
?>
Or just echo it.
<?php
while($row = mysql_fetch_array($query)){
echo '<p>Name:'.$row['full_name'].'</p>';
echo '<p>Email:'.$row['email'].'</p>';
}
?>

Categories