I have a dropdown on a page. I wish to publish the selected option as a link to another web page. If a user selects Kenya in the code below, for example, the value is stored in a database and then the record is retrieved and published as a link to a certain page. See code below:
HTML:
<select name="country" id="country">
<option value="kenya">Kenya</option>
<option value="uganda">Uganda</option>
</select>
PHP/SQL
//Data saving
$user_country = $_POST['country']);
$sql = "INSERT INTO countries (country_name)
VALUES ($user_country)";
//publishing the saved data as a link
$sql = "SELECT user_country FROM countrydb";
$result = $conn->query($sql);
if ($result->num_rows > 0) && ($row["user_country"] = "kenya") {
while($row = $result->fetch_assoc()) {
echo ' Kenya';
}
}
elseif ($result->num_rows > 0) && ($row["user_country"] = "uganda") {
while($row = $result->fetch_assoc()) {
echo 'Uganda';
}
}
else {
echo "no results";
}
It's not clear what the purpose of the database table is in this example - the data stored in it is just recording what the user selected, which PHP already knows, and doesn't seem to add any value. If I've misunderstood the purpose of it, you would need to clarify the question.
And also, if you have many countries in the list, hard-coding the if statements to generate the hard-coded links is going to get very tedious.
There are various ways you could improve this depending on exactly how you want it to work, but if you want to generate links, do it dynamically based on the user's selection, for example:
$user_country = $_POST['country']);
echo ''.$user_country.'';
To take it a step further, rather than having pre-defined pages for each country, (again depending on your exact requirements) you could possibly have a single page which just takes the country name as an input, and then retrieves information about that country from the database. You could also potentially make it redirect to the page immediately after the user has chosen that page in the dropdown, rather than generating a link which they then have to click on again to make the same selection. Those are some things to think about, according to how you want the application to work.
I've written a PHP script that can populate a table in a particular way so that multiple events (or no events) can be put in one square in an HTML - similar to the layout a calendar would have. But, there's a problem, the while statement I created to fill in squares in the table when there is no data doesn't detect when there is data, and fills the entire table with empty squares. This is what the output looks like (The page is styled using Bootstrap 3). From the mysql data I have provided, these events should be in the square at {Period 1, Monday}.
Here is my data in a mysql database; mysql data
Here is a snippet of the part of the page related to this table;
<?php
$query = "SELECT * FROM configtimetabletwo WHERE term = ".$term." AND week = ".$week." ORDER BY period, day LIMIT 100;";
$results = mysqli_query($conn, $query);
$pp=1; //The current y value of the table
$pd=0; //The current x value of the table
echo '<tr><td>';
while($row = mysqli_fetch_row($results)) {
while((pd!=$row[3] or $pp!=$row[4]) and $pp<6){ //This while statement fills in empty squares and numbers each row.
if($pd==0) {
echo $pp."</td><td>";
$pd++;
}
elseif($pd<5){
echo "</td><td>";
$pd++;
}
else {
echo "</td></tr><tr><td>";
$pd=0;
$pp++;
}
}
echo '<a href="?edit='.$row[0].'" class="label label-default">';
echo $row[5].' '.$row[6].' - '.$row[7]."</a><br>";
}
echo "</td></tr></table>"
?>
I haven't been able to figure out why this happens so far, thanks in advance to anyone who has any idea what's going on.
In the comments below my question, pavlovich pointed out the error. In this case, it was simply an issue of forgetting to use a $ to reference a variable. It would seem that this doesn't throw an error in a while statement like it would elsewhere.
I am attempting to write a page with 2 drop down lists. THe first populating a list of tables from mysql db allowing the user to choose the date in question, the second allowing the user to choose a name from a column in that table. I currently have the form written like below to generate an array of values from the specific table/row specified by the user which I can then echo out in to various fields in the html form.
I am struggling with getting the drop down list populated by the table names. Can anyone assist please? Sorry for the long post it's my first and I wanted to be thourough. I know that the connection to my database is working and the second drop down list is working when I specify in the code what table to use.
I need the first box to list the table names and then set the variable $date.
This is the drop down list for the date:
<select name='Date'>
<?php
$date = $_POST['date'];
$cxn=mysqli_connect("localhost",$userid,$password,$db)
or die ("Could not connect to Database");
$result=mysqli_query($cxn,"Show tables from january");
while($row = mysqli_fetch_array($result, MYSQL_ASSOC))
{
echo "<option value='".$row['Value']."'>".$row['Value']."</option>";
}
?>
</select>
This is the drop down list for the names from the table selected above.
<select name='agentname'>
<?php
$name = $_POST['agentname'];
$cxn=mysqli_connect("localhost",$userid,$password,$db)
or die ("Could not connect to Database");
$sql=mysqli_query($cxn,"SELECT Value FROM january.'$date'");
while($row = mysqli_fetch_assoc($sql))
{
echo $value ="<option value='".$row['Value']."'>".$row['Value']."</option>";
}
?>
</select>
<input type='submit' value='Get Dashboard' />
</form>
Creating the form :
<form action='getlist.php' method='post'>
<div align="center">
<p>
<select name='Date'>
<?php
$date = $_POST['date'];
$cxn=mysqli_connect("localhost",$userid,$password,$db)
or die ("Could not connect to Database");
$result=mysqli_query($cxn,"Show tables from january");
while($row = mysqli_fetch_array($result, MYSQL_ASSOC))
{
echo "<option value='".$row['Value']."'>".$row['Value']."</option>";
}
?>
</select>
<select name='agentname'>
<?php
$name = $_POST['agentname'];
$cxn=mysqli_connect("localhost",$userid,$password,$db)
or die ("Could not connect to Database");
$sql=mysqli_query($cxn,"SELECT Value FROM january.'$date'"); //I have specified schema's in my DB for the different months
while($row = mysqli_fetch_assoc($sql))
{
echo $value ="<option value='".$row['Value']."'>".$row['Value']."</option>";
}
?>
</select>
<input type='submit' value='Get Dashboard' />
</form>
TO be used in the following query to generate the array:
<?php
$cxn=mysqli_connect("localhost",$userid,$password,$db)
or die ("Could not connect to Database");
$result=mysqli_query($cxn,"SELECT column1, column2, column3 //just listing examples here for my columns there is about 45 columns for this selection
FROM january.'$date'
WHERE Value='$name'");
while($row = mysqli_fetch_array($result))
{
$stats = $row;
}
?>
Alright...
To address your actual question, I think the problem is the index you're referencing in the output of mysqli_fetch_assoc. Generally, the table names of a "SHOW" query are keyed like "Tables_in_databasename", or something like that. You might print_r your $row variable to see what index you should reference.
Now, unrelated to your questions, I do have to insist you rethink how you're storing your data. It is very bad procedure to have meaningful data represented in the names of your databases and tables. Not only is this not the way databases are meant to be used, which will inevitably cause problems for you, you're also making things unnecessarily hard for you.
I'm not sure what kind of statistics you're storing, but let's say it's something like clicks per day for a website. This data can be very easily stored in one database, and one table. For example:
Database: statistics
Table: daily_statistics
Columns: id, stat_date, value
Now you can store the clicks-per-day easily, and in one table.
id, stat_date, value
1, 2014-01-01, 300
2, 2014-01-02, 400
etc...
Let's say that you have multiple values per day. Not a problem
id, stat_date, value
1, 2014-01-01, 300
2, 2014-01-02, 400
3, 2014-01-02, 250
etc...
// The unique ID, alows multiple records per day.
// If you need them categorized in a different way,
// add another column.
This structure will work for years and year to come.
id, stat_date, value
1, 2014-01-01, 300
2, 2014-01-02, 400
3, 2014-01-02, 250
4, 2015-01-01, 250
5, 2016-01-01, 250
etc...
Now you have access to mysql aggregating functions,you have a situation where it will be easier to import and export data, you won't have to deal with having 100s of databases and even more tables as the years go on, and any developer who comes after you won't have to pull their hair out trying to work with this sticky situation.
needing some advice.
I am wanting to include 4 drop down lists on a website, which all contain data from different fields in a mysql table.
I then want to be able to press a submit button and display the required data on a webpage.
I am having trouble with knowing what programming language to use and also finding it difficult to find any tutorials. Any help would be greatly appreciated.
Thanks
You mean a HTML dropdown list or just a select dropdown in a form?
If you mean a select dropdown in a form you could do something like this:
<?PHP
$query = mysql_query("SELECT value FROM table ORDER BY value ASC") or die(mysql_error());
$result = mysql_num_rows($result);
// If no results have been found or when table is empty?
if ($result == 0) {
echo 'No results have been found.';
} else {
// Display form
echo '<form name="form" method="post" action="your_result.php">';
echo '<select name="list" id="lists">';
// Fetch results from database and list in the select box
while ($fetch = mysql_fetch_assoc($query)) {
echo '<option id="'.$fetch['value'].'">'.$fetch['value'].'</option>';
}
echo '</select>';
echo '</form>';
}
?>
And then in your_result.php you should fetch the data from your MySQL database based on value from the (when you fetch use mysql_real_escape_string):
<?PHP $_POST['list']; ?>
You could also do everything in just one file, but thats up to you. Try to use google, there are dozens of tutorials out there.
I am new to php, mysql. currently i'm building a website. with the search query the data is displayed well. (in this case the search query is 'name %like%'). now i want to open another page that will display the profile of any one of the name displayed (when clicked) in the previous query. how to acheive this?
you'll have to select, for example, user_id when searching.
and then, your code could look like
<?php
include("db.php");
$result = mysql_query("SELECT user_id, username FROM user WHERE username LIKE '%".mysql_real_escape_string($_POST['username'])."%'");
echo "<table>";
while($row = mysql_fetch_assoc($result)
{
echo '<tr><td>'.$row['username'].'</td></tr>';
}
echo "</Table>";