473,322 Members | 1,188 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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

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
3 2657
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Randell D. | last post by:
Folks, I know I could do this with a foreach loop but it looks dirty. I'm wondering if I can do this via array_walk() or asort() and would appreciate some help.. I have an array - an example...
1
by: H. Adam | last post by:
hi ! i m trying to calculate an average array from an 2d data array. my problem is that my function does not generate an average array but just one value. but i do not understand why it does...
18
by: deko | last post by:
I have a counter file that records page hits - each hit is a UNIX timestamp in the file. But I'm only interested in page hits in the last 365 days. The below code creates an array from the file...
32
by: James Curran | last post by:
I'd like to make the following proposal for a new feature for the C# language. I have no connection with the C# team at Microsoft. I'm posting it here to gather input to refine it, in an "open...
12
by: neeraj | last post by:
Hi Can any body give me the syntax for summing the elements of the an array , without looping thanks
4
by: Sandman | last post by:
Hi, So I have 2 arrays: one contains userids. It may look like: user_id =12, user_id =30, user_id =43 The other is a multi-dimensional array with fields like: user_info = Array (
29
by: Jon Slaughter | last post by:
Is it safe to remove elements from an array that foreach is working on? (normally this is not the case but not sure in php) If so is there an efficient way to handle it? (I could add the indexes to...
4
by: mab464 | last post by:
I have this code on my WAMP server running on my XP machine if ( isset( $_POST ) ) { for($i=0; $i<count($_POST);$i++) { if ($ans != NULL ) $ans .= ", " . $_POST ; // Not the first...
2
by: virtualweb | last post by:
Hello guys: Im a perl beginner (self taught) and Im unable to get elements from an array. Here is my situation step by step: 1) A list of emails is submitted, (one email per line), through...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.