Variable drop down menu in php - php

I want to have a dropdown menu with as many options as a number in my database.
This is what i have but doesnt work:
<?php
// set the pointer back to the beginning
mysqli_data_seek($result, 0);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo '<div class="w-clearfix">
<div class="price">' .$row['Price']. '€</div>
//some code
<select class="w-select selectorplatos" id="numeroplatos" name="numeroplatos" data-name="numeroplatos">';
$capacity= "SELECT Capacity FROM Meals WHERE Meal_ID = '".$meal."';";
$resultcap = mysqli_query($conn, $capacity) or die("Couldn't execute query. ". mysqli_error($conn));
$i = 1;
// output data of each row
while($row1 = $resultcap->fetch_assoc()) {
echo '<option value="' .$i. '">' .$i. "</option>";
$i = $i+1;
}
echo '</select>
<input class="w-button pedirya" type="submit" value="¡Pedir ya!" data-wait="Espera por favor..." wait="Espera por favor...">
</form>
// more code here
mysqli_close($conn);
?>
Any clue whats wrong?
The command to display the drop down list cannot be executed.
Cheers!

Try changing your code to this:
echo '<select class="w-select selectorplatos" id="numeroplatos" name="numeroplatos" data-name="numeroplatos">';
$capacity = "SELECT Capacity FROM Meals WHERE Meal_ID = '".$meal."';";
$resultcap = mysqli_query($conn, $capacity) or die("Couldn't execute query. ". mysqli_error($connection));
$i = 1;
while($i < $resultcap) {
echo '<option value="' .$i. '">' .$i. '</option>';
$i = $i+1;
};
echo '</select>';
You weren't properly concatenating you MySQL query. It's also good practice to add or die("Couldn't execute query. ". mysqli_error($connection)) to get feedback on what went wrong when executing your query.
I also added echo because, assuming all of this is wrapped inside <?php ?> tags, well you need to echo HTML.

Please try this. I have tested it:
<select class="w-select selectorplatos" id="numeroplatos" name="numeroplatos" data-name="numeroplatos">'
<?php
$sql = "SELECT Capacity FROM meals";
$result = $conn->query($sql);
$i = 1;
// output data of each row
while($row = $result->fetch_assoc()) {
echo '<option value="' .$i. '">' .$i. "</option>";
$i = $i+1;
}
?>
</select>

Ok after a lot of research and inspiration from comments, i got it working the following way:
<?php
mysqli_data_seek($result, 0);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo '<div class="w-clearfix">
<div class="price">' .$row['Price']. '€</div>
<div class="priceindic">Precio por plato</div>
<div class="w-form numplatosselect">
<form class="w-clearfix" id="numerodeplatos" name="numerodeplatos" data-name="numerodeplatos" action="https://rest.nexmo.com/sms/json?api_key='
<label class="labelselector" for="numeroplatos-2">Numero de platos:</label>
<select class="w-select selectorplatos" id="numeroplatos" name="numeroplatos" data-name="numeroplatos">';
for ($i = 1; $i <= $row['Capacity']; $i++) {
echo '<option value="' .$i. '">' .$i. "</option>";
}
?>
a simple for loop does the trick using fetch_assoc from above and including Capacity in the original query

Related

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 ^^

MySQL Select based on drop down value

I have the following code:
<?php
session_start();
include_once("config.php");
$query = "SELECT Category FROM books";
$result = mysqli_query ($mysqli, $query);
echo '<select name="dropdown" value=""><option value="">Dropdown</option>';
while($row = mysqli_fetch_array($result))
{
echo '<option value="' . $row['Category'] . '">' . $row['Category'] . '</option>';
}
echo "</select>";
?>
the values of the drop down box are filled from the database.
I was wondering if theres a way to have a select statement that will run when a user clicks on one of the options in the drop down menu and then populate the results in a table?
any information will help!
Thanks
Ok, resontant81, you want to fill a table depending on the option selected, next code does exactly what you want, the explanation comes just after :
<html>
<head>
<title>My list</title>
<script type="text/javascript">
//----------------------------------------------------------------
// SENDS SELECTED OPTION TO RETRIEVE DATA TO FILL TABLE.
function send_option () {
var sel = document.getElementById( "my_select" );
var txt = document.getElementById( "my_option" );
txt.value = sel.options[ sel.selectedIndex ].value;
var frm = document.getElementById( "my_form" );
frm.submit();
}
//----------------------------------------------------------------
</script>
</head>
<body>
Click on any option
<br/>
<select id="my_select" onchange="send_option();">
<option>Select an option</option>
<?php
//----------------------------------------------------------------
// LIST FILLED FROM DATABASE (ALLEGEDLY).
for ( $i = 0; $i < 5; $i++ )
{ $text = chr($i+65) . chr($i+65) . chr($i+65);
echo "<option value='" . $text . "'>" . $text . "</option>";
}
//----------------------------------------------------------------
?>
</select>
<br/>
<br/>
<table>
<?php
//----------------------------------------------------------------
// TABLE FILLED FROM DATABASE ACCORDING TO SELECTED OPTION.
if ( IsSet( $_POST["my_option"] ) ) // IF USER SELECTED ANY OPTION.
for ( $i = 0; $i < 4; $i++ ) // DISPLAY ROWS.
{ echo "<tr>";
for ( $j = 0; $j < 6; $j++ ) // DISPLAY COLUMNS.
echo "<td>" . $_POST["my_option"] . "</td>"; // DISPLAY OPTION.
echo "</tr>";
}
else echo "<tr><td>Table empty</td></tr>";
//----------------------------------------------------------------
?>
</table>
<!-- FORM TO SEND THE SELECTED OPTION. -->
<form method="post" action"01.php" style="display:none" id="my_form">
<input type="text" id="my_option" name="my_option"/>
</form>
</body>
</html>
To make things easier for you (and for me), I am not using a database, all you have to do is copy-paste previous code to a text file, rename it "01.php" (because that's the action of the form, you can change it), and run it in your browser, is ready to use.
The dropdown is filled from database (in this case, with letters), when an option is selected the page reloads with the selected option and fills the table.
You said: "a select statement that will run when a user clicks on one of the options in the drop down menu and then populate the results in a table". This select statement you want you must put it right after the line :
if ( IsSet( $_POST["my_option"] ) ) // IF USER SELECTED ANY OPTION.
So your select statement will take the selected option from $_POST and use it to retrieve the right data and display it.
Let me know if it helps you.
This is the code to fill the dropdown, it's my code with yours combined:
// LIST FILLED FROM DATABASE (ALLEGEDLY).
$query = "SELECT Category FROM books";
$result = mysqli_query ($mysqli, $query);
while ( $row = mysqli_fetch_array($result) )
echo "<option value='" . $row['Category'] . "'>" . $row['Category'] . "</option>";
Next edit is to fill the table. Change the query for the right one if it's not right :
// TABLE FILLED FROM DATABASE ACCORDING TO SELECTED OPTION.
$query = "SELECT Category FROM books where category like '" . $_POST["my_option"] . "'";
$result = mysqli_query ($mysqli, $query);
while( $row = mysqli_fetch_array($result) )
echo "<tr>" .
"<td>" . $row['book_name'] . "</td>" .
"<td>" . $row['author'] . "</td>" .
"<td>" . $row['Category'] . "</td>" .
"</tr>";
I'm assuming $mysqli is your db connection and it's made through config.php. I'm also assuming that category is a column name in the books table. It is up to you to sanitize and validate the user input. This is simply an example to get you started.
page.php ....
<?php
session_start();
include_once("config.php");
function categories() {
global $mysqli;
$result = "";
$stmt = "SELECT Category FROM books GROUP BY Category";
$sql = mysqli_query ($mysqli, $stmt);
while ($row = $sql->fetch_array(MYSQLI_BOTH))
{
$result .= '<option value="' . $row['Category'] . '">' . $row['Category'] . '</option>';
}
mysqli_free_result($sql);
mysqli_close($mysqli);
return $result;
}
IF (isset($_POST['ThisForm'])) {
$category = htmlspecialchars(strip_tags(trim($_post['dropdown'])));
$stmt = "SELECT * FROM books WHERE category ='$category'";
$sql = mysqli_query ($mysqli, $stmt);
while ($row = $sql->fetch_array(MYSQLI_BOTH))
{
// do something with result
}
// free result and close connection
mysqli_free_result($sql);
mysqli_close($mysqli);
}ELSE{
// base form
echo '<form action="page.php" name="something" method="post">';
echo '<select name="dropdown" value=""><option value="">Dropdown</option>'.categories().'</select>';
echo '<input type="submit" name="ThisForm" value="submit" />';
echo '<form>';
}
?>

Show all tables from database into select form in php

Hiho,
I have a little problem with showing all table names on page in form .
Code bellow:
<select name="users" onchange="showTables(this.value)">
<option value="">Select a table:</option>';
$result = mysql_list_tables($db_name);
for ($i = 0; $i < count(mysql_num_rows($result)); $i++){
echo '<option value="' . mysql_tablename($result, $i) . '">' . mysql_tablename($result, $i) . '</option>';
}
echo '</select>
</form>
<br>
<div id="tablesDb"><b>All content from selected table will be listed there.</b></div></div>';
I try with mysqli too ,and i get results but still without names of the tables and nothing can be select.
Maybe someone know how to get this work.
First, you definitely want to be using MySQLi, as the MySQL extension is deprecated. Second, you can probably get all of the data you need in a simple SELECT query like this:
select `TABLE_NAME` from information_schema.tables
Try this
$mydbname = 'database_name';
$con=mysqli_connect("localhost","my_user","my_password",$mydbname);
// Check connection
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit();
}
$options = '';
// for the query you can use two of the following lines (line1+line2 OR line3+line4)
//Use line (line1+line2)
$result = mysqli_query($con,"SHOW TABLES");
$column_name ='Tables_in_'.$mydbname;
// OR (line3+line4
$result = mysqli_query($con,"SELECT TABLE_NAME AS tbl FROM information_schema.tables" WHERE TABLE_SCHEMA = \"'.$mydbname.'\"; ");
$column_name ='tbl';
while($row = mysql_fetch_array($result))
$options .= '<option value="' . $row[$column_name] . '">' . $row[$column_name] . '</option>';
echo '<select name="users" onchange="showTables(this.value)">';
echo '<option value="0">Select a table:</option>';
echo $options;
echo '</select>';
I modify code to this:
<select name="db_tablelist" onchange="showTables(this.value)">
<option value="">Select a table:</option>';
$result = mysqli_query($con,"SHOW TABLES");
while($row = mysqli_fetch_array($result)) { echo '<option value="' . $row[0] . '">'.$row[0].''; } echo '';
echo '</select>
</form>
<br>
<div id="tablesDb"><b>All content from selected table will be listed there.</b></div></div>';
And now everything works .

Mark an option as selected

I am populating a dropdown menu with data in a database, using this code:
$query = "SELECT * FROM my_gallery";
$execute = mysqli_query($link, $query);
$results = mysqli_num_rows($execute);
if ($results!=0) {
echo '<label>The galleries are: ';
echo '<select id="galleries" name="galleries">';
echo '<option value=""></option>';
for ($i=0; $i<$results; $i++) {
$row = mysqli_fetch_array($execute);
$name = htmlspecialchars($row['galleryName']);
echo '<option value="' .$name. '">' .$name. '</option>';
}
echo '</select>';
echo '</label>';
}
I want to add the selected=selected to the option selected and then use that option in another query, but I have problem adding the selected tag in the actual selected entry.
More INFO:
I am using dropzone.js to upload images, and I want to select the category on the fly. The category has to be selected from the dropdown menu and used in the INSERT query.
if you have a form with POST then the code is
$selected = "";
if($_POST['galleries']==$name) $selected=" selected='selected'";
echo '<option value="' .$name. '"'.$selected.'>' .$name. '</option>';
Just as an example try this,
$query = "SELECT * FROM my_gallery";
$execute = mysqli_query($link, $query);
$results = mysqli_num_rows($execute);
if ($results!=0) {
echo '<label>The galleries are: ';
echo '<select id="galleries" name="galleries">';
echo '<option value=""></option>';
for ($i=0; $i<$results; $i++) {
$row = mysqli_fetch_array($execute);
$name = htmlspecialchars($row['galleryName']);
if( $i == 1 )
{
$sel = 'selected="selected"';
}
else
{
$sel = '';
}
echo '<option value="' .$name. '"'. $sel .' >' .$name. '</option>';
}
echo '</select>';
echo '</label>';
}
If you want to select a specific option then you are going to have a variable containing that specific option example:
$conditionalName = "Mark";
$query = "SELECT * FROM my_gallery";
$execute = mysqli_query($link, $query);
$results = mysqli_num_rows($execute);
if ($results!=0) {
echo '<label>The galleries are: ';
echo '<select id="galleries" name="galleries">';
echo '<option value=""></option>';
for ($i=0; $i<$results; $i++) {
$row = mysqli_fetch_array($execute);
$name = htmlspecialchars($row['galleryName']);
echo '<option value="' .$name. '"'; if($name=$conditionalName){ echo ' selected=selected';} echo '>'. $name. '</option>';
}
echo '</select>';
echo '</label>';
}

click on a link in a table, then check in what row the link was

What I want to make is this:
I am making a blog. Now I want a query in my mysql-database that finds all the information that is in there. After that it should echo the title's of the blog's on my site in a table.
Then I would like to give people a choice which blog they would like to see. So when they click on the first title they will see the content of that blog.
So now the problem is that I don't know how I can make href's on every title that all go to another php file. In that file it should know which row/title is clicked and then it should make another query in the database that will find the content of that title. Then it should echo it on the site.
I got the table finished. I also know how to make the href's to the other page. the only thing I need to know is which title/href is clicked.
Is it possible to make this with php only? if yes, please explain me how.
I hope I am clear in what i would like to make.
EDIT: this is my code so far:
<?php
session_start();
$connectie = mysql_connect('localhost', 'root', 'usbw');
if ($connectie == false){
echo 'Er is iets fout gegaan met de connectie van de database';
}
if (mysql_select_db('dldatabase', $connectie) == false) {
echo 'Er kon geen verbinding met de database gemaakt worden';
}
$query = "Select *
from forum";
$resultaat = mysql_query($query, $connectie);
echo "<table border='1'>
<tr>
<th>Tijd</th>
<th>Gebruikersnaam</th>
<th>Titel</th>
</tr>";
`while($row = mysql_fetch_array($resultaat))
{
$_SESSION['row'] = $row;
echo "<tr>";
echo "<td>" . $row['Tijd'] . "</td>";
echo "<td>" . $row['Gebruikersnaam'] . "</td>";
echo "<td>" . '<a class="formtitellink" href="forumdocument.php">' . $row['Titel'] . '</a>' . "</td>";
echo "</tr>";
}
echo "</table>";`
this is the blog page
and this is where the content of the blog should be±
session_start();
$row = $_SESSION['row'];
$row = $row + 1;
$query = "Select titel, inhoud
from forum
where `ID` = '$row'";
$resultaat = mysql_query($query, $connectie);
echo "$resultaat";
You can always use a framework like Wordpress, but if you want this kind of structure from scratch then you should read more about URL manipulation and use of GET parameters.
Simplest solution will be:
Create a page say post.php
Now every time you want a post to be displayed, send it with get
parameter. For eg: "page.php?id=hello-world"
On post.php, you can read this "id" parameter and then fire a
query accordingly.
Then print the resultant data on page.php
You need to pages like first page and the second page to open as hyper link.
Here is the sample code:
<?php
echo '<table width="650"><p><tr><th style="margin-left: 10px; padding-right: 70px;"></th>
<th style="width: 350px; ">Item for sale</th> <th>Location</th> <th>Posted</th></tr></table>';
$query1 = "select id from tblads order by priority desc LIMIT 10 OFFSET 1";
$result1 = mysql_query($query1);
$i1 = 0;
while ($row1 = mysql_fetch_row($result1))
{
$count1 = count($row1);
$y1 = 0;
while ($y1<$count1)
{
$c_row1 = current($row1);
$id = $c_row1;
next($row1);
$y1 = $y1 + 1;
//////TABLE
echo'<style> th{padding:0;}</style> ';
echo '<table style=" table-layout:fixed; order-collapse: collapse; " width="650">';
echo '<tr> <th> </th> <th style="width:350px;"> </th> <th style=""></th> <th style="width:100px;"></th></tr>';
$query = "select image,details, location, date from tblads where id=$id";
$result = mysql_query($query);
if (!$result) {$message = 'ERROR:' . mysql_error();return $message;}
else
{ $i = $i1;
$i = 0;
while ($row = mysql_fetch_row($result))
{
echo '<tr> ';
$count = count($row);
$y = 0;
while ($y < $count)
{
$c_row = current($row);
echo '<td style="">' . $c_row . '</td>';
next($row);
$y = $y + 1;
}
echo ' </tr>';
$i = $i + 1;
}
}
}
echo'</table>'; //Content TABLE CLOSE
}
mysql_free_result($result); mysql_close($con);
?>
Then the second page view.php to be opened by the hyperlink:
<?php
$id=$_GET['id'];
$query = "select image, details, location, price, date, id from tblads where id=$id";
$result = mysql_query($query);
if (!$result)
{
$message = 'ERROR:' . mysql_error();
return $message;
}
else
{ $i = 0;
echo '<table><p><tr>';
while ($i < mysql_num_fields($result))
{
$meta = mysql_fetch_field($result, $i);
echo '<th>' . $meta->name . '</th>';
$i = $i + 1;
}
echo '<th>View</th></tr>';
$i = 0;
while ($row = mysql_fetch_row($result))
{
echo '<tr>';
$count = count($row);
$y = 0;
while ($y < $count)
{
$c_row = current($row);
echo '<td>' . $c_row . '</td>';
next($row);
$y = $y + 1;
}
echo '<td></td> </tr>';
$i = $i + 1;
}
echo' </p></table>';
mysql_free_result($result);
mysql_close($con);
}
?>

Categories