Jump to content

Archived

This topic is now archived and is closed to further replies.

extrovertive

Simple mysql Array help

Recommended Posts

This is my output:


[b]ENGINEERING[/b] Electrical Engr
[b]EXAMS [/b]ACT
[b]EXAMS[/b] PSAT
[b]EXAMS[/b] SAT
[b]EXAMS[/b] SAT II Chemistry
[b]EXAMS[/b] SAT II Physics
[b]HUMANITIES [/b]Philosophy
[b]HUMANITIES[/b] Religious Studies
[b]MATH [/b]Algebra 10-12
[b]MATH [/b]Algebra 7-9
...


Based on getting  a result from several tables.
[code=php:0]
$result = mysql_query($query) or die(mysql_error());

while(list($category, $subject) = mysql_fetch_row($result))
{
echo "<strong>". strtoupper($category) ."</strong>\n";
echo ucwords($subject) . "<br/>\n";
}
[/code]

Now, the category is redundant. I just want to output

[b]ENGINEERING [/b]
Electrical Engr
[b]EXAMS [/b]
ACT
PSAT
SAT
SAT II Chemistry
SAT II Physics
... and so on

How is this possible to do in my while loop?

Share this post


Link to post
Share on other sites
Check for change of category
[code]<?php
$prevCat = '';
while(list($category, $subject) = mysql_fetch_row($result))
{
          if ($prevCat != $category) {
                echo "<strong>". strtoupper($category) ."</strong>\n";
                $prevCat = $category;
          }
          echo ucwords($subject) . "<br/>\n";

}
?>[/code]

Share this post


Link to post
Share on other sites

×

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.