View Full Version : A small problem with xml and php.

11-01-2006, 11:26 AM
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, 01:11 PM
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[0] . " 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 :thumbsup: