Jump to content

Question regarding while loops - read for a better description


anthrt

Recommended Posts

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]<?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;
}
?>[/code]

I've trimmed some of the HTML out just to make it easier to look through.
Link to comment
Share on other sites

[code]<?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;
}
}
?>
[/code]

BUT i fear you will get some very poorly formed html in there.....
Link to comment
Share on other sites

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 setnamelong

foreach ($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
Share on other sites

[quote author=ToonMariner link=topic=106499.msg425964#msg425964 date=1157121854]
[code]<?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;
}
}
?>
[/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
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • 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.