1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

how do i Call a php function inside of echo function??

Discussion in 'PHP' started by PlumGreekMob, Dec 31, 2009.

  1. #1
    I have added an adsense share function to my WPMU/BP site.

    I am using the T2 method of adding an adsense unit to show on only the first post on the index page.
    found here http://www.tamba2.org.uk/wordpress/adsense/

    These two parts are used to define and show adsense only on the first post
    1st part
    <?php
    $postnum = 1;
    $showadsense1 = 1;
    ?> 
    Code (markup):
    2nd part

    <?php if ($postnum == $showadsense1) {
    echo '
    <script type="text/javascript"><!--
    google_ad_client = <?php get_adsense_code(); ?>;
    /* xxxxxxxxxxxxxx */
    google_ad_slot = "xxxxxxxxx";
    google_ad_width = xxx;
    google_ad_height = xxx;
    //-->
    </script>
    <script type="text/javascript"
    src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
    </script>
    ';
    } ?>
    
    <?php $postnum++; ?>
    
    Code (markup):
    Now inside of the second part you will notice
    <?php get_adsense_code(); ?>
    Code (markup):
    after google_ad_client =. What this is doing is showing as is when i am viewing the page source. I believe it to be because of a function inside of a function.

    My question is would anyone know of a fix for this or is this something that doesnt have a fix.

    Thank you
     
    PlumGreekMob, Dec 31, 2009 IP
  2. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #2
    try

    <?php if ($postnum == $showadsense1) {
    echo '
    <script type="text/javascript"><!--
    google_ad_client = '.get_adsense_code().';
    /* xxxxxxxxxxxxxx */
    google_ad_slot = "xxxxxxxxx";
    google_ad_width = xxx;
    google_ad_height = xxx;
    //-->
    </script>
    <script type="text/javascript"
    src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
    </script>
    ';
    } ?>
    PHP:
     
    blueparukia, Dec 31, 2009 IP
  3. PlumGreekMob

    PlumGreekMob Member

    Messages:
    382
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    35
    #3
    Thank you very much for the reply.
    It now pulls the code correctly but when it does it puts it in a different spot then defined. ie it should be after google_ad_client = but is now above <script type="text/javascript">. It also shows the publisher # on the page. here is a userblog so you can see what it does.

    http://blogstick.com/alexmaristseaships/







     
    Last edited: Dec 31, 2009
    PlumGreekMob, Dec 31, 2009 IP
  4. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #4
    Just for the record, you cant run different Publishers ID's on different google_ad_slots so you will need to use old style google ads or default the ads by removing the line. google_ad_slot = "xxxxxxxxx";


    At a glance $postnum++; is posting the number ?

    Please post your function code that calls the numbers from wherever they are stored then we can give you the correct variable to use.
     
    MyVodaFone, Dec 31, 2009 IP
  5. PlumGreekMob

    PlumGreekMob Member

    Messages:
    382
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    35
    #5
    <?php get_adsense_code(); ?> is the function code that calls for the numbers

    $postnum++ is the post number

    and thanks for letting me know about adsense, will adjust
     
    PlumGreekMob, Dec 31, 2009 IP
  6. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #6
    
    
    <?php 
    if ($postnum == $showadsense1) {
    $adsense_code = get_adsense_code();
    echo '
    <script type="text/javascript"><!--
    google_ad_client = "'.$adsense_code.'";
    /* xxxxxxxxxxxxxx */
    google_ad_width = xxx;
    google_ad_height = xxx;
    //-->
    </script>
    <script type="text/javascript"
    src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
    </script>
    ';
    } ?>
    
    
    PHP:
    and remove <?php $postnum++; ?>

    Assuming get_adsense_code() gets a number thats returned ( thats why I asked for the code ) $adsense_code = get_adsense_code(); will activate the function and $adsense_code will echo it to the page.
     
    MyVodaFone, Dec 31, 2009 IP
  7. PlumGreekMob

    PlumGreekMob Member

    Messages:
    382
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    35
    #7
    The output works correctly but for some reason it still shows it before <script type="text/javascript"> instead of after google_ad_client =

    if i remove <?php $postnum++; ?> its shows adsense on every post instead of just the first.

    i have tested removing <?php $postnum++; ?> like you said but the output is the same and adsense is on every post.

    current code being used

    <?php 
    if ($postnum == $showadsense1) {
    $adsense_code = get_adsense_code();
    echo '
    <script type="text/javascript"><!--
    google_ad_client = '.$adsense_code.';
    /* 336x280, TOPfirstPOST */
    google_ad_width = 336;
    google_ad_height = 280;
    //-->
    </script>
    <script type="text/javascript"
    src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
    </script>
    ';
    } ?>
    <?php $postnum++; ?>
    Code (markup):



     
    PlumGreekMob, Dec 31, 2009 IP
  8. PlumGreekMob

    PlumGreekMob Member

    Messages:
    382
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    35
    #8
    I have fixed this, Thanks for your help everyone!
     
    PlumGreekMob, Jan 1, 2010 IP