Jump to content


Photo

Simple mysql Array help


  • Please log in to reply
2 replies to this topic

#1 extrovertive

extrovertive
  • Members
  • PipPipPip
  • Advanced Member
  • 235 posts

Posted 15 August 2006 - 05:22 AM

This is my output:


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


Based on getting  a result from several tables.
$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"; 	
	}

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

ENGINEERING
Electrical Engr
EXAMS
ACT
PSAT
SAT
SAT II Chemistry
SAT II Physics
... and so on

How is this possible to do in my while loop?



#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 15 August 2006 - 06:59 AM

Check for change of category
<?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"; 

}
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 extrovertive

extrovertive
  • Members
  • PipPipPip
  • Advanced Member
  • 235 posts

Posted 15 August 2006 - 07:06 AM

Wow, easy logic. Thanks!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users