Jump to content

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


xwishmasterx
Go to solution Solved by Psycho,

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.

 

Link to comment
Share on other sites

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 by Psycho
Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • Solution

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 by Psycho
Link to comment
Share on other sites

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 by davidannis
Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.