How to make an image not show in an else statement

Hello,
I am looking to make an image (prc-header.png) to not show in the else statement. Can anyone point me in the right direction to accomplish this.

Here is a snippet of the code:
[php]print "

[/php]

Here is the full php page code image is located after the //Get PRCs line:
[php]

<?php $dbcon = mysql_connect("localhost", "standup_ipuser", "T942mwxFna%G") or die(mysql_error()); mysql_select_db("standup_ip2location", $dbcon); $ip = $_SERVER['REMOTE_ADDR']; //$ip = '96.253.7.71'; function is_bot(){ $botlist = array("Teoma", "alexa", "froogle", "Gigabot", "inktomi", "looksmart", "URL_Spider_SQL", "Firefly", "NationalDirectory", "Ask Jeeves", "TECNOSEEK", "InfoSeek", "WebFindBot", "girafabot", "crawler", "www.galaxy.com", "Googlebot", "Scooter", "Slurp", "msnbot", "appie", "FAST", "WebBug", "Spade", "ZyBorg", "rabaz", "Baiduspider", "Feedfetcher-Google", "TechnoratiSnoop", "Rankivabot", "Mediapartners-Google", "Sogou web spider", "WebAlta Crawler", "TweetmemeBot", "Butterfly", "Twitturls", "Me.dium", "Twiceler"); foreach($botlist as $bot){ if(strpos($_SERVER['HTTP_USER_AGENT'],$bot)!==false) return true; // Is a bot } return false; // Not a bot } if (is_bot()) { echo "Hey, you're a bot! What's up?"; }else { //Start Web Service Code $query = "http://geoip.maxmind.com/f?l=AFRi0OUowosv&i=" . $ip; $url = parse_url($query); $host = $url["host"]; $path = $url["path"] . "?" . $url["query"]; $timeout = 1; $fp = fsockopen ($host, 80, $errno, $errstr, $timeout) or die('Can not open connection to server.'); if ($fp) { fputs ($fp, "GET $path HTTP/1.0 Host: " . $host . " "); while (!feof($fp)) { $buf .= fgets($fp, 128); } $lines = explode(" ", $buf); $data = $lines[count($lines)-1]; fclose($fp); } else { # enter error handing code here } $geo = str_getcsv($data); $lat1 = $geo[4]; $lon1 = $geo[5]; //End Web Service Code } //GET PRCs $prc_result = mysql_query("SELECT * FROM prc_info") or die(mysql_error()); //$prctotal = mysql_num_rows($prc_result); print "

"; $count = 0; while ($row = mysql_fetch_array($prc_result)) { $prc = $row['name']; $location = $row['location']; $prcState = $row['state']; $prcPhone = $row['phone']; $url = $row['website']; $lat2 = $row['latitude']; $lon2 = $row['longitude']; $theta = $lon1 - $lon2; $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta)); $dist = acos($dist); $dist = rad2deg($dist); $miles = $dist * 60 * 1.1515; $distance = 30; if ($miles <= $distance) { $count = 1; if ($url == '') { ?>
    <div id="prc-spacing"><table cellpadding="2" width="100%"><tr>
    <td valign="top"><img src="/images/preg-icon.png" align="left" style="padding-bottom:10px;"></td>
    <td valign='top'><a href="prc-info?prc=<?php echo $prcid; ?>">
    <?echo $prc;?></a><br>
    <?print "$location, $prcState ($prcPhone)";?>
    </td></tr></table>
    </div>
<?php }else { ?>
    <div id="prc-spacing"><table cellpadding="2" width="100%"><tr>
    <td valign="top"><img src="/images/preg-icon.png" align="left" style="padding-bottom:10px;"></td>
    <td valign='top'><a href="<?echo $url;?>" target="_blank" onClick="recordOutboundLink(this, 'Outbound Links', 'PRC-<?print "$prc ($prcCity, $prcState)";?>');return false;"><?echo $prc;?></a><br><?print "$location, $prcState ($prcPhone)";?></td></tr></table>
    </div>
<?php } } } if ($count == 0) { ?> <?php } print "

"; ?>

[/php]

So appreciate everyone’s expertise and helping me understand so I can hopefully someday be a guru too!

Warm Regards,
Diana

Maybe I’m not understanding the question properly but if you don’t want the image displayed under a certain condition it’s simply:

If (condition == true) {
display image
} else {
do something else
}

You don’t even need an else clause if you aren’t replacing the image with something.

Hi Sabmin,

Thank you for replying. What you are saying is what I thought could be done, but when I remove the else statement, the if statement won’t show either. Some how it is all hooked together and I can’t see it.

Any other suggestions?

Diana

Okay so I’ve reformatted your code here to make it a little easier to read.

[php]<?php
$dbcon = mysql_connect(“localhost”, “standup_ipuser”, “T942mwxFna%G”) or die(mysql_error());
mysql_select_db(“standup_ip2location”, $dbcon);
$ip = $_SERVER[‘REMOTE_ADDR’];
//$ip = ‘96.253.7.71’;
function is_bot() {
$botlist = array(“Teoma”, “alexa”, “froogle”, “Gigabot”, “inktomi”,“looksmart”, “URL_Spider_SQL”, “Firefly”, “NationalDirectory”,“Ask Jeeves”, “TECNOSEEK”, “InfoSeek”, “WebFindBot”, “girafabot”,“crawler”, “www.galaxy.com”, “Googlebot”, “Scooter”, “Slurp”,“msnbot”, “appie”, “FAST”, “WebBug”, “Spade”, “ZyBorg”, “rabaz”,“Baiduspider”, “Feedfetcher-Google”, “TechnoratiSnoop”, “Rankivabot”,“Mediapartners-Google”, “Sogou web spider”, “WebAlta Crawler”, “TweetmemeBot”, “Butterfly”, “Twitturls”, “Me.dium”, “Twiceler”);
foreach($botlist as $bot) {
if(strpos($_SERVER[‘HTTP_USER_AGENT’],$bot)!==false) {
return true;
// Is a bot
}
return false;
// Not a bot
}
if (is_bot()) {
echo “Hey, you’re a bot! What’s up?”;
}else {
//Start Web Service Code
$query = “http://geoip.maxmind.com/f?l=AFRi0OUowosv&i=” . $ip;
$url = parse_url($query);
$host = $url[“host”];
$path = $url[“path”] . “?” . $url[“query”];
$timeout = 1;
$fp = fsockopen ($host, 80, $errno, $errstr, $timeout) or die(‘Can not open connection to server.’);
if ($fp) {
fputs ($fp, “GET $path HTTP/1.0Host: " . $host . “”);
while (!feof($fp)) {
$buf .= fgets($fp, 128);
}
$lines = explode(”", $buf);
$data = $lines[count($lines)-1];
fclose($fp);
} else {
# enter error handing code here
}
$geo = str_getcsv($data);
$lat1 = $geo[4];
$lon1 = $geo[5];
//End Web Service Code
}
//GET PRCs
$prc_result = mysql_query(“SELECT * FROM prc_info”)or die(mysql_error());
//$prctotal = mysql_num_rows($prc_result);
print "

“;
$count = 0;
while ($row = mysql_fetch_array($prc_result)) {
$prc = $row[‘name’];
$location = $row[‘location’];
$prcState = $row[‘state’];
$prcPhone = $row[‘phone’];
$url = $row[‘website’];
$lat2 = $row[‘latitude’];
$lon2 = $row[‘longitude’];
$theta = $lon1 - $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;
$distance = 30;
if ($miles <= $distance) {
$count = 1;
if ($url == ‘’) {
?>










<?echo $prc;?>



<?print "$location, $prcState ($prcPhone)";?>


<?php
} else {
?>









<a href=”<?echo $url;?>" target="_blank" onClick=“recordOutboundLink(this, ‘Outbound Links’, ‘PRC-<?print "$prc ($prcCity, $prcState)";?>’);return false;”>
<?echo $prc;?>



<?print "$location, $prcState ($prcPhone)";?>


<?php
}
}
}
if ($count == 0) {
?>
<?php
}
print “

”;
?>[/php]

The first thing that jumps out at me is you’re missing a couple of closing brackets, so I’m hoping this isn’t all of the code?

Secondly what is the condition you’re checking in which you want the image shown?

Yes that is all the code that I copied from a module in a joomla site I am working on. I didn’t write it but have to fix it, yuk!!

Tried to add a link to our website and a screenshot but your system won’t let me. This might not make any sense now…

What you see is the prc-header image (Local Pregnancy Support) and under that the first lilac box. When there is a PRC within a 30 mile radius of the users ip, then the lilac box lists all the locations. Since I live where there are no PRC’s, I see the above screenshot. What I want to have happen is that when there are no PRC’s that both the prc-header image and lilac box are hidden. I had to add something to the lilac box otherwise it is a long thin box which looks bad and that additional code is:

[code]

Local help available through…
OptionLine.org
[/code] Located after the if ($count == 0) line close to the bottom.

Hope I answered your question Sabmin…trying my best to understand php.

Thanks for your continued help here,
Diana

Diana

This is the entire code in the file? May I ask where you got it? Do you know who wrote it? No offense but for being a joomla Module–while I don’t have much experience with joomla… this being an extension for a public framework its really poorly written. It doesn’t even take advantage of the framework itself…

Sorry I’m just a little confused, when I formatted it earlier it was in basic notepad at work(I don’t have any dev tools at work sadly) and now that I’m plugging it in at home I see some things I MAY? have missed in formatting but they seem designed to confuse for instance lines 8-15:

[php] foreach($botlist as $bot) {
if(strpos($_SERVER[‘HTTP_USER_AGENT’],$bot)!==false) {
return true;
// Is a bot
}
return false;
// Not a bot
}[/php]

Very simple code except we’re forced to wonder if the last curly bracket(’}’) was intended to close the for loop or not… This IF clause looks as if its designed to have an else handler, which isn’t necessary as you can simply have a default to false before calling the loop. But it creates a problem as this little bit of code could be:

[php]
foreach($botlist as $bot) {
if(strpos($_SERVER[‘HTTP_USER_AGENT’],$bot)!==false) {
return true;
// Is a bot
} else { //notice the else here
return false;
// Not a bot
}
[/php]

However if there wasn’t supposed to be an else clause:

[php]
foreach($botlist as $bot) {
if(strpos($_SERVER[‘HTTP_USER_AGENT’],$bot)!==false) {
return true;
// Is a bot
}
}
[/php]
this assumes we created a default, and with the formatting we know this curly bracket’s intent of closing the loop rather than an error’d if statement.

I know I’m getting way off topic here but You did say you’d like to understand the language a little better… PHP is very easy and extremely forgiving but shouldn’t be taken for granted as the writer of this has done.

I also know I’m not answering you’re question… yet… and quite frankly to answer it I may end up rewriting this whole thing for you… I just wanted to express a little and try to help you see the code a little better while I try to see it a little better. :slight_smile: Meanwhile I’ll get back to you on this. If not later tonight than tomorrow.

Replace you’re code with this:

[php]<?php
$dbcon = mysql_connect(“localhost”, “standup_ipuser”, “T942mwxFna%G”) or die(mysql_error());
mysql_select_db(“standup_ip2location”, $dbcon);
$ip = $_SERVER[‘REMOTE_ADDR’];
//$ip = ‘96.253.7.71’;
function is_bot() {
$botlist = array(“Teoma”, “alexa”, “froogle”, “Gigabot”, “inktomi”,“looksmart”, “URL_Spider_SQL”, “Firefly”, “NationalDirectory”,“Ask Jeeves”, “TECNOSEEK”, “InfoSeek”, “WebFindBot”, “girafabot”,“crawler”, “www.galaxy.com”, “Googlebot”, “Scooter”, “Slurp”,“msnbot”, “appie”, “FAST”, “WebBug”, “Spade”, “ZyBorg”, “rabaz”,“Baiduspider”, “Feedfetcher-Google”, “TechnoratiSnoop”, “Rankivabot”,“Mediapartners-Google”, “Sogou web spider”, “WebAlta Crawler”, “TweetmemeBot”, “Butterfly”, “Twitturls”, “Me.dium”, “Twiceler”);
if (in_array($_SERVER[‘HTTP_USER_AGENT’], $botlist)) {
return true;
// Is a bot
} else {
return false;
// Not a bot
}
}
if (is_bot()) {
echo “Hey, you’re a bot! What’s up?”;
} else {
//Start Web Service Code
$query = “http://geoip.maxmind.com/f?l=AFRi0OUowosv&i=” . $ip;
$url = parse_url($query);
$host = $url[“host”];
$path = $url[“path”] . “?” . $url[“query”];
$timeout = 1;
$fp = fsockopen ($host, 80, $errno, $errstr, $timeout) or die(‘Can not open connection to server.’);
if ($fp) {
fputs ($fp, “GET $path HTTP/1.0Host: " . $host . “”);
while (!feof($fp)) {
$buf .= fgets($fp, 128);
}
$lines = explode(”", $buf);
$data = $lines[count($lines)-1];
fclose($fp);
} else {
# enter error handing code here
}
$geo = str_getcsv($data);
$lat1 = $geo[4];
$lon1 = $geo[5];
//End Web Service Code
}
//GET PRCs
$prc_result = mysql_query(“SELECT * FROM prc_info”)or die(mysql_error());
$getRows = mysql_num_rows($prc_result);
if ($getRows > 0) {
print "

“;
} else {
//print “

”;
print “
”;
}
$count = 0;
while ($row = mysql_fetch_array($prc_result)) {
$prc = $row[‘name’];
$location = $row[‘location’];
$prcState = $row[‘state’];
$prcPhone = $row[‘phone’];
$url = $row[‘website’];
$lat2 = $row[‘latitude’];
$lon2 = $row[‘longitude’];
$theta = $lon1 - $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;
$distance = 30;
if ($miles <= $distance) {
$count = 1;
if ($url == ‘’) {
?>










<?echo $prc;?>



<?print "$location, $prcState ($prcPhone)";?>


<?php
} else {
?>









<a href=”<?echo $url;?>" target="_blank" onClick=“recordOutboundLink(this, ‘Outbound Links’, ‘PRC-<?print "$prc ($prcCity, $prcState)";?>’);return false;”>
<?echo $prc;?>



<?print "$location, $prcState ($prcPhone)";?>


<?php
}
}
}
if ($count == 0) {
?>
<?php
}
print “

”;
?>[/php]

Hopefully it works as it’s a simple fix, without having to dig deeper than I already have in which I would end up rewriting it. :slight_smile:

As far as what I changed… I fixed the if/else statement that I mentioned before, I didn’t set a default but I stopped it from returning both false AND true as it would do if it was to be true… I also closed the function so that when you call is_bot you are actually calling the functions output. I reformatted it–again-- as well as created a new if clause after counting the results we got from the DB, if the query was successful we print the image, if its unsuccessful we ignore the image but maintain the formatting, minus the two line breaks which I included but commented out incase theres formatting issues in the template that you can try uncommenting the line(50) if it helps. And a last minute change which I’m not sure if it reduces load on the server or not it would make sense that it would but I sudo-optimized your is_bot function(untested-mind you.)

Oh my, thank you so much Sabmin… :smiley: I will try it out right now! And let you know the outcome.

The code was written by the webmaster I replaced (he was fired), quite a few of the pages he created were not working properly not to mention he got in through a hole and was sabotaging the site. Been quite a bumpy ride keeping on top of locking our sites down.

Thanks mucho thanks again!!

Diana

You’re welcome, hope it’s working!

Hi Sabmin,

Well it kinda worked but it is hard for me to tell because I have to call our office in Oregon to look at the website to see what happens. At first she said that the PRC’s were gone and then the image was gone but back…confusing. So I am going to test again with another person this weekend and do some more tweaking.

Your code looked great and very easy to understand the changes. I will keep you updated on the progress.

Thanks again Sabmin,

Diana

Sponsor our Newsletter | Privacy Policy | Terms of Service