PHP Newbie - Need Help with Random Array

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]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( $content, 0, $options[ 'excerpt_length' ] );

$needle = chr( 194 ) . chr( 160 );
$content = str_replace( $needle, ' ', $content );

$options[ 'bitly_url' ] = syn_shorten_url( $options, $permalink );

$sites = syn_get_sites();
foreach ( $sites as $site ) {
    $randomNumber1 = rand(0, count(keywords1)-1);
    $randomNumber2 = rand(0, count(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>'
            ));[/php]

Any ideas on what the problem is?

Problem is in these two lines (missing $ for keywords1, keywords2):

[php] $randomNumber1 = rand(0, count(keywords1)-1);
$randomNumber2 = rand(0, count(keywords2)-1);[/php]

Added the $ so it’s now ($keywords1) but I’m still getting the same problem.

Well, you’re also using $title assignment within loop - foreach($sites…). Probably you need to use different variable within loop:

[php]$title_new = $keyword1 ." " . $title . " " . $keyword2;[/php]
and then, update this part of code as well:
[php] call_user_func_array( ‘syn_post_’ . $site, array(
$options, $title_new, $content.’

’ . $title_new . ‘


));[/php]

You are the man! ;D

Thank you that fixed it.

Sponsor our Newsletter | Privacy Policy | Terms of Service