HDRebel88 Posted July 7, 2013 Share Posted July 7, 2013 (edited) I have a MySQL query that gets data from two tables via a Join query. The resulting dataset is this: 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 July 7, 2013 by HDRebel88 Quote Link to comment Share on other sites More sharing options...
HDRebel88 Posted July 8, 2013 Author Share Posted July 8, 2013 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` Quote Link to comment 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.