RE: [PHP] Foreach question
And that's exactly how I did it :) Since 'for' is traditionally pre-testing with excetuting the condition prior looping it's working well :) Thanks for all the help! Cheers, Tamas -Original Message- From: Louis Huppenbauer [mailto:louis.huppenba...@gmail.com] Sent: Tuesday, July 05, 2011 5:47 PM To: Robert Cummings Cc: Dajka Tamás; php-general@lists.php.net Subject: Re: [PHP] Foreach question Just use count($arr) in your for-header, as it get's executed again for each loop. 1), array('id'=>2)); for($i=0;$i $arr[$i]['id']+1); } } ?> 2011/7/5 Robert Cummings : > On 11-07-05 10:48 AM, Dajka Tamás wrote: >> >> Thanks, that was interesting :) I think I got one step further in >> understanding PHP :) >> >> BTW, I've changed the loop to 'for' and it's working well :) > > Can you show us your for loop? I'm not immediately sure how you use a for > loop to traverse a growing number of entries in an array without either > updating the extents of the traversal or using for( ; ; ) which is the same > as while( 1 ). Or are you now using the low level array traversal functions > like reset() and next()? > > Cheers, > Rob. > -- > E-Mail Disclaimer: Information contained in this message and any > attached documents is considered confidential and legally protected. > This message is intended solely for the addressee(s). Disclosure, > copying, and distribution are prohibited unless authorized. > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Foreach question
On 11-07-05 11:46 AM, Louis Huppenbauer wrote: Just use count($arr) in your for-header, as it get's executed again for each loop. 1), array('id'=>2)); for($i=0;$i $arr[$i]['id']+1); } } ?> Ok, so the extents are being updated on each pass of the loop :) Cheers, Rob. -- E-Mail Disclaimer: Information contained in this message and any attached documents is considered confidential and legally protected. This message is intended solely for the addressee(s). Disclosure, copying, and distribution are prohibited unless authorized. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Foreach question
Just use count($arr) in your for-header, as it get's executed again for each loop. 1), array('id'=>2)); for($i=0;$i $arr[$i]['id']+1); } } ?> 2011/7/5 Robert Cummings : > On 11-07-05 10:48 AM, Dajka Tamás wrote: >> >> Thanks, that was interesting :) I think I got one step further in >> understanding PHP :) >> >> BTW, I've changed the loop to 'for' and it's working well :) > > Can you show us your for loop? I'm not immediately sure how you use a for > loop to traverse a growing number of entries in an array without either > updating the extents of the traversal or using for( ; ; ) which is the same > as while( 1 ). Or are you now using the low level array traversal functions > like reset() and next()? > > Cheers, > Rob. > -- > E-Mail Disclaimer: Information contained in this message and any > attached documents is considered confidential and legally protected. > This message is intended solely for the addressee(s). Disclosure, > copying, and distribution are prohibited unless authorized. > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Foreach question
On Tue, Jul 5, 2011 at 2:40 PM, Dajka Tamas wrote: > I've bumped into an interesting thing with foreach. I really don't know, if > this is normal working, or why it is, so I got curious. > > The script: > > foreach ( $cats as &$c ) { > echo $c['id']; > if ( $c['id'] < 5 ) { >$c['id']++; >$cats[] = $c; > } > } > > Input 1: > > $cats = array( array( 'id' => 1 ) ); > > Output 1: > > 1 > > Input 2: > > $cats = array( array( 'id' => 1 ), array( 'id' => 2 ) ); > > Output 2: > > 122334455 > > Why is this? Is this normal behaviour? > Looking at the implementation of foreach in the source, the pointer to the next item in the array is calculated after evaluating the condition but before executing that loop. Thus, with a single array element it decides it's at the end of the array before the first loop. My C is a little rusty so I might have the details slightly wrong, but that's the crux of what's happening. Whether that's normal and expected or a bug is one of the internals team, but my guess is that it's a "feature" rather than a bug. -Stuart -- Stuart Dallas 3ft9 Ltd http://3ft9.com/
Re: [PHP] Foreach question
On 11-07-05 10:48 AM, Dajka Tamás wrote: Thanks, that was interesting :) I think I got one step further in understanding PHP :) BTW, I've changed the loop to 'for' and it's working well :) Can you show us your for loop? I'm not immediately sure how you use a for loop to traverse a growing number of entries in an array without either updating the extents of the traversal or using for( ; ; ) which is the same as while( 1 ). Or are you now using the low level array traversal functions like reset() and next()? Cheers, Rob. -- E-Mail Disclaimer: Information contained in this message and any attached documents is considered confidential and legally protected. This message is intended solely for the addressee(s). Disclosure, copying, and distribution are prohibited unless authorized. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Foreach question
On 11-07-05 09:40 AM, Dajka Tamas wrote: foreach ( $cats as&$c ) { echo $c['id']; if ( $c['id']< 5 ) { $c['id']++; $cats[] = $c; } } Given that you seem to want the above functionality obtained when more than one element exists in the input array... the simplest way (I can bother to think up) to achieve what you want with little extra work is to do the following: Cheers, Rob. -- E-Mail Disclaimer: Information contained in this message and any attached documents is considered confidential and legally protected. This message is intended solely for the addressee(s). Disclosure, copying, and distribution are prohibited unless authorized. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Foreach question
Thanks, that was interesting :) I think I got one step further in understanding PHP :) BTW, I've changed the loop to 'for' and it's working well :) -Original Message- From: Robert Cummings [mailto:rob...@interjinn.com] Sent: Tuesday, July 05, 2011 4:45 PM To: Dajka Tamás Cc: php-general@lists.php.net Subject: Re: [PHP] Foreach question On 11-07-05 10:39 AM, Dajka Tamás wrote: > Ok, but if it would be that way I shouldn't get '122334455' for second > output, no? The item count increments with every iteration of the loop. > > Or you're saying that, it checks for an existance of nextitem before every > loop, and that will fail with just one element, but will always return true > with two elements? ( since the first elements copy is pushed as third > element, etc ) BTW, there are reasons you might calculate next item before iterating. If the current iteration removes the current item, then nextItem would still be valid (theoretically :). It's more often the case you might remove the current item, than try to remove the next item in a foreach iteration. Cheers, Rob. -- E-Mail Disclaimer: Information contained in this message and any attached documents is considered confidential and legally protected. This message is intended solely for the addressee(s). Disclosure, copying, and distribution are prohibited unless authorized. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Foreach question
On 11-07-05 10:39 AM, Dajka Tamás wrote: Ok, but if it would be that way I shouldn't get '122334455' for second output, no? The item count increments with every iteration of the loop. Or you're saying that, it checks for an existance of nextitem before every loop, and that will fail with just one element, but will always return true with two elements? ( since the first elements copy is pushed as third element, etc ) BTW, there are reasons you might calculate next item before iterating. If the current iteration removes the current item, then nextItem would still be valid (theoretically :). It's more often the case you might remove the current item, than try to remove the next item in a foreach iteration. Cheers, Rob. -- E-Mail Disclaimer: Information contained in this message and any attached documents is considered confidential and legally protected. This message is intended solely for the addressee(s). Disclosure, copying, and distribution are prohibited unless authorized. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Foreach question
On 11-07-05 10:39 AM, Dajka Tamás wrote: Ok, but if it would be that way I shouldn't get '122334455' for second output, no? The item count increments with every iteration of the loop. Or you're saying that, it checks for an existance of nextitem before every loop, and that will fail with just one element, but will always return true with two elements? ( since the first elements copy is pushed as third element, etc ) Exactly... it's not counting at all. If it were, you wouldn't get to so many iterations with only 2 entries in the array. I can't remember exactly how PHP stores arrays (some kind of bucket structure), but it's likely it traverses the items like a linked list using pointers from one to the next for efficiency. Cheers, Rob. -- E-Mail Disclaimer: Information contained in this message and any attached documents is considered confidential and legally protected. This message is intended solely for the addressee(s). Disclosure, copying, and distribution are prohibited unless authorized. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Foreach question
Ok, but if it would be that way I shouldn't get '122334455' for second output, no? The item count increments with every iteration of the loop. Or you're saying that, it checks for an existance of nextitem before every loop, and that will fail with just one element, but will always return true with two elements? ( since the first elements copy is pushed as third element, etc ) -Original Message- From: Robert Cummings [mailto:rob...@interjinn.com] Sent: Tuesday, July 05, 2011 4:28 PM To: Dajka Tamás Cc: php-general@lists.php.net Subject: Re: [PHP] Foreach question On 11-07-05 10:20 AM, Dajka Tamás wrote: > Hi, > > Yeah, I'm really want to do that, since I'm working with the elements of the > original array ( skipped that part in sample code ). > > I've tried your suggestion, but it gives the same result, so on just one > input is just gives back '1'. Ahhh... you want the behaviour of the multiple elements... I presumed you wanted the other way around. > What troubles me, that foreach gives an inconsistent working. Why is > 'foreach' checking element count at all and working differently with > different element counts? That's not normal is my opinion. 'foreach' > shouldn't do this: > > if ( count($elements) == 1 ) then loop 1; > else loop normally; > > and that's what is does now, since when it's more than one element it's > working like a while loop, with checking the condition before ( and after ) > every run. ( if 'foreach' would check that the current run is the last one > before executing the current loop, the results would be the same with each > case ) You're making an assumption that it is checking the count. It may just be pre-determining whether another element exists for the next iteration. Consider the following pseudo code: nextItem = items->reset(); while( nextItem ) { item = nextItem; nextItem = items->next(); // Do stuff. } There's lots of ways to program a loop... and your PHP foreach loop is being converted to something entirely different internally. The above doesn't count elements, but it will result in the same behaviour as you are experiencing. Cheers, Rob. -- E-Mail Disclaimer: Information contained in this message and any attached documents is considered confidential and legally protected. This message is intended solely for the addressee(s). Disclosure, copying, and distribution are prohibited unless authorized. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Foreach question
I don't think that it does this: if ( count($elements) == 1 ) then loop 1; else loop normally; It's probably more something like that: $i=count($elements); loop: $i--; if($i == 0) $last_loop = true; else $last_loop = false if($last_loop) exit; else goto loop; But aside from that, I would propose you the same thing Robert already did - Just use while or some other loop (for maybe?). -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Foreach question
On 11-07-05 10:20 AM, Dajka Tamás wrote: Hi, Yeah, I'm really want to do that, since I'm working with the elements of the original array ( skipped that part in sample code ). I've tried your suggestion, but it gives the same result, so on just one input is just gives back '1'. Ahhh... you want the behaviour of the multiple elements... I presumed you wanted the other way around. What troubles me, that foreach gives an inconsistent working. Why is 'foreach' checking element count at all and working differently with different element counts? That's not normal is my opinion. 'foreach' shouldn't do this: if ( count($elements) == 1 ) then loop 1; else loop normally; and that's what is does now, since when it's more than one element it's working like a while loop, with checking the condition before ( and after ) every run. ( if 'foreach' would check that the current run is the last one before executing the current loop, the results would be the same with each case ) You're making an assumption that it is checking the count. It may just be pre-determining whether another element exists for the next iteration. Consider the following pseudo code: nextItem = items->reset(); while( nextItem ) { item = nextItem; nextItem = items->next(); // Do stuff. } There's lots of ways to program a loop... and your PHP foreach loop is being converted to something entirely different internally. The above doesn't count elements, but it will result in the same behaviour as you are experiencing. Cheers, Rob. -- E-Mail Disclaimer: Information contained in this message and any attached documents is considered confidential and legally protected. This message is intended solely for the addressee(s). Disclosure, copying, and distribution are prohibited unless authorized. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Foreach question
Tried, gives the same result with one element :( What's working: $cats = array( array( 'id' => 1 ) ); while ( $c = array_shift($cats) ) { echo $c['id']; if ( $c['id'] < 5 ) { $c['id']++; $cats[] = $c; } } But this is 'while' and it pops all elements from the array... Cheers, Tamas -Original Message- From: Louis Huppenbauer [mailto:louis.huppenba...@gmail.com] Sent: Tuesday, July 05, 2011 4:12 PM To: Robert Cummings Cc: Dajka Tamas; php-general@lists.php.net Subject: Re: [PHP] Foreach question Or maybe he tried to do the following? ($c['id'] + 1)); } } ?> 2011/7/5 Robert Cummings : > > On 11-07-05 09:40 AM, Dajka Tamas wrote: >> >> Hi all, >> >> >> >> I've bumped into an interesting thing with foreach. I really don't know, >> if >> this is normal working, or why it is, so I got curious. >> >> >> >> The script: >> >> >> >> foreach ( $cats as&$c ) { >> >>echo $c['id']; >> >>if ( $c['id']< 5 ) { >> >> $c['id']++; >> >> $cats[] = $c; >> >>} >> >> } > > That's a bizarre loop... you're feeding references to elements of the array > back into the array over which the loop is iterating. If you REALLY want to > do what you are doing, then do the following: > > > foreach( array_keys( $cats ) as $key ) > { >$c = &$cats[$key]; > >echo $c['id']; > >if( $c['id'] < 5 ) >{ >$c['id']++; >$cats[] = $c; >} > } > > ?> > > Cheers, > Rob. > -- > E-Mail Disclaimer: Information contained in this message and any > attached documents is considered confidential and legally protected. > This message is intended solely for the addressee(s). Disclosure, > copying, and distribution are prohibited unless authorized. > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Foreach question
Hi, Yeah, I'm really want to do that, since I'm working with the elements of the original array ( skipped that part in sample code ). I've tried your suggestion, but it gives the same result, so on just one input is just gives back '1'. What troubles me, that foreach gives an inconsistent working. Why is 'foreach' checking element count at all and working differently with different element counts? That's not normal is my opinion. 'foreach' shouldn't do this: if ( count($elements) == 1 ) then loop 1; else loop normally; and that's what is does now, since when it's more than one element it's working like a while loop, with checking the condition before ( and after ) every run. ( if 'foreach' would check that the current run is the last one before executing the current loop, the results would be the same with each case ) Cheers, Tamas -Original Message- From: Robert Cummings [mailto:rob...@interjinn.com] Sent: Tuesday, July 05, 2011 4:06 PM To: Dajka Tamas Cc: php-general@lists.php.net Subject: Re: [PHP] Foreach question On 11-07-05 09:40 AM, Dajka Tamas wrote: > Hi all, > > > > I've bumped into an interesting thing with foreach. I really don't know, if > this is normal working, or why it is, so I got curious. > > > > The script: > > > > foreach ( $cats as&$c ) { > > echo $c['id']; > > if ( $c['id']< 5 ) { > >$c['id']++; > >$cats[] = $c; > > } > > } That's a bizarre loop... you're feeding references to elements of the array back into the array over which the loop is iterating. If you REALLY want to do what you are doing, then do the following: Cheers, Rob. -- E-Mail Disclaimer: Information contained in this message and any attached documents is considered confidential and legally protected. This message is intended solely for the addressee(s). Disclosure, copying, and distribution are prohibited unless authorized. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Foreach question
Or maybe he tried to do the following? ($c['id'] + 1)); } } ?> 2011/7/5 Robert Cummings : > > On 11-07-05 09:40 AM, Dajka Tamas wrote: >> >> Hi all, >> >> >> >> I've bumped into an interesting thing with foreach. I really don't know, >> if >> this is normal working, or why it is, so I got curious. >> >> >> >> The script: >> >> >> >> foreach ( $cats as&$c ) { >> >> echo $c['id']; >> >> if ( $c['id']< 5 ) { >> >> $c['id']++; >> >> $cats[] = $c; >> >> } >> >> } > > That's a bizarre loop... you're feeding references to elements of the array > back into the array over which the loop is iterating. If you REALLY want to > do what you are doing, then do the following: > > > foreach( array_keys( $cats ) as $key ) > { > $c = &$cats[$key]; > > echo $c['id']; > > if( $c['id'] < 5 ) > { > $c['id']++; > $cats[] = $c; > } > } > > ?> > > Cheers, > Rob. > -- > E-Mail Disclaimer: Information contained in this message and any > attached documents is considered confidential and legally protected. > This message is intended solely for the addressee(s). Disclosure, > copying, and distribution are prohibited unless authorized. > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Foreach question
On 11-07-05 09:40 AM, Dajka Tamas wrote: Hi all, I've bumped into an interesting thing with foreach. I really don't know, if this is normal working, or why it is, so I got curious. The script: foreach ( $cats as&$c ) { echo $c['id']; if ( $c['id']< 5 ) { $c['id']++; $cats[] = $c; } } That's a bizarre loop... you're feeding references to elements of the array back into the array over which the loop is iterating. If you REALLY want to do what you are doing, then do the following: Cheers, Rob. -- E-Mail Disclaimer: Information contained in this message and any attached documents is considered confidential and legally protected. This message is intended solely for the addressee(s). Disclosure, copying, and distribution are prohibited unless authorized. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Foreach question
Hi there I think that foreach in your first example just knowns that this should be the last loop (as the array only contains 1 element at start) and so stops there. In your 2nd example however the first loop isn't the last, so the array get's checked again, and now there's another element, so... I think that's more or less normal behaviour. Sincerely yours Louis 2011/7/5 Dajka Tamas : > Hi all, > > > > I've bumped into an interesting thing with foreach. I really don't know, if > this is normal working, or why it is, so I got curious. > > > > The script: > > > > foreach ( $cats as &$c ) { > > echo $c['id']; > > if ( $c['id'] < 5 ) { > > $c['id']++; > > $cats[] = $c; > > } > > } > > > > Input 1: > > > > $cats = array( array( 'id' => 1 ) ); > > > > Output 1: > > > > 1 > > > > Input 2: > > > > $cats = array( array( 'id' => 1 ), array( 'id' => 2 ) ); > > > > Output 2: > > > > 122334455 > > > > > > Why is this? Is this normal behaviour? > > > > > > Thanks, > > > > Tamas > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Foreach question
Hi all, I've bumped into an interesting thing with foreach. I really don't know, if this is normal working, or why it is, so I got curious. The script: foreach ( $cats as &$c ) { echo $c['id']; if ( $c['id'] < 5 ) { $c['id']++; $cats[] = $c; } } Input 1: $cats = array( array( 'id' => 1 ) ); Output 1: 1 Input 2: $cats = array( array( 'id' => 1 ), array( 'id' => 2 ) ); Output 2: 122334455 Why is this? Is this normal behaviour? Thanks, Tamas
Re: [PHP] foreach question
On Jul 29, 2008, at 3:33 PM, Daniel Brown wrote: On Tue, Jul 29, 2008 at 3:25 PM, Jason Pruim <[EMAIL PROTECTED]> wrote: function random($random){ $randomQuery = "SELECT * FROM `current` ORDER BY Rand() LIMIT 2"; $result = mysql_query($randomQuery); $row[] = $result; foreach($row as $key => $value) { $random[$key] = $value; } return $random; }//End of function ?> You're missing mysql_fetch_array(), mysql_fetch_assoc(), or something of the like. Example: $v) { $random[$k] = $v; } // code } ?> Added that, then changed how I was calling it and it works great now... Thanks for looking... The problem was definitely between the chair and the keyboard on this one. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] foreach question
You cannot do this: $row[] = $result; You need to loop around this: $row = mysql_fetch_assoc($result); Thank you, Micah Gersten onShore Networks Internal Developer http://www.onshore.com Jason Pruim wrote: > Hey Everyone... > > So I am attempting to pull 2 random records from a MySQL database, so > I wrote a function which I'll paste below. I had it mostly working > with a while() statement, but I wanted to try a foreach to see if I > could get the formatting a little bit better. > > Basically... What it does is grab 2 records at random from the > database, and display the images. What I want is something that looks > like this: VS > > right now though... I'm at a lose to figure out why it's not returning > any records but not throwing any errors... Any ideas what I'm missing? > > //function for pulling random pictures from the database > > > function random($random){ > > $randomQuery = "SELECT * FROM `current` ORDER BY Rand() LIMIT 2"; > > $result = mysql_query($randomQuery); > $row[] = $result; > > > foreach($row as $key => $value) { > $random[$key] = $value; > > } > > return $random; > > }//End of function > > > ?> > > Any ideas? > > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] foreach question
On Tue, Jul 29, 2008 at 3:25 PM, Jason Pruim <[EMAIL PROTECTED]> wrote: > > function random($random){ > >$randomQuery = "SELECT * FROM `current` ORDER BY Rand() LIMIT 2"; > >$result = mysql_query($randomQuery); > $row[] = $result; > > > foreach($row as $key => $value) { > $random[$key] = $value; > > } > > return $random; > > }//End of function > > > ?> You're missing mysql_fetch_array(), mysql_fetch_assoc(), or something of the like. Example: $v) { $random[$k] = $v; } // code } ?> -- Better prices on dedicated servers: Intel 2.4GHz/60GB/512MB/2TB $49.99/mo. Intel 3.06GHz/80GB/1GB/2TB $59.99/mo. Dedicated servers, VPS, and hosting from $2.50/mo. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] foreach question
Hey Everyone... So I am attempting to pull 2 random records from a MySQL database, so I wrote a function which I'll paste below. I had it mostly working with a while() statement, but I wanted to try a foreach to see if I could get the formatting a little bit better. Basically... What it does is grab 2 records at random from the database, and display the images. What I want is something that looks like this: VS right now though... I'm at a lose to figure out why it's not returning any records but not throwing any errors... Any ideas what I'm missing? $value) { $random[$key] = $value; } return $random; }//End of function ?> Any ideas? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Foreach question
On Nov 15, 2007, at 7:50 PM, Juan Marcelo Rodríguez wrote: 2007/11/15, Philip Thompson <[EMAIL PROTECTED]>: On Nov 15, 2007 5:12 PM, Juan Marcelo Rodríguez < [EMAIL PROTECTED]> wrote: Yes, I made a mistake in the first sentence. The code is : [...] foreach ($equipos as $key => $val){ echo ""; echo "1" . ""; // I would like to add the counter here reeplacing "1" echo "".($key+1).""; This is assuming $key starts at 0 and increments by 1 each iteration However, if you can't assume that, just have a separate counter: $c) { echo "".(++$counter).""; ... } very good idea. It also assumes that the array in numerically indexed and corresponds to the value you expect. The counter is safer. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Foreach question
$x = 0; foreach ( $blah as $bleh ) { $x = $x + 1; print "$x: $bleh"; } HI, I'm working with an associative array, and generating its data a form. I use foreach to loops the contents of the array and echo to print the table and the data. Everything goes well, however I would like to add a counter to print the row's number. The question : Am I able to add a counter within foreach to print the row number in each row using echo ?. I tried a few things but I couldn't. Thanks! Juan -- Michael McGlothlin Southwest Plumbing Supply -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Foreach question
Juan Marcelo Rodríguez wrote: I'm working with an associative array, and generating its data a form. I use foreach to loops the contents of the array and echo to print the table and the data. Everything goes well, however I would like to add a counter to print the row's number. The question : Am I able to add a counter within foreach to print the row number in each row using echo ?. I tried a few things but I couldn't. The mind boggles when wondering what you tried. $counter = 1; foreach ($array as $val) { // Do your stuff here // Increment the counter $counter++; } -Stut -- http://stut.net/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Foreach question
On Nov 15, 2007 5:12 PM, Juan Marcelo Rodríguez <[EMAIL PROTECTED]> wrote: > Yes, I made a mistake in the first sentence. > > The code is : > [...] > > foreach ($equipos as $key => $val){ > > echo ""; > echo "1" . ""; // I would like to add the counter here > reeplacing > "1" echo "".($key+1).""; This is assuming $key starts at 0 and increments by 1 each iteration However, if you can't assume that, just have a separate counter: $c) { echo "".(++$counter).""; ... } HTH ~Philip
Re: [PHP] Foreach question (solved)
Thanks. I solved it using this : $x = 0; foreach ($equipos as $key => $val){ $x = $x + 1; echo ""; echo $x . ""; 2007/11/15, Michael McGlothlin <[EMAIL PROTECTED]>: > > $x = 0; > foreach ( $blah as $bleh ) { > $x = $x + 1; > print "$x: $bleh"; > } > > > HI, > > I'm working with an associative array, and generating its data a form. I > use > > foreach to loops the contents of the array and echo to print the table > and > > the data. Everything goes well, however I would like to add a counter to > > print the row's number. > > > > The question : Am I able to add a counter within foreach to print the > row > > number in each row using echo ?. I tried a few things but I couldn't. > > > > Thanks! > > > > Juan > > > > > > > -- > Michael McGlothlin > Southwest Plumbing Supply > >
Re: [PHP] Foreach question
2007/11/15, Philip Thompson <[EMAIL PROTECTED]>: > > On Nov 15, 2007 5:12 PM, Juan Marcelo Rodríguez < > [EMAIL PROTECTED]> > wrote: > > > Yes, I made a mistake in the first sentence. > > > > The code is : > > [...] > > > > foreach ($equipos as $key => $val){ > > > > echo ""; > > echo "1" . ""; // I would like to add the counter here > > reeplacing > > "1" > > > echo "".($key+1).""; > > This is assuming $key starts at 0 and increments by 1 each iteration > However, if you can't assume that, just have a separate counter: > > $counter = 0; > foreach ($a as $b => $c) { > echo "".(++$counter).""; > ... > } very good idea. thanks! Juan HTH > ~Philip >
Re: [PHP] Foreach question
Yes, I made a mistake in the first sentence. The code is : [...] foreach ($equipos as $key => $val){ echo ""; echo "1" . ""; // I would like to add the counter here reeplacing "1" print "" . ""; echo $key . ""; print "" . ""; echo $val . ""; print "" . ""; } ?> I use the array to generate the name of the radio and so on. However I had problems with the counter. Thanks 2007/11/15, Stut <[EMAIL PROTECTED]>: > > Juan Marcelo Rodríguez wrote: > > I'm working with an associative array, and generating its data a form. I > use > > foreach to loops the contents of the array and echo to print the table > and > > the data. Everything goes well, however I would like to add a counter to > > print the row's number. > > > > The question : Am I able to add a counter within foreach to print the > row > > number in each row using echo ?. I tried a few things but I couldn't. > > The mind boggles when wondering what you tried. > > $counter = 1; > foreach ($array as $val) > { > // Do your stuff here > > // Increment the counter > $counter++; > } > > -Stut > > -- > http://stut.net/ >
[PHP] Foreach question
HI, I'm working with an associative array, and generating its data a form. I use foreach to loops the contents of the array and echo to print the table and the data. Everything goes well, however I would like to add a counter to print the row's number. The question : Am I able to add a counter within foreach to print the row number in each row using echo ?. I tried a few things but I couldn't. Thanks! Juan
Re: [PHP] foreach question
On Sun, April 8, 2007 6:29 pm, [EMAIL PROTECTED] wrote: > I have .. > > foreach( $_POST as $key ) { > echo "$key"; > } You are actually echoing out the VALUE, not the KEY... > and that gives me > > item1 > item2 > item3 > item4 > item5 Unless your VALUE has "item1\nitem2\nitem3\nitem4\nitem5" in it, I don't how this could happen... Use "view source" in your browser. -- Some people have a "gift" link here. Know what I want? I want you to buy a CD from some indie artist. http://cdbaby.com/browse/from/lynch Yeah, I get a buck. So? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] foreach question
Quoting Lori Lay <[EMAIL PROTECTED]>: > [EMAIL PROTECTED] wrote: > > Sorry this is the full script... > > > > whois.php > > > > > > > > > > > > Enter Domain Names (one per line) > > > style="font-size:13;font-family:Arial,Verdana;"> > > Gotcha! A textarea does not produce an array. Even though the user > should be separating the lines with a line break, this turns into one > long string with line breaks in it, not separate array elements. You > will have to do this manually. Actually, you could probably use nl2br > to insert BR's before the line breaks (it doesn't replace them, but > that's usually good enough). > > Lori much better, it all makes sense now. This is what I would do: "; } ?> Siavash > > > > > > > > Whois Results: > > > > > > > foreach( $_POST as $key ) { > >echo "$key"; > > } > > ?> > > > > > > > > > > - Original Message - From: "Lori Lay" <[EMAIL PROTECTED]> > > To: <[EMAIL PROTECTED]> > > Cc: > > Sent: Monday, April 09, 2007 5:20 AM > > Subject: Re: [PHP] foreach question > > > > > >> [EMAIL PROTECTED] wrote: > >>> "both examples do the same thing.." > >>> > >>> no, ex1 only has 1 > >>> > >>> so outputs like.. > >>> item1item2item3item4item5 > >>> > >>> Where as I want this.. > >>> > >>> item1 > >>> item2 > >>> item3 > >>> item4 > >>> item5 > >>> > >>> ie a line break after every item. > >>> > >> Silly question, perhaps, but are you sure $_POST is an array (with 5 > >> elements)? What you have written should produce a break after each > >> item if POST is a 5 element array. However if POST is a single > >> element with the five items concatenated together, then they would be > >> printed the way you have it listed above... > >> > >> It might be better to post the full script to the list. > >> > >> Lori > >>> > >>> - Original Message - From: "Sebe" <[EMAIL PROTECTED]> > >>> To: <[EMAIL PROTECTED]> > >>> Cc: > >>> Sent: Monday, April 09, 2007 1:22 AM > >>> Subject: Re: [PHP] foreach question > >>> > >>> > >>>> [EMAIL PROTECTED] wrote: > >>>>> I have .. > >>>>> > >>>>> foreach( $_POST as $key ) {echo "$key"; > >>>>> } > >>>>> > >>>>> and that gives me > >>>>> > >>>>> item1 > >>>>> item2 > >>>>> item3 > >>>>> item4 > >>>>> item5 > >>>>> > >>>>> how do I write it to give me > >>>>> > >>>>> item1 > >>>>> item2 > >>>>> item3 > >>>>> item4 > >>>>> item5 > >>>>> > >>>>> Thanks > >>>>> > >>>> both examples do the same thing.. > >>>> > >>>> -- > >>>> PHP General Mailing List (http://www.php.net/) > >>>> To unsubscribe, visit: http://www.php.net/unsub.php > >>>> > >>>> > >>>> > >>> > >> > >> -- > >> PHP General Mailing List (http://www.php.net/) > >> To unsubscribe, visit: http://www.php.net/unsub.php > >> > >> > >> > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] foreach question
[EMAIL PROTECTED] wrote: Sorry this is the full script... whois.php Enter Domain Names (one per line) style="font-size:13;font-family:Arial,Verdana;"> Gotcha! A textarea does not produce an array. Even though the user should be separating the lines with a line break, this turns into one long string with line breaks in it, not separate array elements. You will have to do this manually. Actually, you could probably use nl2br to insert BR's before the line breaks (it doesn't replace them, but that's usually good enough). Lori Whois Results: "; } ?> - Original Message - From: "Lori Lay" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Cc: Sent: Monday, April 09, 2007 5:20 AM Subject: Re: [PHP] foreach question [EMAIL PROTECTED] wrote: "both examples do the same thing.." no, ex1 only has 1 so outputs like.. item1item2item3item4item5 Where as I want this.. item1 item2 item3 item4 item5 ie a line break after every item. Silly question, perhaps, but are you sure $_POST is an array (with 5 elements)? What you have written should produce a break after each item if POST is a 5 element array. However if POST is a single element with the five items concatenated together, then they would be printed the way you have it listed above... It might be better to post the full script to the list. Lori - Original Message - From: "Sebe" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Cc: Sent: Monday, April 09, 2007 1:22 AM Subject: Re: [PHP] foreach question [EMAIL PROTECTED] wrote: I have .. foreach( $_POST as $key ) {echo "$key"; } and that gives me item1 item2 item3 item4 item5 how do I write it to give me item1 item2 item3 item4 item5 Thanks both examples do the same thing.. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] foreach question
Sorry this is the full script... whois.php Enter Domain Names (one per line) style="font-size:13;font-family:Arial,Verdana;"> Whois Results: "; } ?> - Original Message - From: "Lori Lay" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Cc: Sent: Monday, April 09, 2007 5:20 AM Subject: Re: [PHP] foreach question [EMAIL PROTECTED] wrote: "both examples do the same thing.." no, ex1 only has 1 so outputs like.. item1item2item3item4item5 Where as I want this.. item1 item2 item3 item4 item5 ie a line break after every item. Silly question, perhaps, but are you sure $_POST is an array (with 5 elements)? What you have written should produce a break after each item if POST is a 5 element array. However if POST is a single element with the five items concatenated together, then they would be printed the way you have it listed above... It might be better to post the full script to the list. Lori - Original Message - From: "Sebe" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Cc: Sent: Monday, April 09, 2007 1:22 AM Subject: Re: [PHP] foreach question [EMAIL PROTECTED] wrote: I have .. foreach( $_POST as $key ) {echo "$key"; } and that gives me item1 item2 item3 item4 item5 how do I write it to give me item1 item2 item3 item4 item5 Thanks both examples do the same thing.. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] foreach question
[EMAIL PROTECTED] wrote: "both examples do the same thing.." no, ex1 only has 1 so outputs like.. item1item2item3item4item5 Where as I want this.. item1 item2 item3 item4 item5 ie a line break after every item. Silly question, perhaps, but are you sure $_POST is an array (with 5 elements)? What you have written should produce a break after each item if POST is a 5 element array. However if POST is a single element with the five items concatenated together, then they would be printed the way you have it listed above... It might be better to post the full script to the list. Lori - Original Message - From: "Sebe" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Cc: Sent: Monday, April 09, 2007 1:22 AM Subject: Re: [PHP] foreach question [EMAIL PROTECTED] wrote: I have .. foreach( $_POST as $key ) {echo "$key"; } and that gives me item1 item2 item3 item4 item5 how do I write it to give me item1 item2 item3 item4 item5 Thanks both examples do the same thing.. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] foreach question
Your code is fine and it should work. but in any case, try: foreach ($_POST as $key){ echo $key . ''; } Also, what php version, and what browser are you using? good luck, Siavash > [EMAIL PROTECTED] wrote: > > "both examples do the same thing.." > > > > no, ex1 only has 1 > > > > so outputs like.. > > item1item2item3item4item5 > > > > Where as I want this.. > > > > item1 > > item2 > > item3 > > item4 > > item5 > > > > ie a line break after every item. > > > > hmm, if you're getting 5 results from the loop each should already have > a > so i dont understand what is wrong but the code it's set to put out a > line break after each item. maybe i'm blind but the code is fine (with > the exception that i don't use double quotes). > > > > - Original Message - From: "Sebe" <[EMAIL PROTECTED]> > > To: <[EMAIL PROTECTED]> > > Cc: > > Sent: Monday, April 09, 2007 1:22 AM > > Subject: Re: [PHP] foreach question > > > > > >> [EMAIL PROTECTED] wrote: > >>> I have .. > >>> > >>> foreach( $_POST as $key ) {echo "$key"; > >>> } > >>> > >>> and that gives me > >>> > >>> item1 > >>> item2 > >>> item3 > >>> item4 > >>> item5 > >>> > >>> how do I write it to give me > >>> > >>> item1 > >>> item2 > >>> item3 > >>> item4 > >>> item5 > >>> > >>> Thanks > >>> > >> both examples do the same thing.. > >> > >> -- > >> PHP General Mailing List (http://www.php.net/) > >> To unsubscribe, visit: http://www.php.net/unsub.php > >> > >> > >> > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] foreach question
[EMAIL PROTECTED] wrote: "both examples do the same thing.." no, ex1 only has 1 so outputs like.. item1item2item3item4item5 Where as I want this.. item1 item2 item3 item4 item5 ie a line break after every item. hmm, if you're getting 5 results from the loop each should already have a so i dont understand what is wrong but the code it's set to put out a line break after each item. maybe i'm blind but the code is fine (with the exception that i don't use double quotes). - Original Message - From: "Sebe" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Cc: Sent: Monday, April 09, 2007 1:22 AM Subject: Re: [PHP] foreach question [EMAIL PROTECTED] wrote: I have .. foreach( $_POST as $key ) {echo "$key"; } and that gives me item1 item2 item3 item4 item5 how do I write it to give me item1 item2 item3 item4 item5 Thanks both examples do the same thing.. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] foreach question
"both examples do the same thing.." no, ex1 only has 1 so outputs like.. item1item2item3item4item5 Where as I want this.. item1 item2 item3 item4 item5 ie a line break after every item. - Original Message - From: "Sebe" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Cc: Sent: Monday, April 09, 2007 1:22 AM Subject: Re: [PHP] foreach question [EMAIL PROTECTED] wrote: I have .. foreach( $_POST as $key ) {echo "$key"; } and that gives me item1 item2 item3 item4 item5 how do I write it to give me item1 item2 item3 item4 item5 Thanks both examples do the same thing.. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] foreach question
[EMAIL PROTECTED] wrote: I have .. foreach( $_POST as $key ) {echo "$key"; } and that gives me item1 item2 item3 item4 item5 how do I write it to give me item1 item2 item3 item4 item5 Thanks both examples do the same thing.. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] foreach question
I have .. foreach( $_POST as $key ) { echo "$key"; } and that gives me item1 item2 item3 item4 item5 how do I write it to give me item1 item2 item3 item4 item5 Thanks -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php