For loop question?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • SM

    For loop question?



    I have a couple of arrays that represent different categories. One
    array per category. The arrays contain names of photos. The arrays
    look like this:

    $cat_1 = array('moonligh t.jpg'', shadow.jpg', 'luna.jpg');
    $cat_2 = array(...);
    $cat_3 = array(...);
    $cat_4 = array(...);

    Then, i want to create a thumbnail with all the photos like this:
    for($i=0; ...){
    ....<img src="images/thumbs/<?php echo $cat_1[$i]; ?>" />
    }

    This works OK for the first array.


    What if i'm getting the name of the array from a variable? How do i
    construct my 'for loop'.

    I do it like this:

    $cat = $_GET['cat']; //output: 1
    $sel_array = 'cat_' . $cat;//create the string: cat_1

    Then i create the thumbs like this:
    for($i=0; ...){
    ....<img src="images/thumbs/<?php echo HELP!!!! ;?>" />
    }


    How do i construct the thumbnail?


    Thanks
    Marco
  • =?ISO-8859-15?Q?Iv=E1n_S=E1nchez_Ortega?=

    #2
    Re: For loop question?

    SM wrote:
    $cat_1 = array('moonligh t.jpg'', shadow.jpg', 'luna.jpg');
    >
    for($i=0; ...){
    Don't.

    When iterating through arrays, *always* use foreach().

    --
    ----------------------------------
    Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

    Now listening to: Anggun - Au nom de la lune (1997) - [7] La Mémoire des
    rochers (4:03) (74.500000%)

    Comment

    • SM

      #3
      Re: For loop question?

      On May 8, 3:24 pm, Iván Sánchez Ortega <ivansanchez-...@rroba-
      escomposlinux.-.punto.-.orgwrote:
      SM wrote:
      $cat_1 = array('moonligh t.jpg'', shadow.jpg', 'luna.jpg');
      >
      for($i=0; ...){
      >
      Don't.
      >
      When iterating through arrays, *always* use foreach().
      >
      --
      ----------------------------------
      Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-
      >
      Now listening to: Anggun - Au nom de la lune (1997) - [7] La Mémoire des
      rochers (4:03) (74.500000%)
      I'm using the for loop for pagination not to iterate throught the
      array. I've only created this example for demonstration purposes. The
      idea what to learn how to get the values of the array if the array was
      define in a variable?

      Any ideas, anyone?

      Thanks
      Marco

      Comment

      • Ivan Marsh

        #4
        Re: For loop question?

        On Thu, 08 May 2008 21:24:49 +0200, Iván Sánchez Ortega wrote:
        SM wrote:
        >
        >$cat_1 = array('moonligh t.jpg'', shadow.jpg', 'luna.jpg');
        >>
        >for($i=0; ...){
        >
        Don't.
        >
        When iterating through arrays, *always* use foreach().
        Isn't foreach simply creating a for loop based on the size of the array?

        --
        "Remain calm, we're here to protect you!"

        Comment

        • =?ISO-8859-15?Q?Iv=E1n_S=E1nchez_Ortega?=

          #5
          Re: For loop question?

          SM wrote:
          I'm using the for loop for pagination not to iterate throught the
          array. I've only created this example for demonstration purposes. The
          idea what to learn how to get the values of the array if the array was
          define in a variable?
          In that case, read the PHP manual on the subject on "variable variables".
          It's a feature seldom used, that may fit your case.

          --
          ----------------------------------
          Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

          Nunca temas decirle al mundo quién eres.
          -- Anónimo

          Comment

          • =?ISO-8859-15?Q?Iv=E1n_S=E1nchez_Ortega?=

            #6
            Re: For loop question?

            Ivan Marsh wrote:
            >When iterating through arrays, *always* use foreach().
            >
            Isn't foreach simply creating a for loop based on the size of the array?
            No.

            --
            ----------------------------------
            Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

            "Give me chastity and continence, but not yet."
            - Saint Augustine (354-430)

            Comment

            • Ivan Marsh

              #7
              Re: For loop question?

              On Thu, 08 May 2008 22:05:48 +0200, Iván Sánchez Ortega wrote:
              Ivan Marsh wrote:
              >
              >>When iterating through arrays, *always* use foreach().
              >>
              >Isn't foreach simply creating a for loop based on the size of the array?
              >
              No.
              Can you expand on that?

              --
              "Remain calm, we're here to protect you!"

              Comment

              • chander@otg-nc.com

                #8
                Re: For loop question?

                On May 8, 4:07 pm, Ivan Marsh <ivanma...@yaho o.comwrote:
                On Thu, 08 May 2008 22:05:48 +0200, Iván Sánchez Ortega wrote:
                Ivan Marsh wrote:
                >
                >When iterating through arrays, *always* use foreach().
                >
                Isn't foreach simply creating a for loop based on the size of the array?
                >
                No.
                >
                Can you expand on that?
                >
                --
                "Remain calm, we're here to protect you!"
                Foreach loops work by iterating over the keys of an array, and are
                universal in that they work with both simple arrays (sequential
                integer keys) and associative arrays (those where the keys are not
                strings).

                Furthermore, the standard form of foreach retrieves the associated
                value when it retrieves they key, hence making the iteration over the
                values in an array a bit faster (since no additional lookup is
                required).

                Lastly, a foreach loop can operate on a function that returns an array
                (as opposed to a variable that contains an array) in a fairly
                straightforward manner, whereas a for loop requires that the array be
                assigned to a variable beforehand.

                --
                Chander Ganesan
                Open Technology Group, Inc.
                One Copley Parkway, Suite 210
                Morrisville, NC 27560
                919-463-0999/877-258-8987

                Ask me about Expert on-site and public enrollment PHP Training
                delivered worldwide.

                Comment

                • Rik Wasmus

                  #9
                  Re: For loop question?

                  On Thu, 08 May 2008 21:05:03 +0200, SM <servandomonter o@gmail.comwrot e:
                  >
                  >
                  I have a couple of arrays that represent different categories. One
                  array per category. The arrays contain names of photos. The arrays
                  look like this:
                  >
                  $cat_1 = array('moonligh t.jpg'', shadow.jpg', 'luna.jpg');
                  $cat_2 = array(...);
                  $cat_3 = array(...);
                  $cat_4 = array(...);
                  A lot of people lately seem to be using this kind of construct. To put it
                  simple: _don't_ do that. The name of a variable should hold no important
                  data save for identification of the variable by both interpreter & coder..

                  The correct way to do this is:

                  $cats = array(
                  1 =array('moonlig ht.jpg' .......),
                  2 =array(....),
                  3 =array(....));

                  And then just use:

                  foreach($cats[$_GET['cat'] as $img) echo $img;
                  $cat = $_GET['cat']; //output: 1
                  $sel_array = 'cat_' . $cat;//create the string: cat_1
                  You could use ${'cat_'.$cat} (or $$sel_array). This would be an unwise
                  choice though.
                  --
                  Rik Wasmus

                  Comment

                  • =?ISO-8859-15?Q?Iv=E1n_S=E1nchez_Ortega?=

                    #10
                    Re: For loop question?

                    Rik Wasmus wrote:
                    The correct way to do this is:
                    >
                    $cats = array(
                    1 =array('moonlig ht.jpg' .......),
                    2 =array(....),
                    3 =array(....));
                    An alternate way to do the same is:

                    $cats[1] = array('moonligh t.jpg' .......);
                    $cats[2] = array('foobar' .......);
                    $cats[3] = array(...);
                    And then just use:
                    >
                    foreach($cats[$_GET['cat'] as $img) echo $img;
                    You're missing a "]" here, Rik!

                    foreach($cats[ $_GET['cat'] ] as $img) echo $img;


                    --
                    ----------------------------------
                    Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

                    Un ordenador no es un televisor ni un microondas, es una herramienta
                    compleja.

                    Comment

                    • Rik Wasmus

                      #11
                      Re: For loop question?

                      On Thu, 08 May 2008 23:14:44 +0200, Iván Sánchez Ortega
                      <ivansanchez-alg@rroba-escomposlinux.-.punto.-.orgwrote:
                      Rik Wasmus wrote:
                      >
                      >The correct way to do this is:
                      >>
                      >$cats = array(
                      >1 =array('moonlig ht.jpg' .......),
                      >2 =array(....),
                      >3 =array(....));
                      >
                      An alternate way to do the same is:
                      >
                      $cats[1] = array('moonligh t.jpg' .......);
                      $cats[2] = array('foobar' .......);
                      $cats[3] = array(...);
                      >
                      >And then just use:
                      >>
                      >foreach($cat s[$_GET['cat'] as $img) echo $img;
                      >
                      You're missing a "]" here, Rik!
                      Oh no! Now all I say is suspect, and therefor should not be followed.
                      Forget what I said, having $cat_1 & $cat_2 like variable names must be a
                      great idea :).
                      --
                      Rik Wasmus

                      Comment

                      • SM

                        #12
                        Re: For loop question?

                        On May 8, 5:21 pm, "Rik Wasmus" <luiheidsgoe... @hotmail.comwro te:
                        On Thu, 08 May 2008 23:14:44 +0200, Iván Sánchez Ortega
                        >
                        >
                        >
                        <ivansanchez-...@rroba-escomposlinux.-.punto.-.orgwrote:
                        Rik Wasmus wrote:
                        >
                        The correct way to do this is:
                        >
                        $cats = array(
                        1 =array('moonlig ht.jpg' .......),
                        2 =array(....),
                        3 =array(....));
                        >
                        An alternate way to do the same is:
                        >
                        $cats[1] = array('moonligh t.jpg' .......);
                        $cats[2] = array('foobar' .......);
                        $cats[3] = array(...);
                        >
                        And then just use:
                        >
                        foreach($cats[$_GET['cat'] as $img) echo $img;
                        >
                        You're missing a "]" here, Rik!
                        >
                        Oh no! Now all I say is suspect, and therefor should not be followed.
                        Forget what I said, having $cat_1 & $cat_2 like variable names must be a
                        great idea :).
                        --
                        Rik Wasmus
                        Thanks Rik. Try it and it work perfectly! And i didn't need to read
                        the PHP Manual. I guess that's what a forum is for.
                        Thanks again for beeing 'in the mood'
                        Marco

                        Comment

                        Working...