Re: [PHP] Counting Online users, but not using a Session Table in MySQL

2011-01-28 Thread Fernando
How did you created the table?  Can you count the Id's only?  Wouldn't 
this just count the entries in the index?


On 28/01/2011 12:57, AmirBehzad Eslami wrote:

Dear list,

The common solution for counting online users is to store sessions in a Table.
I've created a Table in MySQL to acheive the result, but it seems this solution
is a little heavy for such a simple task.

Is there a better alternative? Can I use SqlLite to make COUNT(*) queries based
on a where statement?

Is there any better solution? Do you know any way to store sessions in Memory?

Does Memcache support counting sessions?

I'm looking for a high-performance solution to count online users.
What do you think?



Re: [PHP] Counting Online users, but not using a Session Table in MySQL

2011-01-28 Thread Jim Lucas
On 1/28/2011 9:57 AM, AmirBehzad Eslami wrote:
 Dear list,
 
 The common solution for counting online users is to store sessions in a Table.
 I've created a Table in MySQL to acheive the result, but it seems this 
 solution
 is a little heavy for such a simple task.
 
 Is there a better alternative? Can I use SqlLite to make COUNT(*) queries 
 based
 on a where statement?
 
 Is there any better solution? Do you know any way to store sessions in Memory?
 
 Does Memcache support counting sessions?
 
 I'm looking for a high-performance solution to count online users.
 What do you think?
 

The simple way that we do it is to have users table have a boolean field that
gets updated when a person logs in, then a time field showing when they last
visited.  These together should give you the information that you are looking 
for.

This gives you the idea...

SELECT  COUNT(*)
FROMusers_table
WHERE   logged_in = 't'
AND last_activity  FROM_UNIXTIME( UNIX_TIMESTAMP() - 1800 )

Jim Lucas

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Counting Online users, but not using a Session Table in MySQL

2011-01-28 Thread AmirBehzad Eslami
Jim, I'm already using the solution you mentioned.

The problem is about the performance.
One solution is to increase the performance by using Memcached.

But counting online users always requires a __new__fresh__ COUNT(*) query, even
under Memcahched. Since the COUNT(*) result is very dynamic and could
change every second, it should not be cached.

I'm looking for a faster way to count online users.
COUNT(*) is time consuming under MySQL.

Any light-weight solutions?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Counting Online users, but not using a Session Table in MySQL

2011-01-28 Thread David Harkness
If you're using memcached already you could store the number in it and
update it only when a user logs in/out. If no one is logging in/out, the
number isn't changing.

If your site is so popular that hundreds of users are logging in every
second you might want to change the logic so that the process to update the
count only does so when it has expired from the cache and give it a short
expiry time. At that point you probably would want each machine to track its
own number to avoid frequent distribution costs.

In the end I think you're going to have to get comfortable with the idea
that the number will never be exact. If you want exact, you have to pay for
it.

David


Re: [PHP] Counting Online users, but not using a Session Table in MySQL

2011-01-28 Thread Marc Guay
 I'm looking for a faster way to count online users. COUNT(*) is time 
 consuming under MySQL.

If COUNTing is the heavy part, why not create a 'users_logged_in'
field somewhere and increment it when someone logs in and decrement it
when someone logs out?  Then your query is just a straight SELECT.
Just chucking a thought out there.

Marc

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Counting Online users, but not using a Session Table in MySQL

2011-01-28 Thread David Harkness
On Fri, Jan 28, 2011 at 11:03 AM, Marc Guay marc.g...@gmail.com wrote:

 If COUNTing is the heavy part, why not create a 'users_logged_in'
 field somewhere and increment it when someone logs in and decrement it
 when someone logs out?  Then your query is just a straight SELECT.


If this is like most web sites, users don't usually log out--they just stop
requesting pages. With no logout event to decrement the count you need some
other way to stop counting a particular user such as 30 minutes after they
logged in. This is easily handled in a query on the users table, but it
causes performance issues.

David


Re: [PHP] Counting Online users, but not using a Session Table in MySQL

2011-01-28 Thread Jim Lucas
On 1/28/2011 11:23 AM, David Harkness wrote:
 On Fri, Jan 28, 2011 at 11:03 AM, Marc Guay marc.g...@gmail.com wrote:
 
 If COUNTing is the heavy part, why not create a 'users_logged_in'
 field somewhere and increment it when someone logs in and decrement it
 when someone logs out?  Then your query is just a straight SELECT.
 
 
 If this is like most web sites, users don't usually log out--they just stop
 requesting pages. With no logout event to decrement the count you need some
 other way to stop counting a particular user such as 30 minutes after they
 logged in. This is easily handled in a query on the users table, but it
 causes performance issues.
 
 David
 

cron it.

Setup a cron script that runs every 5 minutes.

A seconds work every 5 minutes isn't too much is it?

Jim Lucas

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Counting Occurrences within an If statement

2008-07-20 Thread Micah Gersten
If it's a simple x  y, you might want to consider putting your if
statement in the SQL query so you don't return so many rows.  To make a
counter, just set a variable like:
$counter = 0;

if ($x $y)
{
print $row;
$counter++
}

Thank you,
Micah Gersten
onShore Networks
Internal Developer
http://www.onshore.com



Mark Bomgardner wrote:
 I have been working on the something for the last day and can't figure it
 out.

  

 I am pulling 7500 rows from a MySQL database, but I only want to display a
 certain numbers of rows based up an if statement.  I want to count the rows
 within the if statement.

  

 mysql query result

  

 if(x  y){

 display a row in a table

 }else{

 Don't display anything

 }

  

 Every time a row is displayed, I want to add one to a counter

  

 markb


   

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting with leading zeros

2007-10-01 Thread Martin Alterisio
// glob returns an ordered list (rtfm: man glob)
// relies on the use of the operator ++ to generate string sequences.

function lastFileInSequence($path, $prefix, $first, $suffix) {
return array_pop(glob($path . / . $prefix . str_repeat('?',
strlen($first)) . $suffix));
}

function nextFileInSequence($path, $prefix, $first, $suffix) {
if ($lastFile = basename(lastFileInSequence($path, $prefix, $first,
$suffix), $suffix)) {
return ++$lastFile . $suffix;
} else {
return $prefix . $first . $suffix;
}
}

$nextImage = $path . / . nextFileInSequence($path, foo_, 01, .jpg);

2007/9/26, brian [EMAIL PROTECTED]:

 I have a directory that contains many images (no, not pr0n,
 unfortunately) and i'm working on an admin script for adding to it. I've
 got something that works alright but i'm wondering if there's a Better
 Way.

 Each image is named like: foo_01.jpg, foo_02.jpg, bar_01.jpg, and so on.
 When adding a new image it should be assigned the next number in the
 series (the prefix is known). Thus, given the prefix 'bar' i do
 something like:

 function getNextImage($path, $prefix)
 {
$pattern = /^${prefix}_([0-9]{2})\.[a-z]{3}$/;

$filenames = glob(${path}${prefix}*);

if (is_array($filenames)  sizeof($filenames))
{
  sort($filenames);

  /* eg. 'foo_32.jpg'
   */
  $last = basename(array_pop($filenames));

  /* pull the number from the filename
   */
  $count = intval(preg_replace($pattern, '$1', $last));

  /* increment, format with leading zero again if necessary,
   * and return it
   */
  return sprintf('%02d', ++$count)
}
else
{
  return '01';
}
 }

 Note that there almost certainly will never be more than 99 images to a
 series. In any case, i don't care about there being more than one
 leading zero. One is what i want.

 brian

 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] counting with leading zeros

2007-09-30 Thread Robert Cummings
On Sat, 2007-09-29 at 23:47 -0400, brian wrote:

 It's not so much that i disrespect Rob. It's just that i felt i had to 
 call him out for being a dickhead by taking my statement out of context.

Ok, so I'm a dickhead. What does that make you? A vagina?

Name calling really is the lowest form of argumentation. It was fun when
I was 7. So were swear words.

Cheers,
Rob.
-- 
...
SwarmBuy.com - http://www.swarmbuy.com

Leveraging the buying power of the masses!
...

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting with leading zeros

2007-09-30 Thread Jeffery Fernandez
gee you guys behave like kids. don't you all have other things to do than 
whinge, swear  and blame each other on the list? Take it elsewhere and sort 
it out. This is bad for the PHP community in general. It doesn't reflect what 
the community is about.

my 2c.

On Sunday 30 September 2007 13:47, brian wrote:
 tedd wrote:
  At 8:30 PM -0400 9/27/07, brian wrote:
  I wasn't bitching! And i *thought* that my numerous attempts at
  explaining such would have been enough. Obviously not, but wtf can you
  do with people who seem to want only to stir up shit?
 
  It sure sounded like you were bitching.

 Gee, thanks for pointing that out, Jeeves. I'd say it's pretty damned
 obvious now that Rob thought i was bitching about his response to my
 query. However, if you'd have bothered to note my replies since, you
 might have twigged to the fact that i'd been trying to set that
 straight. But you just *had* to send off your own dickish response.

  And, your off-list fuck you, too, asswipe to me certainly seems to
  support that -- don't you think?

 How so? Had it crossed your little mind that maybe--just maybe--i was
 becoming not a little frustrated with the fact that my reply had been
 taken the wrong way? That, instead of accepting that and moving on, this
 had become far too OT with bullshit comments such as yours? So, yeah,
 you might say that i'm bitching *now*.

 You and i have had our run-ins on this list on more than one occasion.
 Until now, i've chosen to take your crap in stride. If you have nothing
 constructive to add then STFU.

  Look, there's nothing you can say to me that hasn't been said before
  (some of it deserving), so my advice is for you to refrain from such
  conduct and at least try to look professional. That way you'll hide your
  ignorance a little longer -- it's always worked for me.

 This is me laughing. I'm sure you have no bloody idea how funny that
 statement looks from where i'm sitting. See the paragraph above.

  Dan Parry wrote:
  Can I please interject and say that I vastly respect Tedd(ddd) and
  Rob(bb?) and their opinions

 It's not so much that i disrespect Rob. It's just that i felt i had to
 call him out for being a dickhead by taking my statement out of context.

 Tedd ... not so very much at all.

 Robert Cummings wrote:
   On Fri, 2007-09-28 at 12:05 -0400, tedd wrote:
  At 10:39 PM -0400 9/26/07, brian wrote:
  
  fuck you, too, asswipe
  
   Seems to be, an extra, comma, in the phrase. It reads, like Captain
   Kirk, saying, it.

 Funny. Reminds of a Capt. Kirk quote:
 No more blah, blah, blah!

 brian

-- 
Powered by openSUSE 10.2 (i586) Kernel: 2.6.18.8-0.5-default
KDE: 3.5.5 release 45.4
 10:55pm  up 10 days  2:22,  3 users,  load average: 0.13, 0.18, 0.17

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting with leading zeros

2007-09-30 Thread tedd

At 11:47 PM -0400 9/29/07, brian wrote:
You and i have had our run-ins on this list on more than one 
occasion. Until now, i've chosen to take your crap in stride.


brian

I haven't noticed any run-ins. I don't care if you've been the 
biggest prick on the net -- if you ask a question and it's within my 
scope to answer, I'll try to answer -- that's what this list is 
about. I seldom remember who said what, nor do I keep score -- I 
haven't got time for that.


As for taking crap, that's your perception for I don't dish any 
out. If you take offense to anything I say, then say so and I'll 
explain myself -- I'm not here to insult anyone. I haven't got time 
for that either.



 If you have nothing constructive to add then STFU.


If I do shut up, or not, will be my decision and not your's. You seem 
to have an attitude that the net is yours and our time is here for 
you to use and abuse at your will. Clearly, you have some unrealized 
issues -- but, that's your problem to work out.


However, when I see someone try to help another and see that it's not 
appreciated, then I can make comment if I want because it IS 
constructive to point out civility on the net. Telling someone to 
fuck-off or calling someone a dick-head is certainly not constructive 
and degrades the professionalism of the list. That should be obvious.


If you want to be treated fairly, then treat others that way. Don't 
always assume that you're right and everyone else is wrong and thus 
you have liberty to call people names because that doesn't promote an 
exchange of information, which is what this list is about. It's 
clear, at least to me, that you have problems with that as well.


So, why don't we let this thread drop -- you called everyone names 
and used whatever language you thought appropriate and I hope you 
feel better now. Hopefully next time, we can all be more civil to 
each other.


Cheers,

tedd



Look, there's nothing you can say to me that hasn't been said 
before (some of it deserving), so my advice is for you to refrain 
from such conduct and at least try to look professional. That way 
you'll hide your ignorance a little longer -- it's always worked 
for me.


This is me laughing. I'm sure you have no bloody idea how funny that 
statement looks from where i'm sitting. See the paragraph above.



Dan Parry wrote:

Can I please interject and say that I vastly respect Tedd(ddd) and Rob(bb?)
and their opinions



It's not so much that i disrespect Rob. It's just that i felt i had 
to call him out for being a dickhead by taking my statement out of 
context.


Tedd ... not so very much at all.

Robert Cummings wrote:

 On Fri, 2007-09-28 at 12:05 -0400, tedd wrote:


At 10:39 PM -0400 9/26/07, brian wrote:


fuck you, too, asswipe


 Seems to be, an extra, comma, in the phrase. It reads, like Captain
 Kirk, saying, it.



Funny. Reminds of a Capt. Kirk quote:
No more blah, blah, blah!

brian

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



--
---
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



Re: [PHP] counting with leading zeros

2007-09-29 Thread brian

tedd wrote:

At 8:30 PM -0400 9/27/07, brian wrote:


I wasn't bitching! And i *thought* that my numerous attempts at
explaining such would have been enough. Obviously not, but wtf can you
do with people who seem to want only to stir up shit?


It sure sounded like you were bitching.


Gee, thanks for pointing that out, Jeeves. I'd say it's pretty damned 
obvious now that Rob thought i was bitching about his response to my 
query. However, if you'd have bothered to note my replies since, you 
might have twigged to the fact that i'd been trying to set that 
straight. But you just *had* to send off your own dickish response.


And, your off-list fuck you, too, asswipe to me certainly seems to 
support that -- don't you think?


How so? Had it crossed your little mind that maybe--just maybe--i was 
becoming not a little frustrated with the fact that my reply had been 
taken the wrong way? That, instead of accepting that and moving on, this 
had become far too OT with bullshit comments such as yours? So, yeah, 
you might say that i'm bitching *now*.


You and i have had our run-ins on this list on more than one occasion. 
Until now, i've chosen to take your crap in stride. If you have nothing 
constructive to add then STFU.


Look, there's nothing you can say to me that hasn't been said before 
(some of it deserving), so my advice is for you to refrain from such 
conduct and at least try to look professional. That way you'll hide your 
ignorance a little longer -- it's always worked for me.


This is me laughing. I'm sure you have no bloody idea how funny that 
statement looks from where i'm sitting. See the paragraph above.



Dan Parry wrote:

Can I please interject and say that I vastly respect Tedd(ddd) and Rob(bb?)
and their opinions



It's not so much that i disrespect Rob. It's just that i felt i had to 
call him out for being a dickhead by taking my statement out of context.


Tedd ... not so very much at all.

Robert Cummings wrote:
 On Fri, 2007-09-28 at 12:05 -0400, tedd wrote:

At 10:39 PM -0400 9/26/07, brian wrote:

fuck you, too, asswipe

 Seems to be, an extra, comma, in the phrase. It reads, like Captain
 Kirk, saying, it.


Funny. Reminds of a Capt. Kirk quote:
No more blah, blah, blah!

brian

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting with leading zeros

2007-09-28 Thread tedd

At 8:30 PM -0400 9/27/07, brian wrote:

I wasn't bitching! And i *thought* that my numerous attempts at
explaining such would have been enough. Obviously not, but wtf can you
do with people who seem to want only to stir up shit?


It sure sounded like you were bitching.

And, your off-list fuck you, too, asswipe to me certainly seems to 
support that -- don't you think?


Look, there's nothing you can say to me that hasn't been said before 
(some of it deserving), so my advice is for you to refrain from such 
conduct and at least try to look professional. That way you'll hide 
your ignorance a little longer -- it's always worked for me.


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



RE: [PHP] counting with leading zeros

2007-09-28 Thread Dan Parry
Can I please interject and say that I vastly respect Tedd(ddd) and Rob(bb?)
and their opinions

Dsn

-- 
Dan Parry
Senior Developer
Virtua Webtech Ltd
 
Company Number: 5078356 
Vat Number: 827044536 
 
[EMAIL PROTECTED]
www.virtuawebtech.co.uk

01745 354924


 -Original Message-
 From: tedd [mailto:[EMAIL PROTECTED]
 Sent: 28 September 2007 17:20
 To: brian; php-general@lists.php.net
 Subject: Re: [PHP] counting with leading zeros
 
 At 8:30 PM -0400 9/27/07, brian wrote:
 I wasn't bitching! And i *thought* that my numerous attempts at
 explaining such would have been enough. Obviously not, but wtf can you
 do with people who seem to want only to stir up shit?
 
 It sure sounded like you were bitching.
 
 And, your off-list fuck you, too, asswipe to me certainly seems to
 support that -- don't you think?
 
 Look, there's nothing you can say to me that hasn't been said before
 (some of it deserving), so my advice is for you to refrain from such
 conduct and at least try to look professional. That way you'll hide
 your ignorance a little longer -- it's always worked for me.
 
 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
 
 
 
 --
 No virus found in this incoming message.
 Checked by AVG Free Edition.
 Version: 7.5.488 / Virus Database: 269.13.33/1034 - Release Date:
 27/09/2007 17:00

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting with leading zeros

2007-09-27 Thread Robert Cummings
On Wed, 2007-09-26 at 21:56 -0700, Jim Lucas wrote:

 Now, if you plan to delete any images in the list, this will not work.
 
 But if you don't expect to delete any images in the list and always want 
 the ($total_images + 1), then follow along.
 
 ?php
 
 function getNextImage($path, $prefix) {
$filelist = glob(${path}${prefix}*) or return '01';
return str_pad(((int)count($filelist)+1), 2, '0', STR_PAD_LEFT);
 }
 
 ?

Nice :)

Cheers,
Rob.
-- 
...
SwarmBuy.com - http://www.swarmbuy.com

Leveraging the buying power of the masses!
...

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting with leading zeros

2007-09-27 Thread tedd



 Robert Cummings wrote:

Suit yourself. But better programmer's don't just wave their hands in
the air and hope for the best.

Ah crap -- there's another technique I have to cross off my list.

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



Re: [PHP] counting with leading zeros

2007-09-27 Thread tedd

At 3:58 PM -0400 9/26/07, brian wrote:
Well, this is almost precisely the same thing i have, save for using 
POSIX character classes, str_pad instead of sprintf(), and 
incrementing elsewhere. What i was really wondering is if there was 
a *much simpler* way to do this, not just re-arranging things.



Rob just gave you code that works better than what you did and you're 
bitching about it?


Look, none of us are on your payroll so appreciate what you're given.

My advice, says thanks and if the code doesn't meet your expectations 
then learn how to better phrase your question next time.


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



Re: [PHP] counting with leading zeros

2007-09-27 Thread tedd

At 10:39 PM -0400 9/26/07, brian wrote:

Now you're obviously just trying to be a dickhead.


While someone is, it's not Rob.

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



Re: [PHP] counting with leading zeros

2007-09-27 Thread brian

tedd wrote:

At 3:58 PM -0400 9/26/07, brian wrote:

Well, this is almost precisely the same thing i have, save for 
using POSIX character classes, str_pad instead of sprintf(), and 
incrementing elsewhere. What i was really wondering is if there was

 a *much simpler* way to do this, not just re-arranging things.


Rob just gave you code that works better than what you did and you're
 bitching about it?

Look, none of us are on your payroll so appreciate what you're given.

My advice, says thanks and if the code doesn't meet your expectations
 then learn how to better phrase your question next time.

tedd



I wasn't bitching! And i *thought* that my numerous attempts at
explaining such would have been enough. Obviously not, but wtf can you
do with people who seem to want only to stir up shit?

Jim Lucas wrote:

Well, I didn't want to get into the middle of this cat fight, but now
 that the talks have completely gotten off topic.  I will
interject...



Perfect timing, then.


Here is a function that is a bit simpler then either of yours.

Now, if you plan to delete any images in the list, this will not 
work.


There's a daemon that locks the dir, zips up the contents, moves it
elsewhere, then removes everything at once.


?php

function getNextImage($path, $prefix) { $filelist = 
glob(${path}${prefix}*) or return '01'; return 
str_pad(((int)count($filelist)+1), 2, '0', STR_PAD_LEFT); }


?


Sweet! Thanks much!

brian

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting with leading zeros

2007-09-26 Thread Robert Cummings
On Wed, 2007-09-26 at 15:11 -0400, brian wrote:
 I have a directory that contains many images (no, not pr0n, 
 unfortunately) and i'm working on an admin script for adding to it. I've 
 got something that works alright but i'm wondering if there's a Better Way.
 
 Each image is named like: foo_01.jpg, foo_02.jpg, bar_01.jpg, and so on. 
 When adding a new image it should be assigned the next number in the 
 series (the prefix is known). Thus, given the prefix 'bar' i do 
 something like:
 
 function getNextImage($path, $prefix)
 {
$pattern = /^${prefix}_([0-9]{2})\.[a-z]{3}$/;
 
$filenames = glob(${path}${prefix}*);
   
if (is_array($filenames)  sizeof($filenames))
{
  sort($filenames);
 
  /* eg. 'foo_32.jpg'
   */
  $last = basename(array_pop($filenames));
 
  /* pull the number from the filename
   */
  $count = intval(preg_replace($pattern, '$1', $last));
 
  /* increment, format with leading zero again if necessary,
   * and return it
   */
  return sprintf('%02d', ++$count)
}
else
{
  return '01';
}
 }
 
 Note that there almost certainly will never be more than 99 images to a 
 series. In any case, i don't care about there being more than one 
 leading zero. One is what i want.

?php

function getNextImageIndex( $path, $prefix )
{
//
// Matches files like foo_123.png
//
$pattern = '/.*_([[:digit:]]+)\.[[:alpha:]]{3}$/';

$filenames = glob( ${path}${prefix}* );

if( $filenames )
{
sort( $filenames );

$last = array_pop( $filenames );
$nextIndex = (int)preg_replace( $pattern, '$1', $last ) + 1;

return str_pad( $nextIndex, 2, '0', STR_PAD_LEFT );
}

return '01';
}

?

Note that the moment you get 100 images the code breaks because _100
sorts before _99 and so you will always write over the 100th index. To
fix this you need to loop through all found files and pull the index out
and record the highest found.

Cheers,
Rob.
-- 
...
SwarmBuy.com - http://www.swarmbuy.com

Leveraging the buying power of the masses!
...

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting with leading zeros

2007-09-26 Thread brian

Robert Cummings wrote:

On Wed, 2007-09-26 at 15:11 -0400, brian wrote:

I have a directory that contains many images (no, not pr0n, 
unfortunately) and i'm working on an admin script for adding to it. I've 
got something that works alright but i'm wondering if there's a Better Way.


Each image is named like: foo_01.jpg, foo_02.jpg, bar_01.jpg, and so on. 
When adding a new image it should be assigned the next number in the 
series (the prefix is known). Thus, given the prefix 'bar' i do 
something like:


function getNextImage($path, $prefix)
{
  $pattern = /^${prefix}_([0-9]{2})\.[a-z]{3}$/;

  $filenames = glob(${path}${prefix}*);

  if (is_array($filenames)  sizeof($filenames))
  {
sort($filenames);

/* eg. 'foo_32.jpg'
 */
$last = basename(array_pop($filenames));

/* pull the number from the filename
 */
$count = intval(preg_replace($pattern, '$1', $last));

/* increment, format with leading zero again if necessary,
 * and return it
 */
return sprintf('%02d', ++$count)
  }
  else
  {
return '01';
  }
}

Note that there almost certainly will never be more than 99 images to a 
series. In any case, i don't care about there being more than one 
leading zero. One is what i want.



?php

function getNextImageIndex( $path, $prefix )
{
//
// Matches files like foo_123.png
//
$pattern = '/.*_([[:digit:]]+)\.[[:alpha:]]{3}$/';

$filenames = glob( ${path}${prefix}* );

if( $filenames )

{
sort( $filenames );

$last = array_pop( $filenames );



It needs basename here because the array has the full path of each file.


$nextIndex = (int)preg_replace( $pattern, '$1', $last ) + 1;

return str_pad( $nextIndex, 2, '0', STR_PAD_LEFT );
}

return '01';
}

?



Well, this is almost precisely the same thing i have, save for using 
POSIX character classes, str_pad instead of sprintf(), and incrementing 
elsewhere. What i was really wondering is if there was a *much simpler* 
way to do this, not just re-arranging things.



Note that the moment you get 100 images the code breaks because _100
sorts before _99 and so you will always write over the 100th index. To
fix this you need to loop through all found files and pull the index out
and record the highest found.


For my purposes, if any series gets much beyond 40 i'll have bigger 
problems to worry about. I'm content to not worry about overflow in this 
case.


brian

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting with leading zeros

2007-09-26 Thread Robert Cummings
On Wed, 2007-09-26 at 15:58 -0400, brian wrote:
 Robert Cummings wrote:
  On Wed, 2007-09-26 at 15:11 -0400, brian wrote:
  
 I have a directory that contains many images (no, not pr0n, 
 unfortunately) and i'm working on an admin script for adding to it. I've 
 got something that works alright but i'm wondering if there's a Better Way.
 
 Each image is named like: foo_01.jpg, foo_02.jpg, bar_01.jpg, and so on. 
 When adding a new image it should be assigned the next number in the 
 series (the prefix is known). Thus, given the prefix 'bar' i do 
 something like:
 
 function getNextImage($path, $prefix)
 {
$pattern = /^${prefix}_([0-9]{2})\.[a-z]{3}$/;
 
$filenames = glob(${path}${prefix}*);
 
if (is_array($filenames)  sizeof($filenames))
{
  sort($filenames);
 
  /* eg. 'foo_32.jpg'
   */
  $last = basename(array_pop($filenames));
 
  /* pull the number from the filename
   */
  $count = intval(preg_replace($pattern, '$1', $last));
 
  /* increment, format with leading zero again if necessary,
   * and return it
   */
  return sprintf('%02d', ++$count)
}
else
{
  return '01';
}
 }
 
 Note that there almost certainly will never be more than 99 images to a 
 series. In any case, i don't care about there being more than one 
 leading zero. One is what i want.
  
  
  ?php
  
  function getNextImageIndex( $path, $prefix )
  {
  //
  // Matches files like foo_123.png
  //
  $pattern = '/.*_([[:digit:]]+)\.[[:alpha:]]{3}$/';
  
  $filenames = glob( ${path}${prefix}* );
  
  if( $filenames )
  {
  sort( $filenames );
  
  $last = array_pop( $filenames );
 
 
 It needs basename here because the array has the full path of each file.

No it doesn't. I changed the pattern to make it simpler.


  $nextIndex = (int)preg_replace( $pattern, '$1', $last ) + 1;
  
  return str_pad( $nextIndex, 2, '0', STR_PAD_LEFT );
  }
  
  return '01';
  }
  
  ?
 
 
 Well, this is almost precisely the same thing i have, save for using 
 POSIX character classes, str_pad instead of sprintf(), and incrementing 
 elsewhere. What i was really wondering is if there was a *much simpler* 
 way to do this, not just re-arranging things.

You asked for a Better Way. I gave you a Better Way. I improved the
regex, removed the useless basename() call, performed incrementation in
the extraction step, used better function and variable names, and used
the str_pad() function because sprintf() is overkill. I also removed
your redundant else clause. My version is much simpler. How much is
much? You know... much! Other than using a database, it doesn't really
get any simpler. Although you obviously have race condition issues also.
But that's not getting simpler. It's getting more correct but also a
bit more complex.

  Note that the moment you get 100 images the code breaks because _100
  sorts before _99 and so you will always write over the 100th index. To
  fix this you need to loop through all found files and pull the index out
  and record the highest found.
 
 For my purposes, if any series gets much beyond 40 i'll have bigger 
 problems to worry about. I'm content to not worry about overflow in this 
 case.

Suit yourself. But better programmer's don't just wave their hands in
the air and hope for the best.

Cheers,
Rob.
-- 
...
SwarmBuy.com - http://www.swarmbuy.com

Leveraging the buying power of the masses!
...

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting with leading zeros

2007-09-26 Thread brian

Robert Cummings wrote:

On Wed, 2007-09-26 at 15:58 -0400, brian wrote:


Robert Cummings wrote:


On Wed, 2007-09-26 at 15:11 -0400, brian wrote:


I have a directory that contains many images (no, not pr0n, 
unfortunately) and i'm working on an admin script for adding to

 it. I've got something that works alright but i'm wondering if
 there's a Better Way.

Each image is named like: foo_01.jpg, foo_02.jpg, bar_01.jpg, 
and so on. When adding a new image it should be assigned the 
next number in the series (the prefix is known). Thus, given 
the prefix 'bar' i do something like:


function getNextImage($path, $prefix) { $pattern = 
/^${prefix}_([0-9]{2})\.[a-z]{3}$/;


$filenames = glob(${path}${prefix}*);  if 
(is_array($filenames)  sizeof($filenames)) { 
sort($filenames);


/* eg. 'foo_32.jpg' */ $last = basename(array_pop($filenames));



/* pull the number from the filename */ $count = 
intval(preg_replace($pattern, '$1', $last));


/* increment, format with leading zero again if necessary, * 
and return it */ return sprintf('%02d', ++$count) } else { 
return '01'; } }


Note that there almost certainly will never be more than 99 
images to a series. In any case, i don't care about there being

 more than one leading zero. One is what i want.



?php

function getNextImageIndex( $path, $prefix ) { // // Matches 
files like foo_123.png // $pattern = 
'/.*_([[:digit:]]+)\.[[:alpha:]]{3}$/';


$filenames = glob( ${path}${prefix}* );

if( $filenames ) { sort( $filenames );

$last = array_pop( $filenames );



It needs basename here because the array has the full path of each 
file.



No it doesn't. I changed the pattern to make it simpler.


My bad. I didn't look at it closely enough.






$nextIndex = (int)preg_replace( $pattern, '$1', $last ) + 1;

return str_pad( $nextIndex, 2, '0', STR_PAD_LEFT ); }

return '01'; }

?



Well, this is almost precisely the same thing i have, save for 
using POSIX character classes, str_pad instead of sprintf(), and 
incrementing elsewhere. What i was really wondering is if there was

 a *much simpler* way to do this, not just re-arranging things.



You asked for a Better Way. I gave you a Better Way. I improved 
the regex, removed the useless basename() call, performed 
incrementation in the extraction step, used better function and 
variable names, and used the str_pad() function because sprintf() is 
overkill. I also removed your redundant else clause. My version is 
much simpler. How much is much? You know... much! Other than using a 
database, it doesn't really get any simpler. Although you obviously 
have race condition issues also. But that's not getting simpler. 
It's getting more correct but also a bit more complex.




Bad day or something? Did it really seem that i was bitching about your 
response? If my response to that gave you the impression i was

complaining, i assure you that i wasn't. I was simply suggesting
that i was wondering if there was a *much* simpler way to do this, ie.
without using several functions to process the filename. Yes, your
version *does* have some improvements. I'm not disagreeing with you. 
Perhaps i could have been more forthright in my praise for said 
improvements (i'm even going to use some of them--how about that?!)


Note that the moment you get 100 images the code breaks because 
_100 sorts before _99 and so you will always write over the 100th
 index. To fix this you need to loop through all found files and 
pull the index out and record the highest found.


For my purposes, if any series gets much beyond 40 i'll have bigger
 problems to worry about. I'm content to not worry about overflow 
in this case.



Suit yourself. But better programmer's don't just wave their hands in
 the air and hope for the best.



While i agree with you in principle, i'm not interested in that aspect
of it. The application would have halted *long* before it could reach
that many. This is why i went to the trouble of pointing this out in the 
original mail. IOW, i'm not stupid; i'm well aware that 100 comes after 
99 and that it contains 3 digits, not 2, thanks very much.


b

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting with leading zeros

2007-09-26 Thread Robert Cummings
On Wed, 2007-09-26 at 19:10 -0400, brian wrote:
 Robert Cummings wrote:

  Well, this is almost precisely the same thing i have, save for 
  using POSIX character classes, str_pad instead of sprintf(), and 
  incrementing elsewhere. What i was really wondering is if there was
   a *much simpler* way to do this, not just re-arranging things.
  
  
  You asked for a Better Way. I gave you a Better Way. I improved 
  the regex, removed the useless basename() call, performed 
  incrementation in the extraction step, used better function and 
  variable names, and used the str_pad() function because sprintf() is 
  overkill. I also removed your redundant else clause. My version is 
  much simpler. How much is much? You know... much! Other than using a 
  database, it doesn't really get any simpler. Although you obviously 
  have race condition issues also. But that's not getting simpler. 
  It's getting more correct but also a bit more complex.
  
 
 Bad day or something?

Nope.

 Did it really seem that i was bitching about your 
 response?

Kinda.

 If my response to that gave you the impression i was
 complaining, i assure you that i wasn't. I was simply suggesting
 that i was wondering if there was a *much* simpler way to do this, ie.
 without using several functions to process the filename. Yes, your
 version *does* have some improvements. I'm not disagreeing with you. 
 Perhaps i could have been more forthright in my praise for said 
 improvements (i'm even going to use some of them--how about that?!)

Well, I'm not looking for praise... but asking a concise question goes a
long way towards meeting your desires.

  Note that the moment you get 100 images the code breaks because 
  _100 sorts before _99 and so you will always write over the 100th
   index. To fix this you need to loop through all found files and 
  pull the index out and record the highest found.
  
  For my purposes, if any series gets much beyond 40 i'll have bigger
   problems to worry about. I'm content to not worry about overflow 
  in this case.
 
  Suit yourself. But better programmer's don't just wave their hands in
   the air and hope for the best. 
 
 While i agree with you in principle, i'm not interested in that aspect
 of it. The application would have halted *long* before it could reach
 that many. This is why i went to the trouble of pointing this out in the 
 original mail. IOW, i'm not stupid; i'm well aware that 100 comes after 
 99 and that it contains 3 digits, not 2, thanks very much.

Nope, 100 comes *before* 99 when lexically sorting.

You must be new around here.

Cheers,
Rob.
-- 
...
SwarmBuy.com - http://www.swarmbuy.com

Leveraging the buying power of the masses!
...

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting with leading zeros

2007-09-26 Thread brian

Robert Cummings wrote:

On Wed, 2007-09-26 at 19:10 -0400, brian wrote:


If my response to that gave you the impression i was
complaining, i assure you that i wasn't. I was simply suggesting
that i was wondering if there was a *much* simpler way to do this, ie.
without using several functions to process the filename. Yes, your
version *does* have some improvements. I'm not disagreeing with you. 
Perhaps i could have been more forthright in my praise for said 
improvements (i'm even going to use some of them--how about that?!)



Well, I'm not looking for praise... but asking a concise question goes a
long way towards meeting your desires.


How was my question *not* concise? If anything, it was my first reply to 
you that i could have spent more time on. Once again: your reply was 
valid, your corrections were taken into consideration, *but* i was 
simply wondering if there was a more (yes) concise way to do what i 
wanted. My humble apologies (really).


IOW, i'm not stupid; i'm well aware that 100 comes after 
99 and that it contains 3 digits, not 2, thanks very much.



Nope, 100 comes *before* 99 when lexically sorting.

You must be new around here.



Did i say anything there about lexically sorting? The query i posted 
here was about counting. That last comment? Yep, counting again. Now 
you're obviously just trying to be a dickhead. Well done.


You must be charming to work with.

brian

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting with leading zeros

2007-09-26 Thread Robert Cummings
On Wed, 2007-09-26 at 22:39 -0400, brian wrote:
 Robert Cummings wrote:
  On Wed, 2007-09-26 at 19:10 -0400, brian wrote:
  
 If my response to that gave you the impression i was
 complaining, i assure you that i wasn't. I was simply suggesting
 that i was wondering if there was a *much* simpler way to do this, ie.
 without using several functions to process the filename. Yes, your
 version *does* have some improvements. I'm not disagreeing with you. 
 Perhaps i could have been more forthright in my praise for said 
 improvements (i'm even going to use some of them--how about that?!)
  
  
  Well, I'm not looking for praise... but asking a concise question goes a
  long way towards meeting your desires.
 
 How was my question *not* concise? If anything, it was my first reply to 
 you that i could have spent more time on. Once again: your reply was 
 valid, your corrections were taken into consideration, *but* i was 
 simply wondering if there was a more (yes) concise way to do what i 
 wanted. My humble apologies (really).
 
 IOW, i'm not stupid; i'm well aware that 100 comes after 
 99 and that it contains 3 digits, not 2, thanks very much.
  
  
  Nope, 100 comes *before* 99 when lexically sorting.
  
  You must be new around here.
  
 
 Did i say anything there about lexically sorting? The query i posted 
 here was about counting. That last comment? Yep, counting again. Now 
 you're obviously just trying to be a dickhead. Well done.

When you use sort() the default behaviour is a lexical sort. This is why
the 100th index breaks your system. I'm not trying to be a dickhead,
just pointing out the flaw in your logic. You may be well aware that in
counting 100 comes after 99, but it would seem you are not well aware
that in a lexical sort 100 will come first.

 You must be charming to work with.

Quite.

Cheers,
Rob.
-- 
...
SwarmBuy.com - http://www.swarmbuy.com

Leveraging the buying power of the masses!
...

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting with leading zeros

2007-09-26 Thread brian

Robert Cummings wrote:

When you use sort() the default behaviour is a lexical sort. This is why
the 100th index breaks your system. I'm not trying to be a dickhead,
just pointing out the flaw in your logic. You may be well aware that in
counting 100 comes after 99, but it would seem you are not well aware
that in a lexical sort 100 will come first.



I realise that this has been going on for some time now but i did say 
that this app will not--cannot--reach to 100. If it were to do so, my 
system would have become broken *long* before it reached sort(). 
Lexical sorting of '99'  '100' has nothing to do with this.


Just pointing out the flaw in your persistence with this.

b

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting with leading zeros

2007-09-26 Thread Robert Cummings
On Thu, 2007-09-27 at 00:23 -0400, brian wrote:
 Robert Cummings wrote:
  When you use sort() the default behaviour is a lexical sort. This is why
  the 100th index breaks your system. I'm not trying to be a dickhead,
  just pointing out the flaw in your logic. You may be well aware that in
  counting 100 comes after 99, but it would seem you are not well aware
  that in a lexical sort 100 will come first.
  
 
 I realise that this has been going on for some time now but i did say 
 that this app will not--cannot--reach to 100. If it were to do so, my 
 system would have become broken *long* before it reached sort(). 
 Lexical sorting of '99'  '100' has nothing to do with this.
 
 Just pointing out the flaw in your persistence with this.

Actually your original message said the following:

Note that there almost certainly will never be more
 than 99 images to a series. In any case, i don't care
 about there being more than one leading zero. One is
 what i want.

Almost certainly last time I checked does not equal will not.

Cheers,
Rob.
-- 
...
SwarmBuy.com - http://www.swarmbuy.com

Leveraging the buying power of the masses!
...

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting with leading zeros

2007-09-26 Thread Jim Lucas

brian wrote:
I have a directory that contains many images (no, not pr0n, 
unfortunately) and i'm working on an admin script for adding to it. I've 
got something that works alright but i'm wondering if there's a Better Way.


Each image is named like: foo_01.jpg, foo_02.jpg, bar_01.jpg, and so on. 
When adding a new image it should be assigned the next number in the 
series (the prefix is known). Thus, given the prefix 'bar' i do 
something like:


function getNextImage($path, $prefix)
{
  $pattern = /^${prefix}_([0-9]{2})\.[a-z]{3}$/;

  $filenames = glob(${path}${prefix}*);

  if (is_array($filenames)  sizeof($filenames))

  {
sort($filenames);

/* eg. 'foo_32.jpg'
 */
$last = basename(array_pop($filenames));

/* pull the number from the filename
 */
$count = intval(preg_replace($pattern, '$1', $last));

/* increment, format with leading zero again if necessary,
 * and return it
 */
return sprintf('%02d', ++$count)
  }
  else
  {
return '01';
  }
}

Note that there almost certainly will never be more than 99 images to a 
series. In any case, i don't care about there being more than one 
leading zero. One is what i want.


brian




Well, I didn't want to get into the middle of this cat fight, but now 
that the talks have completely gotten off topic.  I will interject...


Here is a function that is a bit simpler then either of yours.

Now, if you plan to delete any images in the list, this will not work.

But if you don't expect to delete any images in the list and always want 
the ($total_images + 1), then follow along.


?php

function getNextImage($path, $prefix) {
  $filelist = glob(${path}${prefix}*) or return '01';
  return str_pad(((int)count($filelist)+1), 2, '0', STR_PAD_LEFT);
}

?

This should work, I don't have a setup to test it on, but you should get 
the idea...


...

now remember...

if glob fails, it will return '01'
if glob returns and empty array, it will return '01'
if glob succeeds, it will return the array length + 1 padded (if needed) 
 by one zero


You asked for less function calls in your function, here you go

Jim

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Counting Capital Letters

2007-06-21 Thread Robin Vickery

On 21/06/07, Richard Davey [EMAIL PROTECTED] wrote:

Hi,

I've written a short regexp which will *count* how many capital letters
are in a given string (the woefully simple: '/[A-Z]/')

Although it's an English language web site, I'm curious how you'd
count capital letters that span beyond just the standard A-Z.

For example characters such as the Latin capital letter S with Acute.
I'm not interested in covering all possible character sets, but I
don't want to piss-off any Europeans who may register on the site and
want to use one of their own capital letters.

Anyone approached this before?


It'd probably be a good start to use [[:upper:]] rather than [A-Z].
There's also \p{Lu} if you use utf-8 mode which matches utf-8
characters with the uppercase letter property.

-robin

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Counting Capital Letters

2007-06-21 Thread tedd

At 12:32 AM +0100 6/21/07, Richard Davey wrote:

Hi,

I've written a short regexp which will *count* how many capital letters
are in a given string (the woefully simple: '/[A-Z]/')

Although it's an English language web site, I'm curious how you'd
count capital letters that span beyond just the standard A-Z.

For example characters such as the Latin capital letter S with Acute.
I'm not interested in covering all possible character sets, but I
don't want to piss-off any Europeans who may register on the site and
want to use one of their own capital letters.

Anyone approached this before?

Cheers,

Rich


Rich:

Can't say that I have, but try this off the top of my head:

1. Explode the string into two arrays (array1, array2);

2. Lowercase one array;

3. Use array_dif.

4 Count array_dif.

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



Re: [PHP] counting code lines

2007-06-14 Thread James McLean

On 6/15/07, Toni Torello [EMAIL PROTECTED] wrote:

hi guys,
just supposing...

which is the right way to to count the number of code lines in a php
application?

do you think that the raw:
$ find . -name '*.php' -exec cat {} \; | wc -l
can be a good estimate?


I recently desired to know a similar estimate of the project I was
working on and came up with essentially the same thing, but automated
it slightly further to encompass other file types also, this is the
(perl) script I came up with for your interest:

#!/usr/bin/perl -w

my @types = qw(php css xml xhtml html js sql);

foreach my $type (@types) {
   print Lines in files of type: \t . $type . : ;
   my $command = find . -name *.$type -type f -exec cat {} + | wc -l;
   print `$command`;
}

Not perfect but it's not bad. Keep in mind it wont see what is a
comment and what isnt for example..

Cheers

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting hyperlink clicks in php

2007-02-20 Thread Fergus Gibson
Brad Bonkoski wrote:
 I think the best way to do this would be to set an onClick (Javascript)
 event handler for each of the links, and then use AJAX style stuff to
 send the information to PHP on the server side, then PHP can log the
 link that was clicked, and keep track of the most clicked links.

The only problem with this suggestion is the dependency on Javascript.
This would not count clicks from browsers that don't support JS or from
users who have disabled it in their browsers.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting hyperlink clicks in php

2007-02-20 Thread Richard Lynch
On Sat, February 17, 2007 1:53 pm, clive wrote:
 Denis L. Menezes wrote:
 Dear friends.

 I have a site where I have news headers. I wish to count the clicks
 on the
 news headers so that I can see how many times each of the the news
 has been
 viewed. This way I can show the most viewed news.

 Can one of you please advise hwo this can be done?


 add to each news item like clicked=1 and in the page where you
 display
 the news item, checked if clicked exists, if does write it to a file
 or
 a db (obviously with the news item id), if your using mysql, the heap
 table type is quite useful, you will however need to move the info to
 a
 more permanent table every x amount of clicks.

Do you really only want to count the clicks from your own page?

Or, put another way, if somebody posts an external link on slashdot
and a news story is getting a zillion views from that, do you want to
count those as well?

It would seem to me that in your view script that displays the story
you could simply do something like:

update story set views = views + 1 where story_id = $story_id

And not even worry about the mucking with AJAX nor tracking it from
only your own page.

-- 
Some people have a gift link here.
Know what I want?
I want you to buy a CD from some starving artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting hyperlink clicks in php

2007-02-17 Thread Brad Bonkoski

Denis L. Menezes wrote:

Dear friends.

I have a site where I have news headers. I wish to count the clicks on the 
news headers so that I can see how many times each of the the news has been 
viewed. This way I can show the most viewed news.


Can one of you please advise hwo this can be done?

Thanks
Denis 

  
I think the best way to do this would be to set an onClick (Javascript) 
event handler for each of the links, and then use AJAX style stuff to 
send the information to PHP on the server side, then PHP can log the 
link that was clicked, and keep track of the most clicked links.

-B

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting hyperlink clicks in php

2007-02-17 Thread clive

Denis L. Menezes wrote:

Dear friends.

I have a site where I have news headers. I wish to count the clicks on the 
news headers so that I can see how many times each of the the news has been 
viewed. This way I can show the most viewed news.


Can one of you please advise hwo this can be done?

  
add to each news item like clicked=1 and in the page where you display 
the news item, checked if clicked exists, if does write it to a file or 
a db (obviously with the news item id), if your using mysql, the heap 
table type is quite useful, you will however need to move the info to a 
more permanent table every x amount of clicks.


clive

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Counting the occurences of items in an array

2006-12-24 Thread Sumeet

Dotan Cohen wrote:

Has this wheel been invented, but simpler? I've an array of words that
I'd like to know how many occurences of each there are. For instance:
$fruits = array(
   lemon,
   orange,
   banana,
   apple,
   orange,
   banana,
   orange);

And I'd like to create this:
$fruit_count = array(
   lemon = 1,
   orange = 3,
   banana = 2,
   apple = 1);



check
http://in2.php.net/manual/en/function.array-count-values.php



--
Thanking You

Sumeet Shroff
http://www.prateeksha.com
Web Designers and PHP / Mysql Ecommerce Development, Mumbai India

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Counting the occurences of items in an array

2006-12-24 Thread tedd

At 8:35 PM +0200 12/24/06, Dotan Cohen wrote:

Has this wheel been invented, but simpler? I've an array of words that
I'd like to know how many occurences of each there are. For instance:
$fruits = array(
   lemon,
   orange,
   banana,
   apple,
   orange,
   banana,
   orange);

And I'd like to create this:
$fruit_count = array(
   lemon = 1,
   orange = 3,
   banana = 2,
   apple = 1);

My current plan of action is to create $fruit_count, and check if
$fruits[0] is listed in it. If not, then I'll add it with a value of
1. If it is already listed then I'll just increase it's number. Ditto
for $fruits[1] and so on...

Has someone cleverer than myself found a better way of doing this?
This function will be used as a word count for text documents, and
some of them have over 5000 words. So if there's a better way of doing
this I'd love to know. Thanks.

Dotan Cohen


Dotan:

array_count_values()

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



Re: [PHP] Counting the occurences of items in an array

2006-12-24 Thread Dotan Cohen

Thanks for the link to the function. Know that I did RTFM before
posting, but I missed that one.

Dotan Cohen

http://lyricslist.com/lyrics/artist_albums/219/xyz.php
http://simplesniff.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] counting clicks

2006-11-29 Thread Jay Blanchard
[snip]
Anyone got a script so I can count clicks on adverts. Doesn't have tosve
to 
myqsl or anything just a text file will do.
[/snip]

?php

if(yes == $adClicked){
   $adClickCountFile = fopen(countClick.txt, w);
   $getCount = fgets($adClickCountFile, 4096);
   $newCount = $getCount + 1;
   fwrite($adClickCountFile, $newCount);
   fclose($adClickCountFile);
}
?

YMMV

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] counting clicks

2006-11-29 Thread Richard Lynch
On Wed, November 29, 2006 11:21 am, Jay Blanchard wrote:
 [snip]
 Anyone got a script so I can count clicks on adverts. Doesn't have
 tosve
 to
 myqsl or anything just a text file will do.
 [/snip]

 ?php

 if(yes == $adClicked){
$adClickCountFile = fopen(countClick.txt, w);
$getCount = fgets($adClickCountFile, 4096);
$newCount = $getCount + 1;
fwrite($adClickCountFile, $newCount);
fclose($adClickCountFile);
 }
 ?

This has a race condition where a busy server, or even a not-so-busy
one where 2 people happen to click at the same time, at least one of
them will be missed, at best.

There's also a distinct possiblity of the counter file getting
completely trashed, depending on the OS and its underlying atomicity
of fwrite().

And, finally, you'd need w+ in that fopen() to avoid wiping out the
old data to start with.

For this reason, and more, you're almost-for-sure better off keeping
track in a database which solves all the nasty race condition issues.

YMMV


 YMMV

 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php




-- 
Some people have a gift link here.
Know what I want?
I want you to buy a CD from some starving artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] counting clicks

2006-11-29 Thread Jay Blanchard
[snip]
This has a race condition where a busy server, or even a not-so-busy
one where 2 people happen to click at the same time, at least one of
them will be missed, at best.

There's also a distinct possiblity of the counter file getting
completely trashed, depending on the OS and its underlying atomicity
of fwrite().

And, finally, you'd need w+ in that fopen() to avoid wiping out the
old data to start with.

For this reason, and more, you're almost-for-sure better off keeping
track in a database which solves all the nasty race condition issues.
[/snip]

Exactly, YMMV

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting clicks

2006-11-29 Thread clive

Ross wrote:
Anyone got a script so I can count clicks on adverts. Doesn't have tosve to 
myqsl or anything just a text file will do.


What about using mysql, but use a memory resident heap table, then every 
once in a while write this to a file or even better to a myisam table





Ta

Ross 



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting records in db

2006-10-31 Thread Ivo F.A.C. Fokkema
On Mon, 30 Oct 2006 12:28:29 +0100, Robin Vickery wrote:

 On 30/10/06, Ivo F.A.C. Fokkema [EMAIL PROTECTED] wrote:
 On Sun, 29 Oct 2006 23:40:47 -0600, Richard Lynch wrote:
  On Fri, October 27, 2006 4:53 pm, Børge Holen wrote:
  On Friday 27 October 2006 19:34, Richard Lynch wrote:
  And the header(Location: ...) requires a full URL.
 
  No it doesn't. but he's missing an ' at first glance
 
  Yes, it does:
  http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30
 
  Note the use of 'absolute' within that section.

 Although I always use a full URL as well, doesn't absolute just mean
 non-relative? As in:
 Location: /Protocols/rfc2616/rfc2616-sec14.html#sec14.30
 (absolute URI)

 Location: ./rfc2616-sec14.html#sec14.30
 (relative URI)
 
 If you need contextual information to make sense of the URI (such as
 the server name from a previous request) then it's not absolute.
 
 RFC 2396: Uniform Resource Identifiers
 
 An absolute identifier refers to a resource independent of the
 context in which the identifier is used. In contrast, a relative
 identifier refers to a resource by describing the difference within a
 hierarchical namespace between the current context and an absolute
 identifier of the resource.

Ah, thanks. I was confusing it with absolute and relative paths.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting records in db

2006-10-31 Thread Roman Neuhauser
# [EMAIL PROTECTED] / 2006-10-27 20:14:58 +0200:
  And the header(Location: ...) requires a full URL.
 Why?

HTTP protocol specification[1] says:

14.30 Location

   The Location response-header field is used to redirect the recipient
   to a location other than the Request-URI for completion of the
   request or identification of a new resource. For 201 (Created)
   responses, the Location is that of the new resource which was created
   by the request. For 3xx responses, the location SHOULD indicate the
   server's preferred URI for automatic redirection to the resource. The
   field value consists of a single absolute URI.

   Location   = Location : absoluteURI

   An example is:

   Location: http://www.w3.org/pub/WWW/People.html

  Note: The Content-Location header field (section 14.14) differs
  from Location in that the Content-Location identifies the original
  location of the entity enclosed in the request. It is therefore
  possible for a response to contain header fields for both Location
  and Content-Location. Also see section 13.10 for cache
  requirements of some methods.

[1] ftp://ftp.rfc-editor.org/in-notes/rfc2616.txt

-- 
How many Vietnam vets does it take to screw in a light bulb?
You don't know, man.  You don't KNOW.
Cause you weren't THERE. http://bash.org/?255991

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting records in db

2006-10-31 Thread Robin Vickery

On 30/10/06, Robert Cummings [EMAIL PROTECTED] wrote:

On Mon, 2006-10-30 at 12:28 +0100, Robin Vickery wrote:
 On 30/10/06, Ivo F.A.C. Fokkema [EMAIL PROTECTED] wrote:
  On Sun, 29 Oct 2006 23:40:47 -0600, Richard Lynch wrote:
 
   On Fri, October 27, 2006 4:53 pm, Børge Holen wrote:
   On Friday 27 October 2006 19:34, Richard Lynch wrote:
   And the header(Location: ...) requires a full URL.
  
   No it doesn't. but he's missing an ' at first glance
  
   Yes, it does:
   http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30
  
   Note the use of 'absolute' within that section.
 
  Although I always use a full URL as well, doesn't absolute just mean
  non-relative? As in:
  Location: /Protocols/rfc2616/rfc2616-sec14.html#sec14.30
  (absolute URI)
 
  Location: ./rfc2616-sec14.html#sec14.30
  (relative URI)

 If you need contextual information to make sense of the URI (such as
 the server name from a previous request) then it's not absolute.

 RFC 2396: Uniform Resource Identifiers

 An absolute identifier refers to a resource independent of the
 context in which the identifier is used. In contrast, a relative
 identifier refers to a resource by describing the difference within a
 hierarchical namespace between the current context and an absolute
 identifier of the resource.

Please note you are quoting from an RFC with the following title:

Uniform Resource Identifiers (URI): Generic Syntax

Pay special attention to Generic Syntax in the title.

The RFC linked by Richard clearly indicates that for the Location
response-header that the field value consists of a single absolute
URI. This currently has the final word for the Location response-header
and therefore is the standard.


What?

Pay special attention to what I wrote.

I wasn't disagreeing with Richard, or the RFC he quoted. The OP
expressed some confusion about what comprised an 'absolute URI' so I
quoted the section of the RFC that defined an 'absolute URI'.

There's no question that a Location header should contain an absolute
URI as its value.

-robin

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting records in db

2006-10-31 Thread Richard Lynch
On Tue, October 31, 2006 4:58 am, Robin Vickery wrote:
 On 30/10/06, Robert Cummings [EMAIL PROTECTED] wrote:
 On Mon, 2006-10-30 at 12:28 +0100, Robin Vickery wrote:
  On 30/10/06, Ivo F.A.C. Fokkema [EMAIL PROTECTED] wrote:
   On Sun, 29 Oct 2006 23:40:47 -0600, Richard Lynch wrote:
  
On Fri, October 27, 2006 4:53 pm, Børge Holen wrote:
On Friday 27 October 2006 19:34, Richard Lynch wrote:
And the header(Location: ...) requires a full URL.
   
No it doesn't. but he's missing an ' at first glance
   
Yes, it does:
http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30
   
Note the use of 'absolute' within that section.
  
   Although I always use a full URL as well, doesn't absolute just
 mean
   non-relative? As in:
   Location: /Protocols/rfc2616/rfc2616-sec14.html#sec14.30
   (absolute URI)
  
   Location: ./rfc2616-sec14.html#sec14.30
   (relative URI)
 
  If you need contextual information to make sense of the URI (such
 as
  the server name from a previous request) then it's not absolute.
 
  RFC 2396: Uniform Resource Identifiers
 
  An absolute identifier refers to a resource independent of the
  context in which the identifier is used. In contrast, a relative
  identifier refers to a resource by describing the difference
 within a
  hierarchical namespace between the current context and an absolute
  identifier of the resource.

 Please note you are quoting from an RFC with the following title:

 Uniform Resource Identifiers (URI): Generic Syntax

 Pay special attention to Generic Syntax in the title.

 The RFC linked by Richard clearly indicates that for the Location
 response-header that the field value consists of a single absolute
 URI. This currently has the final word for the Location
 response-header
 and therefore is the standard.

Please note that the link I posted is part of this document:
http://www.w3.org/Protocols/rfc2616/rfc2616.html

Also note that THIS section of that document:
http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.2.1
clearly states:
For definitive information on URL syntax and semantics, see
Uniform Resource Identifiers (URI):
 Generic Syntax and Semantics,
and provides a link to:
http://www.w3.org/Protocols/rfc2616/rfc2616-sec17.html#bib42
which in turn links to:
http://www.ietf.org/rfc/rfc2396.txt
which is, in fact, the document cited.

Are we all on the same page now?

Is there any doubt in anybody's mind that the standards clearly
require an absoluteURI, complete with scheme (e.g., http://)?

:-) :-) :-)

I just wanted to make it clear to any newbie reader whose wishful
thinking was leading them astray that they should NOT rely on a
non-absolute URI if they want to be standards-compliant.

It is rare that one cannot take an extra few seconds and compose the
standards-compliant URI.

-- 
Some people have a gift link here.
Know what I want?
I want you to buy a CD from some starving artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting records in db

2006-10-31 Thread Robert Cummings
On Tue, 2006-10-31 at 11:58 +0100, Robin Vickery wrote:
 On 30/10/06, Robert Cummings [EMAIL PROTECTED] wrote:
  On Mon, 2006-10-30 at 12:28 +0100, Robin Vickery wrote:
   On 30/10/06, Ivo F.A.C. Fokkema [EMAIL PROTECTED] wrote:
On Sun, 29 Oct 2006 23:40:47 -0600, Richard Lynch wrote:
   
 On Fri, October 27, 2006 4:53 pm, Børge Holen wrote:
 On Friday 27 October 2006 19:34, Richard Lynch wrote:
 And the header(Location: ...) requires a full URL.

 No it doesn't. but he's missing an ' at first glance

 Yes, it does:
 http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30

 Note the use of 'absolute' within that section.
   
Although I always use a full URL as well, doesn't absolute just mean
non-relative? As in:
Location: /Protocols/rfc2616/rfc2616-sec14.html#sec14.30
(absolute URI)
   
Location: ./rfc2616-sec14.html#sec14.30
(relative URI)
  
   If you need contextual information to make sense of the URI (such as
   the server name from a previous request) then it's not absolute.
  
   RFC 2396: Uniform Resource Identifiers
  
   An absolute identifier refers to a resource independent of the
   context in which the identifier is used. In contrast, a relative
   identifier refers to a resource by describing the difference within a
   hierarchical namespace between the current context and an absolute
   identifier of the resource.
 
  Please note you are quoting from an RFC with the following title:
 
  Uniform Resource Identifiers (URI): Generic Syntax
 
  Pay special attention to Generic Syntax in the title.
 
  The RFC linked by Richard clearly indicates that for the Location
  response-header that the field value consists of a single absolute
  URI. This currently has the final word for the Location response-header
  and therefore is the standard.
 
 What?
 
 Pay special attention to what I wrote.

Seems a bit ambiguous :)

 I wasn't disagreeing with Richard, or the RFC he quoted. The OP
 expressed some confusion about what comprised an 'absolute URI' so I
 quoted the section of the RFC that defined an 'absolute URI'.
 
 There's no question that a Location header should contain an absolute
 URI as its value.

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] counting records in db

2006-10-30 Thread Ivo F.A.C. Fokkema
On Sun, 29 Oct 2006 23:40:47 -0600, Richard Lynch wrote:

 On Fri, October 27, 2006 4:53 pm, Børge Holen wrote:
 On Friday 27 October 2006 19:34, Richard Lynch wrote:
 And the header(Location: ...) requires a full URL.

 No it doesn't. but he's missing an ' at first glance
 
 Yes, it does:
 http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30
 
 Note the use of 'absolute' within that section.

Although I always use a full URL as well, doesn't absolute just mean
non-relative? As in:
Location: /Protocols/rfc2616/rfc2616-sec14.html#sec14.30
(absolute URI)

Location: ./rfc2616-sec14.html#sec14.30
(relative URI)


 You can argue that they shouldn't have designed the spec that way.
 
 You can argue that it works in all popular browsers.
 
 But there ain't much to argue about what the spec says...

Agreed. One should always follow the spec.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting records in db

2006-10-30 Thread Robin Vickery

On 30/10/06, Ivo F.A.C. Fokkema [EMAIL PROTECTED] wrote:

On Sun, 29 Oct 2006 23:40:47 -0600, Richard Lynch wrote:

 On Fri, October 27, 2006 4:53 pm, Børge Holen wrote:
 On Friday 27 October 2006 19:34, Richard Lynch wrote:
 And the header(Location: ...) requires a full URL.

 No it doesn't. but he's missing an ' at first glance

 Yes, it does:
 http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30

 Note the use of 'absolute' within that section.

Although I always use a full URL as well, doesn't absolute just mean
non-relative? As in:
Location: /Protocols/rfc2616/rfc2616-sec14.html#sec14.30
(absolute URI)

Location: ./rfc2616-sec14.html#sec14.30
(relative URI)


If you need contextual information to make sense of the URI (such as
the server name from a previous request) then it's not absolute.

RFC 2396: Uniform Resource Identifiers

An absolute identifier refers to a resource independent of the
context in which the identifier is used. In contrast, a relative
identifier refers to a resource by describing the difference within a
hierarchical namespace between the current context and an absolute
identifier of the resource.

-robin

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting records in db

2006-10-30 Thread Robert Cummings
On Mon, 2006-10-30 at 12:28 +0100, Robin Vickery wrote:
 On 30/10/06, Ivo F.A.C. Fokkema [EMAIL PROTECTED] wrote:
  On Sun, 29 Oct 2006 23:40:47 -0600, Richard Lynch wrote:
 
   On Fri, October 27, 2006 4:53 pm, Børge Holen wrote:
   On Friday 27 October 2006 19:34, Richard Lynch wrote:
   And the header(Location: ...) requires a full URL.
  
   No it doesn't. but he's missing an ' at first glance
  
   Yes, it does:
   http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30
  
   Note the use of 'absolute' within that section.
 
  Although I always use a full URL as well, doesn't absolute just mean
  non-relative? As in:
  Location: /Protocols/rfc2616/rfc2616-sec14.html#sec14.30
  (absolute URI)
 
  Location: ./rfc2616-sec14.html#sec14.30
  (relative URI)
 
 If you need contextual information to make sense of the URI (such as
 the server name from a previous request) then it's not absolute.
 
 RFC 2396: Uniform Resource Identifiers
 
 An absolute identifier refers to a resource independent of the
 context in which the identifier is used. In contrast, a relative
 identifier refers to a resource by describing the difference within a
 hierarchical namespace between the current context and an absolute
 identifier of the resource.

Please note you are quoting from an RFC with the following title:

Uniform Resource Identifiers (URI): Generic Syntax

Pay special attention to Generic Syntax in the title.

The RFC linked by Richard clearly indicates that for the Location
response-header that the field value consists of a single absolute
URI. This currently has the final word for the Location response-header
and therefore is the standard.

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] counting records in db

2006-10-29 Thread Richard Lynch
On Fri, October 27, 2006 12:14 pm, [EMAIL PROTECTED] wrote:
 And the header(Location: ...) requires a full URL.
 Why?

Because the docs say so?

Because some user agents will not do what you want them to if you don't?

-- 
Some people have a gift link here.
Know what I want?
I want you to buy a CD from some starving artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting records in db

2006-10-29 Thread Richard Lynch
On Fri, October 27, 2006 4:53 pm, Børge Holen wrote:
 On Friday 27 October 2006 19:34, Richard Lynch wrote:
 And the header(Location: ...) requires a full URL.

 No it doesn't. but he's missing an ' at first glance

Yes, it does:
http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30

Note the use of 'absolute' within that section.

You can argue that they shouldn't have designed the spec that way.

You can argue that it works in all popular browsers.

But there ain't much to argue about what the spec says...

-- 
Some people have a gift link here.
Know what I want?
I want you to buy a CD from some starving artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting records in db

2006-10-27 Thread Satyam
- Original Message - 
From: Paul Novitski [EMAIL PROTECTED]


While we're talking about optimization, I'd want to check to make sure 
COUNT(*) didn't ask MySQL to generate a throw-away recordset consisting of 
all fields.  I wonder if it would be more machine-efficient to use 
COUNT(`last_name`), specifying a single field, in this case the same field 
your query is examining anyway.




If the fields mentioned in the where clause are keys or if there is no where 
clause, a count(*) uses the index trees to make the count, it won't even 
access the data racords at all.  Moreover, it won't even read the whole 
index tree node, just the header record of the block that indicates how many 
key records that block contains.  I don't really know about MySql or any 
database engine, but these are techniques well known in the trade.


On the count(fieldname) issue, it is different from a count(*) and may give 
different results.  When you mentione a field it will only count records 
where that field is not null.  Select count(fieldname) from table is the 
same as doing a select count(*) from table where fieldname is not null.  It 
will take just as longer as processing any additional where clause.


Satyam 


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting records in db

2006-10-27 Thread Richard Lynch
Your idea to use 'COUNT(Username)' is just about the ONLY part of that
script that you are doing correctly...

:-)

Start reading here:
http://phpsec.org/

And the header(Location: ...) requires a full URL.

And you should have better error-checking, probably.
[Though maybe you have a custom error handler not apparent]

And your DB password should probably be a hash/crypted value.

And it looks like maybe you are using register_globals on which is bad.

Once again, start reading:
http://phpsec.org/

On Thu, October 26, 2006 1:16 pm, [EMAIL PROTECTED] wrote:
 Would it be ok to use the same code to check if customer is loged in?

 $query = mysql_query(
SELECT COUNT(Username) as NoOfRecords
FROM customers
WHERE Username = '$Username' AND Password =
 '$Password');
 if (mysql_result($query, 0) == 0)
 {
echo 'Please try again';
 }
 else
 {
header('location: index.php);
exit;
 }

 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php




-- 
Some people have a gift link here.
Know what I want?
I want you to buy a CD from some starving artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting records in db

2006-10-27 Thread afan
 Your idea to use 'COUNT(Username)' is just about the ONLY part of that
 script that you are doing correctly...

 :-)

:D


 And the header(Location: ...) requires a full URL.
Why?


 And you should have better error-checking, probably.
 [Though maybe you have a custom error handler not apparent]
Right. I skipped that part to not waste space in post. $_POST values are
checked first and then used.

 And your DB password should probably be a hash/crypted value.
Correct. I use this, of course.

 And it looks like maybe you are using register_globals on which is bad.
Nope! globals are off.
:D

 Once again, start reading:
 http://phpsec.org/
I'm Shiflett's fan!
:)

-afan

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting records in db

2006-10-27 Thread Børge Holen
On Friday 27 October 2006 19:34, Richard Lynch wrote:
 Your idea to use 'COUNT(Username)' is just about the ONLY part of that
 script that you are doing correctly...

 :-)

 Start reading here:
 http://phpsec.org/

 And the header(Location: ...) requires a full URL.

No it doesn't. but he's missing an ' at first glance




 And you should have better error-checking, probably.
 [Though maybe you have a custom error handler not apparent]

 And your DB password should probably be a hash/crypted value.

 And it looks like maybe you are using register_globals on which is bad.

 Once again, start reading:
 http://phpsec.org/

 On Thu, October 26, 2006 1:16 pm, [EMAIL PROTECTED] wrote:
  Would it be ok to use the same code to check if customer is loged in?
 
  $query = mysql_query(
 SELECT COUNT(Username) as NoOfRecords
 FROM customers
 WHERE Username = '$Username' AND Password =
  '$Password');
  if (mysql_result($query, 0) == 0)
  {
 echo 'Please try again';
  }
  else
  {
 header('location: index.php);
 exit;
  }
 
  --
  PHP General Mailing List (http://www.php.net/)
  To unsubscribe, visit: http://www.php.net/unsub.php

 --
 Some people have a gift link here.
 Know what I want?
 I want you to buy a CD from some starving artist.
 http://cdbaby.com/browse/from/lynch
 Yeah, I get a buck. So?

-- 
---
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] counting records in db

2006-10-26 Thread Dave Goodchild

$query = mysql_query(
  SELECT COUNT(*) as NoOfRecords
  FROM customers
  WHERE last_name = 'Smith');
$result = mysql_result($query, 0);


Re: [PHP] counting records in db

2006-10-26 Thread afan
WOW! That was fast!
:D

Thanks Dave!

-afan


 $query = mysql_query(
SELECT COUNT(*) as NoOfRecords
FROM customers
WHERE last_name = 'Smith');
 $result = mysql_result($query, 0);


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting records in db

2006-10-26 Thread afan
Hm. There is a little problem - this doesn't work.

Warning: mysql_result(): supplied argument is not a valid MySQL result
resource in /var/www/html/xxx/tests/count.php on line 28

?


 $query = mysql_query(
SELECT COUNT(*) as NoOfRecords
FROM customers
WHERE last_name = 'Smith');
 $result = mysql_result($query, 0);


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting records in db

2006-10-26 Thread Paul Novitski

At 10/26/2006 10:38 AM, [EMAIL PROTECTED] wrote:

what would be better solution to count records in table (e.g., how many
customers have last name 'Smith'):

$query = mysql_query(
SELECT COUNT(*) as NoOfRecords
FROM customers
WHERE last_name = 'Smith');
$result = mysql_fetch_array($query);
$NoOfRecords = $result['NoOfRecords'];

OR

$query = mysql_query(
SELECT cust_id
FROM customers
WHERE last_name = 'Smith');
$NoOfRecords = mysql_num_rows($query);



My understanding of why COUNT() is the better solution is that 
mysql_num_rows() requires MySQL to cycle through the found records on 
a second pass to count them, whereas the first gathers the count 
during the first pass, the record-selection phase.


Of course, if you also need to have the selected records accessible 
to a loop, using COUNT() will force you to execute two queries, one 
for the record count and one for the data themselves, so at that 
point the relative advantage is less clear.


While we're talking about optimization, I'd want to check to make 
sure COUNT(*) didn't ask MySQL to generate a throw-away recordset 
consisting of all fields.  I wonder if it would be more 
machine-efficient to use COUNT(`last_name`), specifying a single 
field, in this case the same field your query is examining anyway.


Regards,
Paul 


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting records in db

2006-10-26 Thread afan
Sorry, my bad! It works jsut fine. I did misstake.
:)




  $query = mysql_query(
 SELECT COUNT(*) as NoOfRecords
 FROM customers
 WHERE last_name = 'Smith');
  $result = mysql_result($query, 0);
 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting records in db

2006-10-26 Thread Richard Lynch
On Thu, October 26, 2006 12:57 pm, Paul Novitski wrote:
$query = mysql_query(
 SELECT COUNT(*) as NoOfRecords
 FROM customers
 WHERE last_name = 'Smith');
$result = mysql_fetch_array($query);
$NoOfRecords = $result['NoOfRecords'];

This would be faster.
Index on last_name will probably help a great deal.

$query = mysql_query(
 SELECT cust_id
 FROM customers
 WHERE last_name = 'Smith');
$NoOfRecords = mysql_num_rows($query);

This will be slower as MySQL/PHP have to store and maintain a dataset
of all 'Smith' records.

 My understanding of why COUNT() is the better solution is that
 mysql_num_rows() requires MySQL to cycle through the found records on
 a second pass to count them, whereas the first gathers the count
 during the first pass, the record-selection phase.

That too.

 Of course, if you also need to have the selected records accessible
 to a loop, using COUNT() will force you to execute two queries, one
 for the record count and one for the data themselves, so at that
 point the relative advantage is less clear.

You'd have to test on your hardware and your dataset to see which is
faster at that point, I think.

And if the dataset is small enough to get the whole shebang, then you
probably don't care.

And if it's not, then you want LIMIT/OFFSET and then you want
SQL_CALC_FOUND_ROWS or whatever it is with the mysql_num_rows() so you
know how many rows there were without the LIMIT.

 While we're talking about optimization, I'd want to check to make
 sure COUNT(*) didn't ask MySQL to generate a throw-away recordset
 consisting of all fields.  I wonder if it would be more
 machine-efficient to use COUNT(`last_name`), specifying a single
 field, in this case the same field your query is examining anyway.

count(*) is heavily optimized in MySQL, as I understand it.

All this belongs on a MySQL list, not here, anyway.

-- 
Some people have a gift link here.
Know what I want?
I want you to buy a CD from some starving artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting records in db

2006-10-26 Thread afan
Would it be ok to use the same code to check if customer is loged in?

$query = mysql_query(
   SELECT COUNT(Username) as NoOfRecords
   FROM customers
   WHERE Username = '$Username' AND Password = '$Password');
if (mysql_result($query, 0) == 0)
{
   echo 'Please try again';
}
else
{
   header('location: index.php);
   exit;
}

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting records in db

2006-10-26 Thread Dave Goodchild

Why would you want to do that? Think about what you're trying to do. In the
first case you want a COUNT of records in the database, in the second you
just want to see if the user/password combination or whatever exist, so just
use a normal SELECT query, no need to use the wrong tool for the job...!


RE: [PHP] counting records in db

2006-10-26 Thread Brad Fuller
 Would it be ok to use the same code to check if customer is loged in?
 
 $query = mysql_query(
SELECT COUNT(Username) as NoOfRecords
FROM customers
WHERE Username = '$Username' AND Password = '$Password');
 if (mysql_result($query, 0) == 0)
 {
echo 'Please try again';
 }
 else
 {
header('location: index.php);
exit;
 }

Assuming that the 'Username' field is unique, then the COUNT() is not
necessary in this case as the number of returned results would never be
greater than 1.  

A more reasonable approach would be something like this:

?

$query = SELECT UserID, Password FROM customers WHERE Username =
'{$_POST['username']}';
$result = mysql_query($query);

if(mysql_num_rows($result) == 1) {
if($_POST['password'] == mysql_result($result, 0, 'Password')) {
$_SESSION['logged_in'] = 'Yes';
$_SESSION['user_id'] = mysql_result($result, 0, 'UserID');
header('location: index.php);
}else{
echo Invalid Password!;
}
}else{
echo Invalid Username!;
}

?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting records in db

2006-10-26 Thread Paul Novitski

At 10/26/2006 11:16 AM, [EMAIL PROTECTED] wrote:

Would it be ok to use the same code to check if customer is loged in?

$query = mysql_query(
   SELECT COUNT(Username) as NoOfRecords
   FROM customers
   WHERE Username = '$Username' AND Password = '$Password');
if (mysql_result($query, 0) == 0)



I'd question whether you really needed to get a record count.  If 
your logon system is robust, each member will be listed only once; 
even if not, it's probably not the number of records you're 
interested in learning but whether or not there are any.  I'd suffice with:


SELECT Username
FROM customers
WHERE Username = '$Username' AND Password = '$Password'
LIMIT 1,0;

In a case like this, where I'm confident I have either one record or 
none, I'd be happy to use mysqsl_num_rows().


In the example above, which field you select is arbitrary, unless you 
actually want more information about the logged-on user:


SELECT FirstName, LastName, SecurityLevel
FROM customers
WHERE Username = '$Username' AND Password = '$Password'
LIMIT 1,0;

Regards,
Paul

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting records in db

2006-10-26 Thread afan
Would it be ok to use the same code to check if customer is loged in?

$query = mysql_query(
SELECT COUNT(Username) as NoOfRecords
FROM customers
WHERE Username = '$Username' AND Password =
 '$Password');
if (mysql_result($query, 0) == 0)

I just realized that the answer I already got in previous answers :)
Didn't pay an atention!
:)

Thanks.

-afan

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Counting files in directory and subdirectory!

2006-01-25 Thread tomasz abramowicz

start here: http://www.php.net/readdir

check out as well: 'string functions' for finding 'amrs'
and loops to make recursive search...

t.

Nicholas Couloute wrote:
I want a script where it will count all the amrs in a folder and it's 
sub folders like this setup:

amrs 
50 cent  amrs
nitty  amrs
bob  amrs
How would I do this?
~Nick Couloute
co-owner/Web Designer
Sidekick2Music.Com



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] Counting files in directory and subdirectory!

2006-01-25 Thread Weber Sites LTD
You can start with looking at some code examples that do similar things :

Directory browser
http://www.weberdev.com/get_example-1539.html 

How to get a dir listing of the current dir of php script in an array 
http://www.weberdev.com/get_example-1855.html

filesystem Show Files Script
http://www.weberdev.com/get_example-4240.html

Sincerely 
 
berber 
 
Visit the Weber Sites Today, 
To see where PHP might take you tomorrow. 
PHP code examples : http://www.weberdev.com 
PHP  MySQL Forums : http://www.weberforums.com
Learn PHP  MySQL Playing Trivia : http://www.webertrivia.com
PHP Web Logs : http://www.weberblogs.com 
Web Development Index http://www.weberindex.com 
Web Templates http://www.webertemplates.com
Search for PHP Code from your browser http://toolbar.weberdev.com 
Free Uptime Monitor : http://uptime.weberdev.com
PHP content for your site : http://content.weber-sites.com


-Original Message-
From: Nicholas Couloute [mailto:[EMAIL PROTECTED] 
Sent: Wednesday, January 25, 2006 9:41 PM
To: php-general@lists.php.net
Subject: [PHP] Counting files in directory and subdirectory!

I want a script where it will count all the amrs in a folder and it's sub
folders like this setup:
amrs 
50 cent  amrs
nitty  amrs
bob  amrs
How would I do this?
~Nick Couloute
co-owner/Web Designer
Sidekick2Music.Com

--
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] Counting files in directory and subdirectory!

2006-01-25 Thread Nicholas Couloute
I have used these functions but how would I count them! here is my 
attempt that doesn't work!:

$count = 0;
$dir = /amrs;
$files1 = scandir($dir);
if ($files1 !== '.'  $files1 !== '..') {
foreach ($files1 as $files2){
$dir2 = /amrs/$files2;
$files3 = scandir($dir2);
if ($files3 !== '.'  $files3 !== '..') {
foreach ($files3 as $files4){
$dir3 = /amrs/$files2/$files4;
$files5 = scandir($dir3);
if ($files5 !== '.'  $files5 !== '..') {
foreach ($files5 as $files6){
$count++;
}
}
}
}
}
}
echo $count;

~Nick Couloute
co-owner/Web Designer
Sidekick2Music.Com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Counting files in directory and subdirectory!

2006-01-25 Thread Nicholas Couloute
here is what I tried but it doesn't work! any advice suggestions or 
something?


$count = 0;
$dir = /amrs;
$files1 = scandir($dir);
if ($files1 !== '.'  $files1 !== '..') {
foreach ($files1 as $files2){
$dir2 = /amrs/$files2;
$files3 = scandir($dir2);
if ($files3 !== '.'  $files3 !== '..') {
foreach ($files3 as $files4){
$dir3 = /amrs/$files2/$files4;
$files5 = scandir($dir3);
if ($files5 !== '.'  $files5 !== '..') {
foreach ($files5 as $files6){
$count++;
}
}
}
}
}
}
echo $count;
~Nick Couloute
co-owner/Web Designer
Sidekick2Music.Com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Counting files in directory and subdirectory!

2006-01-25 Thread David Tulloh
That is some nasty code...
Anything that has repitition like that needs a substantial rewrite, but
I'm guessing that you know that, hence the e-mail.

I just knocked up the following function, powered by the magic of
recursion.  It should be close to what you were trying to do.


function count_files($path) {
$number_of_files = 0;
foreach(scandir($path) as $dir) {
if($dir{0} == '.') continue; # Ignore hidden files
if(is_dir($path.'/'.$dir))
$number_of_files += count_files($path.'/'.$dir);
else
$number_of_files++;
}
return $number_of_files;
}

echo count_files(amrs);



David

Nicholas Couloute wrote:
 here is what I tried but it doesn't work! any advice suggestions or
 something?
 
 $count = 0;
 $dir = /amrs;
 $files1 = scandir($dir);
 if ($files1 !== '.'  $files1 !== '..') {
 foreach ($files1 as $files2){
 $dir2 = /amrs/$files2;
 $files3 = scandir($dir2);
 if ($files3 !== '.'  $files3 !== '..') {
 foreach ($files3 as $files4){
 $dir3 = /amrs/$files2/$files4;
 $files5 = scandir($dir3);
 if ($files5 !== '.'  $files5 !== '..') {
 foreach ($files5 as $files6){
 $count++;
 }
 }
 }
 }
 }
 }
 echo $count;
 ~Nick Couloute
 co-owner/Web Designer
 Sidekick2Music.Com
 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] Counting in Hex

2006-01-04 Thread Ben Miller
Never mind - wasn't adding properly.  Found it after all on php.net, anyway.
Thanks anyway.

-Original Message-
From: Ben Miller [mailto:[EMAIL PROTECTED]
Sent: Wednesday, January 04, 2006 5:01 PM
To: php-general@lists.php.net
Subject: [PHP] Counting in Hex


Is there a way in PHP to count using hex instead of numerals so that the
following statement

$var = 237F;
$var++;
echo $var;

would result in

2380

Thanks a bunch.

Ben

--
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] counting nested array

2005-08-17 Thread Robin Vickery
On 8/17/05, Ing. Josué Aranda [EMAIL PROTECTED] wrote:
 OK this the little function i made to solve this..

 function countNested($array){
 foreach($array as $value){
 if(is_array($value)) 
 $total=$this-countNested($value)+$total;
 }else{
 $total=$total+1;
 }
 }
 return $total;
 }

Looks OK-ish - there's a missing '{' on the third line but apart from
that it should work fine as a class method.
 
 any optimizations are welcome

You can simplify the if-block as below, which might save you as much
as a microsecond or two :-)

   function countNested($array){
  $total = 0;
   foreach ($array as $value) {
   $total += is_array($value) ? $this-countNested($value) : 1;
   }
   return $total;
   }

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting nested array

2005-08-17 Thread Ing . Josué Aranda
hahaha, thanks robin you save some seconds of mi life... it looks more
pro with that if...

On 8/17/05, Robin Vickery [EMAIL PROTECTED] wrote:
 On 8/17/05, Ing. Josué Aranda [EMAIL PROTECTED] wrote:
  OK this the little function i made to solve this..
 
  function countNested($array){
  foreach($array as $value){
  if(is_array($value))
  $total=$this-countNested($value)+$total;
  }else{
  $total=$total+1;
  }
  }
  return $total;
  }
 
 Looks OK-ish - there's a missing '{' on the third line but apart from
 that it should work fine as a class method.
 
  any optimizations are welcome
 
 You can simplify the if-block as below, which might save you as much
 as a microsecond or two :-)
 
function countNested($array){
   $total = 0;
foreach ($array as $value) {
$total += is_array($value) ? $this-countNested($value) : 1;
}
return $total;
}
 
 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php
 
 


-- 


JOSUE ARANDA

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting nested array

2005-08-16 Thread Torgny Bjers
Ing. Josué Aranda wrote:

Hi to everyone..

now i have a little problem counting an nested array. Im using it to
fill a Java TreeView... it looks like this:

[snip]

The number of the branches is not always the same.. (it depends on the
query).. when i use count($array, COUNT_RECURSIVE) for nested arrays..
it give to me the total including the nodes in the branches ( in this
case 28).. now here is the question, how i can get only the last nodes
in this case ... exist a easy way to do it?. or its necessary to make
a funcion with a bunch of foreach?.. any suggestions are welcome =o) 
thanks!


Well, if you do a count of the root, you ought to get the amount of
numbers in there, and, with that number of the last item, access that
array, do a count() on that, get the last item, and so forth and so on,
you could write a small recursive function here to call itself with each
new child, getting the last item, down into the structure until X level.

Not sure if there's a PHP native function for doing this, though.
Perhaps someone else knows. If nothing else, try this way.

Regards,
Torgny

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting nested array

2005-08-16 Thread Robin Vickery
On 8/16/05, Ing. Josué Aranda [EMAIL PROTECTED] wrote:

 The number of the branches is not always the same.. (it depends on the
 query).. when i use count($array, COUNT_RECURSIVE) for nested arrays..
 it give to me the total including the nodes in the branches ( in this
 case 28).. now here is the question, how i can get only the last nodes
 in this case ... exist a easy way to do it?. or its necessary to make
 a funcion with a bunch of foreach?.. any suggestions are welcome =o)
 thanks!

If I understand you correctly, you only want the leaves of your tree -
in your example, that would be 20?

I don't think there's a convenient builtin function that'll do it, but
it's not hard to write your own:

?php

function leaf_count($item) {
  $count = 0;

  if (!is_array($item)) { return 1; }

  foreach ($item as $element) {
$count += leaf_count($element);
  }

  return $count;
}

print leaf_count($array);

?

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting nested array

2005-08-16 Thread Ing . Josué Aranda
OK this the little function i made to solve this..
[CODE]

function countNested($array){
foreach($array as $value){
if(is_array($value))
$total=$this-countNested($value)+$total;
}else{
$total=$total+1;
}
}
return $total;
}

[/CODE]

any optimizations are welcome



On 8/16/05, Robin Vickery [EMAIL PROTECTED] wrote:
 On 8/16/05, Ing. Josué Aranda [EMAIL PROTECTED] wrote:
 
  The number of the branches is not always the same.. (it depends on the
  query).. when i use count($array, COUNT_RECURSIVE) for nested arrays..
  it give to me the total including the nodes in the branches ( in this
  case 28).. now here is the question, how i can get only the last nodes
  in this case ... exist a easy way to do it?. or its necessary to make
  a funcion with a bunch of foreach?.. any suggestions are welcome =o)
  thanks!
 
 If I understand you correctly, you only want the leaves of your tree -
 in your example, that would be 20?
 
 I don't think there's a convenient builtin function that'll do it, but
 it's not hard to write your own:
 
 ?php
 
 function leaf_count($item) {
   $count = 0;
 
   if (!is_array($item)) { return 1; }
 
   foreach ($item as $element) {
 $count += leaf_count($element);
   }
 
   return $count;
 }
 
 print leaf_count($array);
 
 ?
 
 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php
 
 


-- 


JOSUE ARANDA

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting ocuring words in a string

2005-05-17 Thread Petar Nedyalkov
On Tuesday 17 May 2005 13:00, Merlin wrote:
 Hi there,

 I am trying to find a way to count the number of times (if any) words are
 inside a string. So I played around with ereg, preg_match_all and so on,
 but could not put together a working code.

 Can anybody help me on that?

 This is the current code:
 function count_words($str, $words) {
if(is_array($words)) {
 foreach($words as $k = $word) {
   $pattern[$k] = /\b($word)\b/is;
 }
}
else {
 $pattern = /\b($words)\b/is;
}
return ereg( $pattern, $str);
 }
 $words = 'php language';
 $str = 'One language which is great is php. PHP works great!';
 $num_hits = count_words($str, $word);
 echo $num_hits;

 I would like to get an case insensitive result. In this example: 3

http://www.php.net/manual/en/function.substr-count.php

Try this.


 I am a bit lost on this one. Thank you for any help.

 Merlin

-- 

Cyberly yours,
Petar Nedyalkov
Devoted Orbitel Fan :-)

PGP ID: 7AE45436
PGP Public Key: http://bu.orbitel.bg/pgp/bu.asc
PGP Fingerprint: 7923 8D52 B145 02E8 6F63 8BDA 2D3F 7C0B 7AE4 5436


pgpvE9fqC9aji.pgp
Description: PGP signature


Re: [PHP] counting ocuring words in a string

2005-05-17 Thread Brian V Bonini
On Tue, 2005-05-17 at 06:00, Merlin wrote:
 Hi there,
 
 I am trying to find a way to count the number of times (if any) words are 
 inside 
 a string. So I played around with ereg, preg_match_all and so on, but could 
 not 
 put together a working code.

Maybe something like this?

?php

$words = array(php, language);
$str = 'One language which is great is php. PHP works great!';
$str = strtolower($str);

echo 'pre';

foreach($words as $word) {
echo 'word = ' .$word;
echo ' (' . substr_count($str, $word). )\n;
}

echo '/pre';

?

Although you'll run into a problem if your words array contains for
example 'air' which will match both 'air' and 'fair',

-- 

s/:-[(/]/:-)/g


BrianGnuPG - KeyID: 0x04A4F0DC | Key Server: pgp.mit.edu
==
gpg --keyserver pgp.mit.edu --recv-keys 04A4F0DC
Key Info: http://gfx-design.com/keys
Linux Registered User #339825 at http://counter.li.org

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting ocuring words in a string

2005-05-17 Thread Jason Wong
On Tuesday 17 May 2005 18:00, Merlin wrote:

 I am trying to find a way to count the number of times (if any) words
 are inside a string.

  explode()
  array_count_values()

-- 
Jason Wong - Gremlins Associates - www.gremlins.biz
Open Source Software Systems Integrators
* Web Design  Hosting * Internet  Intranet Applications Development *
--
Search the list archives before you post
http://marc.theaimsgroup.com/?l=php-general
--
New Year Resolution: Ignore top posted posts

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Counting numbers on while..

2005-03-28 Thread Devraj Mukherjee
You basically dont need the for loop all you have to do is increment the 
value of the variable by one each time in the while loop

Devraj
Louie Miranda wrote:
Hi, im trying to create a number loop on a table where the data came
in a database.
I have tried the for loop and counted $countCards_result from the
table where im going to get all the data.
## code ##
while ($profile-fetchInto($row)) {
for ($i = 1; $i = $countCards_result; $i++) {
$num = $i;
print(
tr
td$num/td
td .$row[2]. /td
/tr
);
} // end of for loop
} // end of while
## code ##
Well, it did loop the numbers from what did came out of
$countCards_result but, the results are wrong because it loops each
row from 1 to 10.
Here's the example:
1   louie   edit | delete
2   louie   edit | delete
3   louie   edit | delete
4   louie   edit | delete
5   louie   edit | delete
6   louie   edit | delete
7   louie   edit | delete
8   louie   edit | delete
9   louie   edit | delete
10  louie   edit | delete
1   miranda edit | delete
2   miranda edit | delete
3   miranda edit | delete
4   miranda edit | delete
5   miranda edit | delete
6   miranda edit | delete
7   miranda edit | delete
8   miranda edit | delete
9   miranda edit | delete
10  miranda edit | delete
And so on..
What i wanted it to do was..
1   louie   edit | delete
2   miranda edit | delete
And so on..
I was wondering how can i get it to count only the rows, and dont
repeat from 1 to 10 on all rows.
Please help!
--
Louie Miranda
http://www.axishift.com

--
Devraj Mukherjee ([EMAIL PROTECTED])
Eternity Technologies Pty. Ltd. ACN 107 600 975
P O Box 5949 Wagga Wagga NSW 2650 Australia
Voice: +61-2-69717131 / Fax: +61-2-69251039
http://www.eternitytechnologies.com/
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] counting chars..

2004-12-15 Thread Jake Press
Hi Louie,
Excellent example! i wish more users would take the time to provide such 
clear examples.

The strlen() exists for you :)
ie.
?php
$string = function yes good;
$display = strlen($string);
echo $display;
?
:)
Best Regards
Jake Press
Louie Miranda wrote:
?php
$string = function yes good;
$display = count_chars($string);
echo $display;
?
i know this is wrong, but how can i count chars used here?
 

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] counting chars..

2004-12-15 Thread Robby Russell
On Thu, 2004-12-16 at 14:28 +0800, Louie Miranda wrote:
 ?php
 $string = function yes good;
 $display = count_chars($string);
 
 echo $display;
 ?
 
 i know this is wrong, but how can i count chars used here?

php.net/strlen ?

-Robby
-- 
/***
* Robby Russell | Owner.Developer.Geek
* PLANET ARGON  | www.planetargon.com
* Portland, OR  | [EMAIL PROTECTED]
* 503.351.4730  | blog.planetargon.com
* PHP/PostgreSQL Hosting  Development
*--- Now supporting PHP5 ---
/

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Counting download problem

2004-07-20 Thread Jason Wong
On Tuesday 20 July 2004 16:44, Rausch Alexandru wrote:

 I have a script that creates automatically a zip arhive.
 The script works fine, but I want to count downloads, and I made that
 to, but if the user click cancel in the download panel the download it
 is also counting. How can I prevent this from happening?

Have a look at connection_aborted().

-- 
Jason Wong - Gremlins Associates - www.gremlins.biz
Open Source Software Systems Integrators
* Web Design  Hosting * Internet  Intranet Applications Development *
--
Search the list archives before you post
http://marc.theaimsgroup.com/?l=php-general
--
/*
Regardless of the legal speed limit, your Buick must be operated at
speeds faster than 85 MPH (140kph).
-- 1987 Buick Grand National owners manual.
*/

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Counting number of characters without spaces between them?

2004-04-23 Thread Tom Rogers
Hi,

Friday, April 23, 2004, 11:50:56 PM, you wrote:
RD Hi all,

RD It's a warm sunny day and I'm trying to wrap my head around the
RD following:

RD I want to be able to check to see if, in a string, the user has
RD entered too many consecutive characters and not enough spaces. For
RD example they might enter a subject like:

RD hello world! how arrre you today?!!

RD Does anyone have a nice technique for checking the following:

RD 1) Count the length of the words in the string, i.e. if there are any
RD words beyond say 20 characters then I need to know what they are so I
RD can flag up a warning.

RD 2) Count the number of punctuation characters vs. alphanumerics - I
RD don't want the exclamation mark syndrome shown above, so I need to set
RD a friendly trade-off limit somehow.

RD Any thoughts appreciated.

RD -- 
RD Best regards,
RD  Richard Davey
RD  http://www.phpcommunity.org/wiki/296.html


You could try something like this

$string = hello world! how arrre you today?!!;
if(preg_match_all('/(\w)\1{4,}|(\W)\2{4,}/',$string,$match)){
  echo too many repeated charactersbr;
  print_r($match);
}
That will catch everything, except numbers, that is repeated 4 or more
times.
-- 
regards,
Tom

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Counting number of characters without spaces between them?

2004-04-23 Thread Chris Shiflett
--- Richard Davey [EMAIL PROTECTED] wrote:
 I want to be able to check to see if, in a string, the user has
 entered too many consecutive characters and not enough spaces. For
 example they might enter a subject like:
 
 hello world! how arrre you today?!!
 
 Does anyone have a nice technique for checking the following:
 
 1) Count the length of the words in the string, i.e. if there are any
 words beyond say 20 characters then I need to know what they are so I
 can flag up a warning.

You can explode on a space and loop through the resulting array, checking
strlen() as you go.

 2) Count the number of punctuation characters vs. alphanumerics - I
 don't want the exclamation mark syndrome shown above, so I need to set
 a friendly trade-off limit somehow.

Perhaps substr_count() and a bit of guesswork to find an acceptable
proportion of punctuation allowed?

Chris

=
Chris Shiflett - http://shiflett.org/

PHP Security - O'Reilly
 Coming Fall 2004
HTTP Developer's Handbook - Sams
 http://httphandbook.org/
PHP Community Site
 http://phpcommunity.org/

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting downloaded files

2004-02-10 Thread Marek Kilimajer
Adriaan Nel wrote:

Hi,

I am using the following piece of code, to download files, but the files are
allways corrupt...what am I doing wrong?
if ($submit) {
 $full_filename = $_POST[full_filename];
 $filename = $_POST[filename];
 $dir = $_POST['dir'];
 $size = $_POST[size];
 $id = $_POST[id];
 if((file_exists($full_filename))  (substr(decoct(fileperms($dir)),-1) =
4)) {
  header(Content-type: . application/exe);
  header(Content-Disposition:  . attachment . ;
filename=\.$filename).\;
  header(Content-Length: .$size);
  $fp = readfile($full_filename, r);
  mysql_query (INSERT INTO files_downloaded
VALUES('$id','$ip',NOW()),$db_connection) or die (mysql_error());
  return $fp;
 }
}
You might have a php warning in the downloaded file, it will be either 
at the beginning or at the end, just open a text editor and view it.

Also you don't check $full_filename, users can submit enything, e.g. 
/etc/shadow or includes/config.php, and the script sends it.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] counting downloaded files

2004-02-10 Thread Adriaan Nel
The variables I submit, are retrieved from a database...so users can't enter
anything,

I do have a warning inserted at the beginning of the file, how do I remove
this warning?


Marek Kilimajer [EMAIL PROTECTED] wrote in message
news:[EMAIL PROTECTED]
 Adriaan Nel wrote:

  Hi,
 
  I am using the following piece of code, to download files, but the files
are
  allways corrupt...what am I doing wrong?
 
 
  if ($submit) {
   $full_filename = $_POST[full_filename];
   $filename = $_POST[filename];
   $dir = $_POST['dir'];
   $size = $_POST[size];
   $id = $_POST[id];
 
   if((file_exists($full_filename))  (substr(decoct(fileperms($dir)),-1)
=
  4)) {
header(Content-type: . application/exe);
header(Content-Disposition:  . attachment . ;
  filename=\.$filename).\;
header(Content-Length: .$size);
$fp = readfile($full_filename, r);
mysql_query (INSERT INTO files_downloaded
  VALUES('$id','$ip',NOW()),$db_connection) or die (mysql_error());
return $fp;
   }
  }
 

 You might have a php warning in the downloaded file, it will be either
 at the beginning or at the end, just open a text editor and view it.

 Also you don't check $full_filename, users can submit enything, e.g.
 /etc/shadow or includes/config.php, and the script sends it.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting downloaded files

2004-02-10 Thread Marek Kilimajer
Adriaan Nel wrote:

The variables I submit, are retrieved from a database...so users can't enter
anything,
What do you mean? Even if the variables are in a hidden input field, 
anyone can change them. For example one can save the page and change 
them in html source.

I do have a warning inserted at the beginning of the file, how do I remove
this warning?
I won't tell you unless you tell what warning it is. But I'm sure if you 
think about the warning a little you will figure it out.



Marek Kilimajer [EMAIL PROTECTED] wrote in message
news:[EMAIL PROTECTED]
Adriaan Nel wrote:


Hi,

I am using the following piece of code, to download files, but the files
are

allways corrupt...what am I doing wrong?

if ($submit) {
$full_filename = $_POST[full_filename];
$filename = $_POST[filename];
$dir = $_POST['dir'];
$size = $_POST[size];
$id = $_POST[id];
if((file_exists($full_filename))  (substr(decoct(fileperms($dir)),-1)
=

4)) {
 header(Content-type: . application/exe);
 header(Content-Disposition:  . attachment . ;
filename=\.$filename).\;
 header(Content-Length: .$size);
 $fp = readfile($full_filename, r);
 mysql_query (INSERT INTO files_downloaded
VALUES('$id','$ip',NOW()),$db_connection) or die (mysql_error());
 return $fp;
}
}
You might have a php warning in the downloaded file, it will be either
at the beginning or at the end, just open a text editor and view it.
Also you don't check $full_filename, users can submit enything, e.g.
/etc/shadow or includes/config.php, and the script sends it.


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


RE: [PHP] counting charactors

2004-01-06 Thread Jay Blanchard
[snip]
can someone point me to the right place for a thing that would count
charactors in a string?
[/snip]

http://www.php.net/strlen

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting charactors

2004-01-06 Thread Richard Davey
Hello Philip,

Tuesday, January 6, 2004, 6:25:34 PM, you wrote:

PJN can someone point me to the right place for a thing that
PJN would count charactors in a string?

count_chars() and strlen()

-- 
Best regards,
 Richardmailto:[EMAIL PROTECTED]

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting charactors

2004-01-06 Thread Tyler Longren
Hi Philip,

strlen() will work.
http://www.php.net/manual/en/function.strlen.php

Best Regards,
--
Tyler Longren
J.D. Web Services, L.C.

On Wed, 2004-01-07 at 01:25, Philip J. Newman wrote:
 can someone point me to the right place for a thing that would count charactors in a 
 string?
 
 ---
 Philip J. Newman
 Master Developer
 PhilipNZ.com [NZ] Ltd.
 [EMAIL PROTECTED]

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Counting back 90 days...

2004-01-05 Thread Richard Davey
Hello Tristan,

Monday, January 5, 2004, 11:29:16 AM, you wrote:

TPrsc But is there an easy was to calculate 90 days from the timestamp...

Based on the current time:

$previous_90_days_timestamp = strtotime (-90 day);

All in one:

$previous_90_days_date = date(Y-m-d, strtotime(-90 day));

-- 
Best regards,
 Richardmailto:[EMAIL PROTECTED]

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Counting back 90 days...

2004-01-05 Thread Jon Haworth
Hi Tris,

 But is there an easy was to calculate 90 days 
 from the timestamp...

There are 86400 seconds in a day... can't you just do something like:
  
  $timestamp -= (86400 * 90);

Or even

  $timestamp -= 7776000; // 90 days

Cheers
Jon

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] Counting back 90 days...

2004-01-05 Thread Jami
Easier way to do that is like so:

$archive_date = date(Y-m-d, (time()-(86400*90)));

I use this same kind of calculation for getting a date in the future.
86400 is the number of seconds in a day. Time returns the Unix
Timestamp, just like you would get with mktime(). Makes for a lot less
code and you don't need a function to do it. Hope this helps.

Jami Moore
LightSPARK Digital Designs
[EMAIL PROTECTED]
http://www.lightsparkdigital.com/


 -Original Message-
 From: [EMAIL PROTECTED] 
 
 
 Got this function...
 I wanna remove 90 days from todays date, giving a final 
 date, in the same 
 format as the original, that counts back 90 days...
 My code is below, and if anyone would like to offer advse, I 
 welcome it...
 
 ===
 $today = date(Y-m-d);
 function minus90($calc){
$year = substr($today, 0, 4);
$month = substr($today, 5, -3);
$day = substr($today, 8, 10);
$timestamp = mktime (0,0,0,$month,$day,$year);
return strftime (%Y-%m-%d, $timestamp);
 }
 $archive_date = minus90($today);
 
 
 echo $archive_date;
 ==

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Counting back 90 days...

2004-01-05 Thread Tristan . Pretty
My word...
6 odd lines, to one...
you're a genius...
Cheers for that...
Tris...




Richard Davey [EMAIL PROTECTED] 
05/01/2004 11:45
Please respond to
Richard Davey [EMAIL PROTECTED]


To
[EMAIL PROTECTED]
cc
[EMAIL PROTECTED] [EMAIL PROTECTED]
Subject
Re: [PHP] Counting back 90 days...






Hello Tristan,

Monday, January 5, 2004, 11:29:16 AM, you wrote:

TPrsc But is there an easy was to calculate 90 days from the timestamp...

Based on the current time:

$previous_90_days_timestamp = strtotime (-90 day);

All in one:

$previous_90_days_date = date(Y-m-d, strtotime(-90 day));

-- 
Best regards,
 Richardmailto:[EMAIL PROTECTED]

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




*
The information contained in this e-mail message is intended only for 
the personal and confidential use of the recipient(s) named above.  
If the reader of this message is not the intended recipient or an agent
responsible for delivering it to the intended recipient, you are hereby 
notified that you have received this document in error and that any
review, dissemination, distribution, or copying of this message is 
strictly prohibited. If you have received this communication in error, 
please notify us immediately by e-mail, and delete the original message.
***



Re: [PHP] Counting back 90 days...

2004-01-05 Thread Paul Chvostek
Richard Davey [EMAIL PROTECTED] 05/01/2004 11:45:
 
 Based on the current time:
 $previous_90_days_timestamp = strtotime (-90 day);
 All in one:
 $previous_90_days_date = date(Y-m-d, strtotime(-90 day));

On Mon, Jan 05, 2004 at 11:55:36AM +, [EMAIL PROTECTED] wrote:
 
 My word...
 6 odd lines, to one...

Beware timestamps, though.  They usually won't work for dates before
1970 or after 2038.  If you suspect you'll need to use dates outside
that period (for example, show the 90 days of whatever as of some date
in 1969), your results may be unpredictable.

For example, strftime(%+,0) returns Wed Dec 31 19:00:00 EST 1969 in
my timezone, and strftime(%+,-1) returns a blank.  Yet, doing a
strtotime(1969-12-31 23:59:59 GMT) will get you a -1, and the date()
function appears to support negative timestamps ... which means you
can't skimp on your testing if you decide to go this route.  ;-)

-- 
  Paul Chvostek [EMAIL PROTECTED]
  it.canadahttp://www.it.ca/
  Free PHP web hosting!http://www.it.ca/web/

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



  1   2   >