anthrt Posted September 1, 2006 Share Posted September 1, 2006 OK, so I have set up a script which will loop through a database (the database contains info on music videos, video thumbnail, number of downloads etc). It also contains the category the video is in, e.g. "english", "foreign" etc.The script is sorting and displaying the videos by category fine. What I would like it to do, is at the start of each category, display the title, so it would show "English" before the english category, then when it gets down to the "foreign" category, it would display the "Foreign" title.Currently the script is showing the category title before every video. For example:English[ english video here ]English[ english video here ]English[ english video here ]Foreign[ foreign video here ]Foreign[ foreign video here ]etc. I would only like it to show the category title one time. Another example:English[ english video here ][ english video here ]Foreign[ foreign video here ][ foreign video here ]You get the point.Here's what I have so far. Pretty simple but it does the job.In this case, `setname` would be the category i'm referring to, e.g english.[code]<?phperror_reporting(E_ALL);include('db.php');$query = mysql_query("SELECT * FROM `vids` ORDER BY `setname`");while($row=mysql_fetch_array($query)) { $title = $row['title']; $descr = $row['descr']; $img = $row['img']; $views = $row['views']; $setname = $row['setnamelong']; echo <<<HTML<h1>$setname</h1><br /><img src="$img" width="100" height="100" /><br /><h1>$title</h1><br />$descr</td> </tr></table><br /><br />HTML;}?>[/code]I've trimmed some of the HTML out just to make it easier to look through. Link to comment https://forums.phpfreaks.com/topic/19367-question-regarding-while-loops-read-for-a-better-description/ Share on other sites More sharing options...
ToonMariner Posted September 1, 2006 Share Posted September 1, 2006 [code]<?phperror_reporting(E_ALL);include('db.php');$query = mysql_query("SELECT * FROM `vids` ORDER BY `setname`");$cat = null;while($row=mysql_fetch_array($query)) { $title = $row['title']; $descr = $row['descr']; $img = $row['img']; $views = $row['views']; $setname = $row['setnamelong'];if (is_null($cat) || strcomp($cat,$setname) !=0){ $cat = $setname; echo <<<HTML<h1>$setname</h1><br /><img src="$img" width="100" height="100" /><br /><h1>$title</h1><br />$descr</td> </tr></table><br /><br />HTML;}else{echo <<<HTML<img src="$img" width="100" height="100" /><br /><h1>$title</h1><br />$descr</td> </tr></table><br /><br />HTML; }}?>[/code]BUT i fear you will get some very poorly formed html in there..... Link to comment https://forums.phpfreaks.com/topic/19367-question-regarding-while-loops-read-for-a-better-description/#findComment-84047 Share on other sites More sharing options...
HuggieBear Posted September 1, 2006 Share Posted September 1, 2006 You could use two queries.I'm not writing actual code, so don't come back complaining about syntax, just want to give you an idea...[code]SELECT distinct(setnamelong) FROM vids ORDER BY setnamelongforeach ($setnamelong as $setname){ echo '$setname' SELECT * FROM vids WHERE setnamelong = '$setname' ORDER BY title while ( ... ){ // then your existing echo loop here }}[/code]What do you think feasible?Rich Link to comment https://forums.phpfreaks.com/topic/19367-question-regarding-while-loops-read-for-a-better-description/#findComment-84048 Share on other sites More sharing options...
anthrt Posted September 1, 2006 Author Share Posted September 1, 2006 [quote author=ToonMariner link=topic=106499.msg425964#msg425964 date=1157121854][code]<?phperror_reporting(E_ALL);include('db.php');$query = mysql_query("SELECT * FROM `vids` ORDER BY `setname`");$cat = null;while($row=mysql_fetch_array($query)) { $title = $row['title']; $descr = $row['descr']; $img = $row['img']; $views = $row['views']; $setname = $row['setnamelong'];if (is_null($cat) || strcomp($cat,$setname) !=0){ $cat = $setname; echo <<<HTML<h1>$setname</h1><br /><img src="$img" width="100" height="100" /><br /><h1>$title</h1><br />$descr</td> </tr></table><br /><br />HTML;}else{echo <<<HTML<img src="$img" width="100" height="100" /><br /><h1>$title</h1><br />$descr</td> </tr></table><br /><br />HTML; }}?>[/code]BUT i fear you will get some very poorly formed html in there.....[/quote]works a charm, besides the html of course :)thanks to you both for posting. Link to comment https://forums.phpfreaks.com/topic/19367-question-regarding-while-loops-read-for-a-better-description/#findComment-84065 Share on other sites More sharing options...
Recommended Posts