Trouble when using 'array_rand' (displaying numbers instead of array items)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ilya Kraft
    New Member
    • Jan 2011
    • 134

    Trouble when using 'array_rand' (displaying numbers instead of array items)

    Hello,

    When I echo my articles on a webpage I need to set random style to them, to do so I need to add random class to my <div>

    This is what I've got so far

    Code:
    [U]$style_array = array("story_style1", "story_style2", "story_style3", "story_style4");
    $rand_style = array_rand($style_array);[/U]
    		
    $storyDisplayList .= '<div class="[B]' .  $rand_style . '[/B]"></div>
    Then I echo $storyDisplayLi st somewhere on my page to show articles. I thought that by random class to <div> in StoryDisplayLis t I could set different styles, but instead of giving it a random class of story_style1 it gives it a class of 0 or sometimes array.

    Does anyone here has any idea of how can I fix it.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    but instead of giving it a random class of story_style1 it gives it a class of 0 or sometimes array.
    but that is what array_rand() is supposed to return.

    from the manual:
    Return Values
    If you are picking only one entry, array_rand() returns the key for a random entry. Otherwise, it returns an array of keys for the random entries. This is done so that you can pick random keys as well as values out of the array.

    Comment

    • ilya Kraft
      New Member
      • Jan 2011
      • 134

      #3
      Hi, thnx, but is there a way to randomly select those "story_styl e1 etc." from array? nothing else just those defined classes I want to use?

      thnx

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        what about $style_array[array_rand($sty le_array)];?

        Comment

        • ilya Kraft
          New Member
          • Jan 2011
          • 134

          #5
          Hi,

          I tried what you suggested, but still instead of setting class of story_style1, story_style 2 etc... it gives class of "Array" to all div's.

          The Code:

          Code:
          $style_array = array("story_style1", "story_style2", "story_style3", "story_style4");
          $style_array[array_rand($style_array)];
          
          $storyDisplayList .= '<div class="' .  $style_array . '"></div>
          
          
          
          //And somewhere in the <body> of the page I echo $storyDisplayList to show all articles.
          In case if I sound confusing here is what I want to do again:

          I want to select random class from this list

          "story_styl e1", "story_styl e2", "story_styl e3", "story_styl e4"

          And put it in the $story_style variable so I can than place it in a place of class <div class="' . $story_style2 . '"></div> and have different designs for content that is in that div.

          Thank You

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            I understood what you intend, but could you explain what line #3 is supposed to do?

            besides that, if you echo out an array it prints "Array".

            Comment

            • ilya Kraft
              New Member
              • Jan 2011
              • 134

              #7
              Alright ))) So I thought that by using line 3 I could get random value (story_style1, as an example) from the array in line 2.

              So than the variable $style_array could be placed as the class of the div, so when viewed in browser that div would have a class of story_style1. And so on, each div on the page would have it's random class selected from line 2, so I can give them 4 different styles.

              Right now the CODE I used above is not working, it gives class of Array ( <div class="Array"></div> ) to all div's on the page.

              Thank You

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                you think too much (no offence meant, but a script doesn’t think either, it just does what it is told to do)

                the variable $style_array is defined on line #2 and not changed throughout the entire script (shown). why should it then at line #5 suddenly contain a string?

                Comment

                • ilya Kraft
                  New Member
                  • Jan 2011
                  • 134

                  #9
                  Uff, I don't quiet understand what you mean. Well I guess, by including it on line #5, $story_array it should than show in the browser like that div has a class equal to one that is in array on line #2. I tried removing line #3 from the code, but the code is still giving me <div class="Array"></div> instead of <div class="story_st yle1"></div> or <div class="story_st yle2"></div> etc.

                  Comment

                  • Dormilich
                    Recognized Expert Expert
                    • Aug 2008
                    • 8694

                    #10
                    the point is, line #3 does nothing. you fetch a random element from the array – and dump it in the void.
                    compare the following:
                    Code:
                    var_dump($style_array[array_rand($style_array)]);
                    var_dump($style_array);

                    Comment

                    • ilya Kraft
                      New Member
                      • Jan 2011
                      • 134

                      #11
                      So, I don't need line 3 at all? I tried removing it so now I only have

                      Code:
                      $style_array = array("story_style1", "story_style2", "story_style3", "story_style4");
                      But this gives me the output of <div class="Array"></div>.

                      Do I need to use array_rand function? How can I select values of those story_style1, story_style2, story_style3 etc. and place them as a class??? I'm really confused now, sorry for tones of my questions, but I can't figure it out. I followed the manual on this:



                      But it didn't work for me. Could you please suggest a right method that can achieve the task? I mean is it array_rand or I should be using something else?

                      Thank You

                      Comment

                      • Dormilich
                        Recognized Expert Expert
                        • Aug 2008
                        • 8694

                        #12
                        your problem is nor array_rand(), your problem is understanding variables and assignments (sorry to be that bold).

                        as you already know, array_rand() selects a random key and I assume you know how to get an array value by means of its associated key.

                        the thing where I can’t follow you is $storyDisplayLi st .= '<div class="' . $style_array . '"></div>'; and why you expect this to give you for instance "story_styl e1". $style_array is an Array, period. why should it be "story_styl e1"?

                        Comment

                        • ilya Kraft
                          New Member
                          • Jan 2011
                          • 134

                          #13
                          Right,
                          I looked at manual again



                          And I'm trying to understand it...

                          Will this work:

                          Code:
                          $style_array  = array("story_style1", "story_style2", "story_style3", "story_style4");
                          $style = array_rand($style_array , [B][U]2[/U][/B]); //Manual Has this number 2, What is it suppouse to do here?
                          Here, I assume that $style will have one of the values from array in $style_array.

                          So I can now include it in my display list
                          Code:
                          $storyDisplayList .= '<div class="' . $style . '"></div>';
                          And If I'm right, when I echo $storyDisplayLi st it should show as <dic class="story_st yle1"></div>

                          I think my problem could be (as you mentioned) in understanding how to get an array value by means of its associated key. I think this is a part which I'm stuck with. How exactly does array_rand() give a key to the array and how to use it?

                          Thank You

                          Comment

                          • Dormilich
                            Recognized Expert Expert
                            • Aug 2008
                            • 8694

                            #14
                            Code:
                            $style = array_rand($style_array , 2); //Manual Has this number 2, What is it suppouse to do here?
                            because in that particular example, they wanted 2 random keys.

                            cf.
                            Description
                            mixed array_rand ( array $input [, int $num_req = 1 ] )

                            Picks one or more random entries out of an array, and returns the key (or keys) of the random entries.


                            I think my problem could be (as you mentioned) in understanding how to get an array value by means of its associated key. […] How exactly does array_rand() give a key to the array and how to use it?
                            the one has nothing to do with the other. for a description of arrays check its manual section.
                            Last edited by Dormilich; Jul 17 '11, 07:51 PM.

                            Comment

                            • ilya Kraft
                              New Member
                              • Jan 2011
                              • 134

                              #15
                              Ok I see,

                              So now when I went through that manual I think I understand what key's are.

                              So i'm not sure, but is this correct?

                              Code:
                              $style = array("styles" => "story_class1", "story_class2", "story_class3", "story_class4");
                              
                              //And then in a displaylist ...
                              
                              $storyDisplayList .= '<div class="' . $style["styles"] . '"></div>
                              Will this achieve the task, if I have 4 values for one key, does this mean that it would randomly take one of those four?

                              Update:

                              I tried it out, it works, but always gives me story_class1, now I only need help in figuring out how to display other 3 values from an array, would I use a key on every value and than somehow random it?

                              Thank You

                              Comment

                              Working...