|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
pulling data from multiple categories at the same time
Hi,
I've put together two functions that pull a list of categories from a db which are hyperlinked to all of the links that correspond to that particular category. And two almost identical functions that display the links. What I would like to do is to have the category names echoed out with all of it's respective links automatically under it as opposed to clicking the category to see them. They are called like so... PHP Code:
Here is the code to display the links PHP Code:
The problem is having to pass the 'catid' via the hyperlink. I can't think of a way to have them automatically displayed when the page is loaded. Anyone have any suggestions? Thanks ![]() |
|
#2
|
||||
|
||||
|
I've done something similar with the following logic (pseudo-code):
Code:
SELECT c.cat_id, c.cat_name, l.link, l.name FROM link_cat as c, links as l WHERE l.cat_id=c.cat_id ORDER BY c.cat_name ASC, l.name ASC
$cat="";
while($row=mysql_fetch_row()){
if($row["cat_name"]!=$cat){
$cat=$row["cat_name"];
print $cat . "<br>";
}
print $row["link"]; //formatted to your liking
}
Basically, you're joining the cat and the link tables and ordering first by category. Then as you loop through the results, you check a variable to see if it equals the current category name. If not, a new category has been reached and you should print it. Else just print the link. I think that'll accomplish what you were wanting, with much less code. Hope I've understood you correctly. |
|
#3
|
|||
|
|||
|
It seems so painfully obvious now. I tweaked it a bit to fit my needs and it's perfect.
Thank you ![]() Rob |
|
#4
|
|||
|
|||
|
I am using this code and it is working okay until a point. Odd things are happening. I currently have three categories and each one has a number of links associated with them. I have added a form to add categories, as well as the links that corresepond to them. This works fine. They show up like so...
CATEGORY_1 link link CATEGORY_2 link link link CATEGORY_3 link link link link link The weird thing is when I add more than 2 links to the first category, or 3 links to the second category, the category name and the link I add are added to the end of the list. Like so... CATEGORY_1 link link CATEGORY_2 link link link CATEGORY_3 link link link link link CATEGORY_1 <-- category 1 already exists! link <-- new link I just added If I were to add a fourth link under CATEGORY_2 it would repeat itself at the bottom as well. The last category doesn't seem to be affected. The two tables involved are set up like so... LINK_CAT catid cat_name LINKS linkid catid name link_url Code below (provided by dhouston)... PHP Code:
Thanks for any help. |
|
#5
|
||||
|
||||
|
It's the lack of an ORDER BY clause in your query. You have to order by the category in order for this to work; else mysql spits out results in database order, with the most recent entries appearing last. The category names show up again because by the time you get to the end of the list, $cat no longer equals CATEGORY_1 (it equals CATEGORY_3) and so it reprints as if it's reached a new category. Check out the ORDER BY clause in my original posting and see if that solves the problem for you.
|
|
#6
|
|||
|
|||
|
Hey dhouston,
Got it. Thanks for your time. Appreciate it. Rob |
![]() |
| Viewing: Dev Articles Community Forums > Programming > General Programming Help > pulling data from multiple categories at the same time |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|