Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Putting unique header on top of categorized MySQL results

Status
Not open for further replies.

thisisafakeaccount12

Technical User
Jul 7, 2005
6
US
I have a small application that displays an HTML table in categories. The problem is I want a category header to be output each time the category changes. I have a numerical field in the database for the category.

I'm pretty dumb, so please be specific with any comments

i.e.

Category 1
cat1 info from db
more cat1 info from db
Category 2
cat2 info from db
more cat2 info from db

My work so far is as follows:

Code:
$query = 'SELECT * FROM `opportunities` WHERE `category` = 1 ORDER BY `dateadded` DESC;';
	$result = mysql_query($query);
	while($row = mysql_fetch_assoc($result)) {
	echo "	
<tr>
	<td>$program</td>
	<td>$description</td>
	<td>$funder</td>
	<td>$duedate</td>
	<td>$nofavalue$guidevalue$applicationvalue</td>
</tr>\n";
	}	
?>

I was thinking that the headers would look like this:
Code:
<tr>
	<td colspan="5">$category<a name="$category"></a></td>
</tr>

Thanks
 
Where do you get your $category from? I mean, the name of the category?
 
$query = 'SELECT * FROM `opportunities` WHERE ORDER BY category_id asc,`dateadded` DESC;';

$TempCategory="";

$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)) {
if($category!=$TempCategory)
{
echo "<tr><td colspan='5'>$category<a name='$category'></a></td></tr>";
}



echo "
<tr>
<td>$program</td>
<td>$description</td>
<td>$funder</td>
<td>$duedate</td>
<td>$nofavalue$guidevalue$applicationvalue</td>
</tr>\n";
}
?>


Known is handfull, Unknown is worldfull
 
Whoops sorry I didn't specify the variables, they are mainly just pulling from MySQL

Code:
	$program = $row['program'];
	$description = $row['description'];
	$funder = $row['funder'];
	$duedate = $row['duedate'];
	$nofa = $row['nofa'];
	$guide = $row['guide'];
	$application = $row['application'];
        $category = $row['category'];
 
Getting very close, the only problem is the result it spits out is like this

Category 1 (Header)
cat 1 entry 1

Category 1 (Header)
cat 1 entry 2

Category 1 (Header)
cat 1 entry 3

Category 2 (Header)
cat 2 entry 1

Meaning that the header is repeated for each entry with a category = $category

here's the code that has brought me to where I am:

Code:
$query = 'SELECT * FROM `opportunities` ORDER BY `category` ASC, `dateadded` DESC;';
	$TempCategory="";
	$result = mysql_query($query);
	while($row = mysql_fetch_assoc($result)) {
	$program = $row['program'];
	$description = $row['description'];
	$funder = $row['funder'];
	$duedate = $row['duedate'];
	$nofa = $row['nofa'];
	$guide = $row['guide'];
	$application = $row['application'];
	$category = $row['category'];
if($category!=$TempCategory)
       {
echo "<tr><td colspan='5' class='h2'>$category<a name='$category'></a></td></tr>";
       }
	   
	   
	echo "	
<tr>
	<td>$program</td>
	<td>$description</td>
	<td>$funder</td>
	<td>$duedate</td>
	<td>$nofavalue$guidevalue$applicationvalue</td>
</tr>\n";
	}	
?>
Thanks for all your held
 
You need to set $tempcategory=$category when $category changes:

if ($tempcategory != $category)
{
echo "header...";
$tempcategory = $category;
}

Otherwise, the if clause is always true.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top