I'm cobbling together a dropdown list that I would like to display the '[ve_venue]", "[ve_town]' from a MySQL query and, upon selection set a variable that I can use to pass the ve_ID on to an update query that adds a venue ID number to a separate table as a lookup key.
So far I've got some code that I've pieced together from various places and I can get it to display the town in a dropdown - I just need to add the venue field to the dropdown so I get "venue, town" in the list and I also need to be able to pass the ve_ID to a variable, say, $ve_ID so I can call it in some separate code (that will be on the same page in a separate include).
Here's what I've got so far....
<?
include('login_admin_sucess.php');
// Connects to your Database
include '../connect_include.php';
$query = "SELECT ve_ID, ve_venue, ve_town FROM `co_venues` ORDER BY ve_ID ";
$result = mysql_query($query);
while($r=mysql_fetch_array($result))
{
$ve_venue = $r['ve_venue'];
$result_array[$ve_venue][] = $r['ve_town'];
}
echo "<select name='x'>";
foreach($result_array as $v_venue => $value)
{
foreach($value as $title) {
echo "<option value='" . $v_venue . "'>" . $title . "</option>";
}
}
echo "</select>";
?>
I realise that mysql_ is deprecated...I'll work on that once I've got everything functioning.
You can concat the id and venue. For example:
$ve_venue = $r['ve_venue'] . "|" . $r['ve_ID'];
When you use the variable make a split with charapter "|" with preg_split function.
Related
I'm learning PHP at the moment, started a practice project to make a todo list.
The list allows the user to enter data into the database with an input field, and uses a while and foreach loop to display the data from the database.
I want to add a check-box to each item displayed that allows the user to check what items on the list they'd like to remove, and for the check-box to have a value corresponding to the id column of the item, then I'll add a submit button that will clear the checked items.
The database table I'm using has two columns an auto increment id column, and a description column.
Here's the loop:
<?php
$query = "SELECT * FROM list_data;";
$list = $mysqli->query($query);
while ($row = $list->fetch_array(MYSQLI_ASSOC)):
echo "<tr>";
foreach($row as $list_item) {
echo "<td>" . $list_item . "</td>";
}
echo "</tr>";
endwhile;
?>
I tried this:
foreach ($row as $id => $description) {
echo "<td>" . $id . $description . "</td>";
}
But for soeme reason this returns the column name, and the values like so:
id1 descriptionTodo List Item Number One.
id2 descriptionTodo List Item Number Two.
id3 descriptionTodo List Item Number Three.
id5 descriptionTodo List Item Number Four.
Can anyone set me on the right path?
I've got the project uploaded onto github if anyone wants to see the whole lot.
Thanks in advance for any help and suggestions.
I don't understand why you are using foreach actually , I would just do this :
<?php
$query = "SELECT * FROM list_data;";
$list = $mysqli->query($query);
while ($row = $list->fetch_array(MYSQLI_ASSOC)):
echo "<tr>";
echo"<td><input type='checkbox' value='".$row['id']."'</td> <td>".$row['description']."</td>";
echo "</tr>";
endwhile;
?>
I am trying to create a dropdown menu that will have as options certain fields recovered from a database using a for loop.
I am doing the following:
for ($article_id=1; $article_id <=30; $article_id++)
{
$sqltitles = "SELECT title FROM articles WHERE article_id='$article_id'";
$cursor = mysqli_query($db, $sqltitles) or die($sqltitles."<br/><br/>".mysql_error());
$row = mysqli_fetch_row($cursor);
$titles = $row[0];
echo $titles;
echo "</br>";
}
Which draws all the titles from the database and shows them one at a time.
Is there any way to make all those titles appear as options to a dropdown menu, so the user can select which one to read?
Something like this should work. I made a modification to how the query is working. If you specify article IDs for a range of articles in your query rather than just one specific ID, you should be able to execute only one query instead of one for each ID you want to retrieve. Regardless of whether or not you decide to use the approach I suggested, the syntax for creating the dropdown menu should be the same.
<select>
<?php
$sqltitles = "SELECT title FROM articles WHERE article_id BETWEEN 1 AND 30" ;
$cursor = mysqli_query($db, $sqltitles) or die($sqltitles."<br/><br/>".mysql_error());
while ($row = mysqli_fetch_row($cursor)) {
echo '<option value ="' . $row[0] . '">' . $row[0] . '</option>';
}
?>
</select>
Here is a good reference for how to create HTML <select> tags.
try something along the lines of...
echo '<select name="dropdownname">';
foreach ($titles as $key => $val) {
echo('<option value="' . $val .'">' . $val . '</option>');
}
echo '</select>';
//DB CONNECTION
$sql = "SELECT `city`,`country` from infotab";
$result = $conn->query($sql);
while ($row = $result->fetch_assoc()) {
echo $row["city"].$row["country"]"<a href='order.php'>order</a>"; }
Table output:
This code will select data. Additionally, there is reference to order.php on every line. When the user clicks on reference( <a href> clause), it opens order.php and there I need to know which row the user selected to work with these data.
Change the code to:
while ($row = $result->fetch_assoc()) {
echo $row["city"] . $row["country"] . "<a href='order.php?city=" . $row["city"] . "&country=" . $row["country"] . "'>order</a>";
}
In order.php you can then access these values by using the $_GET["city"] and $_GET["country"] variables which contain the values from your <a href> link on the previous page. For example, running echo $_GET["city"]; will output the city name.
Edit: As #Rizier123 pointed out, using a unique ID might be more prone to errors in case your database contains more than one entry for the same city or country. You should consider introducing an ID in your table structure and then using that in the link to order.php.
I'm in need of a bit of help >_<. I have two tables:
The first table which populated with car manufacturers called list_vehicle_marks with the columns
id | value
The second table, populated with car models called list_vehicle_models and has the following columns;
id | value | mark_id
The column mark_id has the same value as id in the first table, in other words, Ford has the id no. 37 and all Ford models have the mark_id as 37 too.
How would I create two dynamic dropdown menus, where the second menu shows only the models based on the manufacturer selected in the first menu?
This is what I have done so far:
<?php
connection to host, db, blah blah
if ($db->connect_error) {
echo "Failed to connect to MySQL: (" . $db->connect_error . ") "
. $db->connect_error;
} else {
$sql = "SELECT value FROM list_vehicle_marks";
$result_db = $db->query($sql);
if (!$result_db) {
echo $db->error . ' Error perform query!';
} else {
echo '<select name="value">';
echo '<option value="">Select...</option>';
while ($row = $result_db->fetch_object()) {
echo '<option value="' . $row->value . '">';
echo $row->value;
echo '</option>';
}
echo '</select>';
}
}
$db->close();
?>
In the code you posted, the drop down that you create, it should be something like this
echo '<option value="' . $row->id. '">';
echo $row->value;
echo '</option>';
Notice the $row->id instead of $row->value
To Populate the corresponding dropdown you have two options:
Once a user selects a value from the marks drop down you trigger a PHP function which gets the values of the corresponding vehicle models and re-render the page (You capture the id selected by the user using javascript)
You fetch all the vehicle models along with vehicle marks (add another query to your above code that fetches all the models), keep the result of the models query in a JavaScript variable and generate the second dropdown using javascript once a user makes a selection. Here too you capture the users selection and call a javascript function that spits out a corresponding id drop down.
The second method would not be a good option if your models table has a lot of data.
I have a form on a html page which allows me to select multiple items. The name of the form is name="region[]
It posts to another page where I use this code:
The region(s) selected:
<?php
$values = $_POST['region'];
foreach ($values as $region){
echo $region;
}
?>
This will display the results of the form perfectly; if there is one value it will print the one value, but if there is more than one then it will print them all.
I need to use the results of this in a query:
<?php
$con=mysqli_connect("****","****","****","****");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT
GoogleBusinessData.BusName,
GoogleBusinessData.BusAddress,
PostcodeTbl.PostcodeFirstTown,
PostcodeTbl.PostcodeFirstArea,
PostcodeTbl.Region,
PostcodeTbl.Postcode,
PostcodeTbl.PostcodeFirstLetters,
PostcodeTbl.PostcodeFirstPart,
PostcodeTbl.Country,
GoogleBusinessData.BusPhone,
GoogleBusinessData.BusCats,
GoogleBusinessData.FaroukCat,
GoogleBusinessData.BusWebsite,
GoogleBusinessData.BusDescription,
GoogleBusinessData.BusGoogleBusinessID,
GoogleBusinessData.BusPageURL,
EmailTable.EmailNumberOfEmails,
EmailTable.EmailAddresses
FROM
GoogleBusinessData
INNER JOIN PostcodeTbl ON GoogleBusinessData.BusPostalCode = PostcodeTbl.Postcode
INNER JOIN EmailTable ON GoogleBusinessData.BusWebsite = EmailTable.EmailWebsite
WHERE EmailTable.EmailNumberOfEmails > 0 AND
GoogleBusinessData.FaroukCat = 'Wedding Planner'
GROUP BY
GoogleBusinessData.BusWebsite
ORDER BY
GoogleBusinessData.BusName ASC
LIMIT 0,20");
while($row = mysqli_fetch_array($result))
{
echo $row['BusName'] . " - " . $row['PostcodeFirstTown'] . " - " . $row['PostcodeFirstArea'] . " - " . $row['Region'] . " - " . $row['Postcode'];
echo "<br>";
}
mysqli_close($con);
?>
So I need to add the condition in the WHERE to only return the results if it contains one of the regions with the form. I tried the following with no joy:
WHERE PostcodeTbl.Region IN ('$region') AND
EmailTable.EmailNumberOfEmails > 0 AND
GoogleBusinessData.FaroukCat = 'Wedding Planner'
But this only returns the last selection (as if there were only one selected).
Can anyone help?
In your first script, you are looping through the items. Each item is put into the variable $region one by one. So after the loop, $region contains the last item. If you construct the where clause at that time, it explains why the query only returns the last item.
To fix this, you'll have to construct a variable (e.g. $regions) that contains a list of regions.
For instance:
$regions = "'" . implode($_POST['region'], "','") . "'";
... WHERE PostcodeTbl.Region IN ('$regions') AND ...
Note that this is potentially unsafe, since the input in $_POST is not validated, so it is better to loop through the array (like you did) and validate each item one by one while constructing the string in $regions. You can use mysqli_real_escape_string for that.
Alternatively, and arguably better, is to use the parameter binding capabilities of mysqli. This is a bit tricky with array variables, but the details on how to do that are already described in this question.