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
Related
$result = $vt->prepare('SELECT * FROM streamdetails WHERE idFile ='.$idFile.' AND iStreamType =0');
$result->execute();
foreach($result as $row)
echo "Çözünürlük: ".$row['iVideoWidth'] . " x " .$row['iVideoHeight'] . "<br>";
}
What is wrong?
$idFile variable type is text but idFile is integer on streamdetails.
I have the script I use that displays things from a table in my database, but it keeps giving me this error:
Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\raynerblogger1\posts.php on line 21
someone please help
$sql = "SELECT * FROM blogs;";
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_assoc($result)) {
echo "<div id='message'>";
echo $row['author']; echo(':') . "<br>";
echo $row['post'] . "</div>" . "<br>" . "<br>" . "<br>";
}
Try this, I edited line 1 and 6:
$sql = "SELECT * FROM blogs";
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_assoc($result)) {
echo "<div id='message'>";
echo $row['author'].":". "<br>";
echo $row['post'] . "</div>" . "<br>" . "<br>" . "<br>";
}
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]");
});
I know that this question has been asked before but I am finding it difficult to apply the principle to sort out my issue. I have 2 tables that I want to select data from.
I have tried using the inner join sql query below:
$query1 = "SELECT `entries.id`, `code_id`, FROM `entries` INNER JOIN `codes` ON `code_id` = `codes.id`";
$result = mysql_query($query1);
And My display is as follows:
while($row = mysql_fetch_array( $result ))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['msisdn'] . "</td>";
echo "<td>" . $row['created'] . "</td>";
echo "<td>" . $row['code_id'] . "</td>";
echo "</tr>";
}
I am trying to get the actual code from the table codes, and display it to a website, but I keep getting the id(number) instead of actual code.
Can someone please assist. I have checked around the internet and even got some ebooks, but I am still a newbie and would like someone to guide me through the process. All input appreciated.
You have to assign the fields a new name if there are fields with the same name in different tables like "id":
$query1 = "SELECT `entries.id` as entriesid, entries.name, entries.msisdn, entries.created, `codes.id` as codesid, FROM `entries` INNER JOIN `codes` ON `entries.id` = `codes.id`";
$result = mysql_query($query1);
...
echo "<td>" . $row['entriesid'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['msisdn'] . "</td>";
echo "<td>" . $row['created'] . "</td>";
echo "<td>" . $row['codesid'] . "</td>";
You can do the following:
$sql = mysql_query("select entries.* , codes.* from entries,codes where entries.code_id=codes.id");
while($fetch = mysql_fetch_array($sql)) {
// Do Your Display
}
Hope this helps . Also mysql functions are degrading. So change that to mysqli / PDO
Having real trouble getting my head around multiple joins. Can anyone tell me why this is wrong please?
*EDIT*
$query = db_select('node', 'n');
$query->join('file_usage', 'fu', 'fu.id = n.nid');
$query->join('field_data_field_featured_image', 'ffimage', 'ffimage.entity_id = n.nid');
$query->join('file_managed', 'fm', 'fm.fid = n.nid');
$query
->fields('fu', array('id'))
->fields('fm', array('filename'))
->fields('ffimage', array('field_featured_image_title'))
->fields('n', array('nid', 'title'))
->condition('n.type', 'work')
->orderby('n.nid', 'DESC')
->range(0, 20);
$result = $query->execute();
foreach ($result as $record) {
echo '<li>';
echo '<div class="home_text"><h1>'.$record->title.'</h1></div>';
echo '<a href="...some_node/';
echo $record->id;
echo '">';
echo '<img src="...some_files/';
echo $record->filename;
echo '" width="960px" height="615px" />';
echo '</a>';
echo '</li>';
}