vynsane Posted July 9, 2007 Share Posted July 9, 2007 i've got a script that pulls first name, last name, and suffix from a DB query and echos them only if they are not null. the names are cross-referenced by id number from two other tables. it displays the name pieces that are not null for each "job", and the jobs and names are determined by another id number for an individual issue page. an example of the output is here: www.WallCrawlersWeb.com/database/comics/title/asm/issue/148/creative as you can see, the job "inker" has two names after it. how can i get it to put a comma between those two names. this is the bit that writes that box: $creatorQuery = mysql_query("SELECT * FROM ComicCreators, Creators, CreatorCat WHERE ComicCreators.issueID = '".$ComicIssues['ID']."' AND ComicCreators.creatorID = Creators.ID AND ComicCreators.creatorCatID = CreatorCat.ID ORDER BY CreatorCat.ID"); $creatorNumRows = mysql_num_rows($creatorQuery); if($creatorNumRows > 0) { while($row = mysql_fetch_array($creatorQuery)){ $creators[$row['catName']][] = $row; } foreach($creators as $creatorCat => $creators) { echo "<p><strong>".$creatorCat.":</strong>"; foreach($creators as $creator){ if($creator['Fname'] !=Null){ echo " ".$creator['Fname'].""; } if($creator['Lname'] !=Null){ echo " ".$creator['Lname'].""; } if($creator['suffix'] !=Null){ echo " ".$creator['suffix'].""; } } echo "</p>\n"; } } else { echo "\t\t\t\t\t<p>No creative team information available.</p>\n"; } i've tried using join() but it only works if it's an array, and by the time i've echoed or stringed the name based on what is not null, it's not an array anymore. i've also tried using rtrim() but it gets rid of all commas, because each name is rtrim()ed. anyone got a good solution? Quote Link to comment https://forums.phpfreaks.com/topic/59093-solved-add-commas-between-names/ Share on other sites More sharing options...
Yesideez Posted July 9, 2007 Share Posted July 9, 2007 implode() http://php.net/implode Quote Link to comment https://forums.phpfreaks.com/topic/59093-solved-add-commas-between-names/#findComment-293406 Share on other sites More sharing options...
vynsane Posted July 9, 2007 Author Share Posted July 9, 2007 i love one-word answers, that's awesome. always a lot of fun. as stated above, i've already tried using "join()" which is an alias of "implode()". this doesn't work since the first name, last name and suffix are each independent nodes of the array containing them (since they're in separate columns, Fname, Lname and suffix), and thus will end up with something like this: Inker: Mike, Esposito, David, Hunt whereas i need it to look like this: Inker: Mike Esposito, David Hunt (i also would like it to say "Inkers:" instead of "Inker:" when there's multiple names for that job, but that's not the huge issue... and i can probably figure that one out on my own... but maybe not. probably will just have to make another query inside the "foreach" that checks numrows for multiples of that ID number and echo accordingly.) i suppose i could use either "join()" or "implode()" if i could turn the first, last, and suffix into a string, then for each job make a new array out of the strings THEN implode but that seems pretty long-winded. Quote Link to comment https://forums.phpfreaks.com/topic/59093-solved-add-commas-between-names/#findComment-293418 Share on other sites More sharing options...
GingerRobot Posted July 9, 2007 Share Posted July 9, 2007 I would suggest that you build a string as you go when you do this part: <?php foreach($creators as $creator){ if($creator['Fname'] !=Null){ echo " ".$creator['Fname'].""; } if($creator['Lname'] !=Null){ echo " ".$creator['Lname'].""; } if($creator['suffix'] !=Null){ echo " ".$creator['suffix'].""; } } ?> Where you add a comma after each part of the name, and then strip it out if the next part is not null. Something like: <?php if($creator['Fname'] !=Null){ $name = $creator['Fname'].','; } if($creator['Lname'] !=Null){ $name = substr_replace($name,' '.$creator['Lname'],strlen($name)-1).','; } if($creator['suffix'] !=Null){ $name = substr_replace($name,' '.$creator['suffix'],strlen($name)-1).','; } echo $name; Edit: Hang on, this needs modifying slightly. Quote Link to comment https://forums.phpfreaks.com/topic/59093-solved-add-commas-between-names/#findComment-293428 Share on other sites More sharing options...
rcorlew Posted July 9, 2007 Share Posted July 9, 2007 If you need to have a comma only if both are not null do this: if(($creator['Lname'] !=Null) && ($creator['suffix'] !=Null)){ echo ","; } Try that and let me know Quote Link to comment https://forums.phpfreaks.com/topic/59093-solved-add-commas-between-names/#findComment-293438 Share on other sites More sharing options...
vynsane Posted July 9, 2007 Author Share Posted July 9, 2007 I would suggest that you build a string as you go when you do this part: Where you add a comma after each part of the name, and then strip it out if the next part is not null. Something like: Edit: Hang on, this needs modifying slightly. interesting method... before you made the edit to "hang on" i was going to say it would probably end up with a comma on the end of everything though: Editor: Len Wein, Writer: Gerry Conway, Penciller: Ross Andru, Inker: Mike Esposito, David Hunt, i guess i would only build the string like this after doing a numrows count and finding out if it's necessary - this would also get me to add the "s" after "Inker" so two birds with one stone. otherwise just straight-up echo. i'll experiment with this, thanks! If you need to have a comma only if both are not null do this: if(($creator['Lname'] !=Null) && ($creator['suffix'] !=Null)){ echo ","; } Try that and let me know i only need to add a comma if there are multiple names for each job, so Editor: Len Wein needn't a comma, whereas Inker: Mike Esposito David Hunt does, after "Esposito". but unfortunately i have the "suffix" as well, so i can't just automatically place the comma after the last name. Quote Link to comment https://forums.phpfreaks.com/topic/59093-solved-add-commas-between-names/#findComment-293448 Share on other sites More sharing options...
GingerRobot Posted July 9, 2007 Share Posted July 9, 2007 I forgot that if one of them is null, the last character wouldn't be a comma. So this now checks to see if there is a comma in the string. If there isn't, it simply appends the current part(first name, last name or suffix) to the end of the string. If there is, it replaces the comma with the current part. <?php if($creator['Fname'] !=Null){ $name = $creator['Fname'].','; } if($creator['Lname'] !=Null){ $name =(strpos($name,',')===false) ? $name .= ' '.$creator['Lname'].',' : substr_replace($name,' '.$creator['Lname'].',',strlen($name)-1); } if($creator['suffix'] !=Null){ $name =(strpos($name,',')===false) ? $name .= ' '.$creator['suffix'].',' : substr_replace($name,' '.$creator['suffix'].',',strlen($name)-1); } echo $name; ?> Quote Link to comment https://forums.phpfreaks.com/topic/59093-solved-add-commas-between-names/#findComment-293451 Share on other sites More sharing options...
kenrbnsn Posted July 9, 2007 Share Posted July 9, 2007 The easiest way to do this is to use a temporary array to hold the names. Then you can use implode on the temporary array. <?php $tmp = array(); foreach($creators as $creator){ $tmp[] = implode(' ',$creator); } echo implode(',',$tmp); ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/59093-solved-add-commas-between-names/#findComment-293471 Share on other sites More sharing options...
vynsane Posted July 9, 2007 Author Share Posted July 9, 2007 I forgot that if one of them is null, the last character wouldn't be a comma. So this now checks to see if there is a comma in the string. If there isn't, it simply appends the current part(first name, last name or suffix) to the end of the string. If there is, it replaces the comma with the current part. <?php if($creator['Fname'] !=Null){ $name = $creator['Fname'].','; } if($creator['Lname'] !=Null){ $name =(strpos($name,',')===false) ? $name .= ' '.$creator['Lname'].',' : substr_replace($name,' '.$creator['Lname'].',',strlen($name)-1); } if($creator['suffix'] !=Null){ $name =(strpos($name,',')===false) ? $name .= ' '.$creator['suffix'].',' : substr_replace($name,' '.$creator['suffix'].',',strlen($name)-1); } echo $name; ?> as i suspected, this gives me commas at the end of every name, but it's the closest i've gotten yet. The easiest way to do this is to use a temporary array to hold the names. Then you can use implode on the temporary array. <?php $tmp = array(); foreach($creators as $creator){ $tmp[] = implode(' ',$creator); } echo implode(',',$tmp); ?> Ken this would easily work if the only things in the array were the Fname, Lname and suffix, but i've got a whole lot of other stuff in there as well. i might have to resort to multiple queries to break down the info into bite-size chunks. easier to swallow that way. Quote Link to comment https://forums.phpfreaks.com/topic/59093-solved-add-commas-between-names/#findComment-293518 Share on other sites More sharing options...
GingerRobot Posted July 9, 2007 Share Posted July 9, 2007 Well, you can always go one step further with the building of a string, and add each name to it as you go along. You then strip off the last character which will always be the comma. Quote Link to comment https://forums.phpfreaks.com/topic/59093-solved-add-commas-between-names/#findComment-293529 Share on other sites More sharing options...
sasa Posted July 9, 2007 Share Posted July 9, 2007 you wont to add comma if is not new cat_name and before 1st part of creator name try <?php $creatorQuery = mysql_query("SELECT * FROM ComicCreators, Creators, CreatorCat WHERE ComicCreators.issueID = '".$ComicIssues['ID']."' AND ComicCreators.creatorID = Creators.ID AND ComicCreators.creatorCatID = CreatorCat.ID ORDER BY CreatorCat.ID"); $creatorNumRows = mysql_num_rows($creatorQuery); if($creatorNumRows > 0) { while($row = mysql_fetch_array($creatorQuery)){ $creators[$row['catName']][] = $row;} foreach($creators as $creatorCat => $creators) { echo "<p><strong>".$creatorCat.":</strong>"; $is_new_cat = true; foreach($creators as $creator){ $first_part_of_creator_name = true; if($creator['Fname'] !=Null){ if ($first_part_of_creator_name and !$is_new_cat) echo ','; $first_part_of_creator_name = false; $is_new_cat = false; echo " ".$creator['Fname'].""; } if($creator['Lname'] !=Null){ if ($first_part_of_creator_name and !$is_new_cat) echo ','; $first_part_of_creator_name = false; $is_new_cat = false; echo " ".$creator['Lname'].""; } if($creator['suffix'] !=Null){ if ($first_part_of_creator_name and !$is_new_cat) echo ','; $first_part_of_creator_name = false; $is_new_cat = false; echo " ".$creator['suffix'].""; } } echo "</p>\n"; } } else { echo "\t\t\t\t\t<p>No creative team information available.</p>\n";} ?> Quote Link to comment https://forums.phpfreaks.com/topic/59093-solved-add-commas-between-names/#findComment-293572 Share on other sites More sharing options...
vynsane Posted July 9, 2007 Author Share Posted July 9, 2007 HOLY CRAP! now THAT's some CODIN'! that did it, thanks sasa! i think i'll try to turn that into a function though, in order to cut down on the code on the page. and thanks to everyone else that brain stormed with me. i hope your headaches go away as fast as mine does. Quote Link to comment https://forums.phpfreaks.com/topic/59093-solved-add-commas-between-names/#findComment-293589 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.