Re: extract varying data from array with different formatting

From: Date: Tue, 14 Apr 2009 01:47:23 +0000
Subject: Re: extract varying data from array with different formatting
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-291455@lists.php.net to get a copy of this message
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. <?php ...
        # 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 '<br />';
        } 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. <?php ... 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();
             # 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);
             echo implodeProper($aList), '<br />';
         } else {
             echo 'No authors found';
         }
... ?> That would do the trick.

« previous php.general (#291455) next »