Jump to content

I can't figure out how to handle this ?


hopbop

Recommended Posts

So I'm sitting at my desk scratching my head on how to do a task where I pull information form a database (that i know how to do) but then i need to see if two variables equal a string and if they do count out how many times that two variables equal there two string checks and out put the counted number ?

 

this is what I have thought of but dose not  to work because I cant figure out a way to code it?

 

$dbPull = mysql_fetch_array($result)

 

if( $dbPull['Foo'] == 'string1' && $bdPull['Bar'] == 'string2')

 

    this is were I get lost I'm looking for  (how many times in the DB dose String2 show up in the array were the above, IF statement is true )

 

 

any ideas would be most appreciated

Link to comment
https://forums.phpfreaks.com/topic/262875-i-cant-figure-out-how-to-handle-this/
Share on other sites

I'm not sure if i understand what you need... you need to count how much times these values repeat on the array fetched from the database, right?

 

you can use:

array_search('foo', $yourArray);

 

http://php.net/array_search

 

 

I gave that a try but it echoed out 53 but it should of sad 2 ?

ill try to exsplan it diffrently

 

what i am trying to do is display a count for itemA in the database were if itemB = foo and itemA = bar ...

 

so if say db row one  itemB is  boo and  itemA is bar  and  db row two  itemB is foo and itemA is bar the over all count is 1

Just query it. Depending on how your table is structured, I'm thinking you need to run a sub-select.

 

Example:

SELECT COUNT(*) AS col1_count, col2_count

FROM `table` T1,

    (SELECT COUNT(*) AS col2_count

    FROM `table`

    WHERE `col2` = 'itemB') T2

WHERE `col1` = 'itemA';

thank you, you are gentlemen and scholars in my book I have now got it working and here is the code for record

 


include '../config.php';	
$query = "SELECT COUNT(*) AS varb FROM clients WHERE col1='foo' and col2 ='bar'";
$result = mysql_query($query);
while($data=mysql_fetch_array($result)){
$count = $data['varb'];
}
echo $count; 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.