Jump to content

Simple mysql Array help


extrovertive

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?

Link to comment
https://forums.phpfreaks.com/topic/17588-simple-mysql-array-help/
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]

Archived

This topic is now archived and is 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.