RE: [PHP] extract varying data from array with different formatting

2009-04-17 Thread Ford, Mike
On 14 April 2009 18:12, PJ advised:

> Jim Lucas wrote:
>> PJ wrote:
>>> Jim Lucas wrote:
 PJ wrote:
> foreach does a nice job if you want the results identical each
time.
> What can you use to change the formatting of the results dependent
on
> the number of results. Here's an example:
> foreach ( $authors[$bookID] AS $authorID => $authorData ) {

> # Display the echo "{$authorData['first_name']}
> {$authorData['last_name']}\n";
> }
> 
> will echo - Joe Boe John Blue Andy Candy etc depending on how many
> rows we have.
> What I want is: Joe Boe, John Blue, Andy Candy & Hans Stick (
> separated by commas, except for the last one which is separated
with
> & . I thought of passing a variable to the foreach and then using
if
> elseif... but that can't work because the variable is reset to 0
> after each pass. Can't get switch to do it (maybe I don't
understand
> it right. Help ? 

Needing effectively a three-way flag, I think I'd think a bit outside
the
box and go with something like (untested, but the general principle is
right!):

$count = FALSE;
foreach ($authors[$bookID] as $authorID=>$authorData):
   if ($count===FALSE):
  $count = count($authors[$bookID])-1;
   elseif (--$count):
  echo ', ';
   else:
  echo ' & ';
   endif;
   echo $authorData['first_name'], ' ', $authorData['last_name'];
endforeach;

Cheers!

Mike

 --
Mike Ford,  Electronic Information Developer,
C507, Leeds Metropolitan University, Civic Quarter Campus, 
Woodhouse Lane, LEEDS,  LS1 3HE,  United Kingdom
Email: m.f...@leedsmet.ac.uk
Tel: +44 113 812 4730


To view the terms under which this email is distributed, please go to 
http://disclaimer.leedsmet.ac.uk/email.htm

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



Re: [PHP] extract varying data from array with different formatting

2009-04-14 Thread PJ
Jim Lucas wrote:
> PJ wrote:
>> Jim Lucas wrote:
>>> PJ wrote:
 foreach does a nice job if you want the results identical each time.
 What can you use to change the formatting of the results dependent on
 the number of results.
 Here's an example:
 foreach ( $authors[$bookID] AS $authorID => $authorData ) {
 # Display the
 echo "{$authorData['first_name']}
 {$authorData['last_name']}\n";
 }

 will echo - Joe Boe John Blue Andy Candy etc depending on how many
 rows
 we have.
 What I want is: Joe Boe, John Blue, Andy Candy & Hans Stick (
 separated
 by commas, except for the last one which is separated with & .
 I thought of passing a variable to the foreach and then using if
 elseif... but that can't work because the variable is reset to 0 after
 each pass.
 Can't get switch to do it (maybe I don't understand it right.
 Help ?

>>> your answer lies with not replacing foreach to make your life/output
>>> better.  But with how the data is prepared and handed off to the
>>> foreach statement.
>>>
>>> I am guessing that what you want would be something like this.
>>>
>>> Since this looks like a snippet of code I sent you the other day, I
>>> will snag it "complete" from the other thread.
>>>
>>> >>
>>> ...
>>>
>>> # Test to see if the book has any authors
>>> if ( isset($authors[$bookID]) ) {
>>>
>>> # Tell us how many authors we found
>>> echo 'Found: ', count($author[$bookID]), ' authors';
>>>
>>> # Create an array that will hold the output from the DB.
>>> $aList = array();
>>> # Loop through the authors
>>> foreach ( $authors[$bookID] AS $authorID => $authorData ) {
>>> # Add all the authors to that new array
>>>  $aList[] = "{$authorData['last_name']},
>>> {$authorData['first_name']}";
>>> }
>>>
>>> # Sanitize the output
>>> $aList = array_map('htmlspecialchars', $aList);
>>>
>>> # Get a count of how many authors their is.
>>> $tAuthors = count($aList);
>>>
>>> # If more then one, do...
>>> if ( $tAuthors > 1 ) {
>>> # Take the last one off, so we can handle it
>>> differently
>>> $last_author = array_pop($aList);
>>> echo join(', ', $aList), ' & ', $last_author;
>>>
>>> # If only one, then do...
>>> } elseif ( $tAuthors == 1 ) {
>>> echo join('', $aList);
>>> }
>>> echo '';
>>> } else {
>>> echo 'No authors found';
>>> }
>>>
>>>
>>> ...
>>>
>>> ?>
>>>
>> Hi Jim,
>> Please excuse my writing to you directly. If you don't think it's
>> proper, just let me know & I'll swing off.
>
> No problem with hitting me directly, but your findings could be of use
> to others
> since it is cleaner way of solving your problem.
>
> So, on the list we go
>
>> So, while I have your attention and if you have a moment, I found this
>> interesting possibility: someone threw "implode()" at me and I looked it
>> up in the php manual and hey, found and interesting little function:
>>
>> function ImplodeProper($arr, $lastConnector = ' & ')
>> {
>>   if( !is_array($arr) or count($arr) == 0) return '';
>>   $last = array_pop($arr);
>>   if(count($arr))
>> return implode(', ',$arr).", $lastConnector $last";
>>   else
>> return $last;
>> }
>>
>> Since my limited understanding of php impedes me to implement it
>> experimentally in my application, I was curious if, indeed, it could be
>> another option. It does look to me like it could work, but it would
>> require a slightly different implementation of the query, probably
>> somehow using something like a CONCAT(first_name, " ", last_name). Am I
>> learning something here or is this the wrong tree to bark up?
>>
>
> This would be a very nice way to clean up the code
>
> Although you could do something with CONCAT(), I would suggest leaving
> the code as is.
>
> Data is all that you want to get from MYSQL, not format.
>
> Basically, take the above function and place it in a lib you include
> for everything.  Then replace the if/then/else statement that I built
> for you with a
> call to said function.
>
> 
> ...
> function ImplodeProper($arr, $lastConnector = ' & ')
> {
>if( !is_array($arr) or count($arr) == 0) return '';
>$last = array_pop($arr);
>if(count($arr))
>  return implode(', ',$arr).", $lastConnector $last";
>else
>  return $last;
> }
> ...
>
>  # Test to see if the book has any authors
>  if ( isset($authors[$bookID]) ) {
>
>  # Tell us how many authors we found
>  echo 'Found: ', count($author[$bookID]), ' authors';
>
>  # Create an array that will hold the output from the DB.
>  $aList = array();
>

Re: [PHP] extract varying data from array with different formatting

2009-04-13 Thread Jim Lucas

PJ wrote:

Jim Lucas wrote:

PJ wrote:

foreach does a nice job if you want the results identical each time.
What can you use to change the formatting of the results dependent on
the number of results.
Here's an example:
foreach ( $authors[$bookID] AS $authorID => $authorData ) {
# Display the
echo "{$authorData['first_name']}
{$authorData['last_name']}\n";
}

will echo - Joe Boe John Blue Andy Candy etc depending on how many rows
we have.
What I want is: Joe Boe, John Blue, Andy Candy & Hans Stick ( separated
by commas, except for the last one which is separated with & .
I thought of passing a variable to the foreach and then using if
elseif... but that can't work because the variable is reset to 0 after
each pass.
Can't get switch to do it (maybe I don't understand it right.
Help ?


your answer lies with not replacing foreach to make your life/output
better.  But with how the data is prepared and handed off to the
foreach statement.

I am guessing that what you want would be something like this.

Since this looks like a snippet of code I sent you the other day, I
will snag it "complete" from the other thread.

 $authorData ) {
# Add all the authors to that new array
 $aList[] = "{$authorData['last_name']},
{$authorData['first_name']}";
}

# Sanitize the output
$aList = array_map('htmlspecialchars', $aList);

# Get a count of how many authors their is.
$tAuthors = count($aList);

# If more then one, do...
if ( $tAuthors > 1 ) {
# Take the last one off, so we can handle it differently
$last_author = array_pop($aList);
echo join(', ', $aList), ' & ', $last_author;

# If only one, then do...
} elseif ( $tAuthors == 1 ) {
echo join('', $aList);
}
echo '';
} else {
echo 'No authors found';
}


...

?>


Hi Jim,
Please excuse my writing to you directly. If you don't think it's
proper, just let me know & I'll swing off.


No problem with hitting me directly, but your findings could be of use to others
since it is cleaner way of solving your problem.

So, on the list we go


So, while I have your attention and if you have a moment, I found this
interesting possibility: someone threw "implode()" at me and I looked it
up in the php manual and hey, found and interesting little function:

function ImplodeProper($arr, $lastConnector = ' & ')
{
  if( !is_array($arr) or count($arr) == 0) return '';
  $last = array_pop($arr);
  if(count($arr))
return implode(', ',$arr).", $lastConnector $last";
  else
return $last;
}

Since my limited understanding of php impedes me to implement it
experimentally in my application, I was curious if, indeed, it could be
another option. It does look to me like it could work, but it would
require a slightly different implementation of the query, probably
somehow using something like a CONCAT(first_name, " ", last_name). Am I
learning something here or is this the wrong tree to bark up?



This would be a very nice way to clean up the code

Although you could do something with CONCAT(), I would suggest leaving the code 
as is.

Data is all that you want to get from MYSQL, not format.

Basically, take the above function and place it in a lib you include
for everything.  Then replace the if/then/else statement that I built for you 
with a
call to said function.

 $authorData ) {
 # Add all the authors to that new array
  $aList[] = "{$authorData['last_name']}, 
{$authorData['first_name']}";
 }

 # Sanitize the output
 $aList = array_map('htmlspecialchars', $aList);

 echo implodeProper($aList), '';

 } else {
 echo 'No authors found';
 }

...

?>

That would do the trick.

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



Re: [PHP] extract varying data from array with different formatting

2009-04-12 Thread Ashley Sheridan
On Sat, 2009-04-11 at 16:11 -0500, PJ wrote:
> foreach does a nice job if you want the results identical each time.
> What can you use to change the formatting of the results dependent on
> the number of results.
> Here's an example:
> foreach ( $authors[$bookID] AS $authorID => $authorData ) {
> # Display the
> echo "{$authorData['first_name']} 
> {$authorData['last_name']}\n";
> }
> 
> will echo - Joe Boe John Blue Andy Candy etc depending on how many rows
> we have.
> What I want is: Joe Boe, John Blue, Andy Candy & Hans Stick ( separated
> by commas, except for the last one which is separated with & .
> I thought of passing a variable to the foreach and then using if
> elseif... but that can't work because the variable is reset to 0 after
> each pass.
> Can't get switch to do it (maybe I don't understand it right.
> Help ?
> 
> -- 
> 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
> 
> 
$count = 1;
foreach ( $authors[$bookID] AS $authorID => $authorData )
{
  echo "{$authorData['first_name']} {$authorData['last_name']}\n";
  echo($count < count($authors[$bookID]))?', ':' & ';
  $count ++;
}


Ash
www.ashleysheridan.co.uk


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



Re: [PHP] extract varying data from array with different formatting

2009-04-12 Thread Jim Lucas

PJ wrote:

foreach does a nice job if you want the results identical each time.
What can you use to change the formatting of the results dependent on
the number of results.
Here's an example:
foreach ( $authors[$bookID] AS $authorID => $authorData ) {
# Display the
echo "{$authorData['first_name']} 
{$authorData['last_name']}\n";

}

will echo - Joe Boe John Blue Andy Candy etc depending on how many rows
we have.
What I want is: Joe Boe, John Blue, Andy Candy & Hans Stick ( separated
by commas, except for the last one which is separated with & .
I thought of passing a variable to the foreach and then using if
elseif... but that can't work because the variable is reset to 0 after
each pass.
Can't get switch to do it (maybe I don't understand it right.
Help ?



your answer lies with not replacing foreach to make your life/output better.  But with how the data is prepared and 
handed off to the foreach statement.


I am guessing that what you want would be something like this.

Since this looks like a snippet of code I sent you the other day, I will snag it 
"complete" from the other thread.

 $authorData ) {
# Add all the authors to that new array
 $aList[] = "{$authorData['last_name']}, 
{$authorData['first_name']}";
}

# Sanitize the output
$aList = array_map('htmlspecialchars', $aList);

# Get a count of how many authors their is.
$tAuthors = count($aList);

# If more then one, do...
if ( $tAuthors > 1 ) {
# Take the last one off, so we can handle it differently
$last_author = array_pop($aList);
echo join(', ', $aList), ' & ', $last_author;

# If only one, then do...
} elseif ( $tAuthors == 1 ) {
echo join('', $aList);
}
echo '';
} else {
echo 'No authors found';
}


...

?>

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



[PHP] extract varying data from array with different formatting

2009-04-11 Thread PJ
foreach does a nice job if you want the results identical each time.
What can you use to change the formatting of the results dependent on
the number of results.
Here's an example:
foreach ( $authors[$bookID] AS $authorID => $authorData ) {
# Display the
echo "{$authorData['first_name']} 
{$authorData['last_name']}\n";
}

will echo - Joe Boe John Blue Andy Candy etc depending on how many rows
we have.
What I want is: Joe Boe, John Blue, Andy Candy & Hans Stick ( separated
by commas, except for the last one which is separated with & .
I thought of passing a variable to the foreach and then using if
elseif... but that can't work because the variable is reset to 0 after
each pass.
Can't get switch to do it (maybe I don't understand it right.
Help ?

-- 
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