MasterACE14 Posted April 7, 2009 Share Posted April 7, 2009 I have this script which randomly displays a text advertisement from an array. It is displaying the HTML, but not the array values in it. ads.php <?php /************************************************* * Simple AD Rotator * * Version: 1.0 * Date: 2007-08-30 * * Include this php file into your site and call the * show_ads() function with a number of ads * you want to display. * * Edited by ACE * * * ****************************************************/ // Image ads haven't been added yet! //USAGE $ad[] = array("Title", "Description", "display url", "url"); include("/home/eliteace/public_html/ads/select-ads.php"); // Function to display random ads from the list function show_ads($number_of_ads=1){ // Loading the ads list global $ad; // maximul number of ads = number of ads if($number_of_ads > count($ad)){ $number_of_ads = count($ad); } // Initialize the random generator list($usec, $sec) = explode(' ', microtime()); srand((float) $sec + ((float) $usec * 100000)); // select random ads $rand_keys = array_rand($ad, $number_of_ads); // creade ads $show_ads = "<table><tr>"; for($i=0; $i<$number_of_ads; $i++){ $e = $ad[$rand_keys[$i]]; $show_ads .= " <td onClick=\"document.location='{$e[3]}'\" title='{$e[0]}' width='180' style='cursor:pointer'> <a href='{$e[3]}' style='font:13px Arial; font-weight:bold; color: #0000ff'>{$e[0]}</a><br> <span style='font:12px Arial; color: #333333'>{$e[1]}</span><br> <a href='{$e[3]}' style='font:10px Arial; color: #009900'><i>{$e[2]}</i></a> </td> "; } $show_ads .= "</tr></table>"; echo $show_ads; } ?> select-ads.php <?php /* * * Select the Advertisements * */ include("/home/eliteace/public_html/config/connect.php"); $conAds = mysql_connect("localhost",$mysql['user'],$mysql['pass']) or die(mysql_error()); $conAds = mysql_select_db("eliteace_client",$conAds); $AdsQuery = mysql_query("SELECT * FROM `c_ads` WHERE `active`='1'") or die(mysql_error()); $AdsArr = mysql_fetch_array($AdsQuery); $ad[] = array($AdsArr['title'], $AdsArr['description'], $AdsArr['displayurl'], $AdsArr['url']); //mysql_close($conAds); ?> When I echo the array values after the $ad[] array they are displaying fine. Just don't seem to be working within the show_ads(); function. And I am receiving no errors, warnings or notices. Any help is greatly appreciated. Regards, ACE Link to comment https://forums.phpfreaks.com/topic/152952-solved-random-text-ad-not-displaying/ Share on other sites More sharing options...
Yesideez Posted April 7, 2009 Share Posted April 7, 2009 The first thing I'd do there is use ECHO to chuck some variables out to the browser to see how things are changing as the script goes on. Another thing I'd do is echo numbers out where conditionals are used so you can see exactly where the script is going - helps step through a script. echo '*1*'; if (condition) { echo '*2*'; //do some stuff } else { echo '*3*'; } *1**2* shows the condition evaluates to TRUE, *1**3* shows the condition evaluates to FALSE (just an example) Link to comment https://forums.phpfreaks.com/topic/152952-solved-random-text-ad-not-displaying/#findComment-803290 Share on other sites More sharing options...
JasonLewis Posted April 7, 2009 Share Posted April 7, 2009 I don't see why your using a global. Why don't use just pass the array in as an argument or something? Wouldn't that be easier? And besides, I always thought that the less global's you used, the better. Link to comment https://forums.phpfreaks.com/topic/152952-solved-random-text-ad-not-displaying/#findComment-803294 Share on other sites More sharing options...
MasterACE14 Posted April 7, 2009 Author Share Posted April 7, 2009 I added error_reporting(E_ALL); to the top and I'm getting... Notice: Undefined index: in /home/eliteace/public_html/ads/ads.php on line 43 which is here.... <?php for($i=0; $i<$number_of_ads; $i++){ $e = $ad[$rand_keys[$i]]; // line 43 @ProjectFear - just changed it to an argument Link to comment https://forums.phpfreaks.com/topic/152952-solved-random-text-ad-not-displaying/#findComment-803299 Share on other sites More sharing options...
JasonLewis Posted April 7, 2009 Share Posted April 7, 2009 $rand_keys shouldn't be an array, since more than likely you'll be passing $number_of_ads as 1, so $rand_keys will just contain the key for the randomly selected element. Link to comment https://forums.phpfreaks.com/topic/152952-solved-random-text-ad-not-displaying/#findComment-803302 Share on other sites More sharing options...
MasterACE14 Posted April 7, 2009 Author Share Posted April 7, 2009 select-ads.php <?php /* * * Select the Advertisements * */ include("/home/eliteace/public_html/config/connect.php"); $conAds = mysql_connect("localhost",$mysql['user'],$mysql['pass']) or die(mysql_error()); $conAds = mysql_select_db("eliteace_client",$conAds); $AdsQuery = mysql_query("SELECT * FROM `c_ads` WHERE `active`='1'") or die(mysql_error()); $AdsArr = mysql_fetch_array($AdsQuery); $ad = array($AdsArr['title'], $AdsArr['description'], $AdsArr['displayurl'], $AdsArr['url']); //mysql_close($conAds); ?> ads.php <?php /************************************************* * Simple AD Rotator * * Version: 1.0 * Date: 2007-08-30 * * Include this php file into your site and call the * show_ads() function with a number of ads * you want to display. * * Edited by ACE * * * ****************************************************/ // Image ads haven't been added yet! //USAGE $ad[] = array("Title", "Description", "display url", "url"); include("/home/eliteace/public_html/ads/select-ads.php"); // Function to display random ads from the list function show_ads($number_of_ads=1,$ad) { // maximul number of ads = number of ads if($number_of_ads > count($ad)){ $number_of_ads = count($ad); } // Initialize the random generator list($usec, $sec) = explode(' ', microtime()); srand((float) $sec + ((float) $usec * 100000)); // select random ads $rand_keys = array_rand($ad, $number_of_ads); // creade ads $show_ads = "<table><tr>"; for($i=0; $i<$number_of_ads; $i++){ $e = $ad[$rand_keys[$i]]; $show_ads .= " <td onClick=\"document.location='{$e[3]}'\" title='{$e[0]}' width='180' style='cursor:pointer'> <a href='{$e[3]}' style='font:13px Arial; font-weight:bold; color: #0000ff'>{$e[0]}</a><br> <span style='font:12px Arial; color: #333333'>{$e[1]}</span><br> <a href='{$e[3]}' style='font:10px Arial; color: #009900'><i>{$e[2]}</i></a> </td> "; } $show_ads .= "</tr></table>"; echo $show_ads; } ?> called... <?php include("ads.php"); show_ads(1,$ad); ?> problems? Notice: Undefined index: in /home/eliteace/public_html/ads/ads.php on line 41 same line <?php for($i=0; $i<$number_of_ads; $i++){ $e = $ad[$rand_keys[$i]]; // line 43 That's the latest. I got no idea. Link to comment https://forums.phpfreaks.com/topic/152952-solved-random-text-ad-not-displaying/#findComment-803328 Share on other sites More sharing options...
JasonLewis Posted April 7, 2009 Share Posted April 7, 2009 For starters, should this: $ad = array($AdsArr['title'], $AdsArr['description'], $AdsArr['displayurl'], $AdsArr['url']); Be this: $ad[] = array($AdsArr['title'], $AdsArr['description'], $AdsArr['displayurl'], $AdsArr['url']); Because each ad will be in it's own array. Then, change this: $e = $ad[$rand_keys[$i]]; To this: $e = $ad[$rand_keys]; Which leads me to my last point. You actually might have to change it a bit more because at the moment it's only set up to work with more than 1 ad being displayed. Actually, try replacing this: $e = $ad[$rand_keys[$i]]; With this: if(is_array($rand_keys)){ $e = $ad[$rand_keys[$i]]; }else{ $e = $ad[$rand_keys]; } Then when you make the $ad array, it should look like this: $ad[] = array("title","desc","text","link"); $ad[] = array("title","desc","text","link"); Etc. Hope that makes sense. Link to comment https://forums.phpfreaks.com/topic/152952-solved-random-text-ad-not-displaying/#findComment-803365 Share on other sites More sharing options...
MasterACE14 Posted April 10, 2009 Author Share Posted April 10, 2009 that works! thanks heaps everyone and ProjectFear Link to comment https://forums.phpfreaks.com/topic/152952-solved-random-text-ad-not-displaying/#findComment-806026 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.