Jump to content


Photo

Question regarding while loops - read for a better description


  • This topic is locked This topic is locked
3 replies to this topic

#1 anthrt

anthrt
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 01 September 2006 - 02:32 PM

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.

<?php
error_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;
}
?>

I've trimmed some of the HTML out just to make it easier to look through.

#2 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 01 September 2006 - 02:44 PM

<?php
error_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;
 }
}
?>

BUT i fear you will get some very poorly formed html in there.....
follow me on twitter @PHPsycho

#3 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 01 September 2006 - 02:45 PM

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

SELECT distinct(setnamelong) FROM vids ORDER BY setnamelong

foreach ($setnamelong as $setname){
   echo '$setname'
   SELECT * FROM vids WHERE setnamelong = '$setname' ORDER BY title
   while ( ... ){
      // then your existing echo loop here
   }
}

What do you think feasible?

Rich
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#4 anthrt

anthrt
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 01 September 2006 - 03:03 PM

<?php
error_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;
 }
}
?>

BUT i fear you will get some very poorly formed html in there.....

works a charm, besides the html of course :)

thanks to you both for posting.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users