By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,089 Members | 1,952 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,089 IT Pros & Developers. It's quick & easy.

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

P: n/a
SM
Hello,

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

$cd = array(
589=>'sylver.jpg',
782=>'bigone.jpg',
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.jpg',
782=>'bigone.jpg',
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
Jun 2 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Sun, 25 May 2008 02:27:20 +0200, SM <se*************@gmail.comwrote:
Hello,

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

$cd = array(
589=>'sylver.jpg',
782=>'bigone.jpg',
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.jpg',
782=>'bigone.jpg',
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,$perpage);
foreach($current as $key =$value) //...print out...
--
Rik Wasmus
....spamrun finished
Jun 2 '08 #2

P: n/a
SM wrote:
Hello,

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

$cd = array(
589=>'sylver.jpg',
782=>'bigone.jpg',
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.jpg',
782=>'bigone.jpg',
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.
js*******@attglobal.net
==================
Jun 2 '08 #3

P: n/a
SM
On May 24, 9:01 pm, "Rik Wasmus" <luiheidsgoe...@hotmail.comwrote:
On Sun, 25 May 2008 02:27:20 +0200, SM <servandomont...@gmail.comwrote:
Hello,
I have an array that holds images path of cd covers. The array looks
like this:
$cd = array(
589=>'sylver.jpg',
782=>'bigone.jpg',
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.jpg',
782=>'bigone.jpg',
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,$perpage);
foreach($current 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,$perpage, true);
foreach($current as $key =$value) //...print out...
--

Thansk again
Marco
Jun 2 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.