so i trying to use some query at pdo , but only the first select show results , the second select dont show the results , only show " Selecione "
look my code below
<?php
$con1 = new PDO('mysql:host=localhost;dbname=db','root','passdb');
$con1->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sqlimpressora = $con1->query("SELECT * FROM impressoras");
?>
<html>
<body>
<form>
<select name="impressora" class="form-control">
<option value="selecione">Selecione...</option>
<?php while($prod = $sqlimpressora->fetch(PDO::FETCH_BOTH)) { ?>
<option value="<?php echo $prod['nome'] ?>"><?php echo $prod['nome'] ?></option>
<?php } ?>
</select>
<select name="impressora" class="form-control">
<option value="selecione">Selecione...</option>
<?php while($prod2 = $sqlimpressora->fetch(PDO::FETCH_BOTH)) { ?>
<option value="<?php echo $prod2['nome'] ?>"><?php echo $prod2['nome'] ?></option>
<?php } ?>
</select>
</form>
</body>
</html>
Do not use ->fetch twice, use ->fetchAll and loop over the result.
$sqlimpressora = $con1->query("SELECT * FROM impressoras");
$result = $sqlimpressora->fetchAll();
foreach($result as $prod) {
// <select > .....
}
Each call to $sqlimpressora->fetch() will move the cursor to the next record in the result set. So when your first while loop finished it moved the cursor to the end of the result set and thus another fetch() call fetched nothing.
One possible solution is to fetch the records into an array and use foreach to iterate through it:
$rows = $sqlimpressora->fetchAll();
foreach ($rows as $prod) {}
foreach ($rows as $prod) {}
Related
First option of select must be the name referring to the ID. The remaining select options are the remaining names
<select class="input" name="client_id">
<?php
$sel_client_detail="Select * from client WHERE client_id=".$id."";
$result_detail = mysqli_query($con,$sel_client_detail);
while($new_record_row = mysqli_fetch_assoc($result_detail)) { ?>
<option selected><?php echo $row['nome'];?></option>
<?php };?>
<?php
$sel_client="Select * from client";
$result = mysqli_query($con,$sel_client);
?>
<option>-----------</option>
<?php while($new_record_row = mysqli_fetch_assoc($result)) { ?>
<option><?php echo $new_record_row['nome'];?></option>
<?php };?>
</select>
Output:
<select>
<option selected> Izzi (current ID name)</option>
<option> ____________</option>
<option> Other existing clients</option>
<option> Other existing clients</option>
<option> Other existing clients</option>
<option> Other existing clients</option>
</select>
If you want the user to be first in your option list just run the query once and build the HTML parts in 2 seperate strings. Then once the loop is complete put them together and echo them
<?php
echo '<select class="input" name="client_id">';
$itsme = '';
$others = '<option>-----------</option>';
$sql = "Select * from client";
$result = $con->query($sql);
while($row = $result->fetch_assoc()){
if ( $id == $row['id'] ) {
$itsme = "<option selected='selected'>$new_record_row[nome]</option>";
} else {
$others += "<option>$new_record_row[nome]</option>";
}
}
// put the option tags together in the order you specified
echo $itsme . $others . '</select>';
Here's a different, but more conventional, approach to this common scenario:
Why not just make the chosen ID selected when you get to it in the list? Then it will still show to the user first. It's more efficient than having two separate queries.
Like this:
<select class="input" name="client_id">
<?php
$sel_client="Select * from client";
$result = mysqli_query($con,$sel_client);
?>
<option>-----------</option>
<?php while($new_record_row = mysqli_fetch_assoc($result)) { ?>
<option <?php echo ($new_record_row["client_id"] == $id ? "selected": ""); ?> ><?php echo $new_record_row['nome'];?></option>
<?php };?>
</select>
This question already has answers here:
How can I use mysqli_fetch_array() twice?
(4 answers)
Closed last year.
I have fetched an associative array from MySQL database.
When I try to display data using while loop for the first time it behaves normally.
But when I try to use the same while loop 2 times the first one shows the data but the second one is blank.
My code looks as follows
<?php
include 'inc/dbcon.php';
$query = mysqli_query($con, "select * from product");
?>
<select required class="form-control">
<?php
while ($row = mysqli_fetch_assoc($query)) {
?>
<option><?php echo $row['product_name'] ?></option>
<?php
}
?>
</select>
<select required class="form-control">
<?php
while ($row = mysqli_fetch_assoc($query)) {
?>
<option><?php echo $row['product_name'] ?></option>
<?php
}
?>
</select>
And the out put is like this
Once a row is fetched from the $query, that row is completely removed from the $query so trying to fetch that row again will return nothing.
So to reuse the $query, you should instead fetch all rows into a variable. Try this
<?php
include 'inc/dbcon.php';
$query = mysqli_query($con, "select * from product");
$rows = mysqli_fetch_all($query, MYSQLI_ASSOC);
?>
<select required class="form-control">
<?php
foreach ($rows as $row) {
?>
<option><?php echo $row['product_name'] ?></option>
<?php
}
?>
</select>
<select required class="form-control">
<?php
foreach ($rows as $row) {
?>
<option><?php echo $row['product_name'] ?></option>
<?php
}
?>
</select>
?>
I have a PHP select that dynamically populates based on a MySQL recordset and uses a in_array value to identify and select the value(s) found in the database like this:
<select name="StaffSetSelection[]" size="5" multiple="MULTIPLE" id="StaffSetSelection">
<option <?php if ($totalRows_StaffSetID == 0) { echo "selected"; } ?> value="">Choose a Staff Set</option>
<?php
do {
?>
<option <?php if (in_array($row_StaffSetChoices['EmpNumber'], $StaffSetIDs)) {
echo "selected";
} ?> value="<?php echo $row_StaffSetChoices['EmpNumber'] ?>"><?php echo $row_StaffSetChoices['EmpFirstName'] ?></option><?php
} while ($row_StaffSetChoices = mysql_fetch_assoc($StaffSetChoices));
$rows = mysql_num_rows($StaffSetChoices);
if ($rows > 0) {
mysql_data_seek($StaffSetChoices, 0);
$row_StaffSetChoices = mysql_fetch_assoc($StaffSetChoices);
}
?>
</select>
Instead of displaying the selected values in a form option select I'd like to just display them as text.
I've tried to use the in_array again in a simple php echo but it does not display the data and there are no errors in my apache log.
This is what I've tried:
<?php if(in_array($row_StaffSetChoices['EmpNumber'], $StaffSetIDs)) echo $row_StaffSetChoices['EmpFirstName']?>
I think you forgot the brackets,
Try this way:
<?php
if(in_array($row_StaffSetChoices['EmpNumber'], $StaffSetIDs)) {
echo $row_StaffSetChoices['EmpFirstName'];
}
?>
I fixed the problem so I wanted to share the solution. Hopefully, this helps people that want to populate a combobox.
try{
$conn = new PDO('mysql:host=Jamal-PC;dbname=japanesewords',$username,$password);
$sql ='SELECT id, Englishword FROM Japanesedefinition;';
$stmt = $conn->prepare($sql);
$stmt ->execute();
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
select name="Japanese" id="Japanese">
<?php foreach($data as $row) : ?>
<option value="<?php echo $row['id']; ?>"><?php echo $row['Englishword']; ?></option>
<?php endforeach ?>
</select>
You are wrapping <option> around <option>. You should also use PDO::FETCH_COLUMN since you are selecting a single field.
$data = $stmt->fetchAll(PDO::FETCH_COLUMN);
<select name="Japanese" id="Japanese">
<?php foreach($data as $row) : ?>
<option><?= $row; ?></option>
<?php endforeach ?>
</select>
Edit: I see you changed your query to select 2 fields now instead of just one. Rather than fetching a column, you should use PDO::FETCH_KEY_PAIR. Example:
<?php $data = $stmt->fetchAll(PDO::FETCH_KEY_PAIR); ?>
<select name="Japanese" id="Japanese">
<?php foreach($data as $k => $v)
printf('<option value="%d">%s</option>', $k, $v); ?>
</select>
I can see the query returning results, but I can't seem to be able to put them into a html dropdown box. Also, the dropdown box has just as many entries as the query returns, but THEY ARE ALL WHITE SPACES. HOWEVER, the page source shows correct option values such as
<option value="3 John"></option>
<option value="Jude"></option>
<option value="Revelation"></option>
Can somebody help me out? Why dont they actually show in the dropdown box?
<html>
<?php
//Connect to the database
$mysqli = new mysqli("localhost", "root", "", "bible");
//Return an error if we have a connection issue
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
//Query the database for the results we want
$query = $mysqli->query("select distinct bname as Name from kjv limit 1");
//Create an array of objects for each returned row
while($array[] = $query->fetch_object());
array_pop($array);
//Print out the array results
print_r($array);
?>
<h3>Dropdown Demo Starts Here</h3>
<select name="the_name">
<?php foreach($array as $option) : ?>
<option value="<?php echo $option->Name; ?>"></option>
</select>
<?php endforeach; ?>
Try This
<select name="the_name">
<?php foreach($array as $option) : ?>
<option value="<?php echo $option['Name']; ?>"><?php echo $option['Name']; ?></option>
<?php endforeach; ?>
</select>
After the query is executed use the while loop to add the options to select
$query = $mysqli->query("select distinct bname as Name from kjv limit 1"); ?>
<select>
<?php while($option = $query->fetch_object()){ ?>
<option><?php echo $option->Name; ?></option>
<?php } ?>
</select>
Not sure what the array_pop is doing in the code
AS TIM WAX SAID THIS IS THE SOLUTION
$query = $mysqli->query("select distinct bname as Name from kjv limit 1"); ?>
<select>
<?php while($option = $query->fetch_object()){ ?>
<option><?php echo $option->Name; ?></option>
<?php } ?>
</select>
<select name="the_name">
<?php foreach($array as $option) : ?>
<option value="<?php echo $option->Name; ?>"></option>
<?php endforeach; ?>
</select>
You ended your loop in a way that it also create <select> tag again and again. Change it and try again. I don't know much about .php but it could be a problem in showing your dropdown box.
here is mine .. im a beginner but it works for me,
$query = $mysqli->query("SELECT * FROM `student_type_db`"); //table of student type
echo "<select>";
while($row = $query->fetch_array()){
echo "<option>";
echo $row['student_type'] . " - " . $row['student_description'];
echo "</option>";
}
echo "</select>";
// student type = 1 | student description = regular
// output : 1 - regular