SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Evangelist perpetual_dream's Avatar
    Join Date
    Nov 2005
    Location
    Jerusalem
    Posts
    542
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Filtering mysql fetched result

    Hello,

    I have a table which resembles the following structure: id, category_id

    I have a query that returns all the ids/categories. I would like to limit the number of displayed ids per each category depending on the category_id.How can I do this while looping? Can this be done in the SQL statement itself?

    Code PHP:
    $q="select id.*,category_id from test order by category_id ASC";
    $r=mysql_fetch_array($q);
     
    while($row=mysql_fetch_array){
     
    if (category_id==1) {
     
    //display 4 items 
     
    }
     
    if (Category_id==3) {
     
    // display 2 items
     
    }
     
    }

    Thanks
    Tanzeelnet - Best arabic downloadsite
    Linux Hosting |Free Downloads |

  2. #2
    SitePoint Zealot Kayarc's Avatar
    Join Date
    Sep 2009
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    something like:

    PHP Code:
    <?php
    $q
    ="select id.*,category_id from test order by category_id ASC";
    $r=mysql_fetch_array($q);

    $count_1 0;
    $count_3 0;
     
    while(
    $row=mysql_fetch_array){
        if (
    $row['category_id']==AND $count_1 4) {
            
    //display item
            
    $count_1++;
        }
        if (
    $row['category_id']==AND $count_3 2) {
            
    //display item
            
    $count_3++;
        }
    }
    ?>
    maybe?
    Last edited by Kayarc; Oct 1, 2009 at 16:39. Reason: fixed
    Phoenix Arizona Web Design | info *at* kayarc.com | 602.633.2676

  3. #3
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Use SQL to limit the results returned.
    http://php.about.com/od/mysqlcommands/g/Limit_sql.htm
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  4. #4
    SitePoint Zealot Kayarc's Avatar
    Join Date
    Sep 2009
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    Use SQL to limit the results returned.
    http://php.about.com/od/mysqlcommands/g/Limit_sql.htm
    That would not work for what he is trying to do as I understand it
    Phoenix Arizona Web Design | info *at* kayarc.com | 602.633.2676


Bookmarks

Posting Permissions

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