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..
Related
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%'";
}
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); )
It's been long that I've been encountering this problem and I'll be glad if you guys can help me out on what to do.
This is the illustration.
Take for instance, you have a table called matrics and the table has a column called 'matric_num'.
Now, you create a registration page for users where one of the field is to select any matric num and after the user might have selected a matric number, filled the rest of the form and click on the submit button, it should insert the details into database.
If another user want to register, the matric number that the previous user selected should not be listed in the option menu.
HERE is what I did.
$query = mysqli_query($con, "SELECT *FROM `users`");
$rows = mysqli_num_rows($query);
echo "<select>";
for($i=1;$i<=$rows;$i++)
{
$fetch_user_matric = mysqli_fetch_array($query);
$matric_num = $fetch_user_matric['matric_num'];
$matric_query = mysqli_query($con, "SELECT *FROM `matrics` WHERE `matric_no` <> '$matric_num'");
$matric_rows = mysqli_num_rows($matric_query);
for($j=1;$j<=$matric_rows;$j++)
{
$fetch_matric_num = mysqli_fetch_array($matric_query);
$matric_no = $fetch_matric_num['matric_no'];
echo "<option value='$matric_no'>$matric_no</option>";
}
}
echo "</select>";
It displayed only the first matric number from the database whereas all matric numbers that hasn't been used by any user should be displayed.
Please what do you think is wrong with the sql query.
THANKS.
Just try this query:
SELECT * FROM `matrics` WHERE `matric_no` not in (select `matric_num` from `users`)
i have a table called home which has column_id as a column ,i want to display dropdown items say(column 1 and/or column2 and/or column3) which are not there in home table.
Execute a query like below,
$query = select * from home where description ='' or description is NULL;
Process the above query and send the items to your HTML page to display in the dropdown. I hope this is what you expect.
as i understand your problem try this
<select>
<?php
$res = mysql_query("SELECT column_id FROM `your_table` WHERE description='' ") or die(mysql_error());
while($row = mysql_fetch_assoc($res))
{
echo '<option value="'.$row['column_id'].'">Column-'.$row['column_id'].'</option>';
}
?>
</select>
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>';