StefanRSA Posted July 16, 2009 Share Posted July 16, 2009 For the first time I have the guts to try creating my own function. It is not working but I don't really understand why. Please have a look and direct me in fixing this typical php beginner error... Thanks function GoogleAd() { if ($catkey==1){ echo '<script type="text/javascript"><!-- google_ad_client = "pub-0928155608609322"; /* PROPERTY */ google_ad_slot = "7983841197"; google_ad_width = 728; google_ad_height = 90; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>'; } else if ($catkey==2){ echo '<script type="text/javascript"><!-- google_ad_client = "pub-0928155608609322"; /* STUFF FOR SALE */ google_ad_slot = "5536810507"; google_ad_width = 728; google_ad_height = 90; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>'; } else if ($catkey==3){ echo '<script type="text/javascript"><!-- google_ad_client = "pub-0928155608609322"; /* CARS FOR SALE */ google_ad_slot = "0191739856"; google_ad_width = 728; google_ad_height = 90; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>'; } else if ($catkey==4){ echo '<script type="text/javascript"><!-- google_ad_client = "pub-0928155608609322"; /* JOBS */ google_ad_slot = "7640366802"; google_ad_width = 728; google_ad_height = 90; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>'; } else if ($catkey==5){ echo '<script type="text/javascript"><!-- google_ad_client = "pub-0928155608609322"; /* BUSINESS SERVICES */ google_ad_slot = "3285320338"; google_ad_width = 728; google_ad_height = 90; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>'; } else if ($catkey==6){ echo '<script type="text/javascript"><!-- google_ad_client = "pub-0928155608609322"; /* COMMUNITY */ google_ad_slot = "7462364297"; google_ad_width = 728; google_ad_height = 90; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>'; } else if ($catkey==7){ echo '<script type="text/javascript"><!-- google_ad_client = "pub-0928155608609322"; /* EDUCATION */ google_ad_slot = "8279843684"; google_ad_width = 728; google_ad_height = 90; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>'; } else if ($catkey=={ echo '<script type="text/javascript"><!-- google_ad_client = "pub-0928155608609322"; /* PERSONALS */ google_ad_slot = "5750199803"; google_ad_width = 728; google_ad_height = 90; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>'; } else { echo '<script type="text/javascript"><!-- google_ad_client = "pub-0928155608609322"; /* PERSONALS */ google_ad_slot = "5750199803"; google_ad_width = 728; google_ad_height = 90; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>'; } } Link to comment https://forums.phpfreaks.com/topic/166242-solved-my-function-dont-work-why-not/ Share on other sites More sharing options...
akitchin Posted July 16, 2009 Share Posted July 16, 2009 i haven't looked through the whole function, so there could be syntax errors, but the logical reason why the function doesn't work (which although you haven't explained HOW it doesn't work, i assume it's because it's always showing personals) is because it relies on $catkey, which is never passed to the function. function GoogleAd($catkey) { ... } and then when you call it, you'll need to pass it the $catkey variable. this function could be a lot cleaner if you used a switch() statement. Link to comment https://forums.phpfreaks.com/topic/166242-solved-my-function-dont-work-why-not/#findComment-876685 Share on other sites More sharing options...
StefanRSA Posted July 16, 2009 Author Share Posted July 16, 2009 Thanks!!! Now it works! But I am not sure what u meant by the switch() statement? Link to comment https://forums.phpfreaks.com/topic/166242-solved-my-function-dont-work-why-not/#findComment-876693 Share on other sites More sharing options...
akitchin Posted July 16, 2009 Share Posted July 16, 2009 Thanks!!! Now it works! But I am not sure what u meant by the switch() statement? have a look in the PHP manual at switch: switch. it's basically a slightly more structured way of doing if-elseif-else statements. Link to comment https://forums.phpfreaks.com/topic/166242-solved-my-function-dont-work-why-not/#findComment-876697 Share on other sites More sharing options...
StefanRSA Posted July 16, 2009 Author Share Posted July 16, 2009 Will do so. Thanks for the help anyway!!! Link to comment https://forums.phpfreaks.com/topic/166242-solved-my-function-dont-work-why-not/#findComment-876704 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.