With the following code, I would like to plot the image sun.png when sky is clear, cloud when the sky is cloudy and variable in other cases...but something fails...I always get the image variable.png
<?php
if($sky == "clear" ) {
echo '<img src="images/sun.png" width="40">';
}
elseif ($sky == "cloudy" ){
echo '<img src="images/cloud.png" width="40">';
}
else {
echo '<img src="images/variable.png" width="40">';
}
?>
I consult the database using this code #Jack Goodman
$data_query = mysqli_query($conexionbd,'select * from `weather` where `data` = "2017-03-22" and (`num` = "1" or `num` = "2" or `num` = "3")');
while($data = mysqli_fetch_assoc($data_query)){ ?>
At the end I have solved it, my code had a mistake, the right code is
<?php
if($data['sky'] == "clear" ) {
echo '<img src="images/sun.png" width="40">';
}
elseif ($data['sky'] == "cloudy" ){
echo '<img src="images/cloud.png" width="40">';
}
else {
echo '<img src="images/variable.png" width="40">';
}
?>
Your problem is somewhere in the variable sky. I downloaded pictures and tested it with this code by changing the variable's value and it all works fine.
$sky = "cloudy";
if($sky == "clear" )
{
echo '<img src="sun.jpg" width="40">';
}
elseif ($sky == "cloudy" )
{
echo '<img src="cloud.jpg" width="40">';
}
else
{
echo '<img src="variable.png" width="40">';
}
Show me the code where you get the sky's value please.
Might be clearer using a switch statement, then you could have a catch-all for unmet conditions and use it to debug your issue:
switch($sky){
case 'clear':
echo '<img src="images/sun.png" width="40">';
break;
case 'cloudy':
echo '<img src="images/cloud.png" width="40">';
break;
default:
echo '$sky is something unknown';
var_dump($sky);
break;
}
Related
I am trying to display a different image on my page depending on who the Wordpress author of the post is.
So far I have tried a few scripts but none of them work. Any help is greatly appreciated. Here is what I am trying to do.
<?php $author = get_the_author(); ?>
<?php
if ( $author('author1') ) {
echo '
<img src="">;
'
} elseif ( $author('author2') ) {
echo '
<img src="">;
'
} else {
// if neither, echo something else
}
?>
The get_the_author() function return the author's display name as a string. So you have to simply compare the result of get_the_author(). There is no array or object as return value.
So I would go with the following solution using a switch instead of if:
<?php $author = get_the_author(); ?>
<?php
switch($author) {
case 'author1':
echo '<img src="">';
break;
case 'auhtor2':
echo '<img src="">';
break;
default:
// if neither, echo something else
}
?>
In case you want to use the if statement you can use the following:
<?php $author = get_the_author(); ?>
<?php
if ($author === 'author1') {
echo '<img src="">';
} elseif ($author === 'author2') {
echo '<img src="">';
} else {
// if neither, echo something else
}
?>
I have two websites, each with it's own domain name. On load I have execute a php file that is used on both sites and has the same functionality.
Here is the code of that file:
<?php
echo '1';
if ($_SESSION["template"]=="template1";)
{
echo '2';
if ($_REQUEST["cmdAction"]=='A')
echo file_get_contents('http://localhost/images/template1/a.php');
else if ($_REQUEST["cmdAction"]=='B')
echo file_get_contents('http://localhost/images/template1/b.php');
else if ($_REQUEST["cmdAction"]=='C')
echo file_get_contents('http://localhost/images/template1/c.php');
}
else if ($_SESSION["template"]=="template2";)
{
echo '3';
if ($_REQUEST["cmdAction"]=='A')
echo file_get_contents('http://localhost/images/template2/a.php');
else if ($_REQUEST["cmdAction"]=='B')
echo file_get_contents('http://localhost/images/template2/b.php');
else if ($_REQUEST["cmdAction"]=='C')
echo file_get_contents('http://localhost/images/template2/c.php');
}
else {
echo 'NO DATA';
}
echo '4';
?>
On each of the two sites I set a session variable but in the above code it doesn't seem to work as I expect it to.
Am i missing something?
remove semicolon from if() and else if() statement, also add brackets when you using nested if else because it makes someone to understand easier and looks better
<?php
echo '1';
if ($_SESSION["template"]=="template1")
{
echo '2';
if ($_REQUEST["cmdAction"]=='A')
{
echo file_get_contents('http://localhost/images/template1/a.php');
}
else if ($_REQUEST["cmdAction"]=='B')
{
echo file_get_contents('http://localhost/images/template1/b.php');
}
else if { ($_REQUEST["cmdAction"]=='C')
{
echo file_get_contents('http://localhost/images/template1/c.php');
}
}
else if ($_SESSION["template"]=="template2")
{
echo '3';
if ($_REQUEST["cmdAction"]=='A')
{
echo file_get_contents('http://localhost/images/template2/a.php');
}
else if ($_REQUEST["cmdAction"]=='B')
{
echo file_get_contents('http://localhost/images/template2/b.php');
}
else if ($_REQUEST["cmdAction"]=='C')
{
echo file_get_contents('http://localhost/images/template2/c.php');
}
}
else {
echo 'NO DATA';
}
echo '4';
?>
After reviewing your code I edited my answer. The below code will do exactly the same as your code but requires alot less code.
<?php
if (isset($_SESSION['template']) && isset($_REQUEST['cmdAction'])) {
echo file_get_contents('http://localhost/images/'.$_SESSION['template'].'/'.strtolower($_REQUEST['cmdAction']).'.php');
} else {
echo 'NO DATA';
}
?>
So basically what am trying to achieve is the following.
I am trying to make it so the following script does something in this instance:
If $something == "0" then $something1 == "no"
If $something == "1" then $something1 == "yes"
else echo "Error."
That is how I would explain what Im trying to do.
This is my current code:
<?php
if(isset($_POST['resolve'])){
$api = "http://test.com/php/";
if(strlen($_POST['name'])==0){
echo "fill in all fields!";
} else {
$response = file_get_contents($api.$_POST['name']);
$array = unserialize($response);
?>
<div align="center"><?php echo "".$array['something1']; ?></div>
<?php
}
}
?>
I would like it to echo "no" if the result of array "something" is "0" and echo "yes" if the result of array "something" is "1".
<?php
if($array['something'] == '0'){echo 'No';}
elseif($array['something'] == '1'){ echo 'Yes';}
else{ echo 'Error!'; }
?>
switch case is the most elegant way to go here:
switch($array['something']) {
case 0: echo 'No';break;
case 1: echo 'Yes';break;
default: echo 'Error.';
}
<?php
if(isset($_POST['resolve'])) {
$api = "http://test.com/php/";
if(!$_POST['name']) {
echo "Please, fill in all fields!";
} else {
$response = file_get_contents($api.$_POST['name']);
$array = unserialize($response);
echo "<div align='center'>";
if($array['something'] == '0') {
echo 'No';
}
elseif($array['something'] == '1') {
echo 'Yes';
}
else {
echo 'Error.';
}
echo "</div>";
}
}
?>
Don't forget to also do a security input check on $_POST['name']
Voila
echo $array['something1'] ? "Yes" : "No";
This sets $array['something1'] to either 'yes' or 'no' depending on the value of $array['something'].
<?php
if(isset($_POST['resolve'])){
$api = "http://test.com/php/";
if(strlen($_POST['name'])==0){
echo "fill in all fields!";
} else {
$response = file_get_contents($api.$_POST['name']);
$array = unserialize($response);
$array['something1'] = $array['something'] == 0 ? 'no' : 'yes';
?>
<div align="center"><?php echo "".$array['something1']; ?></div>
<?php
}
}
$yesno = ['No', 'Yes'];
$something1 = $yesno[$something];
That is the simplest way I know of doing it.
So I have a code I want to use
mysql_select_db("website", $con);
$result=mysql_query("SELECT * FROM characters where online='1'");
while ($row=
mysql_fetch_array($result))
{
echo $row['name'];
}
if ($row['race'] == "1");
{
echo '<img src="img/8-0.gif" />';
}
if ($row['class'] == "3");
{
echo '<img src="img/3.gif" ?>';
}
mysql_close($con);
?>
Now I only wanted the two images to show if the online field was 1, but they show no matter what. Does anyone know how I can fix this? Thanks.
For the sake of the argument:
mysql_select_db("website", $con);
$result=mysql_query("SELECT * FROM characters where online='1'");
while ($row= mysql_fetch_array($result))
{
echo $row['name'];
//} This bracket would immediately close your query processing and only display the last images. Or is that the desired behaviour?
if($row['isOnline'] == '1') { //Makes sure, that 'isOnline' is set before displaying.
if ($row['race'] == "1");
{
echo '<img src="img/8-0.gif" />';
}
if ($row['class'] == "3");
{
echo '<img src="img/3.gif" ?>';
}
}
} //This bracket closes the actual query result handling
mysql_close($con);
?>
if ($row['race'] == "1" AND $row['online'] == 1);
{
echo '<img src="img/8-0.gif" />';
}
if ($row['class'] == "3" AND $row['online'] == 1);
{
echo '<img src="img/3.gif" ?>';
}
Currently, I have this code:
<?php
if (isset($_GET['s'])) {
$itemid = $_GET['s'];
$search = "$itemid";
$query = ucwords($search);
echo "<title>Results for $query</title>";
$string = file_get_contents('http://clubpenguincheatsnow.com/tools/newitemdatabase/items.php');
if ($itemid == "") {
echo "Please fill out the form.";
} else {
echo '<div id="content">';
$string = explode('<br>', $string);
foreach ($string as $row) {
preg_match('/^(.+)\s=\s(\d+)\s=\s(\D+)\s=\s(\d+)/', trim($row), $matches);
if (preg_match("/$query/i", "$matches[1]")) {
echo "<a href='http://clubpenguincheatsnow.com/tools/newitemdatabase/info.php?id=$matches[2]'>";
echo $matches[1];
echo "</a><br>";
}
}
echo '</div>';
}
} else {
echo "Item does not exist!";
}
?>
If "$matches[1]" has nothing in it, I want my code it to echo "Item does not exist!" How do I do this though? Please help!
I've tried something like if ($matches[1]=="") { echo "Item does not exist!"; } before, but it didn't work. This is what I got:
http://img685.imageshack.us/img685/998/28990b2c12d0423292d3574.png
See works fine right? Look what happens if $matches[1] DOES exist:
http://img528.imageshack.us/img528/3690/71472c9de6ec49118ee8d48.png
It still comes up! How can I make my code so it only echos the error if there is nothing for $matches[1]? PLEASE HELP ME!
If you are wondering, this is my code when I added the if ($matches[1]=="") { echo "Item does not exist!"; } in:
<?php
if (isset($_GET['s'])) {
$itemid = $_GET['s'];
$search = "$itemid";
$query = ucwords($search);
echo "<title>Results for $query</title>";
$string = file_get_contents('http://clubpenguincheatsnow.com/tools/newitemdatabase/items.php');
if ($itemid == "") {
echo "Please fill out the form.";
} else {
echo '<div id="content">';
$string = explode('<br>', $string);
foreach ($string as $row) {
preg_match('/^(.+)\s=\s(\d+)\s=\s(\D+)\s=\s(\d+)/', trim($row), $matches);
if (preg_match("/$query/i", "$matches[1]")) {
echo "<a href='http://clubpenguincheatsnow.com/tools/newitemdatabase/info.php?id=$matches[2]'>";
echo $matches[1];
echo "</a><br>";
}
}
echo '</div>';
if ($matches[1] == "") {
echo "Item does not exist!";
}
}
} else {
echo "Item does not exist!";
}
?>
Any help to my question would be VERY HIGHLY APPRECIATED!
Look at PHP's comparison operators
if (empty($matches[1])) { echo "Item does not exist!"; }
else {
echo "Item does not exist!";
}
This "else" is in regards to isset($_GET['s']). Is there a variable called s in your query string? If there isn't, then it's normal that you get the message.
Maybe you put that else block in the wrong place?