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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #16
    to check the keys and their associated values, check the output from print_r().

    Comment

    • ilya Kraft
      New Member
      • Jan 2011
      • 134

      #17
      I tried this:

      Code:
      $style_class = array("styles" => "story_style1","story_style2","story_style3","story_style4");
      
      $div_class = print_r($style_class);
      
      $storyDisplayList .= '<div class="'. $div_class .'"></div>
      And it gives this output:
      Code:
      Array
      (
          [styles] => story_style1
          [0] => story_style2
          [1] => story_style3
          [2] => story_style4
      )
      Am I suppose to somehow use it to achieve the task?

      Thnx

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #18
        actually, it doesn’t matter what keys you use, because array_rand() will select one of the available (regardles whether they are numeric or not).

        Comment

        • ilya Kraft
          New Member
          • Jan 2011
          • 134

          #19
          I am really stuck and confused now...

          So I will not use the print_r();

          Instead I'll do this

          Code:
          $style_class = array("styles" => "story_style1","story_style2","story_style3","story_style4");
          $div_class = array_rand($style_class);
          $storyDisplayList .= '<div class="'. $div_class .'">
          This gives output of <div class="styles"> </div>
          So what Am I suppose to do to get final result of

          Code:
          <div class="story_style1"></div>
          <div class="story_style3"></div>
          <div class="story_style2"></div>
          <div class="story_style4"></div>
          <div class="story_style3"></div>
          <div class="story_style1"></div>
          <div class="story_style4"></div>
          
          and so on ...
          I just don't understand what I need to do now...

          Thnx
          Last edited by Niheel; Jul 18 '11, 10:08 AM. Reason: please use code tags over bold

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #20
            print_r() is only for debugging purposes.

            Comment

            • ilya Kraft
              New Member
              • Jan 2011
              • 134

              #21
              Yeh, I understood that when I saw the output, but do you have any suggestion of what I should do next? You can see what I have now in my previous post, I'm now stuck with next step, I have no idea what to do to complete it. This feature is one of the main that I need for my website design, so I really need to understand and get it working... So if you have any suggestions or could point where the issue is could you please help me?

              Thank You

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #22
                I know how to do it, it’s just that I am not the best teacher. and I deem teaching necessary, as this problem turned out to be a very basic one. i.e. giving you the answer would solve the case, but you wouldn’t have a clue what went wrong in the first place.

                maybe we should have an IM session later, as there explanation goes smoother.

                Comment

                • ilya Kraft
                  New Member
                  • Jan 2011
                  • 134

                  #23
                  That would be great, you have been great help, I think that's mee who got things wrong I just got messed up and confused my selves, usually I understand where the problem is when I ask it here, but this is something new in PHP that I don't know yet.

                  If you have free time it would be great to chat, do you have skype? I don't want others to see my nick name here so if you could e-mail me your contact details …
                  Last edited by Dormilich; Jul 18 '11, 12:29 PM. Reason: noted and saved

                  Comment

                  • ilya Kraft
                    New Member
                    • Jan 2011
                    • 134

                    #24
                    Hi ;D

                    It took me a while of research, but I think I figured it out )))

                    Code:
                    $style_class = array("story_style1","story_style2","story_style3","story_style4");
                    
                    $random_class = array_rand($style_class, 1);
                    
                    var_dump($random_class);
                    
                    $div_class = $style_class[$random_class];
                    
                    $storyDisplayList .= '<div class="' . $div_class . '"></div>
                    It gives me output that I wanted in my source code, now div's have random classes that I want, but for some reason that I don't understand :/ I get this thing

                    int(1) int(2) int(0) int(3) int(3) int(2) int(3)

                    On the top of my page, http://inelmo.com < you can see what I mean there.

                    UPDATE: I also remembered that I echo out all errors while I'm in the process of creation

                    e.g

                    Code:
                    error_reporting(0);
                    ini_set('display_errors', '1');
                    Could this be what is causing the problem? As I am not sure whether that is an error or output.

                    Comment

                    • Dormilich
                      Recognized Expert Expert
                      • Aug 2008
                      • 8694

                      #25
                      It gives me output that I wanted in my source code, now div's have random classes that I want, but for some reason that I don't understand :/ I get this thing

                      int(1) int(2) int(0) int(3) int(3) int(2) int(3)
                      that is the output generated by var_dump() (line #5)

                      Comment

                      • ilya Kraft
                        New Member
                        • Jan 2011
                        • 134

                        #26
                        Ohhhh ;D Finally what a relief, it work's now just As I wanted.

                        Thank You Very Much For Awesome help and explaining I really appreciate that! Sorry for taking so much time (25 posts) ;D .

                        Comment

                        • Dormilich
                          Recognized Expert Expert
                          • Aug 2008
                          • 8694

                          #27
                          this would have been the short, unexplained answer, which I had in mind in post #4.
                          Code:
                          $style_array = array("story_style1", "story_style2", "story_style3", "story_style4");
                          $storyDisplayList .= '<div class="' .  $style_array[array_rand($style_array)] . '"></div>

                          Comment

                          Working...