Re: [PHP] what to use instead of foreach

2009-04-14 Thread Jan G.B.
2009/4/13 PJ af.gour...@videotron.ca:
 I have already tried with several count and for schemes. None work
 because foreach ignores any counters once in the loop. Also, this
 foreach is nested within another foreach; don't know if that affects
 anything.


Have you heard of while()? You can use it in combination with
array_pop() and count()

like:
?
while (($data = array_pop($array)) !== NULL) {
/* having $data and count($array) changes with every loop */
}
?

Bye

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] what to use instead of foreach

2009-04-14 Thread Leon du Plessis
Hi PJ,

Maybe you should post your code, or portion thereof. The trained eyes on the
list will quickly gather where the problem in the logic lies. 

Maybe you can also sacrifice some of your data or data structures if not too
sensitive. This will help to validate the route you are trying to follow.

Best wishes
Leon

-Original Message-
From: Jan G.B. [mailto:ro0ot.w...@googlemail.com] 
Sent: 14 April 2009 05:45 PM
To: PJ
Cc: Leon du Plessis; php-general@lists.php.net
Subject: Re: [PHP] what to use instead of foreach

2009/4/13 PJ af.gour...@videotron.ca:
 I have already tried with several count and for schemes. None work
 because foreach ignores any counters once in the loop. Also, this
 foreach is nested within another foreach; don't know if that affects
 anything.


Have you heard of while()? You can use it in combination with
array_pop() and count()

like:
?
while (($data = array_pop($array)) !== NULL) {
/* having $data and count($array) changes with every loop */
}
?

Bye

-- 
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] what to use instead of foreach

2009-04-14 Thread PJ
Leon du Plessis wrote:
 Hi PJ,

 Maybe you should post your code, or portion thereof. The trained eyes on the
 list will quickly gather where the problem in the logic lies. 

 Maybe you can also sacrifice some of your data or data structures if not too
 sensitive. This will help to validate the route you are trying to follow.

 Best wishes
 Leon

 -Original Message-
 From: Jan G.B. [mailto:ro0ot.w...@googlemail.com] 
 Sent: 14 April 2009 05:45 PM
 To: PJ
 Cc: Leon du Plessis; php-general@lists.php.net
 Subject: Re: [PHP] what to use instead of foreach

 2009/4/13 PJ af.gour...@videotron.ca:
   
 I have already tried with several count and for schemes. None work
 because foreach ignores any counters once in the loop. Also, this
 foreach is nested within another foreach; don't know if that affects
 anything.
 


 Have you heard of while()? You can use it in combination with
 array_pop() and count()

 like:
 ?
 while (($data = array_pop($array)) !== NULL) {
 /* having $data and count($array) changes with every loop */
 }
 ?

 Bye

   
Good suggestion Leon, thanks.
I can certainly do that, np. Nothing sensitive. I do have quite a few
questions about the code I have used. Things are moving along very well
and I should be able to put up the code on the actual site in some
directory where it will be possible to view the code and see what it is
doing. I'll post the link when more or less ready and then ask for
comments, observations, suggestions . Posting the code on the list would
be too bumbersome. For now, I'll stick to some snippets. :-)

-- 
unheralded genius: A clean desk is the sign of a dull mind. 
-
Phil Jourdan --- p...@ptahhotep.com
   http://www.ptahhotep.com
   http://www.chiccantine.com/andypantry.php


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] what to use instead of foreach

2009-04-14 Thread Mark Kelly
Hi Phil.

On Monday 13 April 2009, PJ wrote:
 Thanks for the suggestion, Mark. I've already experimented with count;
 you're close, but there is still a small glitch and that's in count();
 foreach doesn't give a damn about count so you can't use that - it is
 reset once inside the foreach loop.

Look again at the code - the count() is not inside the foreach, so it is 
not reset, simply stored in $lastIndex for comparison.

If your array is associative then simply use another variable to find the 
last value in the array - the code doesn't need to change much.

Try actually running the code below - it does work, as does the previous 
version I posted if the array is not associative. 

I'd prefer it if in future you didn't tell me that my code didn't work 
without actually trying it - I tested that snippet before posting it, as I 
did with the following.

HTH

Mark

?php
// Non-associative array (the code I posted previously).
$a = array('1','2','3');

$lastIndex = count($a) - 1;
$outputString = '';
foreach ($a as $index = $value) {
if ($index != $lastIndex) {
$outputString .= $value, ;
} else {
$outputString = rtrim($outputString,', '); // Strip last comma.
$outputString .=   $valuebr /;
}
}
echo $outputString;

// Associative array (changed only very slightly).
$a = array('martha' = '1','jock' = '2','dave' = '3');

$lastIndex = count($a);
$counter = 0;
$outputString = '';
foreach ($a as $index = $value) {
$counter++;
if ($counter != $lastIndex) {
$outputString .= $value, ;
} else {
$outputString = rtrim($outputString,', '); // Strip last comma.
$outputString .=   $valuebr /;
}
}
echo $outputString;
?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] what to use instead of foreach

2009-04-14 Thread PJ
Mark Kelly wrote:
 Hi Phil.

 On Monday 13 April 2009, PJ wrote:
   
 Thanks for the suggestion, Mark. I've already experimented with count;
 you're close, but there is still a small glitch and that's in count();
 foreach doesn't give a damn about count so you can't use that - it is
 reset once inside the foreach loop.
 

 Look again at the code - the count() is not inside the foreach, so it is 
 not reset, simply stored in $lastIndex for comparison.

 If your array is associative then simply use another variable to find the 
 last value in the array - the code doesn't need to change much.

 Try actually running the code below - it does work, as does the previous 
 version I posted if the array is not associative. 

 I'd prefer it if in future you didn't tell me that my code didn't work 
 without actually trying it - I tested that snippet before posting it, as I 
 did with the following.
   
My apologies. It certainly was not meant to disparage the suggestion. I
had tried all sorts of counts as I mentioned before. I spend an awful
lot of time testing and trying but I'm afraid I don't really grasp all
the concepts involved. I am rather visually oriented and not seeing
things is rather frustrating.
I shouldn't assume so much and will certainly examine your suggestion
and learn where I went wrong.
Thanks again.
Phil
 HTH

 Mark

 ?php
 // Non-associative array (the code I posted previously).
 $a = array('1','2','3');

 $lastIndex = count($a) - 1;
 $outputString = '';
 foreach ($a as $index = $value) {
   if ($index != $lastIndex) {
   $outputString .= $value, ;
 } else {
 $outputString = rtrim($outputString,', '); // Strip last comma.
 $outputString .=   $valuebr /;
 }
 }
 echo $outputString;

 // Associative array (changed only very slightly).
 $a = array('martha' = '1','jock' = '2','dave' = '3');

 $lastIndex = count($a);
 $counter = 0;
 $outputString = '';
 foreach ($a as $index = $value) {
   $counter++;
   if ($counter != $lastIndex) {
   $outputString .= $value, ;
   } else {
   $outputString = rtrim($outputString,', '); // Strip last comma.
   $outputString .=   $valuebr /;
   }
 }
 echo $outputString;
 ?

   


-- 
unheralded genius: A clean desk is the sign of a dull mind. 
-
Phil Jourdan --- p...@ptahhotep.com
   http://www.ptahhotep.com
   http://www.chiccantine.com/andypantry.php


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] what to use instead of foreach

2009-04-13 Thread Mark Kelly
Hi.

On Sunday 12 April 2009, PJ wrote:
 foreach does not allow for different formatting for output...

[snip]

 But how do you get result1, result2  result3 // with br at end ?

$lastIndex = count($a) - 1; // Adjust for zero-indexing.
$outputString = '';
foreach ($a as $index = $value) {
if ($index != $lastIndex) {
$outputString .= $value, ;
} else {
$outputString = rtrim($outputString,', '); // Strip last comma.
$outputString .=   $valuebr /;
}
}
echo $outputString;

Like that? If your array is associative you can drop the $lastIndex calc 
and adjust the loop to update a counter instead. 

HTH

Mark

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] what to use instead of foreach

2009-04-13 Thread PJ
Mark Kelly wrote:
 Hi.

 On Sunday 12 April 2009, PJ wrote:
   
 foreach does not allow for different formatting for output...
 

 [snip]

   
 But how do you get result1, result2  result3 // with br at end ?
 

 $lastIndex = count($a) - 1; // Adjust for zero-indexing.
 $outputString = '';
 foreach ($a as $index = $value) {
 if ($index != $lastIndex) {
 $outputString .= $value, ;
 } else {
 $outputString = rtrim($outputString,', '); // Strip last comma.
 $outputString .=   $valuebr /;
 }
 }
 echo $outputString;

 Like that? If your array is associative you can drop the $lastIndex calc 
 and adjust the loop to update a counter instead. 

 HTH

 Mark

   
Thanks for the suggestion, Mark. I've already experimented with count;
you're close, but there is still a small glitch and that's in count();
foreach doesn't give a damn about count so you can't use that - it is
reset once inside the foreach loop. And, the as $index cannot relate
to the $lastIndex because the array index (which would be the index in
the as $index) can be any number and is not sequential. There are a
couple of other ways, one is suggested by Jim Lucas in the thread
extract varying data from array with different formatting; another is
to change the SELECT query to use a ranking reference - I think it may
be a bit more complex, though.
Any other thoughts? ;-)  I'll appreciate them.

-- 
unheralded genius: A clean desk is the sign of a dull mind. 
-
Phil Jourdan --- p...@ptahhotep.com
   http://www.ptahhotep.com
   http://www.chiccantine.com/andypantry.php


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] what to use instead of foreach

2009-04-13 Thread PJ
Hi Leon,
Thanks for the suggestion; I'm quite new to all this, so it's a bit
complicated for my peanut brain.
I have already tried with several count and for schemes. None work
because foreach ignores any counters once in the loop. Also, this
foreach is nested within another foreach; don't know if that affects
anything.
I'll try to understand the second suggestion using for. I'll see what
comes up.
There are actually several conditions that have to be met:
1. if only 1 author = echo authorbr
2. if 2 authors = echo author  author1br
3. if more than 2 authors = echo author, author1, author2  author3br
That's what makes it a toughie

Leon du Plessis wrote:
 You may try something basic like:

 $b = 1;
 foreach ($my_array as $a)
 {
 echo  $a ;

 //Send new line to browser
 if ($b++ == 3) { echo br; $b = 1; }
 }

 Or there are some different ways to approach this also like:
 for ($a = current($my_array); $a; $a = next($my_array))
 {
 //Format 1
 echo  $a ;
 $a = next($my_array);

 //Format 2
 /* you may add checks here to see if $a contains data */
 echo  ~ $a ~ ; $a = next($my_array);

 //Format 3 + NEW LINE
 /* you may add checks here to see if $a contains data */
 echo  ~~ $a ~~br ;
 }

 This way you have some added control over the iteration through the array,
 and you can play around with when  how to display what.

 Regards.

 -Original Message-
 From: PJ [mailto:af.gour...@videotron.ca]
 Sent: 12 April 2009 08:57 PM
 To: php-general@lists.php.net
 Subject: [PHP] what to use instead of foreach

 foreach does not allow for different formatting for output...
 What could be used as a workaround?
 example:
 echo $some_result, br; // will print all results in 1 column
 echo $some_result, ,; // will print all results comma-separated in 1 row

 But how do you get result1, result2  result3 // with br at end ?

-- 
unheralded genius: A clean desk is the sign of a dull mind. 
-
Phil Jourdan --- p...@ptahhotep.com
http://www.ptahhotep.com
http://www.chiccantine.com/andypantry.php

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] what to use instead of foreach

2009-04-13 Thread Leon du Plessis
Hi PJ, 

Ok, If I understand correctly you can attempt to alter your code as per
following example (I am breaking it down a little for readability):

a) If you only wish to output the authors, see also Mark Kelly's example,
You can simply output as many authors you have associated (you will need an
associated array!!:

Or b) I include the following alternative example:


$string_out = :
Foreach ($my_titles as $titles)
{
   Echo Title: $titles By:br;
   Foreach($my_authors[$title] as $author)
  $string_out .= $author, ; /* Building string */

   // Add br
   $string_out .= br;

   // Here you would replace your last comma with the  you want
   // There are a few ways to do this (like Mark Kelly's), but will try 
   // another way (older, maybe less complicated?).
   $final_string = substr($string_out,0,strrpos($string_out,,) - 1);
   $final_string .=. 
  substr($string_out,strrpos($string_out,,) + 1);
}


So all you need is to modal your data around this, and you should be fine. 
You could construct your arrays then as follows as an example:


$my_titles = array(title1,title2);
$my_authors[title1] = array(a someone,a notherone);
$my_authors[title2] = array(mr. a,mr. b);
... 

and so forth...how you construct the data is then very important as you can
then later use it simplify your coding as you progress and as demonstrated
below:

In future, where the need justifies it, you can construct your array to
already contain the needed string you want to output, it may help, but you
will sometimes have the same effort in constructing the data for the arrays,
so it is up to you to decide which approach is going to be best: e.g.
$my_titles = array(title1,title2);
$my_authors[title1] = array(a someone, a notherone  Mr. X);

Then you can simply echo the array value:
echo $my_authors[title1] . br;

Hope it is enough info for to work on for now!!

Have fun!
Leon

-Original Message-
From: PJ [mailto:af.gour...@videotron.ca] 
Sent: 13 April 2009 04:33 PM
To: Leon du Plessis
Cc: php-general@lists.php.net
Subject: Re: [PHP] what to use instead of foreach

Hi Leon,
Thanks for the suggestion; I'm quite new to all this, so it's a bit
complicated for my peanut brain.
I have already tried with several count and for schemes. None work
because foreach ignores any counters once in the loop. Also, this
foreach is nested within another foreach; don't know if that affects
anything.
I'll try to understand the second suggestion using for. I'll see what
comes up.
There are actually several conditions that have to be met:
1. if only 1 author = echo authorbr
2. if 2 authors = echo author  author1br
3. if more than 2 authors = echo author, author1, author2  author3br
That's what makes it a toughie

Leon du Plessis wrote:
 You may try something basic like:

 $b = 1;
 foreach ($my_array as $a)
 {
 echo  $a ;

 //Send new line to browser
 if ($b++ == 3) { echo br; $b = 1; }
 }

 Or there are some different ways to approach this also like:
 for ($a = current($my_array); $a; $a = next($my_array))
 {
 //Format 1
 echo  $a ;
 $a = next($my_array);

 //Format 2
 /* you may add checks here to see if $a contains data */
 echo  ~ $a ~ ; $a = next($my_array);

 //Format 3 + NEW LINE
 /* you may add checks here to see if $a contains data */
 echo  ~~ $a ~~br ;
 }

 This way you have some added control over the iteration through the array,
 and you can play around with when  how to display what.

 Regards.

 -Original Message-
 From: PJ [mailto:af.gour...@videotron.ca]
 Sent: 12 April 2009 08:57 PM
 To: php-general@lists.php.net
 Subject: [PHP] what to use instead of foreach

 foreach does not allow for different formatting for output...
 What could be used as a workaround?
 example:
 echo $some_result, br; // will print all results in 1 column
 echo $some_result, ,; // will print all results comma-separated in 1 row

 But how do you get result1, result2  result3 // with br at end ?

-- 
unheralded genius: A clean desk is the sign of a dull mind. 
-
Phil Jourdan --- p...@ptahhotep.com
http://www.ptahhotep.com
http://www.chiccantine.com/andypantry.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] what to use instead of foreach

2009-04-13 Thread Leon du Plessis
Hi PJ, 

You may want to remove the , before the br...That was a slight oversight
on my partsorry.'bout that...I will leave you to do the fixing, but I am
sure you get the general idea.

Best wishes..Leon

-Original Message-
From: Leon du Plessis [mailto:l...@dsgnit.com] 
Sent: 13 April 2009 06:48 PM
To: 'PJ'
Cc: php-general@lists.php.net
Subject: RE: [PHP] what to use instead of foreach

Hi PJ, 

Ok, If I understand correctly you can attempt to alter your code as per
following example (I am breaking it down a little for readability):

a) If you only wish to output the authors, see also Mark Kelly's example,
You can simply output as many authors you have associated (you will need an
associated array!!:

Or b) I include the following alternative example:


$string_out = :
Foreach ($my_titles as $titles)
{
   Echo Title: $titles By:br;
   Foreach($my_authors[$title] as $author)
  $string_out .= $author, ; /* Building string */

   // Add br
   $string_out .= br;

   // Here you would replace your last comma with the  you want
   // There are a few ways to do this (like Mark Kelly's), but will try 
   // another way (older, maybe less complicated?).
   $final_string = substr($string_out,0,strrpos($string_out,,) - 1);
   $final_string .=. 
  substr($string_out,strrpos($string_out,,) + 1);
}


So all you need is to modal your data around this, and you should be fine. 
You could construct your arrays then as follows as an example:


$my_titles = array(title1,title2);
$my_authors[title1] = array(a someone,a notherone);
$my_authors[title2] = array(mr. a,mr. b);
... 

and so forth...how you construct the data is then very important as you can
then later use it simplify your coding as you progress and as demonstrated
below:

In future, where the need justifies it, you can construct your array to
already contain the needed string you want to output, it may help, but you
will sometimes have the same effort in constructing the data for the arrays,
so it is up to you to decide which approach is going to be best: e.g.
$my_titles = array(title1,title2);
$my_authors[title1] = array(a someone, a notherone  Mr. X);

Then you can simply echo the array value:
echo $my_authors[title1] . br;

Hope it is enough info for to work on for now!!

Have fun!
Leon

-Original Message-
From: PJ [mailto:af.gour...@videotron.ca] 
Sent: 13 April 2009 04:33 PM
To: Leon du Plessis
Cc: php-general@lists.php.net
Subject: Re: [PHP] what to use instead of foreach

Hi Leon,
Thanks for the suggestion; I'm quite new to all this, so it's a bit
complicated for my peanut brain.
I have already tried with several count and for schemes. None work
because foreach ignores any counters once in the loop. Also, this
foreach is nested within another foreach; don't know if that affects
anything.
I'll try to understand the second suggestion using for. I'll see what
comes up.
There are actually several conditions that have to be met:
1. if only 1 author = echo authorbr
2. if 2 authors = echo author  author1br
3. if more than 2 authors = echo author, author1, author2  author3br
That's what makes it a toughie

Leon du Plessis wrote:
 You may try something basic like:

 $b = 1;
 foreach ($my_array as $a)
 {
 echo  $a ;

 //Send new line to browser
 if ($b++ == 3) { echo br; $b = 1; }
 }

 Or there are some different ways to approach this also like:
 for ($a = current($my_array); $a; $a = next($my_array))
 {
 //Format 1
 echo  $a ;
 $a = next($my_array);

 //Format 2
 /* you may add checks here to see if $a contains data */
 echo  ~ $a ~ ; $a = next($my_array);

 //Format 3 + NEW LINE
 /* you may add checks here to see if $a contains data */
 echo  ~~ $a ~~br ;
 }

 This way you have some added control over the iteration through the array,
 and you can play around with when  how to display what.

 Regards.

 -Original Message-
 From: PJ [mailto:af.gour...@videotron.ca]
 Sent: 12 April 2009 08:57 PM
 To: php-general@lists.php.net
 Subject: [PHP] what to use instead of foreach

 foreach does not allow for different formatting for output...
 What could be used as a workaround?
 example:
 echo $some_result, br; // will print all results in 1 column
 echo $some_result, ,; // will print all results comma-separated in 1 row

 But how do you get result1, result2  result3 // with br at end ?

-- 
unheralded genius: A clean desk is the sign of a dull mind. 
-
Phil Jourdan --- p...@ptahhotep.com
http://www.ptahhotep.com
http://www.chiccantine.com/andypantry.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] what to use instead of foreach

2009-04-13 Thread PJ
Hi Leon  thanks.
It sure is complicated. Jim Lucas example did the trick very nicely (in
my other post -
extract varying data from array with different formatting but here I
am learning about other ways  means.
Redoing the arrays means redoing the queries :-(  but I'll have a go at
it if I'm to learn anything. I already have a ranking column set up but
am not using it at the moment for the author display. Anyway, I have
enough to keep my neurons busy for a while.
Thanks.

Leon du Plessis wrote:
 Hi PJ,

 You may want to remove the , before the br...That was a slight
 oversight
 on my partsorry.'bout that...I will leave you to do the fixing,
 but I am
 sure you get the general idea.

 Best wishes..Leon

 -Original Message-
 From: Leon du Plessis [mailto:l...@dsgnit.com]
 Sent: 13 April 2009 06:48 PM
 To: 'PJ'
 Cc: php-general@lists.php.net
 Subject: RE: [PHP] what to use instead of foreach

 Hi PJ,

 Ok, If I understand correctly you can attempt to alter your code as per
 following example (I am breaking it down a little for readability):

 a) If you only wish to output the authors, see also Mark Kelly's example,
 You can simply output as many authors you have associated (you will
 need an
 associated array!!:

 Or b) I include the following alternative example:

 
 $string_out = :
 Foreach ($my_titles as $titles)
 {
 Echo Title: $titles By:br;
 Foreach($my_authors[$title] as $author)
 $string_out .= $author, ; /* Building string */

 // Add br
 $string_out .= br;

 // Here you would replace your last comma with the  you want
 // There are a few ways to do this (like Mark Kelly's), but will try
 // another way (older, maybe less complicated?).
 $final_string = substr($string_out,0,strrpos($string_out,,) - 1);
 $final_string .=.
 substr($string_out,strrpos($string_out,,) + 1);
 }
 

 So all you need is to modal your data around this, and you should be
 fine.
 You could construct your arrays then as follows as an example:

 
 $my_titles = array(title1,title2);
 $my_authors[title1] = array(a someone,a notherone);
 $my_authors[title2] = array(mr. a,mr. b);
 ...

 and so forth...how you construct the data is then very important as
 you can
 then later use it simplify your coding as you progress and as demonstrated
 below:

 In future, where the need justifies it, you can construct your array to
 already contain the needed string you want to output, it may help, but you
 will sometimes have the same effort in constructing the data for the
 arrays,
 so it is up to you to decide which approach is going to be best: e.g.
 $my_titles = array(title1,title2);
 $my_authors[title1] = array(a someone, a notherone  Mr. X);

 Then you can simply echo the array value:
 echo $my_authors[title1] . br;

 Hope it is enough info for to work on for now!!

 Have fun!
 Leon

 -Original Message-
 From: PJ [mailto:af.gour...@videotron.ca]
 Sent: 13 April 2009 04:33 PM
 To: Leon du Plessis
 Cc: php-general@lists.php.net
 Subject: Re: [PHP] what to use instead of foreach

 Hi Leon,
 Thanks for the suggestion; I'm quite new to all this, so it's a bit
 complicated for my peanut brain.
 I have already tried with several count and for schemes. None work
 because foreach ignores any counters once in the loop. Also, this
 foreach is nested within another foreach; don't know if that affects
 anything.
 I'll try to understand the second suggestion using for. I'll see what
 comes up.
 There are actually several conditions that have to be met:
 1. if only 1 author = echo authorbr
 2. if 2 authors = echo author  author1br
 3. if more than 2 authors = echo author, author1, author2  author3br
 That's what makes it a toughie

 Leon du Plessis wrote:
 You may try something basic like:

 $b = 1;
 foreach ($my_array as $a)
 {
 echo  $a ;

 //Send new line to browser
 if ($b++ == 3) { echo br; $b = 1; }
 }

 Or there are some different ways to approach this also like:
 for ($a = current($my_array); $a; $a = next($my_array))
 {
 //Format 1
 echo  $a ;
 $a = next($my_array);

 //Format 2
 /* you may add checks here to see if $a contains data */
 echo  ~ $a ~ ; $a = next($my_array);

 //Format 3 + NEW LINE
 /* you may add checks here to see if $a contains data */
 echo  ~~ $a ~~br ;
 }

 This way you have some added control over the iteration through the
 array,
 and you can play around with when  how to display what.

 Regards.

 -Original Message-
 From: PJ [mailto:af.gour...@videotron.ca]
 Sent: 12 April 2009 08:57 PM
 To: php-general@lists.php.net
 Subject: [PHP] what to use instead of foreach

 foreach does not allow for different formatting for output...
 What could be used as a workaround?
 example:
 echo $some_result, br; // will print all results in 1 column
 echo $some_result, ,; // will print all results comma-separated in
 1 row

 But how do you get result1, result2  result3 // with br at end ?



-- 
unheralded genius: A clean desk is the sign of a dull mind

Re: [PHP] what to use instead of foreach

2009-04-13 Thread Nitsan Bin-Nun
var_dump(implode(br /, $array) . br /);

On Mon, Apr 13, 2009 at 7:07 PM, PJ af.gour...@videotron.ca wrote:

 Hi Leon  thanks.
 It sure is complicated. Jim Lucas example did the trick very nicely (in
 my other post -
 extract varying data from array with different formatting but here I
 am learning about other ways  means.
 Redoing the arrays means redoing the queries :-(  but I'll have a go at
 it if I'm to learn anything. I already have a ranking column set up but
 am not using it at the moment for the author display. Anyway, I have
 enough to keep my neurons busy for a while.
 Thanks.

 Leon du Plessis wrote:
  Hi PJ,
 
  You may want to remove the , before the br...That was a slight
  oversight
  on my partsorry.'bout that...I will leave you to do the fixing,
  but I am
  sure you get the general idea.
 
  Best wishes..Leon
 
  -Original Message-
  From: Leon du Plessis [mailto:l...@dsgnit.com]
  Sent: 13 April 2009 06:48 PM
  To: 'PJ'
  Cc: php-general@lists.php.net
  Subject: RE: [PHP] what to use instead of foreach
 
  Hi PJ,
 
  Ok, If I understand correctly you can attempt to alter your code as per
  following example (I am breaking it down a little for readability):
 
  a) If you only wish to output the authors, see also Mark Kelly's example,
  You can simply output as many authors you have associated (you will
  need an
  associated array!!:
 
  Or b) I include the following alternative example:
 
  
  $string_out = :
  Foreach ($my_titles as $titles)
  {
  Echo Title: $titles By:br;
  Foreach($my_authors[$title] as $author)
  $string_out .= $author, ; /* Building string */
 
  // Add br
  $string_out .= br;
 
  // Here you would replace your last comma with the  you want
  // There are a few ways to do this (like Mark Kelly's), but will try
  // another way (older, maybe less complicated?).
  $final_string = substr($string_out,0,strrpos($string_out,,) - 1);
  $final_string .=.
  substr($string_out,strrpos($string_out,,) + 1);
  }
  
 
  So all you need is to modal your data around this, and you should be
  fine.
  You could construct your arrays then as follows as an example:
 
  
  $my_titles = array(title1,title2);
  $my_authors[title1] = array(a someone,a notherone);
  $my_authors[title2] = array(mr. a,mr. b);
  ...
 
  and so forth...how you construct the data is then very important as
  you can
  then later use it simplify your coding as you progress and as
 demonstrated
  below:
 
  In future, where the need justifies it, you can construct your array to
  already contain the needed string you want to output, it may help, but
 you
  will sometimes have the same effort in constructing the data for the
  arrays,
  so it is up to you to decide which approach is going to be best: e.g.
  $my_titles = array(title1,title2);
  $my_authors[title1] = array(a someone, a notherone  Mr. X);
 
  Then you can simply echo the array value:
  echo $my_authors[title1] . br;
 
  Hope it is enough info for to work on for now!!
 
  Have fun!
  Leon
 
  -Original Message-
  From: PJ [mailto:af.gour...@videotron.ca]
  Sent: 13 April 2009 04:33 PM
  To: Leon du Plessis
  Cc: php-general@lists.php.net
  Subject: Re: [PHP] what to use instead of foreach
 
  Hi Leon,
  Thanks for the suggestion; I'm quite new to all this, so it's a bit
  complicated for my peanut brain.
  I have already tried with several count and for schemes. None work
  because foreach ignores any counters once in the loop. Also, this
  foreach is nested within another foreach; don't know if that affects
  anything.
  I'll try to understand the second suggestion using for. I'll see what
  comes up.
  There are actually several conditions that have to be met:
  1. if only 1 author = echo authorbr
  2. if 2 authors = echo author  author1br
  3. if more than 2 authors = echo author, author1, author2  author3br
  That's what makes it a toughie
 
  Leon du Plessis wrote:
  You may try something basic like:
 
  $b = 1;
  foreach ($my_array as $a)
  {
  echo  $a ;
 
  //Send new line to browser
  if ($b++ == 3) { echo br; $b = 1; }
  }
 
  Or there are some different ways to approach this also like:
  for ($a = current($my_array); $a; $a = next($my_array))
  {
  //Format 1
  echo  $a ;
  $a = next($my_array);
 
  //Format 2
  /* you may add checks here to see if $a contains data */
  echo  ~ $a ~ ; $a = next($my_array);
 
  //Format 3 + NEW LINE
  /* you may add checks here to see if $a contains data */
  echo  ~~ $a ~~br ;
  }
 
  This way you have some added control over the iteration through the
  array,
  and you can play around with when  how to display what.
 
  Regards.
 
  -Original Message-
  From: PJ [mailto:af.gour...@videotron.ca]
  Sent: 12 April 2009 08:57 PM
  To: php-general@lists.php.net
  Subject: [PHP] what to use instead of foreach
 
  foreach does not allow for different formatting for output...
  What could be used as a workaround?
  example:
  echo $some_result, br; // will print all

Re: [PHP] what to use instead of foreach

2009-04-12 Thread Ashley Sheridan
On Sun, 2009-04-12 at 13:56 -0500, PJ wrote:
 foreach does not allow for different formatting for output...
 What could be used as a workaround?
 example:
 echo $some_result, br; // will print all results in 1 column
 echo $some_result, ,; // will print all results comma-separated in 1 row
 
 But how do you get result1, result2  result3 // with br at end ?
 
 -- 
 unheralded genius: A clean desk is the sign of a dull mind. 
 -
 Phil Jourdan --- p...@ptahhotep.com
http://www.ptahhotep.com
http://www.chiccantine.com/andypantry.php
 
 
You need to explain a bit more of what you are trying to achieve. There
are no limits I know of with using foreach to output content.


Ash
www.ashleysheridan.co.uk


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] what to use instead of foreach

2009-04-12 Thread Leon du Plessis
You may try something basic like:

$b = 1;
foreach ($my_array as $a)
{  
echo  $a ;

//Send new line to browser
if ($b++ == 3) { echo br; $b = 1; }
}

Or there are some different ways to approach this also like:
for ($a = current($my_array); $a; $a = next($my_array))
{  
//Format 1
echo  $a ; 
$a = next($my_array);

//Format 2
/* you may add checks here to see if $a contains data */
echo   ~ $a ~ ; $a = next($my_array);

//Format 3 + NEW LINE
/* you may add checks here to see if $a contains data */
echo   ~~ $a ~~br ;
}

This way you have some added control over the iteration through the array,
and you can play around with when  how to display what.

Regards.

-Original Message-
From: PJ [mailto:af.gour...@videotron.ca] 
Sent: 12 April 2009 08:57 PM
To: php-general@lists.php.net
Subject: [PHP] what to use instead of foreach

foreach does not allow for different formatting for output...
What could be used as a workaround?
example:
echo $some_result, br; // will print all results in 1 column
echo $some_result, ,; // will print all results comma-separated in 1 row

But how do you get result1, result2  result3 // with br at end ?

-- 
unheralded genius: A clean desk is the sign of a dull mind. 
-
Phil Jourdan --- p...@ptahhotep.com
   http://www.ptahhotep.com
   http://www.chiccantine.com/andypantry.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] What to use?

2003-09-25 Thread Jay Blanchard
[snip]
I dont want to use such big averages and want to cut the last to digits
off
so it will be something like:
5.00
4.00
44.23
[/snip]

http://www.php.net/number_format

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] What to use?

2003-09-25 Thread Tyler Lane
round()
and number_format() are what i would suggest using.

On Thu, 2003-09-25 at 13:01, Ryan A wrote:
 Hi,
 I am running a query on the database to get the avg of a few fields, I am
 getting a result something like this:
 
 5.
 4.
 5.
 
 I dont want to use such big averages and want to cut the last to digits off
 so it will be something like:
 5.00
 4.00
 44.23
 etc
 
 I visited the manual and had a look at a few functions like trim,
 rtrim,ltrim,chop,explode etcwhich one is right for me? most of them are
 dealing with whitespace so it does not make much sense right now with my
 current output
 
 Please help.
 
 Thanks,
 -Ryan
 
 
 
 
 We will slaughter you all! - The Iraqi (Dis)information ministers site
 http://MrSahaf.com
-- 
Tyler Lane [EMAIL PROTECTED]
Lyrical Communications

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] What to use?

2003-09-25 Thread Jeff McKeon
Correct me if I'm wrong please, but I think number_format() has the
adverse effect of changing the value type from numeric to char and
therefore baring you from using it in mathematical equation later in the
script.

Anyway that's what it seemed to do to me when I used it on results from
a MySQL query...

Jeff

 -Original Message-
 From: Tyler Lane [mailto:[EMAIL PROTECTED] 
 Sent: Thursday, September 25, 2003 4:20 PM
 To: [EMAIL PROTECTED]
 Subject: Re: [PHP] What to use?
 
 
 round()
 and number_format() are what i would suggest using.
 
 On Thu, 2003-09-25 at 13:01, Ryan A wrote:
  Hi,
  I am running a query on the database to get the avg of a 
 few fields, I 
  am getting a result something like this:
  
  5.
  4.
  5.
  
  I dont want to use such big averages and want to cut the last to 
  digits off so it will be something like: 5.00
  4.00
  44.23
  etc
  
  I visited the manual and had a look at a few functions like trim, 
  rtrim,ltrim,chop,explode etcwhich one is right for me? most of 
  them are dealing with whitespace so it does not make much 
 sense right 
  now with my current output
  
  Please help.
  
  Thanks,
  -Ryan
  
  
  
  
  We will slaughter you all! - The Iraqi (Dis)information 
 ministers site 
  http://MrSahaf.com
 -- 
 Tyler Lane [EMAIL PROTECTED]
 Lyrical Communications
 
 -- 
 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] What to use?

2003-09-25 Thread Robert Cummings
On Thu, 2003-09-25 at 16:28, Jeff McKeon wrote:
 Correct me if I'm wrong please, but I think number_format() has the
 adverse effect of changing the value type from numeric to char and
 therefore baring you from using it in mathematical equation later in the
 script.
 
 Anyway that's what it seemed to do to me when I used it on results from
 a MySQL query...

That shouldn't happen. PHP is a typeless language and so type
conversions are juggled on the fly. If you pass a numerical string to a
mathematical operator, then the string will be converted to the
appropriate value.

Cheers,
Rob.
-- 
..
| InterJinn Application Framework - http://www.interjinn.com |
::
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for   |
| creating re-usable components quickly and easily.  |
`'

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] What to use?

2003-09-25 Thread CPT John W. Holmes
From: Robert Cummings [EMAIL PROTECTED]

 On Thu, 2003-09-25 at 16:28, Jeff McKeon wrote:
  Correct me if I'm wrong please, but I think number_format() has the
  adverse effect of changing the value type from numeric to char and
  therefore baring you from using it in mathematical equation later in the
  script.
 
  Anyway that's what it seemed to do to me when I used it on results from
  a MySQL query...

 That shouldn't happen. PHP is a typeless language and so type
 conversions are juggled on the fly. If you pass a numerical string to a
 mathematical operator, then the string will be converted to the
 appropriate value.

If you get a result of 1,234.56 from number_format() and try to use that
as a numerical value (i.e. it's converted to an int or float), you'll end up
with the value being 1, though. Everything after the first non-numeric
character is lost. That's what he's talking about.

---John Holmes...

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] What to use?

2003-09-25 Thread CPT John W. Holmes
From: Jeff McKeon [EMAIL PROTECTED]

 Correct me if I'm wrong please, but I think number_format() has the
 adverse effect of changing the value type from numeric to char and
 therefore baring you from using it in mathematical equation later in the
 script.

You're correct (kind of). The _result_ of number_format() is a string, but
it doesn't change the original number (unless you're assigning it back to
itself). Even if you are, that's why you use it after you've done all of
your math calculations and only when you're ready to display the number.

If it's really an issue, you can alwasy use printf() or sprintf() to solve
this problem, too.

---John Holmes...

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] What to use?

2003-09-25 Thread Eugene Lee
On Thu, Sep 25, 2003 at 10:01:48PM +0200, Ryan A wrote:
: 
: I am running a query on the database to get the avg of a few fields, I am
: getting a result something like this:
: 
: 5.
: 4.
: 5.
: 
: I dont want to use such big averages and want to cut the last to digits off
: so it will be something like:
: 5.00
: 4.00
: 44.23
: etc

Why not keep the precision internally and only deal with it as an output
formatting issue when you display the results?

$a = 5.;
printf(\$a = %.2f \n, $a);

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] What to use?

2003-09-25 Thread Robert Cummings
*slaps self* Yep, forgot about that part of number format :)

Rob.


On Thu, 2003-09-25 at 16:51, CPT John W. Holmes wrote:
 From: Robert Cummings [EMAIL PROTECTED]
 
  On Thu, 2003-09-25 at 16:28, Jeff McKeon wrote:
   Correct me if I'm wrong please, but I think number_format() has the
   adverse effect of changing the value type from numeric to char and
   therefore baring you from using it in mathematical equation later in the
   script.
  
   Anyway that's what it seemed to do to me when I used it on results from
   a MySQL query...
 
  That shouldn't happen. PHP is a typeless language and so type
  conversions are juggled on the fly. If you pass a numerical string to a
  mathematical operator, then the string will be converted to the
  appropriate value.
 
 If you get a result of 1,234.56 from number_format() and try to use that
 as a numerical value (i.e. it's converted to an int or float), you'll end up
 with the value being 1, though. Everything after the first non-numeric
 character is lost. That's what he's talking about.
 
 ---John Holmes...
 
 
-- 
..
| InterJinn Application Framework - http://www.interjinn.com |
::
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for   |
| creating re-usable components quickly and easily.  |
`'

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] What to use?

2003-09-25 Thread Ryan A
Hey,
If anyones intrested, solved the problem like this:


function cun($num)
{
$num = number_format($num,2,'.',',');
return $num;
}

echo cun($var1);


Cheers,
-Ryan


We will slaughter you all! - The Iraqi (Dis)information ministers site
http://MrSahaf.com

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php