Jump to content

Recommended Posts

I have a MySQL query that gets data from two tables via a Join query.
 
The resulting dataset is this:
 
query.png
 
See how I have two rows of the title "Coming Out for Christmas". The only difference is the values in the "name" column. I need to essentially use the data from the first "Coming Out for Christmas" row for most of the page and then piece together a comma separated list from the data in the "name" column. 
 
So I would get a list like "Test A, Test B". 
 
I know I need some kind of PHP flag to do this, but I don't know how or where ... any help?
 
$posterurl="noposter.png";
$titleCheck=null;
$cast=null;
$connected=Connect();
if($filmsQuery=mysqli_prepare($connected, "SELECT `films`.`title`, `films`.`release`, `films`.`poster`, `films`.`synopsis`, `films`.`status`, `cast`.`name` FROM `films` LEFT JOIN `cast` ON `cast`.`filmID`=`films`.`id`")){
mysqli_stmt_execute($filmsQuery);
mysqli_stmt_bind_result($filmsQuery, $title, $release, $poster, $synopsis, $status, $cast);
mysqli_stmt_store_result($filmsQuery);
$numofFilmsReturned=mysqli_stmt_num_rows($filmsQuery);
if($numofFilmsReturned>0){
$i=1;
while(mysqli_stmt_fetch($filmsQuery)){
if($titleCheck!=$title){
$titleCheck=$title;
$castlist=$cast.", ";
$castlist=rtrim($castlist, ",");
if($i==$numofFilmsReturned){$filmWrapper='Last';}else{}
if($poster==null || $poster==''){$posterurl=$posterurl;}else{$posterurl=$poster;}
if($status==0){$status="In Development";}else{$status="Completed";}
$releasedate=strtotime($release);
$releaseYear=date("Y", $releasedate);
$content.='<div id="filmWrapper'.$filmWrapper.'">
<div id="filmTitle">
<div class="filmLeft"><span class="bold">'.$title.'</span><br /><span class="filmStatus">('.$releaseYear.')</span></div>
<div class="filmRight"><span class="filmStatus">Status: '.$status.'</span></div>
</div>
<div id="filmPosterSynopsis">
<div class="filmLeft" id="filmPoster"><img src="'.$posterurl.'" /></div>
<div class="filmRight" id="filmSynopsis">Film Synopsis</div>
</div>
<div id="filmLinksCast">
<div class="filmLeft" id="filmLinks">Website | IMDB | Facebook</div>
<div class="filmRight" id="filmCast"><span class="bold">Cast:</span> '.$castlist.'</div>
</div>
</div>';
$i++;
}
}
}
else{
$content.='<div>Could not find any films.</div>';
}
}
else{
$content.='<div id="queryFailed">Query failed to run correctly.</div>';
}
mysqli_close($connected);

 

This is the result right now: http://www.andrewmccarrick.com/kam/?p=films

Edited by HDRebel88

I just modified the query to use Group_concat.

 

 

SELECT `films`.`title`, `films`.`release`, `films`.`poster`, `films`.`synopsis`, `films`.`status`, GROUP_CONCAT(`cast`.`name` SEPARATOR ', ') AS castlist FROM `films` LEFT JOIN `cast` ON `cast`.`filmID`=`films`.`id` GROUP BY `films`.`id`
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.