Hi Guys,
Sorry I have another question for you pro's.
Basically I am using this code to create a basic sidebar:
So what this does is basically echo out all the categories of all the posts in my database.Code:<?php $result = mysql_query("SELECT * FROM tbprofo"); echo "<ul>"; while($row=mysql_fetch_array($result)) { echo "<li>".$row['category']."</li>"; } echo "</ul>"; ?>
The only problem is that some posts use the same categories (i.e. I have say 5 posts all using category "News") - this means that my code echo's News 5 times.
For the life of me I cant figure out how to only display a category once
Any ideas?
Thanks very much, this forum is so helpful and all these members are great, I really do appreciate all the help that is given on here.
it depends a little on your database table structure, either you couldor you could doCode:select distinct category from tbprofoto get a better list to output.Code:select * from tbprofo group by category
if you don't use the sql query for more than this list, I'd go with the first one.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall
you could also use a while loop inside of that while loop. The first while loop will return the catagories, the second have it return the board that belong in that catagory. so it would be like:
Code:<?php
// return each catagory by oreder number
$getCatagory="Select * from forumCatagory Order by returnOrder DESC";
$runCatagoryQuerry=mysql_query($getCatagory)or die("Sorry The Forum is not working corectly at the moment. Please e-mail support about this : rpgsupport@dynamicfantasies.com <br />" . mysql_error());
while($returnCatagory=mysql_fetch_array($runCatagoryQuerry)){
?>
<tr>
<td class="tableHead">
<?php
echo $returnCatagory['catagory'];
?>
</td>
</tr>
<?php
//return each board by its catagory name
$getBoard="Select * from forumBoard where catagory='$returnCatagory[0]'"; // $returnCatagory['catagory'] would not work, had to use array #
$runBoardQuerry=mysql_query($getBoard)or die("Sorry The Forum is not working corectly at the moment. Please e-mail support about this : rpgsupport@dynamicfantasies.com <br />" . mysql_error());
while($returnBoard=mysql_fetch_array($runBoardQuerry)){
?>
<tr>
<td class="contentOther">
<a href="show_threads.php?boardName=<?php echo $returnBoard['boardName']; ?>&catagory=<?php echo $returnCatagory[0]; ?>"> <?php
echo $returnBoard['boardName'];
?>
</a>
</td>
</tr>
<tr>
<td class="contentOther">
<?php
echo $returnBoard['description'];
?>
<hr width="400px" size="2" />
</td>
</tr>
<?php
//end catagory loop and board loop
}
}
?>
thats how i did it in my forum at anyrate and it works withought error
Thanks guys
Orjan - select distinct works great - thanks very much matey.... zeroradius - you may be able to clean up your code with this.
cheers guys
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks