Building an edit users form for my system and having a little trouble trying to figure out how to do a selected="selected" for the value in the dropdown that goes with the users information in the database.
This is my code: http://pastebin.com/EVdUfTzN
The names of the offices are stored in one table called offices and the value of the office that the patient went to is stored in patients. Basically I want to select the information from both tables and add a selected option on it.
Here is picture of the offices table
Here is a picture of the patients table
Do you understand what i am trying to do? Maybe even simplify it so its one query with INNER JOIN
Try this (replace lines 25-29):
<?php
while ($dd_loc_row = mysql_fetch_array($dd_loc_result)) {
echo "<option value=\"" . $dd_loc_row['office_id'] . "\"".($row['pat_loc'] == $dd_loc_row['office_id'] ? ' selected="selected"' : '').">" . $dd_loc_row["office_name"] . "</option>";
}
?>
Related
I'm building a property website and have created a page to create a new property. When I want to edit it, I'm having difficulty displaying desired results.
Relevant details:
Property table has a developer field which is the id from developer table.
property create page has a drop down to select a string (taken from developer table) shown to the user but the id is stored in developer field
When I want to set an edit page, I need to show the current table values but allow to select as before.
I've tried this but it doesn't show the current table's value and due to developer and id columns being used in both tables, I used "as" to create a shortened unique form.
<?php
$qry=mysql_query("SELECT Distinct a.id as a_id,a.developer as a_dev,b.developer as b_dev FROM developer a left join property b on b.developer=a.id", $con);
if(!$qry)
{
die("Query Failed: ". mysql_error());
}
?>
<div class="title">Select a <?php echo $title2 ?> : </div>
<div class="description">
<select name="field2" id="field2">
<option value=""></option>
<?php
// First, define the desired default value
$default_value = $row['b_dev'];
while($row=mysql_fetch_array($qry))
{
// Then you can mark that one as selected in your "while" loop
$selected = ($row['b_dev'] == $default_value) ? ' selected' : '';
echo "<option value='" . $row['a_id'] . "'" . $selected . ">" . $row['a_dev'] . "</option>";
}
?>
The selection drop down is working fine, but the currently stored field value for b_dev doesn't display. I've tried to use variables also, but it didn't show the stored value. one more caveat is that the stored value in b.developer is a number and the displayed value has to be the string that matches b.developer=a.id so we only see the string and not the integer.
I've given relevant details, if anyone requires more details, please mention in comments, and I will try to provide.
PS:
Yes I know I should be using MySQLi or PDO, but I will convert it once I get the structure and back end completed.
What is $default_value = $row['b_dev'];? Specifically, what is $row in your case? I assume $row is unassigned before your loop.
You need to set $default_value to your current value, which you need to get from the database as a separate query.
Apparently I had to rewrite my sql to take in all the information at once using left joins. It works on some of the stuff i tried.
$qry_main=mysql_query("SELECT a.id, name, description, b.developer as pdev, c.agent as pagent ,g.country as pcountry,f.city as pcity, d.area as parea, size, furnished, h.type as ptype, finished, bed, bath, pool, featured, price,delivery from property a left join developer b on a.developer_id=b.id left join agent c on a.agent_id=c.id left join area d on a.area_id=d.id left join type h on a.type_id=h.id left join city f on d.city_id=f.id left join country g on f.country_id=g.id where a.id='$id'",$con);
My drop down boxes are working fine using multiples of this code which, I admit, is very rudimentary:
$sql = "SELECT Country FROM engravers order by Country";
$result = mysql_query($sql);
echo "<select name\\='Country'>";
echo "<option value='$_POST'>Country</option>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['Country'] . "'>" . $row['Country'] . "</option>";
}
echo "</select>";
The only problem is that if I have more than one item in the field I get a response for each item, for instance, in my testing database there are two rows with Australia as the country. My dropdown then puts Australia in twice. Later, when there will be about a hundred Australian entries this will be a problem.
Also if there is a blank field I get a blank line in the drop down box. I don't seem to be able to find a solution to this. Is there a better way to write the drop down code that doesn't do this. Unfortunately, as a novice, I am using the simplest code I can understand but the results look just like that. Any help would be gratefully accepted.
Try this query:
SELECT DISTINCT Country
FROM engravers
WHERE Country <> ''
ORDER BY Country
SELECT DISTINCT will take care of multiples - if there are 100 rows with Country = Australia, it will only select one.
WHERE Country <> '' will exclude any rows that have a blank value for Country. You could also include AND Country IS NOT NULL to exclude NULL values as well.
References:
<> (Not equal operator), SELECT DISTINCT ...
I have a database that stores data input from a website. I have designed a form to retrieve some of the data from the database. My issue is, I want some of the form fileds to auto populate with data from certain table columns. i don't know the PHP code to insert into the form fileds to do this.
The form looks like this:
http://onlinestudentsadmission.com/schooldemo/studentsdataform.php
The table has the following columns:
userid last_name first_name year friendly_url has_pic Gender Orphan Admmission School County
I want the form to pull the data in year, County, and School columns and populate the field options. What code do I insert into those form fields?
The form submits to studentdatadisplay.php which am yet to code since I dont know how it should look like so as to get the data from the data retrieval form and format it to a table depending on the search criteria.
Any help will be appreciated alot.
If your HTML form inputs have the name attributes set, and your form action is actually posting to the right URL, you don't have to do anything else to your form.
Have a read through this guide for PHP form handling. Basically, the name attributes for each of your inputs will be an element in the $_POST object available to your in your future studentdatadisplay.php script. You'll do any database queries in that script file.
Are you talking about drop-down list that is automatically populated with the different values from a column (such as year, County or School)?
If so, the PHP at the top of your page should look something like this:
<?php
$con=mysqli_connect("localhost","username","password","myDatabase");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$lookupYear = mysqli_query($con,"SELECT DISTINCT year AS value FROM tblStudents ORDER BY year ASC");
$lookupCounty = mysqli_query($con,"SELECT DISTINCT County AS value FROM tblStudents ORDER BY year ASC");
$lookupSchool = mysqli_query($con,"SELECT DISTINCT School AS value FROM tblStudents ORDER BY year ASC");
?>
Then when you're ready to list each one in the body HTML of your page, generate a dropdown option for each one using this example:
<select name="year">
<?php
while($row = mysqli_fetch_array($lookupYear))
{
echo "<option value=\"" . $row['value'] . "\">" . $row['value'] . "</option>";
echo "\n";
}
?>
</select>
I hope that helps.
Multiple categories selection system for Article
Earlier M using two tables
articles & categories and save category ID in articles table
But in this system I can save only one category ID per article
I want to save Article in Multiple categories
While searching I found same question on StackOverflow
I understand the whole concept of adding one more table of relationship & saving Article ID & Category ID in this table.
But not aware how to implement multiple selection system using arrays in New Article Form & Edit Article Form.
earlier I am showing Select in my form to display categories list in Add Article & Edit Article Page.
pls someone explain me How to show categories list in multiple checkbox style in my form so user can select multiple categories and then after POST how to get checkbox data and run query to insert data in both Article Table & New Relationship table with selected categories ID
want to display category List like this screenshot
Many Many Thanks...
EDIT:
I use echo '<input type="checkbox" name="selcats[]" value="$catid"> ' .$catname;
to display Categories check box
Its showing in a row side by side.
how to change display like screenshot i.e. list with scrollbar
& need to process this array and insert in new table while inserting article in databse.
EDIT 2
got the checkbox display correct in a scroll list by using a div :D
<div style="height: 150px; width: 200px; overflow: auto;">
In my case, I use a "tag system", for instance: You have an article with one category, nothing will change that... In adition, you can create another field in the article table with the relevant words (or whatever you want) and separete them with spaces or commas.
// Get the data from your database
$tags = 'world, travel, turism, cities';
// Separate the values
$tags = explode(", ", $tags);
// Create a link for each one
foreach($tags as $t)
{
echo ' ' . ucfirst($t) . ' ' . "\r\n";
}
It should output:
World
Travel
Turism
Cities
And it means that you can SELECT articles that have the title LIKE the tag, or whatever you want to search.
Is that what you was looking for?
You basically require two tables, with this structure
table_categories
_____________________________
| id | title |
-----------+---------------+
table_category_detail
______________________________________________
| id | categoryId | articleId |
-----------+---------------+------------------
To extract all categories, select all from the table_categories and put up into the select menu with mutiple selection enabled.
Next, when posted get the selected box values and insert into table_category_detail one by one
This is how you create a select box
$query = "SELECT * FROM table_categories";
$result = mysql_query($query);
echo '<select multiple="multiple">';
while($row = mysql_fetch_assoc($result)) {
echo '<option value="'.$row['id'].'">'.$row['title'].'</option>';
}
echo "</select>";
Or a Multiple Check Box
while($row = mysql_fetch_assoc($result)) {
echo '<input type="checkbox" name="selcats[]" value="'.$row['id'].'"> ' .$row['title'];
}
After the post:
// Here write codes to insert the article first
$articleId = mysql_insert_id(); //get the id
$values = $_POST['selcats'];
foreach($values as $value) {
$query = "INSERT into `table_category_detail` VALUES(NULL, $value, $articleId)";
$result = mysql_query($result);
}
To give some context, the following dropdown is within a form that gives the current profile description of an animal stored in the database: i.e, this pony is yea high, of this gender, owned by somesuch, etc. The purpose of the form is to edit these current values. There are various options that can be overwitten freely, others where I want the choice limited to options from a dropdown menu.
The following is the current code I am using for the Gender field, which - although it does work - cannot be the proper way of doing things. I would be more interested in a method that queried the current state, gave the current state as the default option, stored the current state, queried other available states not equal to the stored current state, then gave the remaining states as options. It is that method that I could best adapt to all the other dropdowns on the form.
There are two tables referenced - the profile tbl and the prm_breedgender tbl. Each gender is given an ID, each profile is then given a corresponding ID to signify their gender (male=1, female=2, etc.). The $profile variable is that which signifies the current profile being looked at.
<label for="profile-gender">Gender / Type:</label>
<select name="profile-gender">
<?php
$genderresult = mysql_query("SELECT prm_breedgender.BreedGender
FROM profiles, prm_breedgender
WHERE ProfileID = $profile
AND prm_breedgender.BreedGenderID = profiles.ProfileGenderID");
$row = mysql_fetch_array($genderresult);
echo '<option value="' . $row['BreedGenderID'] . '">' . $row[BreedGender] . '</option>';
$exgenderresult = mysql_query("SELECT prm_breedgender.BreedGender
FROM profiles, prm_breedgender
WHERE ProfileID = $profile
AND prm_breedgender.BreedGenderID != profiles.ProfileGenderID");
while ($row = mysql_fetch_array($exgenderresult)) {
echo '<option value="' . $row['BreedGenderID'] . '">' . $row[BreedGender] . '</option>';
}
?>
</select>
Any help would be greatly appreciated. I'm not all that experience (obviously!) so accompanying explainations would be fantastic.
You can combine it into a single query, using a computed 'order by' value:
SELECT ...
FROM ...
WHERE ...
ORDER BY (prm_breedgender.BreedGenderID = profiles.ProfileGenderID) DESC
The order by clause will evaluate to either true (you're on the record that should be first) or false (any other record), which gets cased to a value 1/0 which can be sorted, and by doing it in decreasing order, the true values (1) come out first.