How to count words in rows - php/mysql

Discussion in 'PHP' started by FokeBox, Aug 9, 2011.

  1. #1
    For example I have table "items" with row "goods" where I have some number of word "book" ... so how can I count it using php?
     
    Solved! View solution.
    FokeBox, Aug 9, 2011 IP
  2. jazzcho

    jazzcho Peon

    Messages:
    326
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    jazzcho, Aug 9, 2011 IP
  3. Thorlax402

    Thorlax402 Member

    Messages:
    194
    Likes Received:
    2
    Best Answers:
    5
    Trophy Points:
    40
    #3
    Have you ever used MySQL with PHP before? I just want to make sure you know that you need to use that function jazzcho provided along with a queryID. Something along the lines of this:

    
    $result = mysql_query("SELECT * FROM items");
    $number_of_rows = mysql_num_rows($result);
    
    PHP:
    There is another way of doing it by selecting COUNT(*) from your mysql table instead of getting all the records, but I feel this is easier to wrap your head around. I am a little confused about your table though. Are you simply trying to figure out how many entries there are in the table "items"?
     
    Thorlax402, Aug 9, 2011 IP
  4. elixiusx

    elixiusx Peon

    Messages:
    65
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #4
    <?
    $cnx = mysql_connect('localhost', 'root', 'mypass');
    mysql_select_db('mydb');

    $word = 'book';
    $query = mysql_query("SELECT goods FROM items");

    while( $fetch = mysql_fetch_array( $query ) ):

    $matrix = explode(' ', $fetch['goods']);

    $count = 0;
    foreach( $matrix as $w ):
    if( $w==$word )
    $count++;
    endforeach;

    print $count.'<br />';
    endwhile;
    ?>
     
    elixiusx, Aug 10, 2011 IP
  5. elixiusx

    elixiusx Peon

    Messages:
    65
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #5
    You should split the row by " " (space) and compare all words by the word that you want to count... if the word == yourword so count ++...

    Excuse my english... buena suerte! :)
     
    elixiusx, Aug 10, 2011 IP
  6. FokeBox

    FokeBox Active Member

    Messages:
    432
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    65
    #6
    Well I need the result like this
    Book - (amount)
    in your case it was like
    1
    0
    0
    1
    0
    1
    0
     
    FokeBox, Aug 10, 2011 IP
  7. JohnnySchultz

    JohnnySchultz Peon

    Messages:
    277
    Likes Received:
    4
    Best Answers:
    7
    Trophy Points:
    0
    #7
    this will find the exact word book in each row.. it will disregard the words booking,booker,etc..
    
    // ... the query statement here...
    
    $results = array();
    while($row = mysql_fetch_assoc($result))
    {
       $word_count = preg_match_all("/\bbook\b/",$row['goods']);
       $row['word_count'] = $word_count?$word_count:0;
       $results[] = $row;
    }
    
    
    
    PHP:
    if you want to find every instance of the work "book", just remove the \b at the start and end of it..
     
    JohnnySchultz, Aug 10, 2011 IP
  8. FokeBox

    FokeBox Active Member

    Messages:
    432
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    65
    #8
    It gives me the error
    Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/public_html/count2.php on line 10
     
    FokeBox, Aug 10, 2011 IP
  9. #9
    
    <?
    $cnx = mysql_connect('localhost', 'root', 'mypass');
    mysql_select_db('mydb');
    
    $words = array('book', 'dog', 'cat', 'computer', 'metal');
    $matrix_word = array();
    
    $query = mysql_query("SELECT goods FROM items");
    
    while( $fetch = mysql_fetch_array( $query ) ):
    			
    		$matrix = explode(' ', $fetch['goods']);
    		
    		foreach( $matrix as $w)
    			if( in_array($w, $words) )
    				$matrix_word[$w]++;
    				
    endwhile;
    
    foreach( $words as $word )
    	print $word.' - '.(int)$matrix_word[$word].' <br />';
    ?>
    
    PHP:
    This return something like:

    book - 8
    dog - 2
    cat - 0
    computer - 0
    metal - 1

    What do you think about it?
     
    elixiusx, Aug 10, 2011 IP
  10. FokeBox

    FokeBox Active Member

    Messages:
    432
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    65
    #10
    Cool! Thx ... this that I was looking for )))
     
    FokeBox, Aug 10, 2011 IP
  11. FokeBox

    FokeBox Active Member

    Messages:
    432
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    65
    #11
    How to give a link to output words in this case? e.g. link to word book?
     
    FokeBox, Aug 10, 2011 IP
  12. ausrixy

    ausrixy Peon

    Messages:
    46
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #12
    just add a form with an input field, where they can type the word you want to search for.
     
    ausrixy, Aug 10, 2011 IP
  13. FokeBox

    FokeBox Active Member

    Messages:
    432
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    65
    #13
    There is another way to count same words in a row, but I don't know how to sort results by quantity, would you help me?

    $sql = "SELECT items, count(*) cs FROM users GROUP BY items LIMIT 10";
    $result = mysql_query($sql) or die(mysql_error());
    while ($a = mysql_fetch_assoc($result))
    {
    echo $a["items"]." - ". $a["cs"]."  -  "; 
    }
    PHP:
     
    FokeBox, Aug 10, 2011 IP
  14. JohnnySchultz

    JohnnySchultz Peon

    Messages:
    277
    Likes Received:
    4
    Best Answers:
    7
    Trophy Points:
    0
    #14

    you should replace $results with the variable of your mysql_query return..

    
    
    $result = mysql_query("SELECT * FROM `items`");
    
    $results = array();
    while($row = mysql_fetch_assoc($result))
    {
       $word_count = preg_match_all("/\bbook\b/",$row['goods']);
       $row['word_count'] = $word_count?$word_count:0;
       $results[] = $row;
    }
    
    
    PHP:
     
    JohnnySchultz, Aug 10, 2011 IP
  15. FokeBox

    FokeBox Active Member

    Messages:
    432
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    65
    #15
    Thanks! But solution has been found already ... your example I will have just in case ))) ... Would you help me with my previous question?
     
    FokeBox, Aug 10, 2011 IP
  16. FokeBox

    FokeBox Active Member

    Messages:
    432
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    65
    #16
    solution found )))
    $sql = "SELECT country, count(*) AS cs FROM users GROUP BY country ORDER BY cs DESC";
    PHP:
     
    FokeBox, Aug 11, 2011 IP