Jump to content

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; 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.