0

I need to add random description to the game. Game description must contain a game title like this

'1' => 'some text1 (game_title) some text'

After that, new description send to database. Here is my code.

        $game_descr = array('1' => 'some text1 (post_title) some text' ,
                        '2' => 'some text2 (post_title) some text' ,
                        '3' => 'some text3 (post_title) some text' ,
                        '4' => 'some text4 (post_title) some text' ,
                        '5' => 'some text5 (post_title) some text' ,
                        '6' => 'some text6 (post_title) some text' ,
                        '7' => 'some text7 (post_title) some text' ,
                        '8' => 'some text8 (post_title) some text' ,
                        '9' => 'some text9 (post_title) some text' ,
    );


    $newtable = $wpdb->get_results("SELECT ID, post_title, post_content FROM wp_posts WHERE post_status = 'publish'");
    foreach ($newtable as $gametable) {
            foreach ($game_descr as $i => $value) {
                $rand_value = rand(1,9);
            }
    echo '<div class="game_descr"><textarea name="game_descr">'.$game_descr[$rand_value].'<br />'.$gametable->post_content.'</textarea></div>';
    }

I do not publis database updating code, becouse it work) So, how to add game title to the description?

4
  • Is there any relation between post_title in your $game_descr and the post_title in your query? Is the value from the query supposed to replace the value in the array? Commented May 7, 2016 at 13:19
  • Why are you doing a foreach() loop - foreach ($game_descr as $i => $value) - inside the foreach() loop from your query? You are basically overwriting $rand_value 8 times, and only getting the last value. Just do the $rand_value = rand(1,9); without the foreach() that is wrapping it. Commented May 7, 2016 at 13:21
  • $game_descr contain templates of derscription. game_title value from the query replace the game_title value in the array Commented May 7, 2016 at 13:27
  • becouse, i newbie in php). Thanks, i'll fix that. Commented May 7, 2016 at 13:33

1 Answer 1

1

Use sprintf, use %s as placeholder:

$game_descr = [
  1 => 'some text1 (%s) some text',
  // ...
];

$posts = $wpdb->get_results("SELECT ID, post_title, post_content
  FROM wp_posts WHERE post_status = 'publish'");
foreach ($posts as $p) {
  $index = mt_rand(1, count($game_descr));
  $descr = sprintf($game_descr[$index], $p->post_content);

  echo <<<EOS
<div class="game_descr">
  <textarea name="game_descr">{$descr}<br/>
  {$p->post_content}
  </textarea>
</div>;
EOS;
}
Sign up to request clarification or add additional context in comments.

2 Comments

if i need use ($s) more than 1 time, i need add this $descr = sprintf($game_descr[$index], $p->post_content, $p->post_content); right?
@skit008, adjust the format string accordingly, e.g.: sprintf("%s ... %s", $one, $two);

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.