echo isset with condition - php

When people search for a real estate agent by zip code they will see a message on the site I'm working on that reads: There are x number of our Agents in your neighborhood.
x is the number determined by this php code:
<?php echo isset($total_record) ? $total_record : "";?>
if the number is Zero, the message sounds dumb (There are 0 number of...)
How do I change the message just for those cases with 0 as a search result? so that a different message appears? Something like - Sorry, we don't have any Agent in your immediate area.
Any help, much much appreciated.

Use a simple if statement:
if (isset($total_record) && $total_record > 0){
echo $total_record." number of our Agents in your neighborhood";
} else {
echo "Sorry, we don't have any Agent in your immediate area.";
}

if (isset($total_record))
{
if ($total_record > 0)
{
echo "There are {$total_record} of our Agents in your neighborhood.";
}
else
{
echo "There are no Agents in your neighborhood.";
}
}

Use the empty function rather than isset. The empty function checks if a variable exists and has a value. 0, false, and a few other values are also considered empty, check the manual for a full listing.
echo !empty($total_record) ? 'There are ' . $total_record . ' number of our Agents in your neighborhood.' : 'Sorry, we don\'t have any Agent in your immediate area.';

Related

Using if else statement based off of $_POST value to echo message

Pretty sure this is a quick and easy question but I have a form that on action POST goes to a confirmation page. I need a message to display on the confirmation page if the user selects county1 but if user selects county2, county3, or county4. However, when I setup the statement it's not working. Probably a syntax error or two on my part. Any help would be greatly appreciated.
A messy idea of what I think should work:
<?php $county=$_POST['County'];
if ($county="Polk") {
echo "Important message about your county"; }
else {
echo " "; // Or nothing at all
}
?>
But
<?php echo $_POST['County'] ?>
displays the name of the county so I know the submission is carrying through. Thoughts on why my above code wouldn't be working? If you could flag syntax errors or code placement that'd be much appreciated! Thank you!
Inside the if condition you should use two equal operators instead of one . try this code
<?php
$county = isset($_POST['County'])?$_POST['County']:"";
if ($county == "Polk") {
echo "Important message about your county";
}
else {
echo " "; // Or nothing at all
}
?>
Use double equal in your if statement for comparison
See the answer and read the comment to understand why you have to change it
if ($county="Polk") {
Single equal is assignment operator, it assign value
Change this line to this
if ($county=="Polk") {
So your whole code should look like this
$county=$_POST['County'];
if ($county == "Polk") {
echo "Important message about your county";
}
else {
echo " "; // Or nothing at all
}
Use double equal sign, double equal is comparison operator,
Here you are checking if the $county is equal to Polk or not,
Means you are comparing value

Display an "offline" message when a external page is offline (PHP)

I have an number of ads displaying on my website that reads the balance of a Blockchain.info bitcoin address but their system for keeping the address balances online keeps going down.
What I want is to do is display a message when the address (not the website) can't be read.
The balance is always between 0 and 10 (and can be a decimal within that range).
I will save you reading all the curl code and just post what I have at the end of my PHP:
if ($BitcoinAddressBalance >= -0 && $BitcoinAddressBalance < 10) {
echo 'Online';
} else {
echo 'Offline';
}
My problem is that the code echoes 'Online' even when there is no data/numbers or I use a unregistered domain.
Can I use curl to check a page on a website has a number between 0 and 10?
If you want to check whether if the value in $BitcoinAddressBalance is not empty then you can use the following:
if ( isset($BitcoinAddressBalance) ) {
echo 'Online';
} else {
echo 'Offline';
}

PHP GET Function as NULL

I want to use GET in PHP and I know how to. The only problem is when I start my page I dont have a ?var=... on it. For example my site is
http://localhost/q/question.php
and in my code is
$num = $_GET['num'];
And I want to assume that if $num == NULL or has not been defined as in the link above.
I will generate a different page. because as example below just determines I am starting the questions in what ever page.
No num variable = Start Generating questions in array.
http://localhost/q/question.php?num=1
if(isset($_GET['num')){
header('Location: someotherpage.php'); //redirects user to someotherpage.php
//do something
}
else{
//do something else
}
<?php
if(isset($_GET['num'))
{
echo "is not null";
}
else
{
echo "is null";
}
?>

PHP - Weird issue with POST data

I'm coding out a simple contact form, and the following code is giving me trouble:
if(!strlen($_POST['lastname']) > 0){
echo '<p class="message error">Please enter the parent\'s last name.</p>';
}
if(!strlen($_POST['comments']) > 5){
echo '<p class="message error">Please tell us a little more in the comments field.</p>';
}
Relevant form element:
<textarea name="comments" cols="60" rows="5"><?=(isset($_POST['comments']) ? $_POST['comments'] : '')?></textarea>
When I leave both fields blank, only the first error message (along with the others not shown) displays, whereas the one for the comments field does not.
The error checking even returns with an error if I submit the comments fields with less than 5 characters, as it should, but the error message does not print. In addition, I even echoed the strlen() of the comments field when I submitted with it blank and it prints out 0.
Can anyone see what the problem here is?
if (!strlen > 0) first evaluates strlen, which gives, say, 10. This is then negated by ! to false. This is then compared to > 0, which is false, since false will be cast to 0 and 0 > 0 is false. The other way around, if the string is actually empty, the condition will be true.
You either want if (!(strlen > 0)) or if (strlen <= 0).
Missing parenthesis?
if(!(strlen($_POST['lastname']) > 0)) {
echo '<p class="message error">Please enter the parent\'s last name.</p>';
}
if(!(strlen($_POST['comments']) > 5)) {
echo '<p class="message error">Please tell us a little more in the comments field.</p>';
}
If you want to validate the data whether it is entered you can use following options too
if(empty($_POST['name'])){
echo 'Enter Name';
}
or
if(trim($_POST['name'])==''){
echo 'Enter Name';
}
Just to be clear, the problem here is that the ! applies to the return value from strlen only and not the entire comparison expression. The above if statements will always return false.
It could be rewritten like this to work:
if (!(strlen($_POST['lastname']) > 0)){ /* display error */ }
This is because the ! negates the result of the expression nested in parentheses and not the number returned by strlen.
Also, I would recommend not returning the POSTed value unaltered in the HTML source, that's just asking for trouble...

print a variable generated from another .php file

I'm trying to display the value of $codeOut from a php file called get_rebate.php.
Currently I am using:
<?php include ("get_rebate.php"); echo substr($codeOut,7); ?>
to try and get the output of $codeOut from get_rebate.php to appear in the page but instead it returns the error message that get_rebate.php generates when it can't verify the value.
I'm probably not calling the result of get_rebate.php ($codeOut) properly but looking for guidance with this problem.
get_rebate.php:
<?
$code=$_GET['code'];
$rnum=50000-$_GET['rnum'];
if ($code=="2210" || $code==$rnum) {
makeVerifier($code);
} else { echo "The Rebate Code you have entered (" . $code . ") does not apply to this product.\nPlease consult with your Fitness Expert and request if a Rebate Code\nis available for this product.";
}
function makeVerifier($codeIn) {
$len=strlen($codeIn);
for ($i=0;$i<$len;$i++) {
$codeOut.=ord(substr($codeIn,$i));
if (strlen($codeOut)>7) {break;}
}
echo "Congratulations! Your Manufacturers Rebate Verification Code is M" . substr($codeOut,0,7);
}
?>
The output on the page is: The Rebate Code you have entered () does not apply to this product. Please consult with your Fitness Expert and request if a Rebate Code is available for this product
Obviously $code is not 2210 or $rnum so you get the error message. Even if the code would be correct, it wouldn't work: $codeOut's scope is limited to the makeVerifier() function. You need to call it with $codeOut = makeVerifier( $code ) and at the end of the function add return $codeOut.

Categories