Author Topic: PHP Newbie - Need Help with Random Array  (Read 822 times)

Jericho

  • New Member
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
PHP Newbie - Need Help with Random Array
« on: August 20, 2010, 02:49:48 PM »
I'm trying to get this random array to work properly.  Someone else helped me with the code and I can't find the problem in it.  It's part of a wordpress plugin that remotely posts to other sites.  I was attempting to add keywords to the title and permalink but for some reason it's adding all of the keywords instead of grabbing one.

Here's a partial section of the code:

PHP Code: [Select]
function syn_syndicate_box() {
    global 
$syn_multi_curl;
    
$syn_multi_curl = new MultiCurl();

    
$options syn_get_admin_options();

    
$keywords1 = array("Keyword""Keyword""Keyword""Keyword""Keyword""Keyword""Keyword");
    
$keywords2 = array("Keyword""Keyword""Keyword""Keyword""Keyword""Keyword""Keyword");

    
$post wp_get_single_post$_GET'post_id' ] );
    
$title $post->post_title;
    
$permalink get_permalink$post->ID );
    
$content $post->post_content;
    
$content strip_tags$content );
    
$content substr$content0$options'excerpt_length' ] );

    
$needle chr194 ) . chr160 );
    
$content str_replace$needle' '$content );

    
$options'bitly_url' ] = syn_shorten_url$options$permalink );

    
$sites syn_get_sites();
    foreach ( 
$sites as $site ) {
        
$randomNumber1 rand(0count(keywords1)-1);
        
$randomNumber2 rand(0count(keywords2)-1);
        
$keyword1 $keywords1[$randomNumber1];
        
$keyword2 $keywords2[$randomNumber2];

        
$title $keyword1 ." " $title " " $keyword2;


        
$site str_replace' ''_'strtolower$site ) );
        if ( isSet( 
$_POST$site '_enabled' ] ) ) {
            try {
                
call_user_func_array'syn_post_' $site, array(
                    
$options$title$content.'<p><strong><a href="' $permalink '">' $title '</a></strong></p>'
                
));


Any ideas on what the problem is?

phphelp

  • Administrator
  • Senior Member
  • *****
  • Posts: 777
  • Karma: +3/-0
    • View Profile
    • PHP Help forum
Re: PHP Newbie - Need Help with Random Array
« Reply #1 on: August 20, 2010, 03:40:00 PM »
Problem is in these two lines (missing $ for keywords1, keywords2):

PHP Code: [Select]
        $randomNumber1 rand(0count(keywords1)-1);
        
$randomNumber2 rand(0count(keywords2)-1);
Are you experienced in PHP programming and willing to share your knowledge?
Join us at php help forum!

Jericho

  • New Member
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
Re: PHP Newbie - Need Help with Random Array
« Reply #2 on: August 20, 2010, 03:50:03 PM »
Added the $ so it's now ($keywords1) but I'm still getting the same problem.

phphelp

  • Administrator
  • Senior Member
  • *****
  • Posts: 777
  • Karma: +3/-0
    • View Profile
    • PHP Help forum
Re: PHP Newbie - Need Help with Random Array
« Reply #3 on: August 20, 2010, 04:00:47 PM »
Well, you're also using $title assignment within loop - foreach($sites...). Probably you need to use different variable within loop:

PHP Code: [Select]
$title_new $keyword1 ." " $title " " $keyword2;
and then, update this part of code as well:
PHP Code: [Select]
                call_user_func_array'syn_post_' $site, array(
                    
$options$title_new$content.'<p><strong><a href="' $permalink '">' $title_new '</a></strong></p>'
));
Are you experienced in PHP programming and willing to share your knowledge?
Join us at php help forum!

Jericho

  • New Member
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
Re: PHP Newbie - Need Help with Random Array
« Reply #4 on: August 20, 2010, 04:13:15 PM »
You are the man!  ;D

Thank you that fixed it.