Listbox items in edit mode in php - php

In add store page,i have a category listbox displaying list of categories from the table category in db.
The code for category listbox in add store is
<select id="category" multiple="multiple" name="category">
<?php
$ct_query="SELECT * FROM category ORDER BY category_id ASC";
$ct_sel=mysql_query($ct_query);
while($ct_res=mysql_fetch_array($ct_sel))
{
if($row['category_id'] == $ct_res['category_id'])
echo "<option value='".$ct_res['category_id']."' selected=selected>".$ct_res['category_name']."</option>";
else
echo "<option value='".$ct_res['category_id']."'>".$ct_res['category_name']."</option>";
}
?>
</select>
In edit store page, the same listbox is there.When i select a single category in the listbox in add store page, it displays correctly in edit store page.But when i select multiple items in listbox in add store page,the selected listbox items is not displayed in edit store page.The code for category list box in edit store page is
<select id="category" multiple="multiple" name="category">
<?php
$ct_query="SELECT * FROM category ORDER BY category_id ASC";
$ct_sel=mysql_query($ct_query);
while($ct_res=mysql_fetch_array($ct_sel))
{
if($row['category_id'] == $ct_res['category_id'])
echo "<option value='".$ct_res['category_id']."' selected=selected>".$ct_res['category_name']."</option>";
else
echo "<option value='".$ct_res['category_id']."'>".$ct_res['category_name']."</option>";
}
?>
</select>
In edit store page,the multi selected listbox items should be displayed correctly,when i select multiple items from listbox in add store page.
I have tried various codes.Nothing worked.Provide me a valuable solution for this.

You must use the same code to list all items even if you want to show, edit, etc,
You need to use mysqli instead mysql, and the code must be something like:
$sql = "SELECT id, name FROM category ORDER BY id ASC;";
$result = connection()->query($sql);
$rs = mysqli_fetch_assoc($result);
echo "<select id='category' multiple='multiple' name='category'>";
while ($rs != ''){
echo "<option id='".$rs['id']."' value='".$rs['id']."'>".$res['name']."</option>";
}
echo "</select>";
Then if you have some stored on database you can read selected items and mark as selected with jQuery (if i dont remember wrong it must be something like: $('#idselected').attr(select selected); )

Related

How to sum values from a database based on user input on a form using PHP?

So, I created a bunch of drop down options like this:
And I have a table in my database called 'recipes' which will contain meal names and the number of the various ingredients required to make them.
When a user chooses inputs using the drop down boxes and submits it via the POST function, I want to be able to sum the total ingredients required for all the meals they have chosen and echo those values. How can I do this?
My code for the drop down boxes is here:
<label for="dinner7">S</label>
<select name="dinner7" id="mealSelect">
<option name=default id=defaultSelect>None</option>
<?php
$sqli = "SELECT * FROM recipes;";
$result = mysqli_query($connect, $sqli);
while ($row = mysqli_fetch_assoc($result)) {
$mealname = $row['mealname'];
echo "<option>$mealname</option>\n";
}
?>
</select>

Using searchbox to query same data as dropdown

I am currently developing an application which allows the user to pick an option from a dropdown, and list table data based on this option, and done so dynamically. I was pondering the thought of a search bar which allows the user to search for a country name (within a continent) and list said country name upon searching. The part which confuses me is can I make it so that the user can pick a continent from the dropdown, and input a country name into the search bar then click the same button and receive the listing?
I understand the importance of picking relevant code however I think it would be important for the reader to see both my home page, and action page to understand what I am trying to achieve.
Here is the index code:
require_once('repeat_code.php');
$db = dbConn();
//Make an SQL statement
$sqlContinents = "SELECT DISTINCT ID as contID, Name as contName from w_Continent order by contName;";
//Execute SQL statement
$stmt = $db->query($sqlContinents);
//Start a form
echo "<form action='listCont.php' method='get'>\n";
//Start a select box
echo "<select name='contID'>\n";
//Loop through all continents
while ($continent = $stmt->fetchObject()) {
//Display each one as an option in the dropdown
echo "\t\t<option value='{$continent->contID}'> {$continent->contName} </option>\n";
}//end loop
// end select box
echo "</select>\n";
// display submit button
echo "<input type='submit' value='Find Country' />\n";
// end form
echo "</form>\n";
Here is the action page:
if(!empty($contID)) {
//Connect to the database
require_once('repeat_code.php');
$db = dbConn();
//Create SQL statement using ID
$sqlCountries = "SELECT w_Country.Name, w_Continent.Name as 'contName', w_Country.Region as 'regionname', w_Country.HeadOfState, w_Country.Capital
FROM w_Continent JOIN w_Country on w_Continent.ID = w_Country.Continent
WHERE w_Continent.ID = '$contID'";
//Execute statement to get a record set back
$stmt = $db->query($sqlCountries);
// Start a table
echo "<table border='1'>\n";
// Start a header row
echo "<tr><th>Country</th><th>Continent</th>\n";
//Loop through the record set
while ($continent = $stmt->fetchObject()) {
//Display each student in a row
echo "\t<tr><td>{$continent->Name}</td><td>$continent->contName</td>\n";
}//End loop
//end the table
echo "</table>";
Any help is appreciated.
Create a drop down and input box in a single line.
Drop Down Contain the continent and input box is empty.
In database create 2 tables in following format.
Continent Table
id | Continent_name
Countries Table
id | country_name | continent_id
Now run query in first table and display all the continent in drop down.
now when user select the continent name from the drop down and also write country name in the search box, then you have to query like this.
<form method="post">
<input type="hidden" name="command" value="search">
<select name="select_box">
<option value ="1">Asia</option>
<option value ="2">Africa</option>
</select>
<input type ="text" name="search_box" placeholder="Type Country">
</form>
Now Run PHP code like below.
if($_REQUEST['command']=='search'){
$continent_id = $_REQUEST['select_box']; //suppose user select asia
$country_name = $_REQUEST['search_box']; // suppose user type "Pakistan"
//now execute below query in countries table.
$query ="select * from countries where continent_id='$continent_id' and country_name like '%$country_name%'";
}

PHP code to retrieve from database to HTML drop down list not working

My database contains list of countries with ccode and country name. This code works here except that the list of countries are not displayed in dropdown list. but when i blindly select a random selection from drop down list, it is inserted into db.
Country
Select Country
<!-- PHP code to retreive drop down list from database countries -->
<?php
include('connection.php');
$sql = "SELECT country FROM countries ORDER BY ccode ASC";
$result = mysql_query($sql);
while($row = mysql_fetch_row($result))
{
echo '<option value="'.$row['country'].'">'.'</option>';
}
?>
</select>
Include the value inside the <option> tag.
echo '<option value="'.$row['country'].'">'.$row['country'].'</option>';
The value attribute is for specifying the value which is sent with the form, usually an id. You have to add the country names between the tags to display them.
echo '<option value="'.$row['country'].'">'.$row['country'].'</option>';
But when the two are the same you don't need to specify value:
echo '<option>'.$row['country'].'</option>';

Dropdown Box shows as blank instead of pulling items from the Database

For some reason, my PHP code won't show up database in the drop down box that I retrieve from MySQL. CS_JOBS and CS_JOBS_CATEGORY are tables in the database. I join CS_JOBS_CATEGORY and CS_JOBS, since they both have the same category_code column. I already tested connect.php, it's working fine.
<?php
require("connect.php");
$sql = "SELECT category_name FROM CS_JOBS_CATEGORY";
$result = mysql_query($sql);
echo "<select name='category_name'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='".$row['category_name']."'>".$row['category_name']."</option>";
}
echo "</select>";
?>
My goal is to show all category_name in the drop down box. Every time a user clicks on each category_name, it will show up all jobs(CS_JOBS) in each category. CS_JOBS consists of job_order_number, location, date_close ..etc..

in PHP, how do I get info from one table to pull up in a form for another

This is probably pretty basic, but I'm a beginner in PHP - I have one table for categories (cat_id is the primary key & cat_name is the only other field), and another table for posts which uses the cat_id as a foreign key.
My question is when creating a form for a user to submit a new post, how do I get the list of already created categories to appear in the form (by their category name) by way of a drop down box?
I'm assuming I'll have to select the table, assign values/variables to the data, or make it an array of some sort and then print the data in a table for the user to select?
There is no restriction on running queries for one table when you've already run a query for another. Use a simple SELECT * FROM category, perhaps with an ORDER BY clause. I'm assuming you're using mysql or mysqli, so use a while loop to go through the results:
// run the query
$result = $mysqli->query('SELECT * FROM category ORDER BY cat_name ASC');
// initialize the html string
$select = '';
// loop through all results
while ($row = $result->fetch_array()) {
// escape any characters that would break the html
$row['cat_id'] = htmlspecialchars($row['cat_id']);
$row['cat_name'] = htmlspecialchars($row['cat_name']);
// create this item's option element and append it to the html string
$select .= "<option value=\"$row[cat_id]\">$row[cat_name]</option>";
}
// wrap the options in a select
$select = "<select name=\"category\">$select</select>";
You'll end up with a html string $select that you can echo wherever you like, as many times as you like.
Dropdown boxes, or <select> elements, have the following syntax:
<select name="fieldname">
<option value="val">Name</option>
....
</select>
To generate this using PHP, you simply select all categories from your table, then loop through the results, printing them in the above format.
Simple example:
<?php $results = mysql_query(/* select all categories */); ?>
<select name="category_id">
<?php while ($row = mysql_fetch_assoc($results)) : ?>
<option value="<?php echo $row['cat_id']; ?>">
<?php echo htmlspecialchars($row['cat_name']); ?>
</option>
<?php endwhile; ?>
</select>
// PHP
$db = new PDO(...);
$stmt = $db->prepare('SELECT cat_id, cat_name FROM categories ORDER BY cat_name');
$stmt->execute();
$categories = $stmt->fetchAll(PDO::FETCH_ASSOC);
// HTML / PHP
<select name="category">
<? foreach ($categories as $category) : ?>
<option value="<?= htmlspecialchars($category['cat_id']) ?>">
<?= htmlspecialchars($category['cat_name']) ?>
</option>
<? endforeach ?>
</select>

Categories