• Resolved herculesnetwork

    (@herculesnetwork)


    how to apply condition for print values ​​of an array

    hello staff wordpress.org
    I need help from you again,

    this function shuffle numbers,

    function gen_num()
    {
    $caracteres = "012345678910111213141516171819";
    $mistura = substr(str_shuffle($caracteres),0,15);
    print $mistura;
    }

    I needed a condition to print or not the numbers at the posts, and the @leglesslizard User helped me. It works perfectly, the big problem down was that each new access to posts, or refresh the kapok, a new number / value was generated, and this could not happen, I needed to write permanently on those values ​​in the posts and below the solution but can not apply in arrays!

    function gen_num()
    {
    global $post;
    $mistura = get_post_meta( $post->ID, 'my_custom_meta', true );
    
    if ( '' == $mistura ) {
    $caracteres = "012345678910111213141516171819";
    $mistura = substr(str_shuffle($caracteres),0,10);
    update_post_meta( $post->ID, 'my_custom_meta', $mistura );
    }
    
    print $mistura;
    }

    I am now trying to make arrays and I can not!

    function shuflenames()
    {
    $input = array("Neo", "Morpheus", "Trinity", "Cypher", "Tank");
    $rand_keys = array_rand($input, 2);
    echo $input[$rand_keys[0]] . "\n";
    echo $input[$rand_keys[1]] . "\n";
    }

    my failed attempt to apply condition with arrays:

    function shuflenames()
    {
    global $post;
    $mixnames = get_post_meta( $post->ID, 'my_custom_meta', true );
    if ( '' == $mixnames) {
    $input = array("Neo", "Morpheus", "Trinity", "Cypher", "Tank");
    $rand_keys = array_rand($input, 2);
    update_post_meta( $post->ID, 'my_custom_meta', $mixnames );
    }
    echo $input[$rand_keys[0]] . "\n";
    echo $input[$rand_keys[1]] . "\n";
    }

    but the words change as the condition does not exist. every new access words are changed, they are not actually written in the posts

    Thanks to all, Best Regards.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter herculesnetwork

    (@herculesnetwork)

    Printed values ​​were not written permanently in the pages, it change every new access on pages. want a shuffle to all be different, but in the first run he writes permanently, as in the first case not writing, so I made that change with the condition to not write if already there is a value in place, he decided, but I do not make your arrays. I want someone to tell me how to do this with arrays.

    —–

    note that showed a first case that could solve, the values ​​in numeric shuffle I managed to get them to be written once, with the solution below it, I want to do that in the second case, a function with arrays, I do not do the function write truly permanent in the posts before the first case, each access generated a new number the pages, never saw the same number now managed to solve with the solution below, each page has a unique number, and ever written, not change more each refresh on the page, not to do with managing arrays 🙁

    Moderator bcworkz

    (@bcworkz)

    Thread Starter herculesnetwork

    (@herculesnetwork)

    You @bcworkz again 🙂 thank you, that your other help also worked! 🙂 Now writes permanently on the field! but I need to write more than one name, and I’m trying a few hours and I’m not getting it.

    function gen_num()
    {
      global $post;
      $mixnames = get_post_meta( $post->ID, 'fieldnames', true );
    
      if ( '' == $mixnames ) {
        $input = array("Neo", "Morpheus", "Trinity", "Cypher", "Tank");
        $rand_keys = array_rand($input, 3);
        $mixnames = $input[$rand_keys[1]];
       // $mixnames = $input[$rand_keys[2]];
       // $mixnames = $input[$rand_keys[3]];
        update_post_meta( $post->ID, 'fieldnames', $mixnames );
      }
      // DISPLAYS THE OUTCOME
      print $mixnames;
    }

    if I uncomment I break the code 🙁

    I tried so also, don’t break the code, but does not work :(:

    function gen_num()
    {
      global $post;
      $mixnames = get_post_meta( $post->ID, 'fieldnames', true );
    
      if ( '' == $mixnames ) {
        $input = array("Neo", "Morpheus", "Trinity", "Cypher", "Tank");
        $rand_keys = array_rand($input, 3);
        $mixnames = $input[$rand_keys[0]] . "\n";
        $mixnames = $input[$rand_keys[1]] . "\n";
        $mixnames = $input[$rand_keys[2]] . "\n";
        update_post_meta( $post->ID, 'fieldnames', $mixnames );
      }
      // DISPLAYS THE OUTCOME
      print $mixnames;
    }

    I need to show multiple names, how to display multiple input?
    thanks for help me again.

    Thread Starter herculesnetwork

    (@herculesnetwork)

    learned, was only concatenate strings (credits to member LTasty of the stack over flow)

    $mixnames = $input[$rand_keys[0]].$input[$rand_keys[1]].$input[$rand_keys[2]];

    thanks BCWORKZ by help, you helped me a lot!THANKS

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘how to apply condition for print values ​​of an array’ is closed to new replies.