Trying to display a corresponding image based on $enrollment_points equalling one of 3 values; 1000, 750 or 500.
With $enrollment_points setup successfully in a vars.php file, it only returns/displays the first image from the if statement, even though $enrollment_points = 750. It does not seem to get past the if statement or evaluate the integer from the $enrollment_points string.
I can't figure out why?
thanks in advance
<?php
if ($enrollment_points = "1000") {
echo "<img src='../1_LandingPage_Content/images/offers/1000_enrollment_700x600_.png' alt='' />";
} elseif ($enrollment_points = "750") {
echo "<img src='../1_LandingPage_Content/images/offers/750_enrollment_700x600_.png' alt='' />";
} elseif ($enrollment_points = "500") {
echo "<img src='../1_LandingPage_Content/images/offers/500_enrollment2_700x600_.png' alt='' />";
} else {
echo "<img src='../1_LandingPage_Content/images/offers/enrollment_700x600_.png' alt='' />";
}
?>
i expect the corresponding image file to be displayed based on the value of $enrollment_points.
To compare for equality, you should use == and not =
So change it to
if ($enrollment_points == "1000") {
echo "<img src='../1_LandingPage_Content/images/offers/1000_enrollment_700x600_.png' alt='' />";
EDIT:
For your question about == or ===. == compares value only. === compares the value AND type. See below:
1 === 1: true
1 == 1: true
1 === "1": false // 1 is an integer, "1" is a string
1 == "1": true // type is ignored, so true.
One "=" is use for assign value, Two "==" and three "===" use for compare.
<?php
if ($enrollment_points == "1000") {
echo "<img src='../1_LandingPage_Content/images/offers/1000_enrollment_700x600_.png' alt='' />";
} elseif ($enrollment_points == "750") {
echo "<img src='../1_LandingPage_Content/images/offers/750_enrollment_700x600_.png' alt='' />";
} elseif ($enrollment_points == "500") {
echo "<img src='../1_LandingPage_Content/images/offers/500_enrollment2_700x600_.png' alt='' />";
} else {
echo "<img src='../1_LandingPage_Content/images/offers/enrollment_700x600_.png' alt='' />";
}
?>
You are setting the variables inside your if statement rather than comparing:
if($enrollment_points = "750")
change to:
if($enrollment_points == "750")
Remember, one "=" for setting, two or three for comparing
If you are assigning the value of 1000 to $enrollment_points variable then yes thats the case but if you want them to equal, as in equality statement use the '==' sign.
Use == for if statement, not =. http://php.net/manual/en/language.operators.comparison.php
Related
I have the following situation.
If there isn't an image in the DB, the page it's on shows a big image placeholder. What is the best way to hide the image placeholder if an image doesn't exist?
<img src="<?php echo '../img/artists/' . $row_rsAccents['artistPhoto']; ?>" width="100%"/>
http://westerndesignconference.com/intheloop/
You can do this with a simple if/else statement like so:
//I prefer to set things with variables
$placeholder_img = "../img/artists/placeholder.jpg";
$db_img = $row_rsAccents['artistPhoto'];
if($db_img){
$img_src = $db_img;
} else {
$img_src = $placeholder_img;
}
echo "<img src='$img_src' alt='' width='100%' />";
If there is a value returned - show an image. If the condition fails, no <img> will be displayed, preventing the blank gap
if (isset($row_rsAccents['artistPhoto'])) {
echo '<img src="../img/artists/' . $row_rsAccents['artistPhoto'] . '" width="100%"/>'
}
if (file_exists('artist.jpg') {
echo "<img src='artist.jpg'>";
}
else {
echo "<img src='default.jpg'>";
}
The code in question :
<?php /*tests added by jason*/
echo "<br />";
echo "count = " . $this->countModules('showcase');
echo "<br />";
echo "hidebyview = " . $hideByView;
echo "<br />";
if($hidebyview == true) {
echo "T";
}
else {
echo "F";
}
echo "<br />";
if ($this->countModules('showcase') && $hideByView == false) {
echo "pass";
}
else {
echo "fail";
}
echo "<br />";
?>
Site 1 output Apache/2.2.22 (Ubuntu) PHP Version 5.3.10-1ubuntu3.7 (where everything works fine):
count = 1
hidebyview =
F
pass
Site 2 output Apache/2.2.13 (Win32) PHP/5.3.26 (where the thing is broken) :
count = 1
hidebyview = 1
F
fail
I guess it boils down to how can the part that evaluates to "fail" evaluate to different answers?
$hideByView == false is not (always) equal to !($hidebyview == true) because of casting and other possible automatic conversions. So your debug info is not really showing you what your expression $hideByView == false evaluates to.
I am new to PHP and learning. I'm trying to pass a value through a url link but it doesn't seem to work.
The link value I am passing is http://www.mysite.com/index.php?id=f
I want to run a js script if ID not F seen below but right now when I run it. It doesn't do anything:
<?php
$ShowDeskTop = $_GET['id'];
if (isset($ShowDeskTop)){
echo $ShowDeskTop;
if ($ShowDeskTop != "f"){
echo "ShowDeskTop Value is not F";
echo "<script type=\"text/javascript\">";
echo "if (screen.width<800)";
echo "{";
echo "window.location=\"../mobile/index.php\"";
echo "}";
echo "</script>";
};
};
?>
I know this is easy PHP 101 but I can't figure it out. I have tried everything from w3schools to other sites on Google for the answer and having no luck. Could someone please tell me what I am doing wrong?
Thank you!
$ShowDeskTop is not the same as $ShowDesktop variables names are case sensitive!
This is never gonna work since you set the variable AFTER checking if it exist..
The most easy way:
<?php
if (isset($_GET['id'])) {
echo $_GET['id'];
if ($_GET['id'] != 'f') {
?>
<script type="text/javascript">
if (screen.width < 800) {
window.location = "../mobile/index.php";
}
</script>
<?php
}
}
?>
I don't think <> is valid in PHP (it is in VB.NET ..) the is not operator is != or !== (strict/loose comparison).
Also you don't have to close if statements with a ;
This:
if (expr) {
}
Is valid and not this:
if (expr) {
};
I thought about writing != instead of <>.
You have a number of problems including bad variable case (i.e. variables not matching), checking for variables before they exist, etc. You can simply do something like this:
if (!empty($_GET['id'])) { // note I check for $_GET['id'] value here not $ShowDeskTop
$ShowDeskTop = $_GET['id'];
echo $ShowDeskTop; // note I change case here
if ($ShowDeskTop !== "f"){ // note the use of strict comparison operator here
echo "YES, the id doesn't = f";
echo "<script type=\"text/javascript\">";
echo "if (screen.width<800)";
echo "{";
echo "window.location=\"../mobile/index.php\"";
echo "}";
echo "</script>";
} // note the removal of semicolon here it is not needed and is bad coding practice in PHP - this is basically just an empty line of code
} // removed semicolon here as well
Fist thing, you need ; at the end of echo $ShowDesktop
And, what does f mean in if ($ShowDeskTop <> "f"){
use strcmp() instead of <> operator.
Try
if(!strcmp($ShowDeskTop, "f")){
echo "YES, the id doesn't = f";
}
<?php
$ShowDeskTop = $_GET['id']; // assign before checking
if (isset($ShowDeskTop)){
//echo $ShowDeskTop;
if ($ShowDeskTop !== "f"){
echo "YES, the id doesn't = f";
echo "<script type='text/javascript'>";
echo "if (screen.width<800)";
echo "{";
echo "window.location.replace('../mobile/index.php');"; // assuming your path is correct
echo "}";
echo "</script>";
}
}
?>
I have this code and there is a bug in it but I cannot see where I have gone wrong. Can someone please help? The problem is that I am trying to display a certain image to corrospond with the content of text files. I think i have that part sorted but when it comes to displaying the images there is always a bug (E.G it is always green even when the if statment says otherwize.Here is the code:
<?php
if (empty($_GET['unit'])) {
$output="Please Enter A Unit Number";
echo $output;
}
else {
$filepathhalf = "/data/";
$file = "false";
$date = date("Ymd");
$unitnum = $_GET['unit'];
$ext = ".txt";
$filepath = $filepathhalf.$unitnum.$date.$ext;
echo $filepath;
echo $file;
if(file_exists($filepath))
{
$fh = fopen($filepath, 'r');
$file = fread($fh, 5);
fclose($fh);
}
echo $file; //This echo comes back as false as set but the green.png image still displays.
if ($file = "true ")
{
echo "<img src=\"images/green.png\" width=\"15\" height=\"15\" />";
}
else
{
echo "<img src=\"images/red.png\" width=\"15\" height=\"15\" />";
}
echo $_GET['unit'];
}
?>
There is a difference between comparing two instances and assigning one to the other.
See the below lines from your snippet and see if you might spot the error with the above clue:
if ($file = "true ")
{
echo "<img src=\"images/green.png\" width=\"15\" height=\"15\" />";
}
else
{
echo "<img src=\"images/red.png\" width=\"15\" height=\"15\" />";
}
Otherwise hover with your mouse over the spoiler below!
If you want an explanation regarding the issue, do the same...
$file = "true " will always evaluate to true, first it
will assign the string "true " to $file and then the value of
$file will be evaluated.
You are most probably looking for if($file == true), which will compare the value of $file to true.
You use a single =, which is used when assigning variables, not comparing them. When checking if two values are equal, use ==.
if ($file == true)
{
echo "<img src=\"images/green.png\" width=\"15\" height=\"15\" />";
}
hope that helps
It should be == to check condition.
if ($file != "false")
{
echo "<img src=\"images/green.png\" width=\"15\" height=\"15\" />";
}
not only you're using a single "=" but also you compare it to "true " (with a concatenated space!). I would change the code to:
if ($file === true)
{
echo "<img src=\"images/green.png\" width=\"15\" height=\"15\" />";
}
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