[PHP] count clicks to count most important news
I have a website that posts the most important news according to the number of clicks to that news the question is : what is the best way to prevent multiple clicks from the same visitor?
Re: [PHP] count clicks to count most important news
On Jan 1, 2012, at 11:26 AM, muad shibani wrote: I have a website that posts the most important news according to the number of clicks to that news the question is : what is the best way to prevent multiple clicks from the same visitor? Not a fool-proof method, but use Javascript on the client-side to stop users' from continuous clicking. Then create a token and verify the click on the server-side before considering the click as being acceptable. Cheers, tedd _ t...@sperling.com http://sperling.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] count clicks to count most important news
On Sun, 2012-01-01 at 11:49 -0500, Tedd Sperling wrote: On Jan 1, 2012, at 11:26 AM, muad shibani wrote: I have a website that posts the most important news according to the number of clicks to that news the question is : what is the best way to prevent multiple clicks from the same visitor? Not a fool-proof method, but use Javascript on the client-side to stop users' from continuous clicking. Then create a token and verify the click on the server-side before considering the click as being acceptable. Cheers, tedd _ t...@sperling.com http://sperling.com There are still problems with this, GET data (which essentially only what a clicked link would produce if you leave Javascript out the equation - you can't rely on Javascript) shouldn't be used to trigger a change on the server (in your case a counter increment) I did something similar for a competition site a few years ago, and stupidly didn't think about this at the time. Someone ended up gaming the system by including an image with the clicked-through URL in the src attribute, and put that on their MySpace profile page, which had more than a few visitors. Each of those visitors browser attempted to grab that image which registered a click, and because of the number of unique visitors, the clicks were registered as genuine. I'd recommend using POST data for this reason, as it's a lot more difficult for people to game. -- Thanks, Ash http://www.ashleysheridan.co.uk
Re: [PHP] count clicks to count most important news
On 01-01-2012 20:08, Ashley Sheridan wrote: On Sun, 2012-01-01 at 11:49 -0500, Tedd Sperling wrote: On Jan 1, 2012, at 11:26 AM, muad shibani wrote: I have a website that posts the most important news according to the number of clicks to that news the question is : what is the best way to prevent multiple clicks from the same visitor? Not a fool-proof method, but use Javascript on the client-side to stop users' from continuous clicking. Then create a token and verify the click on the server-side before considering the click as being acceptable. Cheers, tedd _ t...@sperling.com http://sperling.com There are still problems with this, GET data (which essentially only what a clicked link would produce if you leave Javascript out the equation - you can't rely on Javascript) shouldn't be used to trigger a change on the server (in your case a counter increment) I did something similar for a competition site a few years ago, and stupidly didn't think about this at the time. Someone ended up gaming the system by including an image with the clicked-through URL in the src attribute, and put that on their MySpace profile page, which had more than a few visitors. Each of those visitors browser attempted to grab that image which registered a click, and because of the number of unique visitors, the clicks were registered as genuine. I'd recommend using POST data for this reason, as it's a lot more difficult for people to game. I agree, POST data is indeed the way to go here. Personally, I would use a like image-like thing which is actually a button, using some clever javascript (personally I would use jquery for this) you can then POST data to the server based on the click. Then set a cookie which disables the button (and keeps it disabled on future visits). This should prevent average person from repeatedly clicking it. You could also log the person's IP adress and filter based on that aswell; combining various methods would be best in this case I think. To prevent the method which Ashley mentioned, using POST data isn't enough. You would want to guarantee that the link came from YOUR server instead of some different place. There are multiple ways to do this: - use a unique key as an argument in the POST which can only be clicked once. Register the key in a database before serving the page, and then unregister it once it has been served and clicked. Though if a person were to repeatedly open the page, your cache would be exhausted, and the method would become useless. - require a referrer address to come from your domain; also reasonably easily circumvented in this case - there are more, but it really depends on how much effort you want to put into preventing attacks and how much effort you expect others to put into attacking it. For example, large sites like youtube are sure to use extensive measures to prevent people from spam-clicking in any way. While sites that only cater to say 3 visitors a month don't require all that effort in the first place. Hope that helps, - Tul -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] count clicks to count most important news
On 1 Jan 2012, at 16:26, muad shibani wrote: I have a website that posts the most important news according to the number of clicks to that news the question is : what is the best way to prevent multiple clicks from the same visitor? I'm assuming this is not a voting system, and the news items you're counting are sourced from your own site and, with all due respect to Ash, unlikely to be a target for false clicks. All you're really wanting to do is prevent the site from registering multiple hits from the same user in a short period of time. I would probably use memcached on the server-side to store short-term information about clicks. When a news item is loaded... 1) Construct the memcache key: newsclick_article_id_ip_address. 2) Fetch the key from memcache. 3a) If it does not exist, log the hit. 3b) If it does exist, compare time() with the value and only log the hit if time() is greater. 4) Store the key with a value of time() + 300 and an expiry of the same value. This will prevent hits being logged for the same news item from the same IP address within 5 minutes of other hits. Other alternatives would be to use cookies (could get messy, and not very reliable since it requires the response from click 1 to be processed before click 2 gets started), Javascript (as suggested by tedd but without the token - it would work pretty well and would be a lot easier to implement than the above, but you sacrifice having full control over it). If I'm interpreting the requirement correctly my solution is almost certainly overkill, and a simple Javascript solution would be more than sufficient. -Stuart -- Stuart Dallas 3ft9 Ltd http://3ft9.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] count clicks to count most important news
All the answers are great but Stuart Dallas' answer is what I was asking about .. thank u all I really appreciate it a lot On Sun, Jan 1, 2012 at 11:10 PM, Stuart Dallas stu...@3ft9.com wrote: On 1 Jan 2012, at 16:26, muad shibani wrote: I have a website that posts the most important news according to the number of clicks to that news the question is : what is the best way to prevent multiple clicks from the same visitor? I'm assuming this is not a voting system, and the news items you're counting are sourced from your own site and, with all due respect to Ash, unlikely to be a target for false clicks. All you're really wanting to do is prevent the site from registering multiple hits from the same user in a short period of time. I would probably use memcached on the server-side to store short-term information about clicks. When a news item is loaded... 1) Construct the memcache key: newsclick_article_id_ip_address. 2) Fetch the key from memcache. 3a) If it does not exist, log the hit. 3b) If it does exist, compare time() with the value and only log the hit if time() is greater. 4) Store the key with a value of time() + 300 and an expiry of the same value. This will prevent hits being logged for the same news item from the same IP address within 5 minutes of other hits. Other alternatives would be to use cookies (could get messy, and not very reliable since it requires the response from click 1 to be processed before click 2 gets started), Javascript (as suggested by tedd but without the token - it would work pretty well and would be a lot easier to implement than the above, but you sacrifice having full control over it). If I'm interpreting the requirement correctly my solution is almost certainly overkill, and a simple Javascript solution would be more than sufficient. -Stuart -- Stuart Dallas 3ft9 Ltd http://3ft9.com/ -- *___* * * السجل .. كل الأخبار من كل مكان www.alsjl.com صفحة السجل على فيسبوك http://www.facebook.com/alsjl *Muad Shibani* * * Aden Yemen Mobile: 00967 733045678 www.muadshibani.com
[PHP] Count the Number of Certain Elements in An Array
Hi, This seems like a pretty simple problem, but I can't seem to be able to figure it out. I have a lot of elements in an array, and some of them are duplicates, but I don't want to delete them because I have other purposes for it. Is it possible for me to find out the number of certain elements in an array? For example, // Create a simple array. $array = array(1, 2, 3, 4, 5, 3,3,4,2); $total = count($array); echo $total; If I run the code, the value of $total would be 9. However, what I really want to do is to get the values of the different instances of each, like: 1 = 1 2 = 2 3 = 3 4= 2 5= 1 Is there a simple code that I use to find this out? Thanks for your help. Alice _ Hotmail: Trusted email with powerful SPAM protection. http://clk.atdmt.com/GBL/go/196390707/direct/01/
Re: [PHP] Count the Number of Certain Elements in An Array
On Mon, Jan 11, 2010 at 6:21 PM, Alice Wei aj...@alumni.iu.edu wrote: Hi, This seems like a pretty simple problem, but I can't seem to be able to figure it out. I have a lot of elements in an array, and some of them are duplicates, but I don't want to delete them because I have other purposes for it. Is it possible for me to find out the number of certain elements in an array? For example, // Create a simple array. $array = array(1, 2, 3, 4, 5, 3,3,4,2); $total = count($array); echo $total; If I run the code, the value of $total would be 9. However, what I really want to do is to get the values of the different instances of each, like: 1 = 1 2 = 2 3 = 3 4= 2 5= 1 Is there a simple code that I use to find this out? Thanks for your help. Alice _ Hotmail: Trusted email with powerful SPAM protection. http://clk.atdmt.com/GBL/go/196390707/direct/01/ Hi, Try the function array_count_values. Regards, Jonathan -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Count the Number of Certain Elements in An Array
Date: Mon, 11 Jan 2010 18:31:43 -0300 Subject: Re: [PHP] Count the Number of Certain Elements in An Array From: tapi...@gmail.com To: aj...@alumni.iu.edu CC: php-general@lists.php.net On Mon, Jan 11, 2010 at 6:21 PM, Alice Wei aj...@alumni.iu.edu wrote: Hi, This seems like a pretty simple problem, but I can't seem to be able to figure it out. I have a lot of elements in an array, and some of them are duplicates, but I don't want to delete them because I have other purposes for it. Is it possible for me to find out the number of certain elements in an array? For example, // Create a simple array. $array = array(1, 2, 3, 4, 5, 3,3,4,2); $total = count($array); echo $total; If I run the code, the value of $total would be 9. However, what I really want to do is to get the values of the different instances of each, like: 1 = 1 2 = 2 3 = 3 4= 2 5= 1 Is there a simple code that I use to find this out? Thanks for your help. Alice _ Hotmail: Trusted email with powerful SPAM protection. http://clk.atdmt.com/GBL/go/196390707/direct/01/ Hi, Try the function array_count_values. Thanks, this is cool. There is already a function for it! Regards, Jonathan _ Hotmail: Free, trusted and rich email service. http://clk.atdmt.com/GBL/go/196390708/direct/01/
[PHP] count() total records for pagination with limit
I seem to recall that it is possible to count all instances of a query that is limited by $RecordsPerPage without repeating the same query. I believe that COUNT() had to called immediately after the SELECT word but I neglected to bookmark the source. Dummy! I don't like the idea of count(*) over count() or something like that as it seems rather slow fram what I read. right now I do this: $sql = SELECT * FROM book WHERE id IN (SELECT bookID FROM book_author WHERE authID IN (SELECT author.id FROM author WHERE LEFT(last_name, 1 ) = '$Auth')) ; $Count1 = mysql_num_rows(mysql_query($sql, $db)); $books = array(); $SQL = SELECT * FROM book WHERE id IN (SELECT bookID FROM book_author WHERE authID IN (SELECT author.id FROM author WHERE LEFT(last_name, 1 ) = '$Auth')) ORDER BY $sort $dir LIMIT $offset, $records_per_page ; if ( ( $results = mysql_query($SQL, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { $books[$row['id']] = $row; } } $Count = mysql_num_rows($results); $Count gives me the actual rows for display - $Count1 gives me the total rows available. Can this be streamlined any? -- unheralded genius: A clean desk is the sign of a dull mind. - Phil Jourdan --- p...@ptahhotep.com http://www.ptahhotep.com http://www.chiccantine.com/andypantry.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] count() total records for pagination with limit
PJ wrote: I seem to recall that it is possible to count all instances of a query that is limited by $RecordsPerPage without repeating the same query. I believe that COUNT() had to called immediately after the SELECT word but I neglected to bookmark the source. Dummy! You're probably thinking of SQL_CALC_FOUND_ROWS (http://dev.mysql.com/doc/refman/5.0/en/limit-optimization.html) It's not always faster though (http://www.mysqlperformanceblog.com/2007/08/28/to-sql_calc_found_rows-or-not-to-sql_calc_found_rows/). I don't like the idea of count(*) over count() or something like that as it seems rather slow fram what I read. right now I do this: $sql = SELECT * FROM book WHERE id IN (SELECT bookID FROM book_author WHERE authID IN (SELECT author.id FROM author WHERE LEFT(last_name, 1 ) = '$Auth')) ; $Count1 = mysql_num_rows(mysql_query($sql, $db)); The problem with this is if your '*' includes 50 fields (from all of the tables in the joins etc) then that is still processed in mysql taking up memory especially. Doing a count() just has 1 field - the count. $sql = select count(1) as count from book ; $result = mysql_query($sql, $db); $row = mysql_fetch_assoc($result); $count = $row['count']; $Count gives me the actual rows for display - $Count1 gives me the total rows available. Can this be streamlined any? Not really. -- Postgresql php tutorials http://www.designmagick.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] count() total records for pagination with limit
Chris wrote: PJ wrote: I seem to recall that it is possible to count all instances of a query that is limited by $RecordsPerPage without repeating the same query. I believe that COUNT() had to called immediately after the SELECT word but I neglected to bookmark the source. Dummy! You're probably thinking of SQL_CALC_FOUND_ROWS (http://dev.mysql.com/doc/refman/5.0/en/limit-optimization.html) It's not always faster though (http://www.mysqlperformanceblog.com/2007/08/28/to-sql_calc_found_rows-or-not-to-sql_calc_found_rows/). I don't like the idea of count(*) over count() or something like that as it seems rather slow fram what I read. right now I do this: $sql = SELECT * FROM book WHERE id IN (SELECT bookID FROM book_author WHERE authID IN (SELECT author.id FROM author WHERE LEFT(last_name, 1 ) = '$Auth')) ; $Count1 = mysql_num_rows(mysql_query($sql, $db)); The problem with this is if your '*' includes 50 fields (from all of the tables in the joins etc) then that is still processed in mysql taking up memory especially. Doing a count() just has 1 field - the count. $sql = select count(1) as count from book ; $result = mysql_query($sql, $db); $row = mysql_fetch_assoc($result); $count = $row['count']; $Count gives me the actual rows for display - $Count1 gives me the total rows available. Can this be streamlined any? Not really. OK. Your suggestion does help, though. :-) -- unheralded genius: A clean desk is the sign of a dull mind. - Phil Jourdan --- p...@ptahhotep.com http://www.ptahhotep.com http://www.chiccantine.com/andypantry.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Count the Number of Elements Using Explode
2008/10/31 Stut [EMAIL PROTECTED]: On 31 Oct 2008, at 17:32, Maciek Sokolewicz wrote: Kyle Terry wrote: -- Forwarded message -- From: Kyle Terry [EMAIL PROTECTED] Date: Fri, Oct 31, 2008 at 9:31 AM Subject: Re: [PHP] Count the Number of Elements Using Explode To: Alice Wei [EMAIL PROTECTED] I would use http://us2.php.net/manual/en/function.array-count-values.php On Fri, Oct 31, 2008 at 9:29 AM, Alice Wei [EMAIL PROTECTED] wrote: Hi, I have a code snippet here as follows: $message=1|2|3|4|5; $stringChunks = explode(|, $message); Is it possible to find out the number of elements in this string? It seems that I could do things like print_r and find out what is the last element of $stringChunks is, but is there a way where I can use code from the two lines and have it print out 5 as the number of elements that contains after the splitting? Or, am I on the wrong direction here? Thanks in advance. Alice First of all, don't top post, please. Secondly, that won't do what the OP asked for. array_count_values returns an array which tells you how often each VALUE was found in that array. so: array_count_values(array(1,2,3,4,5)) will return: array(1=1,2=1,3=1,4=1,5=1). While what the OP wanted was: some_function(array(1,2,3,4,5)) to return 5 (as in the amount of values, not how often each value was found in the array). count() would do it directly, and the other suggestions people gave do it indirectly, assuming that the values between '|' are never 1 char wide. I think you'll find Kyle was suggesting that the OP use that function to count the |'s in the string. Add 1 to that and you have the number of elements explode will produce. More efficient if you're simply exploding it to count the elements, but count would be better if you need to explode them too. -Stut For the case where you want to explode a string and afterwards count the number of elements you wind up with, Maciek is right: count() is the correct function to use. array_count_values() will not work on the original string and after exploding will have no useful effect on the resulting string as far as counting the number of separators goes. If you don't actually need to explode the string but just want to count the separators, you could use some weird logic to get array_count_values() to count the number of times '|' appeared in the original string--but it would be pointless. count_chars() would be a much better choice for what you are suggesting. Torben -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Count the Number of Elements Using Explode
Hi, I have a code snippet here as follows: $message=1|2|3|4|5; $stringChunks = explode(|, $message); Is it possible to find out the number of elements in this string? It seems that I could do things like print_r and find out what is the last element of $stringChunks is, but is there a way where I can use code from the two lines and have it print out 5 as the number of elements that contains after the splitting? Or, am I on the wrong direction here? Thanks in advance. Alice _ Check the weather nationwide with MSN Search: Try it now! http://search.msn.com/results.aspx?q=weatherFORM=WLMTAG -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Fwd: [PHP] Count the Number of Elements Using Explode
-- Forwarded message -- From: Kyle Terry [EMAIL PROTECTED] Date: Fri, Oct 31, 2008 at 9:31 AM Subject: Re: [PHP] Count the Number of Elements Using Explode To: Alice Wei [EMAIL PROTECTED] I would use http://us2.php.net/manual/en/function.array-count-values.php On Fri, Oct 31, 2008 at 9:29 AM, Alice Wei [EMAIL PROTECTED] wrote: Hi, I have a code snippet here as follows: $message=1|2|3|4|5; $stringChunks = explode(|, $message); Is it possible to find out the number of elements in this string? It seems that I could do things like print_r and find out what is the last element of $stringChunks is, but is there a way where I can use code from the two lines and have it print out 5 as the number of elements that contains after the splitting? Or, am I on the wrong direction here? Thanks in advance. Alice _ Check the weather nationwide with MSN Search: Try it now! http://search.msn.com/results.aspx?q=weatherFORM=WLMTAG -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- Kyle Terry | www.kyleterry.com -- Kyle Terry | www.kyleterry.com
Re: [PHP] Count the Number of Elements Using Explode
On Fri, Oct 31, 2008 at 11:29 AM, Alice Wei [EMAIL PROTECTED] wrote: Hi, I have a code snippet here as follows: $message=1|2|3|4|5; $stringChunks = explode(|, $message); Is it possible to find out the number of elements in this string? It seems that I could do things like print_r and find out what is the last element of $stringChunks is, but is there a way where I can use code from the two lines and have it print out 5 as the number of elements that contains after the splitting? http://php.net/strlen http://php.net/count -- /Daniel P. Brown http://www.parasane.net/ [EMAIL PROTECTED] || [EMAIL PROTECTED] Ask me about our current hosting/dedicated server deals! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Count the Number of Elements Using Explode
Hi Alice, U can simple do: $nr = ( strlen( $message ) / 2 ) + 1 ); $nr = count( explode( '|', $message ); On Fri, Oct 31, 2008 at 4:32 PM, Daniel Brown [EMAIL PROTECTED] wrote: On Fri, Oct 31, 2008 at 11:29 AM, Alice Wei [EMAIL PROTECTED] wrote: Hi, I have a code snippet here as follows: $message=1|2|3|4|5; $stringChunks = explode(|, $message); Is it possible to find out the number of elements in this string? It seems that I could do things like print_r and find out what is the last element of $stringChunks is, but is there a way where I can use code from the two lines and have it print out 5 as the number of elements that contains after the splitting? http://php.net/strlen http://php.net/count -- /Daniel P. Brown http://www.parasane.net/ [EMAIL PROTECTED] || [EMAIL PROTECTED] Ask me about our current hosting/dedicated server deals! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- Thanks, Diogo Neves Web Developer @ SAPO.pt by PrimeIT.pt
RE: [PHP] Count the Number of Elements Using Explode
Hi, Thanks for all who responded, and the function is now working properly. It turned out that count function works for strings that contain the actual data inside, but when it is an empty string, a new if clause had to be set to create the count. Alice Alice Wei Indiana University, MIS 2008 Programmer/Computer Application Developer ProCure Treatment Centers, Inc. 420 N. Walnut St. Bloomington, IN 47404 812-330-6644 (office) 812-219-5708 (mobile) [EMAIL PROTECTED](email) http://www.procurecenters.com/index.php (web) Date: Fri, 31 Oct 2008 17:00:44 + From: [EMAIL PROTECTED] To: [EMAIL PROTECTED] CC: [EMAIL PROTECTED]; php-general@lists.php.net Subject: Re: [PHP] Count the Number of Elements Using Explode Hi Alice, U can simple do: $nr = ( strlen( $message ) / 2 ) + 1 ); $nr = count( explode( '|', $message ); On Fri, Oct 31, 2008 at 4:32 PM, Daniel Brown wrote: On Fri, Oct 31, 2008 at 11:29 AM, Alice Wei wrote: Hi, I have a code snippet here as follows: $message=1|2|3|4|5; $stringChunks = explode(|, $message); Is it possible to find out the number of elements in this string? It seems that I could do things like print_r and find out what is the last element of $stringChunks is, but is there a way where I can use code from the two lines and have it print out 5 as the number of elements that contains after the splitting? http://php.net/strlen http://php.net/count -- http://www.parasane.net/ [EMAIL PROTECTED] || [EMAIL PROTECTED] Ask me about our current hosting/dedicated server deals! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- Thanks, Diogo Neves Web Developer @ SAPO.pt by PrimeIT.pt _ Check the weather nationwide with MSN Search: Try it now! http://search.msn.com/results.aspx?q=weatherFORM=WLMTAG -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Count the Number of Elements Using Explode
Diogo Neves wrote: Hi Alice, U can simple do: $nr = ( strlen( $message ) / 2 ) + 1 ); $nr = count( explode( '|', $message ); Or... $num = (substr_count($message, |) + 1); -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: Fwd: [PHP] Count the Number of Elements Using Explode
Kyle Terry wrote: -- Forwarded message -- From: Kyle Terry [EMAIL PROTECTED] Date: Fri, Oct 31, 2008 at 9:31 AM Subject: Re: [PHP] Count the Number of Elements Using Explode To: Alice Wei [EMAIL PROTECTED] I would use http://us2.php.net/manual/en/function.array-count-values.php On Fri, Oct 31, 2008 at 9:29 AM, Alice Wei [EMAIL PROTECTED] wrote: Hi, I have a code snippet here as follows: $message=1|2|3|4|5; $stringChunks = explode(|, $message); Is it possible to find out the number of elements in this string? It seems that I could do things like print_r and find out what is the last element of $stringChunks is, but is there a way where I can use code from the two lines and have it print out 5 as the number of elements that contains after the splitting? Or, am I on the wrong direction here? Thanks in advance. Alice _ Check the weather nationwide with MSN Search: Try it now! http://search.msn.com/results.aspx?q=weatherFORM=WLMTAG -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php First of all, don't top post, please. Secondly, that won't do what the OP asked for. array_count_values returns an array which tells you how often each VALUE was found in that array. so: array_count_values(array(1,2,3,4,5)) will return: array(1=1,2=1,3=1,4=1,5=1). While what the OP wanted was: some_function(array(1,2,3,4,5)) to return 5 (as in the amount of values, not how often each value was found in the array). count() would do it directly, and the other suggestions people gave do it indirectly, assuming that the values between '|' are never 1 char wide. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Count the Number of Elements Using Explode
On 31 Oct 2008, at 17:32, Maciek Sokolewicz wrote: Kyle Terry wrote: -- Forwarded message -- From: Kyle Terry [EMAIL PROTECTED] Date: Fri, Oct 31, 2008 at 9:31 AM Subject: Re: [PHP] Count the Number of Elements Using Explode To: Alice Wei [EMAIL PROTECTED] I would use http://us2.php.net/manual/en/function.array-count-values.php On Fri, Oct 31, 2008 at 9:29 AM, Alice Wei [EMAIL PROTECTED] wrote: Hi, I have a code snippet here as follows: $message=1|2|3|4|5; $stringChunks = explode(|, $message); Is it possible to find out the number of elements in this string? It seems that I could do things like print_r and find out what is the last element of $stringChunks is, but is there a way where I can use code from the two lines and have it print out 5 as the number of elements that contains after the splitting? Or, am I on the wrong direction here? Thanks in advance. Alice _ Check the weather nationwide with MSN Search: Try it now! http://search.msn.com/results.aspx?q=weatherFORM=WLMTAG -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php First of all, don't top post, please. Secondly, that won't do what the OP asked for. array_count_values returns an array which tells you how often each VALUE was found in that array. so: array_count_values(array(1,2,3,4,5)) will return: array(1=1,2=1,3=1,4=1,5=1). While what the OP wanted was: some_function(array(1,2,3,4,5)) to return 5 (as in the amount of values, not how often each value was found in the array). count() would do it directly, and the other suggestions people gave do it indirectly, assuming that the values between '|' are never 1 char wide. I think you'll find Kyle was suggesting that the OP use that function to count the |'s in the string. Add 1 to that and you have the number of elements explode will produce. More efficient if you're simply exploding it to count the elements, but count would be better if you need to explode them too. -Stut -- http://stut.net/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Count
I am wanting to create an select menu for displaying the order of the item on a page. I am guessing that I need to get the count of how many items are in the db and them put them into a select menu. Your question doesn't really make a great deal of sense. Your SQL could be: SELECT COUNT(*) FROM your_table WHERE 1 Which will give you a single numeric value back (the number of rows). To put them into an HTML page: select name=mySelect optionChoose.../option option value=1Foo/option /select This is a very basic select. You can simply loop through your query results adding more option tags to add more items to the select. -- Richard Heyes http://www.websupportsolutions.co.uk Mailing list management service allowing you to reach your Customers and increase your sales. ** NOW OFFERING FREE ACCOUNTS TO CHARITIES AND NON-PROFITS ** -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Count
Greetings, I am wanting to create an select menu for displaying the order of the item on a page. I am guessing that I need to get the count of how many items are in the db and them put them into a select menu. Does anyone know how to do this or can point me in the right direction? I hope this makes sense. -- Steve M.
Re: [PHP] Count
Steve, Check out the while loop (http://us.php.net/manual/en/control-structures.while.php). It'll do what you need. -- Mike -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Count
Richard, Thank you so much for your response. The select would be created from the number of records in the db. option value=²$variable²$variable2/option The problem that I am having is rather than getting just the number of records, which is 3 currently, I would like it to be 1 for the first option, 2 for the second..., so that the user has the choice of any record. The idea would be to order the content by the highest number. I hope this makes more sense. -- Steve M. on 1/17/08 1:54 PM Richard Heyes ([EMAIL PROTECTED]) wrote: I am wanting to create an select menu for displaying the order of the item on a page. I am guessing that I need to get the count of how many items are in the db and them put them into a select menu. Your question doesn't really make a great deal of sense. Your SQL could be: SELECT COUNT(*) FROM your_table WHERE 1 Which will give you a single numeric value back (the number of rows). To put them into an HTML page: select name=mySelect optionChoose.../option option value=1Foo/option /select This is a very basic select. You can simply loop through your query results adding more option tags to add more items to the select.
[PHP] count vs mysql_num_rows
I've got a little function that just checks to see if something is in a mysql db. There are several ways to do this and was curious as to the best way. The following are 2 (simplified) versions that work just fine. If these are the best ways, which of the following is better from a performance standpoint. And if there is a way that is better than one of these, I'd love to know what that is. $query = SELECT count(id) as count FROM wncci_intranet.iAdmin_users WHERE name = '$name' LIMIT 1; $results = mysql_query($query); $row = mysql_fetch_array($results); $count = $row[count]; return $count; OR $query = SELECT id FROM wncci_intranet.iAdmin_users WHERE name = '$name' LIMIT 1; $results = mysql_query($query); $count = mysql_num_rows($results); return $count; Thanks. -- Kevin Murphy Webmaster: Information and Marketing Services Western Nevada College www.wnc.edu 775-445-3326 P.S. Please note that my e-mail and website address have changed from wncc.edu to wnc.edu.
Re: [PHP] count vs mysql_num_rows
On 10/8/07, Kevin Murphy [EMAIL PROTECTED] wrote: I've got a little function that just checks to see if something is in a mysql db. There are several ways to do this and was curious as to the best way. The following are 2 (simplified) versions that work just fine. If these are the best ways, which of the following is better from a performance standpoint. And if there is a way that is better than one of these, I'd love to know what that is. $query = SELECT count(id) as count FROM wncci_intranet.iAdmin_users WHERE name = '$name' LIMIT 1; $results = mysql_query($query); $row = mysql_fetch_array($results); $count = $row[count]; return $count; OR $query = SELECT id FROM wncci_intranet.iAdmin_users WHERE name = '$name' LIMIT 1; $results = mysql_query($query); $count = mysql_num_rows($results); return $count; i would do a select count() from the database. no sense in returning all the row data back to the script just to do a count on it there. that's additional network/socket transfer between the app and the database, then processing on the app side to collect all the rows into memory, etc. you also don't need to do select count(id) as count, just do a select count(id) and do a mysql_fetch_array($results, MYSQL_NUM) or mysql_fetch_row($results) - you don't need to make it an associative array, since it's only one column and the name is irrelevant. mysql_fetch_row() is the best function for what you want. you'll save any additional PHP overhead with different types of arrays being setup. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] count vs mysql_num_rows
Kevin Murphy wrote: I've got a little function that just checks to see if something is in a mysql db. There are several ways to do this and was curious as to the best way. The following are 2 (simplified) versions that work just fine. If these are the best ways, which of the following is better from a performance standpoint. And if there is a way that is better than one of these, I'd love to know what that is. $query = SELECT count(id) as count FROM wncci_intranet.iAdmin_users WHERE name = '$name' LIMIT 1; $results = mysql_query($query); $row = mysql_fetch_array($results); $count = $row[count]; return $count; OR $query = SELECT id FROM wncci_intranet.iAdmin_users WHERE name = '$name' LIMIT 1; $results = mysql_query($query); $count = mysql_num_rows($results); return $count; Thanks. Is this a joke? You are using a LIMIT 1, so your count is always going to be 1. But, if you didn't use the LIMIT 1 and really wanted to see how many the results would be found, then I would do it like this. $SQL = SELECT count(id) FROMiAdmin_users WHERE name = '{$name}'; $results = mysql_query($SQL) OR die('Mysql Error: '.mysql_error()); $count = 0; if ( $results ) { list($count) = mysql_fetch_row($results); } return $count; -- Jim Lucas Some men are born to greatness, some achieve greatness, and some have greatness thrust upon them. Twelfth Night, Act II, Scene V by William Shakespeare -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] count vs mysql_num_rows
Is this a joke? You are using a LIMIT 1, so your count is always going to be 1. No, its not a joke. The answer is not going to always 1, its going to be 1 (the value exists in the DB at least once) or 0 (the value doesn't exist in the DB), that's what I am trying to test for. I don't need to know how many times something exists, just if it does. -- Kevin Murphy Webmaster: Information and Marketing Services Western Nevada College www.wnc.edu 775-445-3326
RE: [PHP] count vs mysql_num_rows
[snip] Is this a joke? You are using a LIMIT 1, so your count is always going to be 1. No, its not a joke. The answer is not going to always 1, its going to be 1 (the value exists in the DB at least once) or 0 (the value doesn't exist in the DB), that's what I am trying to test for. I don't need to know how many times something exists, just if it does. [/snip] If you just need to know if data exists use the query with limit 1 and test to see if 1 === $result['count']. I agree with the poster about not returning all of the rows, it is more efficient to do it this way. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] count vs mysql_num_rows
At 12:47 PM -0700 10/8/07, Kevin Murphy wrote: Is this a joke? You are using a LIMIT 1, so your count is always going to be 1. No, its not a joke. The answer is not going to always 1, its going to be 1 (the value exists in the DB at least once) or 0 (the value doesn't exist in the DB), that's what I am trying to test for. I don't need to know how many times something exists, just if it does. -- Kevin Murphy Webmaster: Information and Marketing Services Western Nevada College www.wnc.edu 775-445-3326 Kevin: Considering what you want to do, you might want to investigate having mysql do it for you, see here: http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Count empty array
I'm wondering why this is. $data = ; $array = explode(,,$data); $count = count($array); $count will = 1 $data = Test; $array = explode(,,$data); $count = count($array); $count will = 1 $data = Test,Test; $array = explode(,,$data); $count = count($array); $count will = 2 Why doesn't the first one give me an answer of 0 instead of 1. I know I could do a IF $data == [empty] and then not count if its empty and just set it to 0, but am wondering if there was a better way. -- Kevin Murphy Webmaster: Information and Marketing Services Western Nevada Community College www.wncc.edu 775-445-3326
Re: [PHP] Count empty array
On Thu, 2006-12-21 at 13:31 -0800, Kevin Murphy wrote: I'm wondering why this is. $data = ; $array = explode(,,$data); $count = count($array); $count will = 1 $data = Test; $array = explode(,,$data); $count = count($array); $count will = 1 $data = Test,Test; $array = explode(,,$data); $count = count($array); $count will = 2 Why doesn't the first one give me an answer of 0 instead of 1. For the same reason the second one gives you a count of one. There are no commas and so only one item exists. Whether that be an string with content or the empty string itself is irrelevant. Cheers, Rob. -- .. | InterJinn Application Framework - http://www.interjinn.com | :: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Count empty array
Kevin Murphy wrote: I'm wondering why this is. $data = ; $array = explode(,,$data); $count = count($array); $count will = 1 $data = Test; $array = explode(,,$data); $count = count($array); $count will = 1 $data = Test,Test; $array = explode(,,$data); $count = count($array); $count will = 2 Why doesn't the first one give me an answer of 0 instead of 1. This: var_dump(explode(',','')); Returns this: array(1) { [0]= string(0) } And oddly, This: var_dump(explode(',',NULL)); Returns this: array(1) { [0]= string(0) } That explains the count() result. It seems to me that the first case could go either way (The part of before the ',' is still ), but I'm unable to think of a logical reason why the second doesn't return an empty array. I know I could do a IF $data == [empty] and then not count if its empty and just set it to 0, but am wondering if there was a better way. $array = empty($data) ? array() : explode(',',$data); /* That what you're looking for? */ jon -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Count empty array
On Thu, 21 Dec 2006, Kevin Murphy wrote: I'm wondering why this is. $data = ; $array = explode(,,$data); $count = count($array); $count will = 1 $array has 1 element: An empty string. $data = Test; $array = explode(,,$data); $count = count($array); $count will = 1 $array has 1 element: The string Test $data = Test,Test; $array = explode(,,$data); $count = count($array); $count will = 2 $array has 2 elements:. Why doesn't the first one give me an answer of 0 instead of 1. I know I could do a IF $data == [empty] and then not count if its empty and just set it to 0, but am wondering if there was a better way. Because explode divides the string in n+1 elements, where n is the amount of , (or your favorite delimiter) found in the string. So if no , is found, explode will return 1 element: the whole string (even if it's empty). -- 21:50:04 up 2 days, 9:07, 0 users, load average: 0.92, 0.37, 0.18 - Lic. Martín Marqués | SELECT 'mmarques' || Centro de Telemática| '@' || 'unl.edu.ar'; Universidad Nacional| DBA, Programador, del Litoral | Administrador - -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Count empty array
Not sure why it does it, but doesn't seem to be a huge deal. I'm guessing it's because an empty string is still a string. It's not null. Anyway, it's documented at: http://us3.php.net/manual/en/function.explode.php A user writes: If you split an empty string, you get back a one-element array with 0 as the key and an empty string for the value. -TG = = = Original message = = = I'm wondering why this is. $data = ; $array = explode(,,$data); $count = count($array); $count will = 1 $data = Test; $array = explode(,,$data); $count = count($array); $count will = 1 $data = Test,Test; $array = explode(,,$data); $count = count($array); $count will = 2 Why doesn't the first one give me an answer of 0 instead of 1. I know I could do a IF $data == [empty] and then not count if its empty and just set it to 0, but am wondering if there was a better way. -- Kevin Murphy Webmaster: Information and Marketing Services Western Nevada Community College www.wncc.edu 775-445-3326 ___ Sent by ePrompter, the premier email notification software. Free download at http://www.ePrompter.com. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] count string within a string
I've figured out I need the substr_count function. However, counting strings like this: [1] [2] [3] ... [215] ... inside a large string element. damn. The string got quite a few different numbers and keys but the main key is alway within [] and is numeric. I imagine I could use a $counter++. I just dont see how I should aproach this. Anyone got an Idea? -- --- Børge Kennel Arivene http://www.arivene.net --- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] count string within a string
On 11/17/06, Børge Holen [EMAIL PROTECTED] wrote: I've figured out I need the substr_count function. However, counting strings like this: [1] [2] [3] ... [215] ... inside a large string element. damn. The string got quite a few different numbers and keys but the main key is alway within [] and is numeric. I imagine I could use a $counter++. I just dont see how I should aproach this. Anyone got an Idea? -- --- Børge Kennel Arivene http://www.arivene.net --- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php Apologies if I missed what you were after.. but maybe you might try this: ?php $string = 'However, counting strings like this: [1] [2] [3] ... [215] ...'; $result = preg_match_all('/\[([0-9]+)\]/U', $string, $matches); if (! $result ) echo No matches\n; echo Count: . count($matches[1]); -- Results in: Count: 4 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Count of elements in XML
I was wondering if there is a way to get the number of foo elements in my XML below? This way I could loop through each element and print out the attributes. $string = a foo name=\one\ game=\lonely\1/foo foo name=\two\ game=\again\2/foo foo name=\three\ game=\blah\3/foo foo name=\four\ game=\goodness\4/foo /a; $xml = simplexml_load_string($string); // this doesn't return the correct number of elements but instead returns the data inbetween foo/foo $cnt = count($xml-foo); for($i=0; $i$cnt; $i++) { foreach($xml-foo[$i]-attributes() as $a = $b) { echo $a,'=',$b,\\n; } } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Count of elements in XML
Hi Jay, Check the example at http://www.weberdev.com/Manuals/PHP/function.simplexml-element-children.html Thanks, Richard -Original Message- From: Jay Paulson [mailto:[EMAIL PROTECTED] Sent: Friday, January 20, 2006 11:06 PM To: PHP LIST Subject: [PHP] Count of elements in XML I was wondering if there is a way to get the number of foo elements in my XML below? This way I could loop through each element and print out the attributes. $string = a foo name=\one\ game=\lonely\1/foo foo name=\two\ game=\again\2/foo foo name=\three\ game=\blah\3/foo foo name=\four\ game=\goodness\4/foo /a; -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Count and Preg_Replace Using Version 4.4.1
form'.$count.'... You can use preg_replace_callback or the e pattern modifier to let you run php code for the replacement: http://us3.php.net/manual/en/function.preg-replace-callback.php http://us3.php.net/manual/en/reference.pcre.pattern.modifiers.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Count and Preg_Replace Using Version 4.4.1
On Sun, Dec 04, 2005 at 07:00:00PM -, Shaun wrote: comex [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] form'.$count.'... You can use preg_replace_callback or the e pattern modifier to let you run php code for the replacement: http://us3.php.net/manual/en/function.preg-replace-callback.php http://us3.php.net/manual/en/reference.pcre.pattern.modifiers.php Hi Comex, Thanks for your reply, I have modified the code so I have a call back function: echo = preg_replace_callback( '/p[^]*(.*?)\/p/ms', callback, file_get_contents($_GET['file']) ); function callback($matches){ return 'form name=form target=_parent ... But I still can't see how I can increment a value in each return string? function callback($matches) { static $i = 0; // -- maintains counter within function scope $i++; return form name=form$i/form; } btw, i've been meaning to mention this in the last few posts of yours, but have you considered using DOM to make this happen. When ever I see a regex solution for parsing an html document, it makes me cringe. Curt. -- cat .signature: No such file or directory -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Count and Preg_Replace Using Version 4.4.1
Curt Zirzow [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] On Sun, Dec 04, 2005 at 07:00:00PM -, Shaun wrote: comex [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] form'.$count.'... You can use preg_replace_callback or the e pattern modifier to let you run php code for the replacement: http://us3.php.net/manual/en/function.preg-replace-callback.php http://us3.php.net/manual/en/reference.pcre.pattern.modifiers.php Hi Comex, Thanks for your reply, I have modified the code so I have a call back function: echo = preg_replace_callback( '/p[^]*(.*?)\/p/ms', callback, file_get_contents($_GET['file']) ); function callback($matches){ return 'form name=form target=_parent ... But I still can't see how I can increment a value in each return string? function callback($matches) { static $i = 0; // -- maintains counter within function scope $i++; return form name=form$i/form; } btw, i've been meaning to mention this in the last few posts of yours, but have you considered using DOM to make this happen. When ever I see a regex solution for parsing an html document, it makes me cringe. Curt. -- cat .signature: No such file or directory Hi Curt, Thanks for your reply. So basically I do something like: $doc = new DOMDocument(); $doc-loadHTML($file); $p = $doc-getElementsByTagName('p'); How would then wrap the following around each p tag: form name=form target=_parent action=/index.php?action=edit_paragraphfile='.$_GET['file'].' method=post input type=hidden name=old_html value='.htmlentities($matches[0]).' pa href=javascript:; onclick=document.form.submit();'.$matches[1].'/a/p /form BTW if its not obvious I am trying to create a CMS that lets users edit anything within a p tag! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Count totals (might be 0T)
On Thu, April 21, 2005 3:36 pm, Ryan A said: Hey, Thanks for replying. That means I will have to run a query per categorywhich I would like to avoid if possible as they could be a high amount of categories as they are user created and not defined by us. ?php /* category_count.inc Provides $CATEGORY_COUNT and $CATEGORY_SIZE variables for the application. */ $query = select cno, count(*), sum(pic_size_kb) from TABLE group by cno; $cat_info = mysql_query($query) or die(mysql_error()); $CATEGORY_COUNT = array(); $CATEGORY_SIZE = array(); while (list($cno, $count, $size) = mysql_fetch_row($cat_info)){ $CATEGORY_COUNT[$cno] = $count; $CATEGORY_SIZE[$cno] = $size; } ? Only problem is that you could have a slight timing issue where the select above gets done, then somebody adds a new image, and then you display your counts, and then you display the list, and that extra image is in the list, but not counted. You *COULD* solve this by wrapping a transaction around the whole mess if it's really critical, but you probably don't need it. Thanks, Ryan On 4/22/2005 12:08:31 AM, Drewcore ([EMAIL PROTECTED]) wrote: run another sql query: select pic_size_kb from table where pic_category='$current_category, then make a loop that increments a) the total number of records (ie total number of pics in category), and then adds the pic size from each record to some variable (eg $total_kb) and then you have your total. hope that helps. drew On 4/21/05, Ryan A [EMAIL PROTECTED] wrote: Hi again, Am faced with a new problem (either that or working straight 10 hours is catching up!) Heres my table (easy to figure out so i wont waste your time explaining the fields): cno, date_added, pic_name, pic_size_kb, pic_desc_text, pic_category the way i am displaying the data is like this (thanks to Phillip and Andy from this list) __ category here -- No virus found in this outgoing message. Checked by AVG Anti-Virus. Version: 7.0.308 / Virus Database: 266.10.1 - Release Date: 4/20/2005 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- Like Music? http://l-i-e.com/artists.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Count totals (might be 0T)
Hi again, Am faced with a new problem (either that or working straight 10 hours is catching up!) Heres my table (easy to figure out so i wont waste your time explaining the fields): cno, date_added, pic_name, pic_size_kb, pic_desc_text, pic_category the way i am displaying the data is like this (thanks to Phillip and Andy from this list) __ category here 2pics, 90kb | - | pic-here | pic_desc_text | | pic-here | pic_desc_text | __ category here 2pics, 160kb | - | pic-here | pic_desc_text | | pic-here | pic_desc_text | - When I sql the DB I call it like this: select picture_name,more fields from table where cno=x order by category. Any ideas as to how i get how many pics per category and size per category? I'm not sure if its a PHP thing or a MySql thing... Last problem for the day...then i hit the sack! Thanks in advance, Ryan -- No virus found in this outgoing message. Checked by AVG Anti-Virus. Version: 7.0.308 / Virus Database: 266.10.1 - Release Date: 4/20/2005 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Count totals (might be 0T)
Hey, Thanks for replying. That means I will have to run a query per categorywhich I would like to avoid if possible as they could be a high amount of categories as they are user created and not defined by us. Thanks, Ryan On 4/22/2005 12:08:31 AM, Drewcore ([EMAIL PROTECTED]) wrote: run another sql query: select pic_size_kb from table where pic_category='$current_category, then make a loop that increments a) the total number of records (ie total number of pics in category), and then adds the pic size from each record to some variable (eg $total_kb) and then you have your total. hope that helps. drew On 4/21/05, Ryan A [EMAIL PROTECTED] wrote: Hi again, Am faced with a new problem (either that or working straight 10 hours is catching up!) Heres my table (easy to figure out so i wont waste your time explaining the fields): cno, date_added, pic_name, pic_size_kb, pic_desc_text, pic_category the way i am displaying the data is like this (thanks to Phillip and Andy from this list) __ category here -- No virus found in this outgoing message. Checked by AVG Anti-Virus. Version: 7.0.308 / Virus Database: 266.10.1 - Release Date: 4/20/2005 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Count totals (might be 0T)
Hi, Friday, April 22, 2005, 8:03:06 AM, you wrote: RA Hi again, RA Am faced with a new problem (either that or working straight 10 hours is RA catching up!) RA Heres my table (easy to figure out so i wont waste your time explaining the RA fields): RA cno, RA date_added, RA pic_name, RA pic_size_kb, RA pic_desc_text, RA pic_category RA the way i am displaying the data is like this (thanks to Phillip and Andy RA from this list) RA __ RA category here 2pics, 90kb | RA - RA | pic-here | pic_desc_text | RA | pic-here | pic_desc_text | RA __ RA category here 2pics, 160kb | RA - RA | pic-here | pic_desc_text | RA | pic-here | pic_desc_text | RA - RA When I sql the DB I call it like this: select picture_name,more fields from table where cno=x order by category. RA Any ideas as to how i get how many pics per category and size per category? RA I'm not sure if its a PHP thing or a MySql thing... RA Last problem for the day...then i hit the sack! RA Thanks in advance, RA Ryan Something like SELECT COUNT(pic_name) as pic_count, SUM(pic_size_kb) as pic_total FROM pic_table GROUP BY pic_category -- regards, Tom -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Count totals(might be 0T)
Ryan A mailto:[EMAIL PROTECTED] on Thursday, April 21, 2005 3:03 PM said: When I sql the DB I call it like this: select picture_name,more fields from table where cno=x order by category. Any ideas as to how i get how many pics per category and size per category? I'm not sure if its a PHP thing or a MySql thing... By either running a query for each category grabbing the data you want. Or by grabbing the data you want in the initial query and adding it all up BEFORE you start to do you display logic. So the second option would be: 1. Get all the data. 2. Loop through all the data creating the tally(s) you want. 3. Loop through the data again creating your visual gropings and throwing in the tally(s) you created earlier. Try both options and time them. Chris. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] count with match probelm
Richard Lynch wrote: ... $how_many = mysql_result(mysql_query(SELECT COUNT(*), MATCH(ad_sub, ad_text) AGAINST('$words') AS score from .$tcname.ads WHERE MATCH(ad_sub, ad_text) AGAINST('$words') FROM .$tcname.ads where is_confirmed=1),0); H. Actually, I *can* tell you that you shouldn't have FROM in there twice. You've messed up your query something awful with that. also he just wants the count with this statement so: 'MATCH(ad_sub,ad_text) AGAINST('$words') AS score' shouldn't be part of this query, instead: SELECT COUNT(*) FROM .$tcname.ads where is_confirmed=1 WHERE MATCH(ad_sub,ad_text) AGAINST('$words') -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] count with match probelm
Hi, I am running this kind of a statement: $q=mysql_query(select cno,date_and_time,MATCH(ad_sub, ad_text) AGAINST('$words') AS score from .$tcname.ads WHERE MATCH(ad_sub, ad_text) AGAINST('$words') AND is_confirmed=1 LIMIT $limit1, $limit2); which is running fine...my problem is I am using a class for pagination which requires the total number of records that will be returned to break the query into pagesand which will give me the values of $limit1 and , $limit2 which i will use in the above statement...but running a count() is not working, it gives me an error, what other options do i have other than running the same query twice and using mysql_num_rows? (which i think would be quite intensive/wasteful as i am querying @ 100 rows each time) This is my count() query, maybe theres a problem here? $how_many = mysql_result(mysql_query(SELECT COUNT(*), MATCH(ad_sub, ad_text) AGAINST('$words') AS score from .$tcname.ads WHERE MATCH(ad_sub, ad_text) AGAINST('$words') FROM .$tcname.ads where is_confirmed=1),0); Thanks in advance, Ryan -- No virus found in this outgoing message. Checked by AVG Anti-Virus. Version: 7.0.308 / Virus Database: 266.8.0 - Release Date: 3/21/2005 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] count with match probelm
On Tue, March 22, 2005 11:30 pm, Ryan A said: $q=mysql_query(select cno,date_and_time,MATCH(ad_sub, ad_text) AGAINST('$words') AS score from .$tcname.ads WHERE MATCH(ad_sub, ad_text) AGAINST('$words') AND is_confirmed=1 LIMIT $limit1, $limit2); This is pretty much a MySQL question, really... But: There is a new-ish SQL_CALC_FOUND_ROWS constant you can use in your query to get MySQL to tell you how many rows you would have gotten without the LIMIT clause. You may be able to use that. which is running fine...my problem is I am using a class for pagination which requires the total number of records that will be returned to break the query into pagesand which will give me the values of $limit1 and , $limit2 which i will use in the above statement...but running a count() is not working, it gives me an error, what other options do i have other than running the same query twice and using mysql_num_rows? (which i think would be quite intensive/wasteful as i am querying @ 100 rows each time) This is my count() query, maybe theres a problem here? Probably. But since you only told us it gives me an error and don't tell use *WHAT* the error is, we really can't help you, can we? $how_many = mysql_result(mysql_query(SELECT COUNT(*), MATCH(ad_sub, ad_text) AGAINST('$words') AS score from .$tcname.ads WHERE MATCH(ad_sub, ad_text) AGAINST('$words') FROM .$tcname.ads where is_confirmed=1),0); H. Actually, I *can* tell you that you shouldn't have FROM in there twice. You've messed up your query something awful with that. -- Like Music? http://l-i-e.com/artists.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] count index prevents ldap_add() after ldap_search() directly
I'm doing a bunch of LDAP work with PHP-4.3.6 and keep running up against this annoyance. If I do an ldap_search() and get the first entry, I can't then tweak some of the attribute/values and then directly use ldap_add() to put a new version into the directory. The reason is that the ldap_get_entries() function returns any array or sub-array with a gratuitous count index. For example, the following code (error checking removed for clarity): $results = ldap_search($ldapConn, $baseDN, uid=cshenton); $entries = ldap_get_entries($ldapConn, $results); $entry = $entries[0]; echo entry=pre . print_r($entry, true) . /pre; Prints the following (with some tedious attribute/value pairs removed): entry= Array ( [cn] = Array ( [count] = 1 [0] = Chris Shenton-2 ) [0] = cn [sn] = Array ( [count] = 1 [0] = Shenton ) [1] = sn [objectclass] = Array ( [count] = 2 [0] = inetOrgPerson [1] = qmailUser ) ... [count] = 14 [dn] = cn=Chris Shenton-2,ou=Headquarters,o=National Aeronautics and Space Administration,c=us ) My code then fiddles with the old DN to create a new entry, then tries to add it to LDAP: if (! ldap_add($ldapConn, $newDN, $entry)) { die(ldap_add failed, errno=. ldap_errno($ldapConn) . : . ldap_error($ldapConn)); } It fails (but oddly, ldap_error() reports Success). The problem is the 'count' and attribute-named indices (e.g., 'sn') in addition to the numeric indices: Warning: ldap_add(): Value array must have consecutive indices 0, 1, ... in /home/cshenton/public_html/newhorseadmin/ldap_search_add_test.php on line 45 ldap_add failed, errno=0: Success Am I missing something or is it not possible to simply take a search result, then add it back? Seems I have to strip out all these gratuitous 'count' and non-numeric indices each time, which isn't intuitive, obvious, or the most direct way of doing things. Thanks. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] COUNT(*) Output Question
Hey- I'm query my mySQL database to see how many of each Sku has been ordered. I am doing my query as: $count=mysql_query(SELECT sku, COUNT(*) FROM orders GROUP BY sku); But my question is how do I use PHP to output the COUNT(*) results? When I run the command when I'm logged into mySQL I get the following: mysql select sku, count(*) from orders group by sku\g +--+--+ | sku | count(*) | +--+--+ | 10001|1 | | 10001a |1 | | 10003|2 | | 10005|1 | | 10011|1 | +--+--+ I just can't figure out a way to output that Count(*) column into something HTML friendly...any help or thoughts on this would be appreciated! TIA -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] COUNT(*) Output Question
Wouldn't count(sku) work? -Original Message- From: Tom Ray [Lists] [mailto:[EMAIL PROTECTED] Sent: Monday, August 02, 2004 3:20 PM To: [EMAIL PROTECTED] Subject: [PHP] COUNT(*) Output Question Hey- I'm query my mySQL database to see how many of each Sku has been ordered. I am doing my query as: $count=mysql_query(SELECT sku, COUNT(*) FROM orders GROUP BY sku); But my question is how do I use PHP to output the COUNT(*) results? When I run the command when I'm logged into mySQL I get the following: mysql select sku, count(*) from orders group by sku\g +--+--+ | sku | count(*) | +--+--+ | 10001|1 | | 10001a |1 | | 10003|2 | | 10005|1 | | 10011|1 | +--+--+ I just can't figure out a way to output that Count(*) column into something HTML friendly...any help or thoughts on this would be appreciated! TIA -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] COUNT(*) Output Question
* Thus wrote Tom Ray [Lists]: Hey- I'm query my mySQL database to see how many of each Sku has been ordered. I am doing my query as: $count=mysql_query(SELECT sku, COUNT(*) FROM orders GROUP BY sku); alias the count(*) column: SELECT sku, COUNT(*) as qty FROM orders GROUP BY sku Curt -- First, let me assure you that this is not one of those shady pyramid schemes you've been hearing about. No, sir. Our model is the trapezoid! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] COUNT(*) Output Question
From: Tom Ray [Lists] [EMAIL PROTECTED] $count=mysql_query(SELECT sku, COUNT(*) FROM orders GROUP BY sku); But my question is how do I use PHP to output the COUNT(*) results? When I run the command when I'm logged into mySQL I get the following: I assume you're fetching associative arrays from the result and $r['sku'] works, but you're not sure how to get the COUNT(*) value? Well, $r['COUNT(*)'] would work, but you could also use an alias: SELECT sku, COUNT(*) AS mycount FROM orders GROUP BY sku and then use $r['mycount'] to retrieve the value (after using mysql_fetch_assoc/array, of course). ---John Holmes... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] COUNT(*) Output Question
Thanks JohnI didn't think of thatI've been working on this catalog admin interface for to long..my brain is mush. John W. Holmes wrote: From: Tom Ray [Lists] [EMAIL PROTECTED] $count=mysql_query(SELECT sku, COUNT(*) FROM orders GROUP BY sku); But my question is how do I use PHP to output the COUNT(*) results? When I run the command when I'm logged into mySQL I get the following: I assume you're fetching associative arrays from the result and $r['sku'] works, but you're not sure how to get the COUNT(*) value? Well, $r['COUNT(*)'] would work, but you could also use an alias: SELECT sku, COUNT(*) AS mycount FROM orders GROUP BY sku and then use $r['mycount'] to retrieve the value (after using mysql_fetch_assoc/array, of course). ---John Holmes... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] count number of occurences of character in a string
what function can I use to count the number of occurences of a certain character in a string? -- Diana Castillo Global Reservas, S.L. C/Granvia 22 dcdo 4-dcha 28013 Madrid-Spain Tel : 00-34-913604039 Fax : 00-34-915228673 email: [EMAIL PROTECTED] Web : http://www.hotelkey.com http://www.destinia.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] count number of occurences of character in a string
Hello Diana, Tuesday, June 8, 2004, 11:11:27 AM, you wrote: DC what function can I use to count the number of occurences of a certain DC character in a string? substr_count or count_chars would work. Best regards, Richard Davey -- http://www.launchcode.co.uk - PHP Development Services I am not young enough to know everything. - Oscar Wilde -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] count number of occurences of character in a string
what function can I use to count the number of occurences of a certain character in a string? substr_count() ...? rich -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] count number of occurences of character in a string
substr_count() http://www.php.net/manual/en/function.substr-count.php what function can I use to count the number of occurences of a certain character in a string? -- Diana Castillo Global Reservas, S.L. C/Granvia 22 dcdo 4-dcha 28013 Madrid-Spain Tel : 00-34-913604039 Fax : 00-34-915228673 email: [EMAIL PROTECTED] Web : http://www.hotelkey.com http://www.destinia.com
Re: [PHP] count the elements of each dimension of the array
[EMAIL PROTECTED] wrote: I have this script: ? $ojpp[] = 1; $ojpp[] = 2; $ojpp[] = 3; $ojpp[] = 4; This is illegal. Only one pair of empty square brackets is allowed. How whould php know where to add the new key? $ojpp[][] = 1; -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] count the elements of each dimension of the array
I have this script: ? $ojpp[] = 1; $ojpp[] = 2; $ojpp[] = 3; $ojpp[] = 4; $ojpp[][] = 1; $ojpp[][] = 2; $ojpp[][] = 3; $ojpp[][][] = 1; $ojpp[][][] = 2; $test1 = count($ojpp); // first dimension $test2 = count($ojpp[0]); // second dimension $test3 = count($ojpp[0][0]); // third dimension echo $test1; echo $test2; echo $test3; ? I want to count the elements of each dimension, but the script give me an error, thanks in advanced for any help.
Re: [PHP] count()
Anthony Ritter wrote: Greetings- I'm have the follwing string called $text and throughout this string I'd like to split this into an array by calling spliti() at the tag called PAGEBREAK. So, I count the array elements starting at 0 and get to 9. When I call count(), I get 11. Could somebody please explain why this is? Thank you. TR . ? $text= aaa[PAGEBREAK] //this is the 0 element bbb[PAGEBREAK] //[1] ccc[PAGEBREAK] //[2] ddd[PAGEBREAK] //[3] eee[PAGEBREAK] //[4] fff[PAGEBREAK] //[5] ggg[PAGEBREAK] //[6] hhh[PAGEBREAK] //[7] iii[PAGEBREAK] //[8] jjj[PAGEBREAK] //...and this is the 9th element ; There are 10 '[PAGEBREAK]' substrings in the string. If the string is split apart at these substrings, it will give you total 11 parts. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] count()
Marek Kilimajer [EMAIL PROTECTED] wrote in message: There are 10 '[PAGEBREAK]' substrings in the string. If the string is split apart at these substrings, it will give you total 11 parts. Thank you. TR -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] count()
Greetings- I'm have the follwing string called $text and throughout this string I'd like to split this into an array by calling spliti() at the tag called PAGEBREAK. So, I count the array elements starting at 0 and get to 9. When I call count(), I get 11. Could somebody please explain why this is? Thank you. TR . ? $text= aaa[PAGEBREAK] //this is the 0 element bbb[PAGEBREAK] //[1] ccc[PAGEBREAK] //[2] ddd[PAGEBREAK] //[3] eee[PAGEBREAK] //[4] fff[PAGEBREAK] //[5] ggg[PAGEBREAK] //[6] hhh[PAGEBREAK] //[7] iii[PAGEBREAK] //[8] jjj[PAGEBREAK] //...and this is the 9th element ; $regexp=\[PAGEBREAK]; $textarray=spliti($regexp,$text); $num_array=count($textarray); $another_num_array=count($textarray)-1; echo There are .$num_array. elements using the count() function.br; echo If I subtract 1 since elements begin at 0, there are .$another_num_array..; //This is the output using count(): //... //There are 11 elements using the count() function. //If I subtract 1 since elements begin at 0, there are 10 ? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Count online users question
Hi all, I am trying to figure an accurate way to calculate how many users are viewing a site, at any particular time. This task is very simple except for one part - How do you determine when a person has left the site...apache hasn't served anymore requests from a particular ip for xx minutes ?? - MD Mike Dunlop AWN, Inc. // www.awn.com [ e ] [EMAIL PROTECTED] [ p ] 323.606.4237 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Count online users question
If your website uses PHP sessions, and you store those sessions in a database table you could; $query = select count(*) from session_table where .session_datetime \ .date(Y-m-d H:i:s, strtotime(date(Y-m-d H:i:s). -1 hour)) .\ ; ...the reset of a mysql query to get result. Note that the 1 hour is an arbitrary amount of time and people do come back after 1 hour, but if your session lifetime is set to be less than an hour, you may want to use that same time limit. You were correct in your original assumption that the count will almost never be very precise. Warren -Original Message- From: Mike D [mailto:[EMAIL PROTECTED] Sent: Wednesday, November 26, 2003 2:57 PM To: [EMAIL PROTECTED] Subject: [PHP] Count online users question Hi all, I am trying to figure an accurate way to calculate how many users are viewing a site, at any particular time. This task is very simple except for one part - How do you determine when a person has left the site...apache hasn't served anymore requests from a particular ip for xx minutes ?? - MD .. Mike Dunlop AWN, Inc. // www.awn.com [ e ] [EMAIL PROTECTED] [ p ] 323.606.4237 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Count lines or chars
Hi PHP-gurus :-) I'd like to count the lines within a file and store the result within a variable. Every line begins with the char %. Would it be easier to count these chars and how could I do that? Thanks for your help. markus -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Count lines or chars
I guess you could do something like : ?php function countLines($filename) { if(file_exists($filename)) { $linecount=0; $fs = file($filename); while (list ($linenum, $linec) = each($fs)) { $linecount++; } return $linecount; } } $local_file = /path/to/testfile.txt; $lines = countLines($local_file); ? That would count all the lines in the file. /Morten -Original Message- From: Markus [mailto:[EMAIL PROTECTED] Sent: 17. oktober 2003 11:25 To: [EMAIL PROTECTED] Subject: [PHP] Count lines or chars Hi PHP-gurus :-) I'd like to count the lines within a file and store the result within a variable. Every line begins with the char %. Would it be easier to count these chars and how could I do that? Thanks for your help. markus -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Count lines or chars
Markus wrote: Hi PHP-gurus :-) I'd like to count the lines within a file and store the result within a variable. Every line begins with the char %. Would it be easier to count these chars and how could I do that? Thanks for your help. markus Maybe this way: $file = file( '/some/file.ext' ); $lines_count = count( $file ); -- Pavel a.k.a. Papi -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] count() numerical arrays....
Hey all. Another simple, yet baffling for me, question: I have a comma delimited list of numbers; e.g. $num_list = 1,2,3,4 I split the number list to array -- $num_list_array = split(, $num_list) I then count the number of elements -- $count = count($num_list_array); I do not get 4 for $count, rather 1! I can't stand coming up with cludgy workarounds just to count the number of elements in a numerical list. Any ideas? --Noah -- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] count() numerical arrays....
Use explode instead... $num_list_array = explode(, $num_list) more information at php.net/explode Cesar CF High wrote: Hey all. Another simple, yet baffling for me, question: I have a comma delimited list of numbers; e.g. $num_list = 1,2,3,4 I split the number list to array -- $num_list_array = split(, $num_list) I then count the number of elements -- $count = count($num_list_array); I do not get 4 for $count, rather 1! I can't stand coming up with cludgy workarounds just to count the number of elements in a numerical list. Any ideas? --Noah -- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] count() numerical arrays....
CF High wrote: I split the number list to array -- $num_list_array = split(, $num_list) Any ideas? split uses a regular expression. I think you want to use explode here. $num_list_array = explode(',', $num_list) - Brad -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] count() numerical arrays....
* Thus wrote CF High ([EMAIL PROTECTED]): Hey all. Another simple, yet baffling for me, question: I have a comma delimited list of numbers; e.g. $num_list = 1,2,3,4 I split the number list to array -- $num_list_array = split(, $num_list) I then count the number of elements -- $count = count($num_list_array); I do not get 4 for $count, rather 1! I can't stand coming up with cludgy workarounds just to count the number of elements in a numerical list. So you just want to count how many digits their are? $num_list = 1,2,3,4; $count = substr_count($num_list, ,) + 1; http://php.net/substr_count No sense of all the extra work in putting it into an array. HTH, Curt -- I used to think I was indecisive, but now I'm not so sure. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] count rows
Help me out here. I want to get a numeral on the number of records in a table. What is the function? http://www.php.net/manual-lookup.php?pattern=fetch%2Browslang=en http://www.php.net/manual-lookup.php?pattern=fetch%2Brecordslang=en ? :) John -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] count rows
mysql_num_rows gives the number of rows. But don't even think of doing it this way if you just want the number of rows and don't want the data. The correct way is to use a query such as [sql] select count(*) from table [/sql] this will return just one row and it wil tell you how many rows are in your table and your database will be a lot happier :-) John Taylor-Johnston wrote: Help me out here. I want to get a numeral on the number of records in a table. What is the function? http://www.php.net/manual-lookup.php?pattern=fetch%2Browslang=en http://www.php.net/manual-lookup.php?pattern=fetch%2Brecordslang=en ? :) John -- http://www.raditha.com/php/progress.php A progress bar for PHP file uploads. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] COUNT(*)
Hi I used to use old fashioned mysql quieries but moderm mysql apparently became more flexible so. I want to understand how to use some of the new features like COUNT(*) Let's say I have $result=SELECT COUNT(*) FROM pet; $sql=mysql_query($result); while ($row=mysql_fetch_array($sql)){ $pet_name=$row['pet_name']; echo ..; } the question is how do I actually retrieve the number of rows of sql query result? Yury -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] COUNT(*)
Actually that does retrieve the number of rows.. // Query $query = SELECT COUNT(*) AS count FROM pet; // Execute Query $result = mysql_query($query); // Get the result of query named count $count = mysql_result($result,0); echo $count. is the number of rows; Cheers! Rick In order to seek truth it is necessary once in the course of our life to doubt as far as possible all things. - Rene Descartes From: Yury B. [EMAIL PROTECTED] Reply-To: [EMAIL PROTECTED] Date: Thu, 31 Jul 2003 19:46:40 +0200 To: [EMAIL PROTECTED] Subject: [PHP] COUNT(*) Hi I used to use old fashioned mysql quieries but moderm mysql apparently became more flexible so. I want to understand how to use some of the new features like COUNT(*) Let's say I have $result=SELECT COUNT(*) FROM pet; $sql=mysql_query($result); while ($row=mysql_fetch_array($sql)){ $pet_name=$row['pet_name']; echo ..; } the question is how do I actually retrieve the number of rows of sql query result? Yury -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Count on Multiple Tables
I've been stuck on this one all morning. Can't seem to figure it out. I have 2 tables, one with affiliate sales and another with affiliate clickthroughs. I have to query both tables, so that I can get clickthrough dates, hits, and then query affiliate sales table to get number of orders for each date. I want to display the results like this: DATE | TOTAL HITS | TOTAL SALES 05/03/2003 6 1 05/04/2003 7 0 I've managed to get dates and total hits by using the following query: SELECT DATE_FORMAT(affiliate_clientdate, '%M %D, %Y') AS date, COUNT(*) AS hits FROM affiliate_clickthroughs WHERE affiliate_id = '111' AND affiliate_stores_id = '123' AND MONTH(affiliate_clientdate) = '06' AND YEAR(affiliate_clientdate) = '2003' GROUP BY date; But then I can't get the total number of sales on affiliate sales table. Any suggestions? Your help is greatly appreciated. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] count up from 7
Richard -- ...and then Richard Whitney said... % % Braindead! Hey, who are you callin' names?!? % % I need to evaluate a number if greater than 7 and add a value per increment. Greater than seven is easy. Adding a value is easy. Per increment can be a little tricky but we'll assume for the moment that the window is 1. The code $sum = 67 ; # starting value $trg = 7 ;# trigger value $add = 6 ;# what we add each time $num = 10 ; # what we're evaluating (hardcoded here!) for ( $num ; $trg $num ; $trg++ ) { $sum += $add ; } print when num = $num, sum = $sum\n ; should do for a starting point, though, and produces bash-2.05a$ php -q /tmp/inc.php when num = 10, sum = 85 bash-2.05a$ php -q /tmp/inc.php when num = 20, sum = 145 bash-2.05a$ php -q /tmp/inc.php when num = 7, sum = 67 bash-2.05a$ php -q /tmp/inc.php when num = 1, sum = 67 when run. Extra credit if you write it as a recursive function :-) % I'm frazzled and can't think straight. You can't ask straight, either :-) Give us more details! % % Thanks Guys! HTH HAND :-D -- David T-G * There is too much animal courage in (play) [EMAIL PROTECTED] * society and not sufficient moral courage. (work) [EMAIL PROTECTED] -- Mary Baker Eddy, Science and Health http://justpickone.org/davidtg/ Shpx gur Pbzzhavpngvbaf Qrprapl Npg! pgp0.pgp Description: PGP signature
[PHP] count up from 7
Braindead! I need to evaluate a number if greater than 7 and add a value per increment. I'm frazzled and can't think straight. Thanks Guys! -- Richard Whitney * Transcend Development Producing the next phase of your internet presence. [EMAIL PROTECTED] * http://xend.net* 602-971-2791 * * * * * *__** _/ \___ * * / *\** */ * * \ **/\_ |\ / \_ / \ / \/\ / \ /\ / \ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] count up from 7
Huh? Not really following what you want to achieve, but: ? $n = 9; $floor = 7; if($n $floor) { $diff = $n - $floor; //eg 2 // no idea what you want to do from here } ? on 25/03/03 10:58 AM, Richard Whitney ([EMAIL PROTECTED]) wrote: Braindead! I need to evaluate a number if greater than 7 and add a value per increment. I'm frazzled and can't think straight. Thanks Guys! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] count up from 7
not sure if this is what you're looking for: if ( $var 7 ) { // do stuff } - cheers, Sebastian - Original Message - From: Richard Whitney [EMAIL PROTECTED] | Braindead! | | I need to evaluate a number if greater than 7 and add a value per increment. | I'm frazzled and can't think straight. | | Thanks Guys! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] count characters
I want to count characters without space character. $char=hello world so the result should be 10. Can anybody help me? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] count characters
On Sun, 12 Jan 2003 [EMAIL PROTECTED] wrote: I want to count characters without space character. $char=hello world so the result should be 10. Can anybody help me? $char=hello world; $temp=str_replace( , , $char); echo strlen($temp); should give you 10. RDB -- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] count characters
Try the following code, please note you may want to trim() the string as well if it is coming from a user. ?php $char = 'hello world'; $number_characters = strlen(str_replace(' ', '', $char')); ? Jason On Sun, 2003-01-12 at 10:32, [EMAIL PROTECTED] wrote: I want to count characters without space character. $char=hello world so the result should be 10. Can anybody help me? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] count characters without space
who knowes how to count the charcaters in a string without the space character? thanks harry -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] count characters without space
On Saturday 11 January 2003 08:51 pm, [EMAIL PROTECTED] wrote: who knowes how to count the charcaters in a string without the space character? you need to be more clear. number of characters in the string except space? number of distinct characters in the string except space? include other non-space whitespace (\t \n \r etc)? case sensitive? this is really not hard at all and would be educational for you to write yourself, as an exercise in learning php. one approach: 1. create an empty string, charsfound. for each character in your input string, see if the character already exists in charsfound (use strchr or strpos). if it's a space, continue the loop (don't exec the code below). if it's already there, skip to the next character. if it's not yet there, increment a counter, add the character to charsfound and continue the loop. at the end of the input string exit the loop. the counter is the number of distinct non-space characters in the input string. putting the increment a counter elsewhere (but in the appropriate places) in the loop will give you the number of non-space (not distinct) characters in the input string. tiger -- Gerald Timothy Quimpo tiger*quimpo*org gquimpo*sni-inc.com tiger*sni*ph Public Key: gpg --keyserver pgp.mit.edu --recv-keys 672F4C78 Veritas liberabit vos. Doveryai no proveryai. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] count characters without space
who knowes how to count the charcaters in a string without the space character? Using a regular expression to strip out all the spaces before passing to the word counting function seems easiest. You'll be left to adapt it to PHP, but the regexp is: 's/ //g' That removes all spaces. If it gives an error, try s/\ //g to escape the space. Pass the result to whatever you use to count the number of characters. -Jeff SIG: HUP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Count lines from php files
How can I count how many code lines I have written? I have many php files in one directory. I'm using linux.Do you know any non-php way to count the lines. thanks, antti -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Count in PHP
why not: select count(*) as counter from table - Original Message - From: Tyler Longren [EMAIL PROTECTED] To: Chuck PUP Payne [EMAIL PROTECTED] Cc: [EMAIL PROTECTED] Sent: Tuesday, September 10, 2002 1:02 PM Subject: Re: [PHP] Count in PHP $sql = mysql_query(SELECT * FROM table ORDER BY whatever); echo(mysql_num_rows($sql)); that should do it. tyler On Tue, 10 Sep 2002 00:58:26 -0400 Chuck \PUP\ Payne [EMAIL PROTECTED] wrote: I am wanting to do a count in PHP. I want to be able to count the number of records from a given database and display this count on the page. Can this be done using PHP or is the sql thing? Chuck Payne -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Count in PHP
I am wanting to do a count in PHP. I want to be able to count the number of records from a given database and display this count on the page. Can this be done using PHP or is the sql thing? Chuck Payne -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Count in PHP
it's best done in sql using select count(*) but can be done in php by looping through the result set -Original Message- From: Chuck PUP Payne [mailto:[EMAIL PROTECTED]] Sent: Tuesday, September 10, 2002 2:58 PM To: PHP General Subject: [PHP] Count in PHP I am wanting to do a count in PHP. I want to be able to count the number of records from a given database and display this count on the page. Can this be done using PHP or is the sql thing? Chuck Payne -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Count in PHP
$sql = mysql_query(SELECT * FROM table ORDER BY whatever); echo(mysql_num_rows($sql)); that should do it. tyler On Tue, 10 Sep 2002 00:58:26 -0400 Chuck \PUP\ Payne [EMAIL PROTECTED] wrote: I am wanting to do a count in PHP. I want to be able to count the number of records from a given database and display this count on the page. Can this be done using PHP or is the sql thing? Chuck Payne -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] count errors
I have a function that resets the count of fields with certain values when called. 99.9 percent of the time it works fine. However occaisionally it wont update the database. Even after breaking the selects up it still fails sometimes. If anyone has an idea of why I would really appreciate hearing it. The code is below. Thanks, Julian ? //the selects had to be broken up to get it to work consistently function resetCounter() { include(../config.php); $query = select categories from categories; $result = mysql_db_query($db, $query); while($r = mysql_fetch_array($result)) { $cats = $r[categories]; // $active = $r[active]; // echo$catsBR; $query1 = select count(*) from recipes where category1 = '$cats' and active = 'yes'; // echo mysql_errno().: .mysql_error(). 1BR; // uncomment to troubleshoot db problems $result2 = mysql_db_query($db, $query1); $rows1 = mysql_fetch_row($result2); $query2 = select count(*) from recipes where category2 = '$cats' and active = 'yes'; // echo mysql_errno().: .mysql_error(). 1BR; // uncomment to troubleshoot db problems $result3 = mysql_db_query($db, $query2); //echo mysql_errno().: .mysql_error(). 2BR; // uncomment to troubleshoot db problems $rows2 = mysql_fetch_row($result3); $query3 = select count(*) from recipes where category3 = '$cats' and active = 'yes'; // echo mysql_errno().: .mysql_error(). 1BR; // uncomment to troubleshoot db problems $result4 = mysql_db_query($db, $query3); // echo mysql_errno().: .mysql_error(). 2BR; // uncomment to troubleshoot db problems $rows3 = mysql_fetch_row($result4); // echo mysql_errno().: .mysql_error(). 3BR; // uncomment to troubleshoot db problems $therows = $rows1[0] + $rows2[0] + $rows3[0]; $query4 = UPDATE categories SET count = '$therows' where(categories = '$cats'); $result5 = mysql_db_query($db, $query4); // echo$therows --tr $rows1[0] --r1 $rows2[0] --r2 $rows3[0] --r3 $cats -- catsbr; } } ? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Count number of rows in table.
And do I need to use any other functions like mysql_fetch_row() ? Martín marqués [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED]... On Sáb 24 Ago 2002 11:19, Tony Harrison wrote: How would I count the number of rows in a mysql table with a WHERE clause? select count(*) from table_name WHERE . Saludos... :-) -- Porqué usar una base de datos relacional cualquiera, si podés usar PostgreSQL? - Martín Marqués |[EMAIL PROTECTED] Programador, Administrador, DBA | Centro de Telematica Universidad Nacional del Litoral - -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Count number of rows in table.
On Sáb 24 Ago 2002 11:19, Tony Harrison wrote: How would I count the number of rows in a mysql table with a WHERE clause? Martín marqués [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED]... select count(*) from table_name WHERE . From: Tony Harrison [EMAIL PROTECTED] Sent: Saturday, August 24, 2002 5:54 PM Subject: Re: [PHP] Count number of rows in table. And do I need to use any other functions like mysql_fetch_row() ? Yes. It's a regular sql statement, so you must fetch the results. $sql = select count(*) from table_name WHERE .; $result = mysql_query($sql) or die(mysql_error); $row = mysql_fetch_row($result); $rowCount=$row[0]; -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Count number of rows in table.
$sql = select count(*) from table_name WHERE .; $result = mysql_query($sql) or die(mysql_error); $row = mysql_fetch_row($result); $rowCount=$row[0]; or $sql = select count(*) from table_name WHERE .; $result = mysql_query($sql) or die(mysql_error); $rowCount=mysql_result($result); -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Count number of rows in table.
or, you should be able to simply do this, without the cost of fetching the results: $result = mysql_query(SELECT count(*) FROM table WHERE); $num_rows = mysql_num_rows($result); echo $num_rows Rows\n; $sql = select count(*) from table_name WHERE .; $result = mysql_query($sql) or die(mysql_error); $row = mysql_fetch_row($result); $rowCount=$row[0]; or $sql = select count(*) from table_name WHERE .; $result = mysql_query($sql) or die(mysql_error); $rowCount=mysql_result($result); -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php