Jump to content

"transforming" a table list to a comma-separeted list instead?


xwishmasterx

Recommended Posts

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.

 

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

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

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.