homepage Welcome to WebmasterWorld Guest from 54.196.62.23
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Javascript as an output of PHP if/else statement
Newbie needing assistance!
TMNQueenMum



 
Msg#: 4435331 posted 5:23 pm on Mar 30, 2012 (gmt 0)

Hello all,

Apologies first for my ignorance. I am not a programmer, but unfortunately, I have to fix a problem for our company ASAP, so Im left to figure it out on my own. I've spent the last day googling and cannot seem to find a solution that works for me, so I am hoping that you all will take pity on me and help me out!

I have a nonprofit website where we used to have a "sponsor" on the home page. Last year, our former leadership disabled this in lieu of using google adsense.

Adsense is doing well for us, but for these websites, I would still like the option of having a local sponsor. I thought I could accomplish this using a php if/else statement, to use php to look in the database and see if a sponsorship was there and if it was, to display it, otherwise to display adsense.

I'm really struggling with getting the adsense javascript to work within this statement. I have tried several options I have found online, including closing out the php, putting in the javascript, and then reopening and continuing the php (this just displays the adsense regardless of whether the condition is met or not). I have also tried using "echo" and "print" for the javascript (I've done my quotes like \" and also tried the <<<HERE ... HERE; trick to no avail).

It seems no matter what I do, if the javascript for adsense is within the php, it doesn't work... and if it is not, it will show it regardless of whether the condition is met.

I've also tried putting the javascript in its own php file and then including that file if the condition is met... doesn't seem to work either.

I KNOW this is my own user error. I fully admit that this is not something I do well -- I know enough to break things! But I also run a dinky nonprofit, recently lost my volunteer tech person and the tech person on my board is gone for two weeks... I desperately need to fix this so we don't lose these sponsors, but I'm admitting this is above my level...

Can anyone take pity on a poor girl and help me fix this?

This is the entire code for the module, which is included in our index.php page (each index.php page on our chapters is a clone of this page). The version I am showing here results in the googleadsense showing up regardless of whether the condition is met.

<?php
// homepage FEATURED SPONSORS display
//this module displays a sponsor ad on the homepage (index) -- triggered by location_id 500 in database

require "/export/home/reference/tmn_common.php";
require "$tmn_db_inc_path/national.php";

//determine if there are any sponsorships for the current forum
$currenttime = time();
$sql = "SELECT * FROM mom_national.sponsorships WHERE (site_id='$site' AND location_id='500' AND status='active' AND ($currenttime BETWEEN startdate AND enddate))";
$resultid = mysql_query($sql, $linkid);
$headline = "<p align=\"center\"><span class=\"header\">Featured Sponsors</span></p>";
while ($resulttext = mysql_fetch_object($resultid))
{
$id = $resulttext->id;
$clickthru = "http://www.$tmn_accountname.com/mommies/modules/sponsor_clickthru.php?id=$id";
$imagename = $resulttext->imagename;
if ($headlinedisplayed) $headline=""; //only allow headline to be displayed once

$html = <<<END
<table width="100%" border="0"><tr><td valign="top">
$headline
<p align="center"><a href="$clickthru" target="_blank"><img src="http://www.themommiesnetwork.org/images/sponsorships/$imagename" border="0"></a></p>
</td></tr></table>
<img src="/mommies/images/spacer.gif" width="5" height="5"><hr width=75%>
END;


if ((mysql_affected_rows) == 0)

{
?>

<!-- TMN Adsense -->
<script type="text/javascript"><!--
google_ad_client = "ca-pub-4730108747622378";
/* Chapter Homepage */
google_ad_slot = "5136031496";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
<!-- End TMN Adsense -->

<?php
}

else

{print $html;}

}
?>

 

penders

WebmasterWorld Senior Member penders us a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



 
Msg#: 4435331 posted 9:11 pm on Mar 30, 2012 (gmt 0)

if ((mysql_affected_rows) == 0)


mysql_affected_rows() [uk3.php.net] is a function, but you are treating it like a constant [uk3.php.net]. This expression will always evaluate to TRUE, so your JavaScript (Google Ads) will always get output. This should give you a warning message, since the 'constant' is undefined (but that depends on the error_reporting level). But anyway, it's the wrong function to use here, you need mysql_num_rows() [uk3.php.net], which returns the number of rows in the SELECT query which I assume is what you are after.

So, replace the above line with...

if (mysql_num_rows($resultid) == 0)

...and see what happens.

TMNQueenMum



 
Msg#: 4435331 posted 9:20 pm on Mar 30, 2012 (gmt 0)

Thank you!

I changed that (I knew it was user-error!) and it does show the local sponsor now if there is one, but it is still showing a blank page if it is supposed to show the adsense.

Any suggestions for what might be wrong there?

penders

WebmasterWorld Senior Member penders us a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



 
Msg#: 4435331 posted 9:47 pm on Mar 30, 2012 (gmt 0)

An entirely blank page? Or just the Adsense is not showing? You said above that, "...the googleadsense showing up regardless of whether the condition is met." - is this not the case anymore?

I've just noticed, you have an error (an additional curly brace) at the end of your code...

else 

{print $html;}

}
?>


That last curly brace (in red) needs to be removed.

TMNQueenMum



 
Msg#: 4435331 posted 9:52 pm on Mar 30, 2012 (gmt 0)

Well, both -- the adsense is the only thing that would print on the page if it was supposed to, but nothing is printing at all.

Here are some examples:

[charlottemommies.com...]

This is one with a local sponsor

[littlerockmommies.com...]

This is one without a local sponsor (It should be showing adsense)

This module will eventually be included on the index page of each chapter, in place of the current adsense code that is there.

Regarding the extra bracket -- I thought that too, but it is actually the closing bracket from line 15.

TMNQueenMum



 
Msg#: 4435331 posted 9:55 pm on Mar 30, 2012 (gmt 0)

Oh to clarify:
Before adsense was showing up regardless of the condition.
When I made that first change, it made it so if the condition was met, the local sponsor showed up, but if not, nothing shows up (the page is just blank)

The examples above should show you what I mean.. (I hope!)

penders

WebmasterWorld Senior Member penders us a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



 
Msg#: 4435331 posted 10:59 pm on Mar 30, 2012 (gmt 0)

Regarding the extra bracket -- I thought that too, but it is actually the closing bracket from line 15.


Ah yes, so it is (it doesn't help that this forum removes any white-space/indentation by default)! :) And that's actually part of the problem... your code to display the Google Adsense is inside the block of code (while loop) that generates the HTML for the local sponsor. If there is no local sponsor, then the code that writes out the Google Adsense is not even executed.

I think you just need to restructure your code blocks a little bit, to either generate the HTML for the sponsor OR (ELSE) for the Google Adsense... I've not actually changed that much, but it's easier to re-paste everything from the while() (line#14?) down...

I've changed the while to an if since your SELECT query is only returning at most 1 record (the local sponsor).

if ($resulttext = mysql_fetch_object($resultid)) 
{
$id = $resulttext->id;
$clickthru = "http://www.$tmn_accountname.com/mommies/modules/sponsor_clickthru.php?id=$id";
$imagename = $resulttext->imagename;
if ($headlinedisplayed) $headline=""; //only allow headline to be displayed once
$html = <<<END
<table width="100%" border="0"><tr><td valign="top">
$headline
<p align="center"><a href="$clickthru" target="_blank"><img
src="http://www.themommiesnetwork.org/images/sponsorships/$imagename" border="0"></a></p>
</td></tr></table>
<img src="/mommies/images/spacer.gif" width="5" height="5"><hr width=75%>
END;
}
else {
$html = <<<END
<!-- TMN Adsense -->
<script type="text/javascript"><!--
google_ad_client = "ca-pub-4730108747622378";
/* Chapter Homepage */
google_ad_slot = "5136031496";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
<!-- End TMN Adsense -->
END;
}
print $html;


If a record is returned then store the HTML for the local sponsor in the variable $html ELSE store the HTML for Google Adsense in $html. Finally print $html.

And mysql_num_rows() is no longer required.

TMNQueenMum



 
Msg#: 4435331 posted 2:04 pm on Apr 2, 2012 (gmt 0)

Thank you so much!

I ended up having to take out the "<<<END ... END;" stuff on both of the $html things because for whatever reason it was not letting the code run... but once I did that and just \ out all the " it worked perfectly!

Thank you so much! You have really helped out our little nonprofit and now have over 30,000 mommies who think you're awesome!

penders

WebmasterWorld Senior Member penders us a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



 
Msg#: 4435331 posted 2:41 pm on Apr 2, 2012 (gmt 0)

You're welcome.

I ended up having to take out the "<<<END ... END;" stuff


This should have been OK (it's called HEREDOC syntax for specifying strings). Just to note that you can use any identifier instead of "END" (uppercase by convention) providing the opening/closing delimiters match. And the closing delimiter MUST appear on a line all by itself without any white-space before it. For example...

// This is my string: 
$myString = <<<MYTEXTBLOCK
Some
text
goes
here
MYTEXTBLOCK;
// Note that the closing delimiter (above) is hard left, no spaces
// and nothing follows it on the line, except an optional semi-colon (;)

TMNQueenMum



 
Msg#: 4435331 posted 3:25 pm on Apr 2, 2012 (gmt 0)

Right. I read that and it was working with the part where it pulled in the sponsorship, but it was kinking out with the javascript part for some reason...

Once I took it out, it worked. I have no idea why, because I did double check about the spaces and everything to make sure it was right. It kept giving me an error on the first line where it started and when I swapped it, it worked... so I'm not going to complain :D

Thank you again so much! I may be back for more help in the future... until I can find a volunteer programmer to help with some things, I'm kind of all we have here!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved