Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Best syntax to use for counting how many times a certain 2 letter word appears....
Old 02-22-2009, 11:00 AM Best syntax to use for counting how many times a certain 2 letter word appears....
Average Talker

Posts: 26
Trades: 0
I have a php script which is pulling some data out of a database and putting it into variables. What I want to do now is count how many times a particular set of values is coming up in the variables that I have stored... e.g.

var1 = NU
var2 = NU
var3 = MI
var4 = NU
var5 = MI

In example above i'd like the php to tell me that NU is there 3 times.. etc etc

Hope this makes it clear what I need.

I looked at the count command but that seems to be more of a math logic question?

Anyone know which command i need to use?
PauloMillerz is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 02-22-2009, 12:22 PM Re: Best syntax to use for counting how many times a certain 2 letter word appears...
Truly's Avatar
Ultra Talker

Posts: 322
Trades: 0
Do something like this

<?php $result = mysql_query("SELECT * FROM table WHERE stuff='123ABC'") or die(mysql_error());
echo mysql_num_rows($result); ?>

In this case it will look in the database and pull up every case where stuff=123ABC and then it will echo the number of times that occured. The result will be a number.

To make it look for some characters within a string which it sounds like you are trying to do use this:

SELECT * FROM table WHERE stuff LIKE '%123%'

In this case ABC123ABC would be found whereas otherwise it would be skipped.

Hope that helps.
__________________
DVD Movie Release Database:
Please login or register to view this content. Registration is FREE
Truly is offline
Reply With Quote
View Public Profile
 
Old 02-22-2009, 01:28 PM Re: Best syntax to use for counting how many times a certain 2 letter word appears...
Average Talker

Posts: 26
Trades: 0
Thanks just gunna try it in my script.. following the logic of it, seems to be doing what i need.. so fingers crossed.. will post the answer lol
PauloMillerz is offline
Reply With Quote
View Public Profile
 
Old 02-22-2009, 01:37 PM Re: Best syntax to use for counting how many times a certain 2 letter word appears...
Average Talker

Posts: 26
Trades: 0
ah ur theory was good... but the data i want to analyse is stored in a variable...
PauloMillerz is offline
Reply With Quote
View Public Profile
 
Old 02-28-2009, 08:32 AM Re: Best syntax to use for counting how many times a certain 2 letter word appears...
Average Talker

Posts: 26
Trades: 0
Any1 else got ideas? Endless searching not yet found me an answer...
PauloMillerz is offline
Reply With Quote
View Public Profile
 
Old 02-28-2009, 09:54 AM Re: Best syntax to use for counting how many times a certain 2 letter word appears...
Average Talker

Posts: 26
Trades: 0
Quote:
<?php
$text = file_get_contents('myfile.txt');

$text = explode(' ',$text);

foreach ($text as $key => $word ) {
if (strlen($word) >1) {
$words[] = $word;
}
}

$count = array_count_values($words);

array_multisort($count,SORT_DESC);

$i = 1;
foreach ($count as $key => $value) {
if( $i <= 20 ) {

$countresultvalue = $value;
$countresultkey = $key;
$i++;
} else {
exit();
}
}

echo "$countresultvalue $countresultkey";
?>
ok so i got round my problem by getting the varaibles printed to a txt file and the above is reading the file and counting the number of times each word is appearing in the file.. which is what i want it to count... however, the next thing i want to do with this info is check to see if any of them are on there more than 3 times... Problem i am getting is i can't put it into an "if" question because the variable is being over-ridden, and just ending up with the last one... is there a way I can get this scrip to allow me to check if a certain word is showing up more than 3 times?? Help...
PauloMillerz is offline
Reply With Quote
View Public Profile
 
Old 02-28-2009, 10:25 AM Re: Best syntax to use for counting how many times a certain 2 letter word appears...
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
How about this?

PHP Code:

// get text from somewhere and store into $text

$words explode(' '$text);

$countedWords = array();

foreach (
$words as $word) {
   if (
array_key_exists($word$countedWords)) {
      
$countedWords[$word]++;
   } else {
      
$countedWords[$word] = 1;
   }
}

/* 
 * $countedWords now holds all words once with
 * a number saying how many times it occured, as in
 * $countedWords = array(
 *    'NU' => 3,
 *    'MI' => 2
 * );
 *
 */ 
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 02-28-2009, 10:37 AM Re: Best syntax to use for counting how many times a certain 2 letter word appears...
Average Talker

Posts: 26
Trades: 0
That sounds like it to me... now just gotta figure out how to make it do a logical test on it..

or im being really thick.. (my brain is having a blonde day 2day)

Last edited by PauloMillerz; 02-28-2009 at 10:40 AM..
PauloMillerz is offline
Reply With Quote
View Public Profile
 
Old 02-28-2009, 10:52 AM Re: Best syntax to use for counting how many times a certain 2 letter word appears...
Average Talker

Posts: 26
Trades: 0
Quote:
<?php
$text = file_get_contents('file.txt');

$words = explode(' ', $text);

$countedWords = array();

foreach ($words as $word) {
if (array_key_exists($word, $countedWords)) {
$countedWords[$word]++;
} else {
$countedWords[$word] = 1;
}
}

echo "$countedWords";
?>
is this right?
PauloMillerz is offline
Reply With Quote
View Public Profile
 
Old 02-28-2009, 04:53 PM Re: Best syntax to use for counting how many times a certain 2 letter word appears...
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
No, you can't echo an array that way, it would just print the word "Array". $countedWords holds all the words as keys and the number of times they occured as values, as shown above. To print them all out, try
PHP Code:
foreach ($countedWords as $word => $count) {
   echo 
"The word <b>$word</b> occured <b>$count</b> times.<br />";

lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 03-01-2009, 01:16 PM Re: Best syntax to use for counting how many times a certain 2 letter word appears...
Average Talker

Posts: 26
Trades: 0
would this work for putting them into variables so i can do an if on them to check if any of them go over 3? or am i being thick again? As u see organising arrays is not my strength.
PauloMillerz is offline
Reply With Quote
View Public Profile
 
Old 03-01-2009, 02:14 PM Re: Best syntax to use for counting how many times a certain 2 letter word appears...
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
I still don't really know what you want with all this, so I'll give you two small examples and we'll see what you think.

PHP Code:
// Example 1
// Print the words which occured 3 times or more
foreach ($countedWords as $word => $count) {
   if (
$count >= 3) {
      echo 
"Oh my god, the word $word were used $count times! ;)";
   }
}

// Example 2
// Remove words that didn't occur 3 times or more
foreach ($countedWords as $word => $count) {
   if (
$count 3) {
      unset(
$countedWords[$word]);
   }
}
/* Now, all the words that are left in the array
 * has occured 3 times or more, as in
 * 
 * array(
 *    'word1' => x,
 *    'word2' => y,
 *    'word3' => z
 * )
 * 
 * Where both x, y and z is 3 or more
 */

// Example 3
// Sort all words according to how many times they occured
$sorted = array();
foreach (
$countedWords as $word => $count) {
   if (
is_array($sorted[$count])) {
      
$sorted[$count][] = $word;
   } else {
      
$sorted[count] = array($word);
   }
}
/* Now $sorted holds all the words, grouped by the number of
 * times they occured, as in
 * 
 * array(
 *    1 => array('word1', 'word6'), // words that occured 1 time
 *    2 => array('word4', 'word5'), // words that occured 2 times
 *    3 => array('word2'),           // words that occured 3 times
 *    4 => array('word3', 'word8'), // words that occured 4 times
 *    5 => array('word7')            // words that occured 5 times
 * )
 * 
 */ 
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 03-02-2009, 07:13 AM Re: Best syntax to use for counting how many times a certain 2 letter word appears...
Average Talker

Posts: 26
Trades: 0
Thank you for your help so far. I'll give you more detail on what I am trying to do.

I'm building a fantasy football website, and i need to include in the registration process a check to see if they have selected players from any given team more than 3 times. So if they have 3 players from AV it passes, but if they have 4 players from AV it fails.

The data is stored in a database and so far i have extracted the data into variables (for another part of the site). I want to take those variables (which contain the clubs for each player) and check to see if there are more than 3 instances of each club.

Hope this explains and helps you understand. If not I will try again?
PauloMillerz is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Best syntax to use for counting how many times a certain 2 letter word appears....
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 1.52545 seconds with 12 queries