Dynamically pull from a database depending on dropdown result - sql

I am currently pulling the information I need from my database but would like the information being displayed to change dependant on the dropdown result. I have done some research and think the best way to do this will be using a form but am not sure how it will work.
Here is the code I have below:
<section id="compare">
<select>
<option>Select Gym</option>
<option>fitness first</option>
<option>anytime fitness</option>
<option>the gym</option>
<option>its leisure ltd</option>
<option>the armoury</option>
</select>
<select>
<option>Select Gym</option>
<option>fitness first</option>
<option>anytime fitness</option>
<option>the gym</option>
<option>its leisure ltd</option>
<option>the armoury</option>
</select>
<section id="left">
<?php
mysql_select_db("gyms", $con);
$result = mysql_query("SELECT * FROM gym WHERE id='1'") or die ('Error: '.mysql_error ());
while($row = mysql_fetch_array($result)){
echo "<h1>" . $row['name'] . "</h1>";
echo "<p><h6>type</h6>" . $row['type'] . "</p>";
echo "<p><h6>price</h6>" . $row['price'] . "</p>";
echo "<p><h6>hours</h6>" . $row['hours'] . "</p>";
echo "<p><h6>parking</h6>" . $row['parking'] . "</p>";
echo "<p><h6>facilities</h6>" . $row['facilities'] . "</p>";
}
?>
</section>
<section id="right">
<?php
$result = mysql_query("SELECT * FROM gym WHERE id='2'") or die ('Error: '.mysql_error ());
while($row = mysql_fetch_array($result2)){
echo "<h1>" . $row['name'] . "</h1>";
echo "<p><h6>type</h6>" . $row['type'] . "</p>";
echo "<p><h6>price</h6>" . $row['price'] . "</p>";
echo "<p><h6>hours</h6>" . $row['hours'] . "</p>";
echo "<p><h6>parking</h6>" . $row['parking'] . "</p>";
echo "<p><h6>facilities</h6>" . $row['facilities'] . "</p>";
}
mysql_close($con);
?>
</section>
</section>

You're absolutely right, you need to use a form implementing the POST method which posts the selected value in the form, matching the 'id' field included in your database.
Here is an example:
<form action="compare.php" method="post">
<select name="gyms-1">
<option value="0">Select Gym</option>
<option value="1">fitness first</option>
</select>
<select name="gyms-2">
<option value="0">Select Gym</option>
<option value="1">fitness first</option>
</select>
<input name="send" id="send" type="submit" value="compare" />
</form>
Then here is the PHP code which implements the post method:
<?php
$gyms=$_POST['gyms-1'];
$jimmy=$_POST['gyms-2'];
mysql_select_db("gyms", $con);
$result = mysql_query("SELECT * FROM gym WHERE id='$gyms'") or die ('Error: '.mysql_error ());
while($row = mysql_fetch_array($result)){
echo "<h1>" . $row['COLUMN NAME'] . "</h1>";
echo "<p><h6>type</h6>" . $row['COLUMN NAME'] . "</p>";
}
$result2 = mysql_query("SELECT * FROM gym WHERE id='$jimmy'") or die ('Error: '.mysql_error ());
while($row = mysql_fetch_array($result2)){
echo "<h1>" . $row['COLUMN NAME'] . "</h1>";
echo "<p><h6>type</h6>" . $row['COLUMN NAME'] . "</p>";
}
mysql_close($con);
?>
This should allow you to display multiple rows in your database depending on the value selected by the user. Any problems give me a shout!

User jquery .change. For example along the way of:
$("#left").change(function(){
$("#right").html("[post your data]");
});

Related

How To add Two Variables To An Ahref

i have two variables
one is $wallet
the second is
$link
i liked to make something like that:
<div>hi' <?php echo $userRow['user_wallet']; ?> Check Your Bitcoins </div>
How Can I Do This?
i finally i tank so i discover how to do..
$user_points = $row['user_points'];
$wallet = $row['user_wallet'];
$link = "https://faucetbox.com/check/";;
$resultlink = $link.$wallet;
<a href="<?php echo "$link"; ?><?php echo $userRow['user_wallet']; ?>" >Check Your Adress</a>

Create multiple variable from foreach loop

I have this script that gets weather data from a API.
But I need to have the temperatures of every day in seperate strings.
$day1Max, $day1Min, $day2Max, $day2Min, etc...
Is it possible to get these variables from a foreach loop?
<?php
$conditions_week = $forecast->getForecastWeek($latitude, $longitude);
echo "Forecast this week:\n";
foreach($conditions_week as $conditions) {
?>
<tr>
<th><?php echo $conditions->getTime('Y-m-d') . ': ' . $conditions->getMaxTemperature() . "\n"; ?></th>
<th><?php echo $conditions->getTime('Y-m-d') . ': ' . $conditions->getMinTemperature() . "\n"; ?></th>
</tr>
<?php
}
?>
This is the output:
Here is your solution :
<?php
class Conditions{
function __construct($min,$max){
$this->max = $max;
$this->min = $min;
}
function getMaxTemperature(){
return $this->max;
}
function getMinTemperature(){
return $this->min;
}
}
$conditions_week = array(new Conditions(5,10),new Conditions(-5,3));
echo "Forecast this week:\n";
foreach($conditions_week as $k=>$conditions) {
$varMin = "day".($k+1)."min";
$varMax = "day".($k+1)."max";
$$varMin = $conditions->getMinTemperature();
$$varMax = $conditions->getMaxTemperature();
?>
<tr>
<th><?php echo "Max : ". $conditions->getMaxTemperature() . "\n"; ?></th>
<th><?php echo "Min : ". $conditions->getMinTemperature() . "\n"; ?></th>
</tr>
<?php
}
echo "day1min : " . $day1min;
echo '<br>';
echo "day1max : " . $day1max;
echo "<hr>";
echo "day2min : " . $day2min;
echo '<br>';
echo "day2max : " . $day2max;
You to generate your php variables $day1min, $day1max... inside the foreach and then you can use it outside the loop.
Here's the full example : https://3v4l.org/vX5bW

How can I connect my php code to MySQL database?

How can I connect my php code to MySQL database?
I have tried many times and it cannot access the database.
These are the codes in my php file:
<html>
<head>
<title>Brandon's Search Engine - Results</title>
</head>
<body>
<h2>Brandon's Search Engine</h2>
<form action='./search.php' method='get'>
<input type='text' name='k' size='80' value='<?php echo $_GET['k']; ?>' />
<input type='submit' value='Search' >
</form>
<hr />
<?php
$k = $_GET['k'];
$terms = explode(" ", $k);
$query = "SELECT * FROM search WHERE ";
foreach ($terms as $each){
$i++;
if ($i == 1)
$query .= "keywords LIKE '%$each%' ";
else
$query .= "OR keywords LIKE '%$each%' ";
}
// connect
mysql_connect("sql102.freezoy.com", "frzoy_13854016", "xxxx");
mysql_select_db("frzoy_13854016_search");
$query = mysql_query($query);
$numrows = $query->num_rows;
if ($numrows > 0){
while ($row = mysql_fetch_assoc($query)){
$id = $row('id');
$title = $row('title');
$description = $row('description');
$keywords = $row('keywords');
$link = $row('link');
echo "<h2><a href='$link'>$title</a></h2>
$description<br /><br />";
}
}
else
echo"No results found for \"<b>$k</b>\"";
// disconnect
mysql_close();
?>
</body>
</html>
I have insert some data into the database.
Please help. Thanks in advance.

Get Drop Down Menu, PHP SQL HTML

I am a student and also working in my university. I am not studying computer science.
I mainly do support stuff, but now I should take care of our internship database.
Task: Create a drop down menu from a town which a student choses, then show all internships in this town.
I have an existing SQL database, can create a drop down menu from it, but have NO idea how I can use the selected value. A submit button would be fine, any solution which works is fine.
How can I put the chosen value from drop down menu to a PHP variable?
<?php
$dbanfrage = "SELECT DISTINCT Stadt FROM $tabelle WHERE Land = 'China' OR Land = 'Taiwan' OR Land = 'VR China' ORDER BY STADT ";
$result = mysql_db_query ($dbname, $dbanfrage, $dbverbindung);
if (!$result)
{
$message = 'Ungültige Abfrage: ' . mysql_error() . "\n";
$message .= 'Gesamte Abfrage: ' . $dbanfrage;
die($message);
}
echo "<select>";
echo "<option>Stadt auswählen</option>"; // printing the list box select command
while($nt = mysql_fetch_array($result)) //Array or records stored in $nt
{
echo "<option value = $nt[id]>$nt[Stadt]</option>"; /* Option values are added by looping through the array */
}
echo "</select>"; // Closing of list box
mysql_close ($dbverbindung);
?>
Just suggesting how you can do it --
<select name="country" id="country" onChange="showState();" >
<option value="">Select Country</option>
<option value="1">India</option>
<option value="2">Nepal</option>
</select>
Your Script--
<script type="text/javascript">
function showState( )
{
var value = document.getElementById('country').value;
var url = '<?php echo base_url ?>showstate.php';
$.ajax({
type: "GET",
url: url,
data:{'country':value},
success:function(results)
{
$('#div_state').html(results);
}
});
}
</script>
Your showstate.php php page --
//INCLUDE CONNECTION file to for db query
$type = $_GET['country'];
//Your DB QUERIES
This will load the content in your #div_state div. You can also use Jquery For this..i think this will help you :) an d let me know if you have any query.
<select name="name">
<option value="select">select</option>
<?php
$dbanfragee=mysql_query("SELECT DISTINCT Stadt FROM $tabelle WHERE Land = 'China' OR Land = 'Taiwan' OR Land = 'VR China' ORDER BY STADT ");
while($row=mysql_fetch_array($$dbanfrage)){?>
<option value="<?=$row['id']?>"><?=$row['stadt']?></option>
<?php }?>
</select>

How to get include preg_match_all results in header location?

I have the following function written in function.php in a wordpress theme
function get_mms($my_post) {
$post_id = $my_post; //$_GET["p"];
$queried_post = get_post($post_id);
$title = $queried_post->post_title;
preg_match_all('#\[mms\](.+)\[\/mms\]#', $queried_post->post_content, $matches);
echo 'EN: ' . $matches[1][0];
echo '<br><br><form action= " ' . get_permalink( $id ) . ' " method="post">
Numar: <input type="text" name="fname" />
<input name="submito" type="submit" />
</form>';
$numero = $_POST["fname"];
if(isset($_POST['submito'])&& $_POST['fname']){
$numero = $_POST["fname"];
header("Location: http://server/&to=$numero&text='.$matches[1][0].'&from=Moldcell");
}
When I submit the form instead of getting the value of $matches[1][0] I get Array[0].
Is there someting I did wrong? What can I do to make it grab the value of preg_match_all results?