I'm trying to create a select box full of options based on data from an array that is built from an sql query, I've tried researching where I'm going wrong but the closest I've managed to get is echoing alot of empty option boxes my code is as follows:
$result = mysqli_query($cons,"SELECT user_name FROM users");
while($row = mysqli_fetch_array($result)){
foreach($row as $key => $value){
echo '<option value="'.$value.'"</option>';}}
If anybody could point me in the right direction that would be great.
EDIT: I can't believe it was something as simple as that! Thank you very much.
On a separate note It's actually giving all of the data twice.
I'll mark as correct answer as soon as I can.
echo '<option value="'.$value.'">'.$value.'</option>';
or
echo "<option value=\"{$value}\">{$value}</option>";
You never closed the opening option tag. If you view your source you'll see it's wonky.
BTW, You don't need the value="" part if you're using the same value in the text of the option.
echo "<option>{$value}</option>";
Entire code as I would do it
$result = mysqli_query($cons,"SELECT user_id, user_name FROM users");
while($row = mysqli_fetch_assoc($result)){
echo "<option value=\"{$row['user_id']}\">{$row['user_name']}</option>";
}
Related
I think I am nearly there with this problem, but I can't quite see why my solution isn't working. I'm trying to pre-select an item from a php drop down list that uses a MySQL table. The drop down list populates as expected, but the pre-select doesn't work. My code:
echo $Trans1E; // Display Existing value
echo '<p>Trans1: ';
$q = "SELECT TR1_Name FROM sb_TR1 ORDER BY TR1_Name";
$r = #mysqli_query ($dbc, $q);
$row = mysqli_fetch_array ($r, MYSQLI_NUM);
echo "<select name='Trans1' value=''>Trans1</option>"; // list box select command
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC))
{//Array of records stored in $row
$selected='';
if($row[TR1_Name]==$Trans1E) //determine if the row value is the same as the Existing
{//if it it then mark as selected
echo "<option value=$row[TR1_Name] selected='selected'>$row[TR1_Name]</option>";
}
else
{// if it is not then just inlcude it in the drop down list
echo "<option value=$row[TR1_Name] >$row[TR1_Name]</option>";
}
}
echo "</select>";// Closing of list box
echo '
</p>';
I've seen similar things on this forum for drop down lists which I've incorporated in my code or at least experimented with but without success, I suspect an error in: if($row[TR1_Name]==$Trans1E), but have run out of thinsg to try.
It seems I was closer to the correct code than I thought, I resolved the problem by adding the line:
$Trans1E=mysqli_real_escape_string($dbc, trim(htmlentities($Trans1E)));
before the code.
Despite displaying the content of Trans1E, it seems there was an unwanted element in it, which I hadn't spotted so I thought the code further down was at fault.
Thanks for you comments. Hope this helps others.
I am writing code to register a new project via a html form. I want to be able to click a dropdown box which pulls the values from a table on the database.
At the moment a dropdown box displays but with no values.
PLEASE NOTE: I am a learning the basics so apologies if this is a simple question/answer scenario.
My code is below, any help is appreciated, I connect to the database via a php include script. The table is called 'customers' and the item I want to list is 'name';-
<?php
$result = mysql_query("SELECT customers FROM name");
echo "<select name='client'>";
while($row = mysql_fetch_assoc($result))
{
echo "<option value = '".$row[name]."'>".$row[name]."</option>";
}
echo "</select>";
?>
"The table is called 'customers' and the item I want to list is 'name';" -
Do SELECT name FROM customers instead of SELECT customers FROM name
Using mysql_error() to mysql_query()
would have shown you the error that the table name does not exist.
Plus,
[name] are missing quotes inside them => ['name'] which are being treated as constants.
in
echo "<option value = '".$row[name]."'>".$row[name]."</option>";
as caught and kudos to devdesign
echo "<option value = '".$row['name']."'>".$row['name']."</option>";
However, you are using a deprecated MySQL library. If you are still not getting results, then this could mean that you need to use (and should use) mysqli_ or PDO instead.
Here are a few links on the subject:
mysqli with prepared statements
PDO with prepared statements.
Your code should be. Also note the single quotes within $row['name']. You missed that.
<?php
$result = mysql_query("SELECT name FROM customers");
echo "<select name='client'>";
while($row = mysql_fetch_assoc($result))
{
echo "<option value = '".$row['name']."'>".$row['name']."</option>";
}
echo "</select>";
?>
I am currently using the code below to print out a MySQL table. However, when I am printing multiple rows the format is pretty ugly. What is a way or command I could create columns similar to the Tab key to make it more aesthetically pleasing?
$google= mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_assoc($google)){
foreach($row as $cname => $cvalue){
print "$cvalue\t <tr>";
}
print "<br>";
}
The query is printed using user input. using the
<form action="google.php" method="post">
command. I am also looking for a way to print this table on the same page as the HTML page that I accept the user input.
Your code is pretty good and only needs HTML added to it to make it look nicer. Using tables (this has not been tested but it should work):
$google = mysqli_query($query) or die(mysqli_error());
echo '<table>';
while($row = mysqli_fetch_assoc($google)) {
echo '<tr>';
foreach($row as $cvalue) {
print '<td>'.$cvalue.'</td>';
}
echo '</tr>';
}
echo '</table>';
This is a quick and dirty way to output a MySQL table, but if you also want the names of the columns to show up, things get quite a bit trickier. Also, if your results contain special characters such as ">", this can mess up the output. If that happens, just look up the documentation for htmlspecialchars().
If you want this table to show up on the same page as your form, just create a submit button and check the value in PHP like so:
if (isset($_POST['SubmitButtonNameHere']) { ... }
I'm not sure of the structure of your form so this code might need to be different to determine whether the form has been submitted. You can put the code for creating the table inside of the curly braces and place the code for your form either above or below the if statement.
<tr> is an HTML element used to add rows to a <table> element. The <td> element adds cells to those rows. Also, your code was using mysql_*. This was changed to mysqli_* as the mysql prefix is deprecated in PHP. Hope this answers your question.
Just use this code instead :)
$google= mysql_query($query) or die(mysql_error());
echo "<table><tbody>";
while($row = mysql_fetch_assoc($google)){
foreach($row as $cname => $cvalue){
echo "<tr><td>".$cname."</td><td>".$cvalue."</td></tr>";
}
echo "</tbody></table>";
Done :)
I am trying to print the selected dropdwon item. I have already written the code for dropdown to fetch a column from database.
Now i should print the only the id of selcted dropdown item. i don't know how to make it. please help me, this is my code
<?
$query_name="SELECT id_cat,name FROM `fs01_metier_cat` ORDER BY `fs01_metier_cat`.`name` ASC";
$result = mysql_query ($query_name);
echo "<select name=category value=''></option>";
while($nt=mysql_fetch_array($result))
{
echo "<option value=$nt[name]>$nt[name]</option>";
}
echo "</select>";
$query_id="SELECT id_cat FROM `fs01_metier_cat`";
$result1 = mysql_query ($query_id);
while($row = mysql_fetch_assoc($result1))
{
echo $row['id_cat'];
}
?>
try this:
while($nt=mysql_fetch_array($result)){
echo "<option value='{$nt['id_cat']}'>{$nt['name']}</option>";
}
with {} php can insert also array values into a string and you should set ' ' around the attribute "value"'s value (alot of values here.. ^^), that the html is w3c conform (i dont know if a browser would take it as correct without them..)
without the { } it would look like that:
while($nt=mysql_fetch_array($result)) {
echo "<option value='".$nt['id_cat']."'>".$nt['name']."</option>";
}
depending on your editor code highlighting might work better in the second case ;)
and about the selected item:
i would suggest you to use jQuery to get the content of the selected item
var selectedId = $('#yourselectid option:selected').attr('value');
and then you can e.g. write it to the document or to a div:
document.write(selectedId);
or
$('#yourDivIdWhereYouWantToPutTheSelectedId').html(selectedId);
important: please note that i changed the value's index to id_cat because then you can handle the options with their id
Since the selected option changes everytime you change the dropdown selection, you can not handle this via php. There are ways to do that without a huge library like jQuery (since they are also just simple Javascript) but they simplify such things alot ;)
In the PHP code below i want to select all the column values of a table and make them options of a select form. The result is that i dont get any options at all. Could someone help? Thanks
<?php
// ....
$userid=$_SESSION['userid'];
echo "<select>";
$sql = "SELECT * FROM users where userid='".$userid."'";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
echo "<option>" .$row['company']. "</option>";
}
echo "</select>";
mysqli_close();
?>
It may just be a typo but you have mixed mysql and mysqli functions, you have stated mysql_query and mysql_fetch_array, whereas you have closed it with mysqli_close, could be an issue depending on what you wrote above this code or indeed if this is not just a typo.
Other things to try would be try the query in your mysql client / phpmyadmin and see if it comes up with any results or errors.