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" ?>';
}
Related
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;
}
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.
I was wondering if it's possible to have an if statement within an echo.
I have if statement which works fine when echoing results through the a while loop... This is the statement:
<div><?php if ($row['image'] == '') {}
else {echo "<img src='data:image/jpeg;base64,".base64_encode($row['image'])."'>";} ?>
<?php if ($row['video'] == '') {}
else {echo "<iframe src={$row['video']}></iframe>";} ?></div>`
So basically it's either a video or an image which works fine but then I implemented an infinite scroll to my blog which echoes the data from the database through and if statement like so:
if ($results) {
while($obj = $results->fetch_object())
{
echo '
<div><h3>'.$obj->headline.'</h3> </div>
<div><img src='data:image/jpeg;base64,".base64_encode('.$obj->image.')."'></div>'
So I wondering if anyone knows if it's possible to transfer that if statement within this echo so that it display an image firstly and then knows whether one is present or when a video is present within the database.
Thanks in advance for any help.
PS: I'm very new to coding/php!
Of course. Just split up the echo into multiple statements:
while($row = $results->fetch_object()) {
echo '<div>';
if ($row['image'] == '') {
} else {
echo "<img src='data:image/jpeg;base64,".base64_encode($row['image'])."'>";
}
if ($row['video'] == '') {
} else {
echo "<iframe src={$row['video']}></iframe>";
}
echo '</div>';
}
Try this one.
//first initialize a variable as a string
$result="";
while($obj = $results->fetch_object()) {
$result.="<div>";
if (!empty($obj['image'])){
$result.="<img src='data:image/jpeg;base64,".base64_encode($obj['image'])."'>";
}
elseif (!empty($obj['video'])){
$result.="<iframe src={$obj['video']}></iframe>";
}else{
//show some notification or leave it
//echo 'not Found';
}
$result.="</div>";
}
//finally you need to print the result variable.
echo $result;
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?
I have a variable that is posted through a html form:
$_POST['ref']
And a variable that is pulled from a table in a database:
$row['ref']
i have a basic comparison script to check if they are both the same:
$ref = $_POST['ref'];
$result = mysql_query("SELECT * FROM logbook.job");
if (!$result) {
die("Query to show fields from table failed");
}
$row = mysql_fetch_array($result);
$refdb = $row['ref'];
$refform = $_POST['ref'];
echo $_POST['ref'] ."<br>". $row['ref'] . "<br><br>";
if ($refdb == $refform) {
echo "Yes they are<br><br>";
}
else {
echo "No they are not<br><br>";
}
if (is_string($_POST['ref']))
{
echo "Yes";
} else {
echo "No";
}
echo "<br>";
if (is_string($row['ref']))
{
echo "Yes";
} else {
echo "No";
}
Which outputs:
G2mtxW
G2mtxW
No they are not
Yes
Yes
I echo them both out. Than i ask if they are the same. Then i check whether each is a string.
How come they are not the same? How can i get them to match
Any help would be appreciated
Try using the binary-safe comparison for String:
result = strcmp($str1, $str2);
If the result is 0, then both are the same. Otherwise, they aren't.
One of your strings (probably the one from the DB) might be null-terminated. I've tested the following
$foo = "abc\0";
$bar = "abc";
echo "$foo\n$bar\n";
if($foo == $bar)
echo "Equal.";
else
echo "Not equal."
Output is
abc
abc
Not equal.
Try var_dump-ing both values, check their lengths and inspect them using view-source. They are different in someway.
echo $status_message;
echo "Accepted";
if(strcmp($status_message,"Accepted")==0)
{
echo "equal";
}
else
{
echo "not equal";
}
?>
$row['status'] is a field from table