1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Need PHP Help with Random Array

Discussion in 'PHP' started by Jericho33, Aug 20, 2010.

  1. #1
    First off I'm a total noob and no almost nothing about PHP. I had someone help me with the following code. It's a WP plugin that remotely posts to other sites. I am trying to make it so that I can add random keywords to the title and permalink.

    The issue is when it posts for some reason it posts all the keywords instead of picking one. Can anyone see any problems with this section of the code? It's the only part that's been edited.

    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>'
    				));
    Code (markup):
     
    Jericho33, Aug 20, 2010 IP
  2. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I don't see a problem at first glance. You should be using array_rand() to pull random elements from the array though.
     
    exam, Aug 20, 2010 IP
  3. Jericho33

    Jericho33 Peon

    Messages:
    225
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Ok I'll try that thanks.

    I must have put it in the wrong place or something because I get this error: Warning: array_rand() [function.array-rand]: First argument has to be an array
     
    Last edited: Aug 20, 2010
    Jericho33, Aug 20, 2010 IP
  4. Pudge1

    Pudge1 Well-Known Member

    Messages:
    912
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    140
    Digital Goods:
    1
    #4
    The first variable you put in array_rand() has to be an array... that should have been fairly obvious.
     
    Pudge1, Aug 20, 2010 IP