Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
11-01-2006, 10:26 AM #1
- Join Date
- Nov 2006
- Thanked 0 Times in 0 Posts
A small problem with xml and php.
Hi. I'm having a small challenge.
Based om this simplified xml file I want to present only two articles per. categori
The xml is made so every new article comes on top, so the newest articles are always on the top.
So what I want to do is to say to php that you will show every article but only two for each category.
Does anyone have a simple solution to this issue?
Kind regards, Cato
11-01-2006, 12:11 PM #2
- Join Date
- Oct 2006
- United Kingdom
- Thanked 30 Times in 29 Posts
I am not an expert on XML, but I think that you can manipulate the output generated by selecting through SQL statements.
I am assuming that you have two tables (1) Categories, and (2) Articles. and you have a one-many relationship.
First, you need to select all the categories from the categories table. Again, I am assuming that you have a column called cat_name which represents categories in your table.
than, you need to loop though the results, and issue another sql statement to select the last two articles based on the current category.
$cat_results = mysql_query( "SELECT cat_name FROM CATEGORIES" );
while( $rows = mysql_fetch_array( $cat_results ) )
$article_results = mysql_query( "SELECT artcile_name FROM Articles where cat_name = " . $rows . " ORDER BY article_ID DESC" );
// create another while loop to obtain results
// for the two first articles, and then break out of the loop...
// don't forget...you need to build your xml file while looping.
} # ends while loop
Note that I have not tested the above example, and I am sure that someone else will have another solution to this problem. However, the algorithms should yield the results you explained above.
Thanks and good luck