[PHP] counting values in mysql_fetch_array()?

Discussion in 'Scripts, 3rd Party Apps, and Programming' started by bonzo meier, Feb 18, 2008.

  1. bonzo meier

    bonzo meier Member

    Messages:
    47
    Likes Received:
    0
    Trophy Points:
    6
    hi,

    i have a question about a simple php-sql routine: I select some content items from a database and display their data, this works fine.

    Code:
    $contentResult = @ mysql_query($contentQuery, $connection);
    
     while ($contentitems = @ mysql_fetch_array($contentResult)) 
    {
    print $contentitems['ID'];
    }
    
    
    now my question: is it possible to find out how many of the $contentitems contain the $ID=2 before the while-loop is performed?

    thanks for help,
    peace, bonzo
     
  2. Adam01

    Adam01 New Member

    Messages:
    114
    Likes Received:
    0
    Trophy Points:
    0
    You could also do it during the loop.

    PHP:
    $contentResult = @ mysql_query($contentQuery$connection);

     while (
    $contentitems = @ mysql_fetch_array($contentResult)) 
    {

    if(
    $contentitems['ID'] == '2'){
    ECHO 
    $contentitems['values...'];
    }
    }

    and dont you meen "echo" instead of print?
     
    Last edited: Feb 18, 2008
  3. sunils

    sunils New Member

    Messages:
    2,266
    Likes Received:
    0
    Trophy Points:
    0
    I have made a small inclusion in adam's post
    PHP:
    $contentResult $mysql_query($contentQuery$connection); 
    $count=0;
    while (
    $contentitems $mysql_fetch_array($contentResult))  

              if(
    $contentitems['ID'] == '2')
              { 
                    
    $count++;
              } 

    print 
    "There were ".$count." items";
     
  4. Adam01

    Adam01 New Member

    Messages:
    114
    Likes Received:
    0
    Trophy Points:
    0
    Ohh yeah, woops, misread.
     
  5. bonzo meier

    bonzo meier Member

    Messages:
    47
    Likes Received:
    0
    Trophy Points:
    6
    yes, these are ways of handling the problem i have already thought about. the point is, the query is selecting all different content items, some are articles, forms, and some are (id=2) newsfeeds.

    now i want the script to check if there are more than one newsfeeds to one topic (which is one page), and if there are, to combine them in one box of headlines.

    if i do the counting inside the while-loop, and say the first and the third found item are newsfeeds, the script will not be able to merge the two feeds. the script is now like

    Code:
    [COLOR=#000000][COLOR=#007700]while ([/COLOR][COLOR=#0000BB]$contentitems [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000BB]@mysql_fetch_array[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]$contentResult[/COLOR][COLOR=#007700]))   
    {  
              [/COLOR][/COLOR][COLOR=#000000][COLOR=#007700]if([/COLOR][COLOR=#0000BB]$contentitems[/COLOR][COLOR=#007700][[/COLOR][COLOR=#DD0000]'ID'[/COLOR][COLOR=#007700]] == [/COLOR][COLOR=#DD0000]'1'[/COLOR][COLOR=#007700]) 
              {  
                    [/COLOR][COLOR=#0000BB]make_article()[/COLOR][COLOR=#007700]; 
              } [/COLOR][/COLOR]
    [COLOR=#000000][COLOR=#007700]
              if([/COLOR][COLOR=#0000BB]$contentitems[/COLOR][COLOR=#007700][[/COLOR][COLOR=#DD0000]'ID'[/COLOR][COLOR=#007700]] == [/COLOR][COLOR=#DD0000]'2'[/COLOR][COLOR=#007700]) 
              {  
                    [/COLOR][COLOR=#0000BB]make_newsbox()[/COLOR][COLOR=#007700]; 
              }
    //... and so on... 
    }  [/COLOR][/COLOR]
    
    so now if the script comes across the first newsfeed-item, it makes a newsbox and when it comes to the next, it makes another one.

    while i am writing this, a possible solution is getting clearer for me: what if i did the counting of newsfeeds inside the while and put the newsbox-generating function right after it... hmmmm... if this works, i give big props to you guys who helped me on the way.

    i´ll try it, if it works, i´ll post the solution.
    thanks to all of you! peace, bonzo.
    Edit:
    ok, this is how it works:

    Code:
    [COLOR=#000000][COLOR=#007700]while ([/COLOR][COLOR=#0000bb]$contentitems [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000bb]@mysql_fetch_array[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000bb]$contentResult[/COLOR][COLOR=#007700]))   
    {[/COLOR][/COLOR]
         if ($contentitems['ID'] == 1)
         {
         make_article();
         }
         
         if ($contentitems['ID'] == 2)
         {
         $feeds .= $contentitems['itemID'].';'; //first semi-colon is used as a string separator later, when I explode the string into a new array
         }
    }
    if(!empty($feeds)
    {
    $max_headlines = 5; //these are required in my make_newsbox() function
    make_newsbox($feeds, $max_headlines);
    }
     
    
    another good thing is that now the newsbox is always at the bottom of the content area - if I wanted it somewhere else inside the content area I had another problem, but like this I´m happy :)

    peace, bonzo
     
    Last edited: Feb 19, 2008

Share This Page