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
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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