How do i pick the 3 first elements and so on of the above array usingthe foreach loop function?

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

    How do i pick the 3 first elements and so on of the above array usingthe foreach loop function?

    Hello,

    I have an array that holds images path of cd covers. The array looks
    like this:

    $cd = array(
    589=>'sylver.jp g',
    782=>'bigone.jp g',
    158=>'dime.jpg'
    );

    I'm using a string key because i need to reference the CD in web pages
    like this:


    Also, because i want to be able to add elements in between and the key
    string must remain.
    $cd = array(
    364=>'Moonlight .jpg,
    589=>'sylver.jp g',
    782=>'bigone.jp g',
    925=>'liar.jpg' ,
    158=>'dime.jpg'
    );

    With this array, i'm creating a thumbnail of cd covers. I'm also using
    PHP pagination. I pick the first 3 elements and show the first 3

    cd covers and so on..


    So, here's my question: How do i pick the 3 first elements and so on
    of the above array using the foreach loop function. Or should i use a
    foreach loop in this case.

    Can i use the foreach function to loop from n element to n element?

    Here's what the code to contruct the thumbnail looks like. I'm using a
    for loop but even with the for loop, i don't know how to get the key
    string and the value referenced.

    $start=0;
    $perpage = 3;
    for($i = $start; $i < $perpage $i++)
    {
    <a href="<?php echo $SERVER['PHP_SELF']; ?>?cd=<?php echo ??get the
    key string??; ?>">
    <img src="images/cd/thumbs/<?php echo ???get the value???; ?>"
    alt="" />
    </a>
    }

    Any ideas?
    Marco
  • Rik Wasmus

    #2
    Re: How do i pick the 3 first elements and so on of the above array using the foreach loop function?

    On Sun, 25 May 2008 02:27:20 +0200, SM <servandomonter o@gmail.comwrot e:
    Hello,
    >
    I have an array that holds images path of cd covers. The array looks
    like this:
    >
    $cd = array(
    589=>'sylver.jp g',
    782=>'bigone.jp g',
    158=>'dime.jpg'
    );
    >
    I'm using a string key because i need to reference the CD in web pages
    like this:

    >
    Also, because i want to be able to add elements in between and the key
    string must remain.
    $cd = array(
    364=>'Moonlight .jpg,
    589=>'sylver.jp g',
    782=>'bigone.jp g',
    925=>'liar.jpg' ,
    158=>'dime.jpg'
    );
    >
    With this array, i'm creating a thumbnail of cd covers. I'm also using
    PHP pagination. I pick the first 3 elements and show the first 3
    >
    cd covers and so on..
    >
    >
    So, here's my question: How do i pick the 3 first elements and so on
    of the above array using the foreach loop function. Or should i use a
    foreach loop in this case.
    >
    Can i use the foreach function to loop from n element to n element?
    >
    Here's what the code to contruct the thumbnail looks like. I'm using a
    for loop but even with the for loop, i don't know how to get the key
    string and the value referenced.
    >
    $start=0;
    $perpage = 3;
    for($i = $start; $i < $perpage $i++)
    {
    <a href="<?php echo $SERVER['PHP_SELF']; ?>?cd=<?php echo ??get the
    key string??; ?>">
    <img src="images/cd/thumbs/<?php echo ???get the value???; ?>"
    alt="" />
    </a>
    }
    $start = 0;
    $perpage = 3;
    $current = array_slice($cd ,$start,$perpag e);
    foreach($curren t as $key =$value) //...print out...
    --
    Rik Wasmus
    ....spamrun finished

    Comment

    • Jerry Stuckle

      #3
      Re: How do i pick the 3 first elements and so on of the above arrayusing the foreach loop function?

      SM wrote:
      Hello,
      >
      I have an array that holds images path of cd covers. The array looks
      like this:
      >
      $cd = array(
      589=>'sylver.jp g',
      782=>'bigone.jp g',
      158=>'dime.jpg'
      );
      >
      I'm using a string key because i need to reference the CD in web pages
      like this:

      >
      Also, because i want to be able to add elements in between and the key
      string must remain.
      $cd = array(
      364=>'Moonlight .jpg,
      589=>'sylver.jp g',
      782=>'bigone.jp g',
      925=>'liar.jpg' ,
      158=>'dime.jpg'
      );
      >
      With this array, i'm creating a thumbnail of cd covers. I'm also using
      PHP pagination. I pick the first 3 elements and show the first 3
      >
      cd covers and so on..
      >
      >
      So, here's my question: How do i pick the 3 first elements and so on
      of the above array using the foreach loop function. Or should i use a
      foreach loop in this case.
      >
      Can i use the foreach function to loop from n element to n element?
      >
      Here's what the code to contruct the thumbnail looks like. I'm using a
      for loop but even with the for loop, i don't know how to get the key
      string and the value referenced.
      >
      $start=0;
      $perpage = 3;
      for($i = $start; $i < $perpage $i++)
      {
      <a href="<?php echo $SERVER['PHP_SELF']; ?>?cd=<?php echo ??get the
      key string??; ?>">
      <img src="images/cd/thumbs/<?php echo ???get the value???; ?>"
      alt="" />
      </a>
      }
      >
      Any ideas?
      Marco
      You could use a foreach loop. But if you're always going to be looking
      at 3 elements, I might think of something like (not checked for syntax
      or validity):

      function showImage($key, $value) {
      echo "<a href='{$SERVER['PHP_SELF']}cd=" .urlencode($key ) ."'>\n";
      echo "<img src='images/cd/thumbs/$value' alt=''></a>";
      }

      reset($cd);
      showImage(key($ cd), current($cd));
      next($cd);
      showImage(key($ cd), current($cd));
      next($cd);
      showImage(key($ cd), current($cd));

      Of course there are a bunch of other ways to do it, also.

      --
      =============== ===
      Remove the "x" from my email address
      Jerry Stuckle
      JDS Computer Training Corp.
      jstucklex@attgl obal.net
      =============== ===

      Comment

      • SM

        #4
        Re: How do i pick the 3 first elements and so on of the above arrayusing the foreach loop function?

        On May 24, 9:01 pm, "Rik Wasmus" <luiheidsgoe... @hotmail.comwro te:
        On Sun, 25 May 2008 02:27:20 +0200, SM <servandomont.. .@gmail.comwrot e:
        Hello,
        >
        I have an array that holds images path of cd covers. The array looks
        like this:
        >
        $cd = array(
        589=>'sylver.jp g',
        782=>'bigone.jp g',
        158=>'dime.jpg'
        );
        >
        I'm using a string key because i need to reference the CD in web pages
        like this:
        http://www.webname.com/?cd=589
        >
        Also, because i want to be able to add elements in between and the key
        string must remain.
        $cd = array(
        364=>'Moonlight .jpg,
        589=>'sylver.jp g',
        782=>'bigone.jp g',
        925=>'liar.jpg' ,
        158=>'dime.jpg'
        );
        >
        With this array, i'm creating a thumbnail of cd covers. I'm also using
        PHP pagination. I pick the first 3 elements and show the first 3
        >
        cd covers and so on..
        >
        So, here's my question: How do i pick the 3 first elements and so on
        of the above array using the foreach loop function. Or should i use a
        foreach loop in this case.
        >
        Can i use the foreach function to loop from n element to n element?
        >
        Here's what the code to contruct the thumbnail looks like. I'm using a
        for loop but even with the for loop, i don't know how to get the key
        string and the value referenced.
        >
        $start=0;
        $perpage = 3;
        for($i = $start; $i < $perpage $i++)
        {
        <a href="<?php echo $SERVER['PHP_SELF']; ?>?cd=<?php echo ??get the
        key string??; ?>">
        <img src="images/cd/thumbs/<?php echo ???get the value???; ?>"
        alt="" />
        </a>
        }
        >
        $start = 0;
        $perpage = 3;
        $current = array_slice($cd ,$start,$perpag e);
        foreach($curren t as $key =$value) //...print out...
        --
        Rik Wasmus
        ...spamrun finished
        Thanks Rik. It's such a clean & simple solution and yet so powerful.
        Just the way i like them. I've done some research about the function
        and just wanna mention to the folks out there that the array_slice()
        will reorder and reset the array indices by default. You can change
        this behaviour by setting preserve_keys to TRUE. So, in my example i
        would do it like this:
        $current = array_slice($cd ,$start,$perpag e, true);
        foreach($curren t as $key =$value) //...print out...
        --

        Thansk again
        Marco

        Comment

        Working...