Welcome Guest [Log In] [Register]
Add Reply
[PHP] counting with PHP
Topic Started: Mar 3 2009, 02:13 PM (300 Views)
PauloMillerz
Member Avatar

Ok i had no luck yet on this one in the other forums so trying here.... see how our brainy guys here can do!! ;O

Quote:
 
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?


Quote:
 
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?


Do not double post -Alex
Edited by Alex, Mar 3 2009, 04:23 PM.
If you like... RATE IT!! Add to my reputation to the left?
Quote:
 
Bidet to you sir!

Posted Image
Offline Profile Quote Post Goto Top
 
Dillon
Member Avatar

What type of database are you using? This is how I would do it in MySQL,

This is just a quick thing I threw together...

Quote:
 
//This is the MySQL Information
$username="database";
$password="password";
$database="database_name";

//This tells to connect to the MySQL Database, via the provided information above
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

//The query is to select everything from the whatever table you want
$query = "SELECT * FROM WHATEVER TABLE YOU WANT";
$result = mysql_query($query);
mysql_close();

$int == "WHATEVER VARIABLE YOU WANT IT TO FIND";

while ($num = mysql_fetch_object($result)) {
$variable = $num->variable;

if ($variable == $int){

echo "This is the variable you want: $int</br>";

}
}


Hope this helps!

-Dillon
Edited by Dillon, Mar 3 2009, 06:08 PM.
<?

$text == "Hello World";

echo $text;

?>
Offline Profile Quote Post Goto Top
 
Darksorrow131

As Dillon said, if the "variables" you talked about come from a database, just use SELECT COUNT(*) FROM TABLE WHERE var = NU and it will return 3.
If they are simply in an array, write a loop through the array and increment a counter each time you see 'NU'.
(let loop () (loop))
((lambda (x) (x x)) (lambda (x) (x x)))
(let ((k #f)) (call/cc (lambda (cc) (set! k cc)) (k))
((call/cc call/cc) (call/cc call/cc))

-- Infinite loops are awesome! --
Tell me if you have other awesome infinite loops!
Offline Profile Quote Post Goto Top
 
PauloMillerz
Member Avatar

No the variables are actual php $ variables.. which has come from a database, yes mysql.. reason they are in varaibles is because I have got them printed elsewhere on the page. I've got 15 team names, e.g. AV, MU, ST etc etc.. I'm trying to avoid using the database to store this info because it means setting it up in various places.

Basically at the moment i have the team in a table and all the player data in another one. At the moment the script here is getting the player names from the one table and putting that into the query for the player data to get the club they play for, and then putting each one in a $variable...

Edit: Bah I'll put it into the table.. at least i can do stuff with the data then a lot easier..
Edited by PauloMillerz, Mar 6 2009, 06:49 AM.
If you like... RATE IT!! Add to my reputation to the left?
Quote:
 
Bidet to you sir!

Posted Image
Offline Profile Quote Post Goto Top
 
1 user reading this topic (1 Guest and 0 Anonymous)
« Previous Topic · Web-Based Programming Support · Next Topic »
Add Reply

Banner and Logo by TheKeith