xwishmasterx Posted March 27, 2013 Share Posted March 27, 2013 I have the following code that obviously displays a table with some name: $cast = $movie->cast(); if (!empty($cast)) { ++$rows; echo '<TR><TD valign=top><B>Cast:</B></TD><TD>'; echo "<table align='left' border='1' style='border-collapse:collapse;background-color:#ddd;'><tr><th style='background-color:#07f;'>Actor</th><th style='background-color:#07f;'>Role</th></tr>"; for ($i = 0; $i < count($cast); $i++) { echo '<tr><td width=200>'; echo "<a href='person.php?engine=$engine&mid=".$cast[$i]["imdb"]."'>"; echo $cast[$i]["name"].'</a></td><td>'; echo $cast[$i]["role"]."</td></tr>"; } echo "</table></td></tr>\n"; } flush(); How can I display this as a comma seperated list? I only need the "names" ($cast[$i]["name"]) and nothing else as one sinle list with commas. Quote Link to comment https://forums.phpfreaks.com/topic/276222-transforming-a-table-list-to-a-comma-separeted-list-instead/ Share on other sites More sharing options...
Psycho Posted March 27, 2013 Share Posted March 27, 2013 (edited) Well, it depends. If the list is small and the total length will be less than 1024 characters, you can do it directly in the query using GROUP_CONCAT(). Otherwise, you can just process the result set into an array and use implode(). Using query SELECT GROUP_CONCAT(field1) as field1List FROM table_name GROUP BY field2 Using PHP $field1Values = array(); while($row = mysql_fetch_assoc($result)) { $field1Values[] = $row['field1']; } $field1List = implode(', ', $field1Values); Edited March 27, 2013 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/276222-transforming-a-table-list-to-a-comma-separeted-list-instead/#findComment-1421408 Share on other sites More sharing options...
xwishmasterx Posted March 27, 2013 Author Share Posted March 27, 2013 Thanks for the fast reply Psycho:) I think I was a little fast on the opening question, so please allowe me explain what I actually are trying to achieve here. Further down my little script I need to write this comma separated list to a simple txt file (I'm no wizard at this but so far this works well): $stringData = MY-COMMA-SEPERATED-LIST-SHUOLD-BE-HERE; fwrite($fh, $stringData); My main problem is I have no clue how to transform my list to something that works here :/ Quote Link to comment https://forums.phpfreaks.com/topic/276222-transforming-a-table-list-to-a-comma-separeted-list-instead/#findComment-1421410 Share on other sites More sharing options...
Solution Psycho Posted March 27, 2013 Solution Share Posted March 27, 2013 (edited) My main problem is I have no clue how to transform my list to something that works here :/ Did you try either of my suggestions? EDIT: I could have sworn your original question mentioned getting the records from the database, but I see you state they are in an array. But, I see the values are in an array with each value in different sub-arrays. So, you can to put the values in a single-dimensional array and then implode them. Basically what I already suggested in option 2. $names = array(); foreach($cast as $record) { $names[] = $record['name']; } $stringData = implode(', ', $names); Edited March 27, 2013 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/276222-transforming-a-table-list-to-a-comma-separeted-list-instead/#findComment-1421411 Share on other sites More sharing options...
davidannis Posted March 27, 2013 Share Posted March 27, 2013 (edited) I think that if you use Psycho's code the list is in the variable $field1List then just $field1Values = array(); while($row = mysql_fetch_assoc($result)) { $field1Values[] = $row['field1']; } $field1List = implode(', ', $field1Values); fwrite ($fh, $field1List); Edited March 27, 2013 by davidannis Quote Link to comment https://forums.phpfreaks.com/topic/276222-transforming-a-table-list-to-a-comma-separeted-list-instead/#findComment-1421412 Share on other sites More sharing options...
xwishmasterx Posted March 27, 2013 Author Share Posted March 27, 2013 Sorry Psycho. I'm not getting these results from my own database. I'm wondering how I can "add" this part of the code (the results) $cast[$i]["name"] to the last part of code: $stringData = $cast[$i]["name"] ; fwrite($fh, $stringData); basically what I want, but obviously not working Quote Link to comment https://forums.phpfreaks.com/topic/276222-transforming-a-table-list-to-a-comma-separeted-list-instead/#findComment-1421414 Share on other sites More sharing options...
Psycho Posted March 27, 2013 Share Posted March 27, 2013 Sorry Psycho. I'm not getting these results from my own database. I'm wondering how I can "add" this part of the code (the results) Please see the edit in my last response. Quote Link to comment https://forums.phpfreaks.com/topic/276222-transforming-a-table-list-to-a-comma-separeted-list-instead/#findComment-1421415 Share on other sites More sharing options...
xwishmasterx Posted March 27, 2013 Author Share Posted March 27, 2013 That works just perfect! Thank you so much for taking time to help a novice! Quote Link to comment https://forums.phpfreaks.com/topic/276222-transforming-a-table-list-to-a-comma-separeted-list-instead/#findComment-1421418 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.