Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    ca2
    ca2 is offline
    New to the CF scene
    Join Date
    Nov 2006
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question 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

    <articles>
    <article>
    <category></category>
    <title></title>
    <text></text>
    </article>
    </articles>

    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

  • #2
    ess
    ess is offline
    Regular Coder
    Join Date
    Oct 2006
    Location
    United Kingdom
    Posts
    866
    Thanks
    7
    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.

    PHP Code:
    <?php
    $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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •