Need small script that can store/read a string

Q

Lifer
Jul 21, 2005
12,046
4
81
I suck at programming. I need a script where I can save Adsense pusblisher ID's, and then have them randomly (or in order) be 'inserted' into the proper place in the Adsense code in my .php file. Everytime a page is loaded so that it shows a different ad each time.

I know this must be simple, but can someone whip it up real fast for me?
 

apinomus

Senior member
Dec 14, 2005
394
0
0
Are you needing to manage a database list of ID numbers on the admin-side, and then a little script that randomly plucks one from the database on page-load and drops it into your code? That's not exactly something you could just write out here in a thread...

I can code something like this for you if you are willing to pay a bit :) PM me if interested.
 

troytime

Golden Member
Jan 3, 2006
1,996
1
0
the simplest method
an array containing all your required adsense IDs

<?
$arr = array();
$arr[] = 1111;
$arr[] = 2222;
$arr[] = 3333;
$arr[] = 4444;
$arr[] = 5555;
$arr[] = 6666;
$arr[] = 7777;

// pick a random key
$randomId = rand(0,count($arr)-1);
?>

then echo the random key wherever you need it
<?=$randomId;?>
 

Q

Lifer
Jul 21, 2005
12,046
4
81
Originally posted by: troytime
the simplest method
an array containing all your required adsense IDs

<?
$arr = array();
$arr[] = 1111;
$arr[] = 2222;
$arr[] = 3333;
$arr[] = 4444;
$arr[] = 5555;
$arr[] = 6666;
$arr[] = 7777;

// pick a random key
$randomId = rand(0,count($arr)-1);
?>

then echo the random key wherever you need it
<?=$randomId;?>

That works some of the time. Do I need anything in the [] or the () after $arr=arry(); ?
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
<?
$arr = array();
$arr[0] = 1111;
$arr[1] = 2222;
$arr[2] = 3333;
$arr[3] = 4444;
$arr[4] = 5555;
$arr[5] = 6666;
$arr[6] = 7777;

// pick a random key
$randomId = rand(0,count($arr)-1);
?>

then echo the random key wherever you need it
<?echo =$arr[$randomId];?>
 

Q

Lifer
Jul 21, 2005
12,046
4
81
Originally posted by: Crusty
<?
$arr = array();
$arr[0] = 1111;
$arr[1] = 2222;
$arr[2] = 3333;
$arr[3] = 4444;
$arr[4] = 5555;
$arr[5] = 6666;
$arr[6] = 7777;

// pick a random key
$randomId = rand(0,count($arr)-1);
?>

then echo the random key wherever you need it
<?echo =$arr[$randomId];?>

Parse error: syntax error, unexpected '=', expecting ',' or ';' in /home/photo/public_html/index.php on line 105
 

Q

Lifer
Jul 21, 2005
12,046
4
81
I did, still didn't work, it shows only 1 of the 5 ads. It keeps showing array[0] , that is the only one
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
The $randomId is probably out of scope when you try to output, so it's defaulting to 0.
 

Q

Lifer
Jul 21, 2005
12,046
4
81
Ok, hm how do I fix this? I know this is annoying you Crusty, sorry

Is it losing it when we close the php by doing ?> ?
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
No, not annoying. Basically, it seems like you have absolutely no idea what you are doing and shouldn't be messing with the sites code unless you are willing to spend some time and learn some basic php and programming skills. What you are doing is simple enough, but asking someone to debug your code when they have absolutely no idea of the end result or of any of other code on the site it's kind of quite difficult.

It's like asking a mechanic what's wrong with your car when he doesn't know any details about your car, as in what kind of engine etc.

You would be better off either learning a bit yourself, or by giving someone access to the rest of your code. Either via direct access or by pasting some more relevant pieces of code.
 

Q

Lifer
Jul 21, 2005
12,046
4
81
That's the code that is supposed to output a Google Ad with a diff. publisher ID everytime the page is refreshed but it is not working for some reason. And yes I understand what you mean Crusty, I understand I just didn't expect to have this happen.

 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
There's no need for the <?=$randomId;?> before your </div>, all that's doing is displaying the random index number you generated. Likewise, inside your actual <div> your "google_ad_client =" statement will only ever get set to that index, not that value in the array. Hence the, <? echo $arr[$randomId]; ?>, or shorthand for <?$arr[$randomId];?>
 

Q

Lifer
Jul 21, 2005
12,046
4
81
still doesn't work, it's OK, thanks for the help though. I will figure something out
 

esun

Platinum Member
Nov 12, 2001
2,214
0
0
Try putting quotes around it:

<?
$arr = array();
$arr[0] = "pub-3773532250564500";
$arr[1] = "pub-3440800076797949";
$arr[2] = "pub-1111111111111111";


// pick a random key
$randomId = rand(0,count($arr)-1);
?>

<div id="banner">
<script type="text/javascript"><!--
google_ad_client = "<?=$arr[$randomId];?>";
google_ad_width = 728;
google_ad_height = 90;
google_ad_format = "728x90_as";
google_ad_type = "text_image";
google_ad_channel = "";
google_color_border = "FFFFCC";
google_color_bg = "66B5FF";
google_color_link = "0000FF";
google_color_text = "000000";
google_color_url = "008000";
google_ui_features = "rc:10";
//-->"
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>
 

troytime

Golden Member
Jan 3, 2006
1,996
1
0
whoops, didn't know the IDs were strings and not integers - hence the requirements for quotes

also, don't do $arr[0] = "blahblah";, just leave the [] empty
php will assign the keys (and if you mess up your count, the random code won't function as planned)