Jump to content

Need PHP Help - I can't figure this out


waresoft

Recommended Posts

Hello,

I hope you guys can figure this one out for me.  I have been pulling my hair out.

I'll explain what I'm trying to do:

I have a script that creates a Google Base Feed.  It works perfect, but I just found out some bad news.  With the bulk upload feed, I can only specify 100 items for Google Adwords.  My site is growing larger by the day as more products are added.  I could use the ad_category_name attribute, however I feel it is stupid and will be a waste of clicks on untargeted traffic.  I want to make 100 ads.  Not one ad for 100 or 1000 items.

I have a variable that needs to be set to "False" for all but 100 items.  100 of them would be set to true randomly.  It can't be more than 100 and preferably exactly 100, not less. 

I believe I would need to create a variable with how many items are in the table, and use a function to figure it out from there.  This function would be called everytime a new line was written.  I don't believe it can be called recursively, because the tab delimited file will need to be created by going through my entire function one time, but calling the random (true or false) function every time before a line is written.

Can anybody offer any suggestions or examples?
Link to comment
Share on other sites

Here's an algorithm for doing that (for consecutive integer indexed arrays starting at 0)

[code=php:0]function select($array, $num) {
  $array_count = count($array);
  if ($array_count <= $num) return $array;

  $results = array();
  for ($i = 0; $i < $num; $i++) {
    $choose = rand(0, $array_count-1);
    $results[] = $array[$choose];
    $array[$choose] = $array[$array_count-1];
    unset($array[$array_count-1]);
    $array_count--;
  }

  return $results;
}[/code]


Basically, this will choose a random item from the array, copy the last element in the array into the spot where that item was, and remove the last item.  Then it'll choose another random item.

It will give you a random set of 100 items, and it'll work very quickly even with large arrays.
Link to comment
Share on other sites

Thank you very much.  However, it seems the majority of my ads got rejected because they contained "trademarks".  Duh!  I'm selling software, of course it's going to.  So, I'm just going to forget it and just upload a normal google base feed without the ads.  >:(

Thanks so much for your help and time though.  :)
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.