[PHP] Why does this script run out of memory?

2011-10-28 Thread Jim Long
I'm running PHP 5.3.8 on FreeBSD 8.2 with MySQL 5.1.55.

The script below is designed to be able to WHILE it's way through
a MySQL query result set, and process each row.

However, it runs out of memory a little after a quarter million
rows.  The schema fields total to about 200 bytes per row, so
the row size doesn't seem very large.

Why is this running out of memory?

Thank you!

Jim

?php

$test_db_host = localhost;
$test_db_user = foo;
$test_db_pwd  = bar;
$test_db_name = farkle;

$db_host = $test_db_host;
$db_user = $test_db_user;
$db_name = $test_db_name;
$db_pwd  = $test_db_pwd;

if (!($db_conn = mysql_connect( $db_host, $db_user, $db_pwd )))
die( Can't connect to MySQL server\n );

if (!mysql_select_db( $db_name, $db_conn ))
die( Can't connect to database $db_name\n );

$qry = select * from test_table order by contract;

if ($result = mysql_query( $qry, $db_conn )) {

$n = 0;
while ($row = mysql_fetch_assoc( $result )) {
// process row here
$n++;
} // while

mysql_free_result($result);
echo $n\n;

} else {

die( mysql_error() . \n );

}

?


PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to 
allocate 20 bytes) in xx3.php on line 24

Line 24 is:

24  while ($row = mysql_fetch_assoc( $result )) {


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



Re: [PHP] Why does this script run out of memory?

2011-10-28 Thread Jim Long
On Fri, Oct 28, 2011 at 01:21:36PM -0400, Eric Butera wrote:
 On Fri, Oct 28, 2011 at 12:38 PM, Jim Long p...@umpquanet.com wrote:
  I'm running PHP 5.3.8 on FreeBSD 8.2 with MySQL 5.1.55.
 
  The script below is designed to be able to WHILE it's way through
  a MySQL query result set, and process each row.
 
  However, it runs out of memory a little after a quarter million
  rows. ??The schema fields total to about 200 bytes per row, so
  the row size doesn't seem very large.
 
  Why is this running out of memory?
 
  Thank you!
 
  Jim
 
  ?php
 
  $test_db_host = localhost;
  $test_db_user = foo;
  $test_db_pwd ??= bar;
  $test_db_name = farkle;
 
  $db_host = $test_db_host;
  $db_user = $test_db_user;
  $db_name = $test_db_name;
  $db_pwd ??= $test_db_pwd;
 
  if (!($db_conn = mysql_connect( $db_host, $db_user, $db_pwd )))
  ?? ?? ?? ??die( Can't connect to MySQL server\n );
 
  if (!mysql_select_db( $db_name, $db_conn ))
  ?? ?? ?? ??die( Can't connect to database $db_name\n );
 
  $qry = select * from test_table order by contract;
 
  if ($result = mysql_query( $qry, $db_conn )) {
 
  ?? ?? ?? ??$n = 0;
  ?? ?? ?? ??while ($row = mysql_fetch_assoc( $result )) {
  // process row here
  ?? ?? ?? ?? ?? ?? ?? ??$n++;
  ?? ?? ?? ??} // while
 
  ?? ?? ?? ??mysql_free_result($result);
  ?? ?? ?? ??echo $n\n;
 
  } else {
 
  ?? ?? ?? ??die( mysql_error() . \n );
 
  }
 
  ?
 
 
  PHP Fatal error: ??Allowed memory size of 134217728 bytes exhausted (tried 
  to allocate 20 bytes) in xx3.php on line 24
 
  Line 24 is:
 
  ?? ??24 ?? ?? ?? ?? ??while ($row = mysql_fetch_assoc( $result )) {
 
 
  --
  PHP General Mailing List (http://www.php.net/)
  To unsubscribe, visit: http://www.php.net/unsub.php
 
 
 
 Not sure what is happening inside process row here, but I'm sure
 that is where your issue is.  Instead of building some giant structure
 inside of that while statement you should flush it out to the screen.

Eric:

Thanks for your reply.

process row here is a comment.  It doesn't do anything.  The
script, exactly as shown, runs out of memory, exactly as shown.

Jim

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



Re: Re: [PHP] Why does this script run out of memory?

2011-10-28 Thread Jim Long
On Fri, Oct 28, 2011 at 01:32:32PM -0400, James wrote:
 
 On Fri, Oct 28, 2011 at 12:38 PM, Jim Long p...@umpquanet.com wrote:
  I'm running PHP 5.3.8 on FreeBSD 8.2 with MySQL 5.1.55.
 
  The script below is designed to be able to WHILE it's way through
  a MySQL query result set, and process each row.
 
  However, it runs out of memory a little after a quarter million
  rows. ??The schema fields total to about 200 bytes per row, so
  the row size doesn't seem very large.
 
  Why is this running out of memory?
 
  Thank you!
 
  Jim
 
  ?php
 
  $test_db_host = localhost;
  $test_db_user = foo;
  $test_db_pwd ??= bar;
  $test_db_name = farkle;
 
  $db_host = $test_db_host;
  $db_user = $test_db_user;
  $db_name = $test_db_name;
  $db_pwd ??= $test_db_pwd;
 
  if (!($db_conn = mysql_connect( $db_host, $db_user, $db_pwd )))
  ?? ?? ?? ??die( Can't connect to MySQL server\n );
 
  if (!mysql_select_db( $db_name, $db_conn ))
  ?? ?? ?? ??die( Can't connect to database $db_name\n );
 
  $qry = select * from test_table order by contract;
 
  if ($result = mysql_query( $qry, $db_conn )) {
 
  ?? ?? ?? ??$n = 0;
  ?? ?? ?? ??while ($row = mysql_fetch_assoc( $result )) {
  // process row here
  ?? ?? ?? ?? ?? ?? ?? ??$n++;
  ?? ?? ?? ??} // while
 
  ?? ?? ?? ??mysql_free_result($result);
  ?? ?? ?? ??echo $n\n;
 
  } else {
 
  ?? ?? ?? ??die( mysql_error() . \n );
 
  }
 
  ?
 
 
  PHP Fatal error: ??Allowed memory size of 134217728 bytes exhausted (tried 
  to allocate 20 bytes) in xx3.php on line 24
 
  Line 24 is:
 
  ?? ??24 ?? ?? ?? ?? ??while ($row = mysql_fetch_assoc( $result )) {
 
 
 Not sure what is happening inside process row here, but I'm sure
 that is where your issue is.  Instead of building some giant structure
 inside of that while statement you should flush it out to the screen.
 
 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php
 
 Try unsetting the $row variable, you may be fetching extremely
 large rows but that's a big if, because your script is allowed to
 allocate 128MB of memory before puking. Are you dealing with very
 large data sets from the database? If you are dealing with large
 data sets, then try redefining your query.

James:

Thanks for taking time to help.

The row size is small by my standards (see below).  The query result
has just under 300,000 records, and it's puking about 90% of the way
through.

Changing the while loop to:

while ($row = mysql_fetch_assoc( $result )) {
$n++;
echo sprintf( %7d %12d\n, $n, memory_get_peak_usage() );
} // while

the tail end of the output becomes:

 274695134203084
 274696134203524
 274697134203964
PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to 
allocate 240 bytes) in xx3.php on line 26

Changing the while loop further to:

while ($row = mysql_fetch_assoc( $result )) {
unset( $row );
$n++;
echo sprintf( %7d %12d\n, $n, memory_get_peak_usage() );
} // while

the tail end of the output becomes:

 274695134202232
 274696134202672
 274697134203112
 274698134203552
 274699134203992
PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to 
allocate 240 bytes) in xx3.php on line 27

So it does get a little farther through the dataset, but not much.

Jim


mysql describe test_table;
+--+-+--+-+-+---+
| Field| Type| Null | Key | Default | Extra |
+--+-+--+-+-+---+
| contract | int(11) | YES  | | NULL|   |
| A| int(8) unsigned | NO   | | 0   |   |
| B| datetime| YES  | | NULL|   |
| C| int(8) unsigned | YES  | | 0   |   |
| D| char(8) | YES  | | NULL|   |
| E| char(8) | YES  | | |   |
| F| int(4)  | YES  | | 0   |   |
| G| int(1)  | YES  | | 0   |   |
| H| char(8) | YES  | | 00:00   |   |
| I| varchar(100)| YES  | | XXX |   |
+--+-+--+-+-+---+
10 rows in set (0.00 sec)


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



Re: [PHP] BP for Looping through an Array

2011-10-28 Thread Jim Long
On Fri, Oct 28, 2011 at 12:09:24PM -0600, George Langley wrote:
   Hi all. Am wondering if there is a best practice for looping through an 
 array, when you need both the item and its position (ie. 1, 2, 3). The 
 two ways I know of:
 
 // the for loop tracks the position and you get each item from the array
   $allFiles = array(coffee.jpg, tea.jpg, milk.jpg);
   $numberOfFiles = count($allFiles);
   for ($i=1; $i=$numberOfFiles; $i++) {
   $currFile = $allFiles[$i - 1]; // since arrays start with 0
   doThisWith($currFile);
   doThatWith($i);
   }
 
 OR:
 
 // the for loop gets each item and you track the position
   $allFiles = array(coffee.jpg, tea.jpg, milk.jpg);
   $counter = 1;
   foreach ($allFiles as $currFile) {
   doThisWith($currFile);
   doThatWith($counter);
   $counter += 1;
   }
 
   Both are the same number of lines, but my tests (see code below) 
 indicate that the foreach loop is twice as fast.
   Anyone have a better way - faster, more efficient, cooler, etc.? 
 (Or see some flaw in my test code below?)
   Thanks.
 
 George Langley

If you are certain that your array is consecutively indexed from 
0, you can shave two lines off your code with:

$allFiles = array(coffee.jpg, tea.jpg, milk.jpg);
foreach ($allFiles as $key = $currFile) {
doThisWith($currFile);
doThatWith($key+1);
}


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



Re: [PHP] Why does this script run out of memory?

2011-10-28 Thread Jim Long
On Fri, Oct 28, 2011 at 03:24:37PM -0400, Jim Giner wrote:
 If all you want to do is count the records, why are you not letting sql do 
 it for you instead of doing the while loop?  That's all that script is 
 doing, if that is the exact code you ran. 

Hi, Jim.

Thank you for replying.

One of the key concepts of troubleshooting is that when you
encounter a problem, you try to state the problem with as simple
a test case as possible, so that testing will not be complicated
by extraneous variables or code that may or may not have any
impact on the problem.

I don't want to just count the records, I want to get some work
done for a client.  That work involves processing every record in
the result set.  Counting is just a simple process to conduct
for the purpose of debugging this loop algorithm.

The problem is that this algorithm fails to process all the
records in the result set.

I appreciate any insights you have as to why that is happening.

Jim


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



Re: [PHP] Why does this script run out of memory?

2011-10-28 Thread Jim Long
On Fri, Oct 28, 2011 at 03:42:48PM -0400, Eric Butera wrote:
 On Fri, Oct 28, 2011 at 3:29 PM, Daniel Brown danbr...@php.net wrote:
  On Fri, Oct 28, 2011 at 13:25, Jim Long p...@umpquanet.com wrote:
 
  Eric:
 
  Thanks for your reply.
 
  process row here is a comment. ??It doesn't do anything. ??The
  script, exactly as shown, runs out of memory, exactly as shown.
 
  ?? ??My response presumes that you're planning on placing something
  into this comment area, in which the memory will only further
  increase. ??If *presumed* should be replaced by *ASSumed* in this case,
  skip mysql_unbuffered_query() and go straight for mysql_num_rows().
  Do not pass GO. ??Do not collect $200.
 
  --
  /Daniel P. Brown
  Network Infrastructure Manager
  http://www.php.net/
 
 
 I was glad to learn what comments were.

Eric: Please forgive me if I was curt in my message to you.  I
don't mean to bite the hands that are trying to help.  As Daniel
rightly observed, my concern is just that if a pretty much empty
while loop runs out of memory when trying to step through each
record, I'm really going to be hosed if I try to start doing some
productive work within the while loop.

Daniel's memory trend analysis is helpful.  I'm testing from the
command line, so there's no Apache overhead, but the memory usage
starts as:

  1 12145496
  2 12145976
  3 12146408
  4 12146804
  5 12147200
  6 12147596
...

I normally prefer to work in PostgreSQL, but the client has already
gone down the MySQL road.  Just for edification's sake, I exported
the table in PostgreSQL and re-worked my code:

if (!($db_conn = pg_connect( host=$db_host user=$db_user dbname=$db_name 
password=$db_pwd )))
die( Can't connect to SQL server\n );

$qry = select * from test_table order by contract;

if ($result = pg_query( $db_conn, $qry )) {

$n = 0;
while ($row = pg_fetch_assoc( $result )) {
unset( $row );
$n++;
echo sprintf( %7d %12d\n, $n, memory_get_peak_usage() );
} // while

pg_free_result($result);
echo $n\n;

} else {

die( pg_last_error() . \n );

}

Using PostgreSQL (on a completely different machine), this runs
to completion, and memory consumption is nearly flat:

  1   329412
  2   329724
  3   329796
  4   329796
  5   329796
...
 295283   329860
 295284   329860
 295285   329860
 295286   329860
 295287   329860
295287

If one were to describe the memory consumption as a 'leak', then
PostgreSQL is leaking at a much slower rate than MySQL.  Postgres
leaks as much over the entire run (329860-329412=448) as MySQL
does on each row.  Put another way, the MySQL version leaks
memory almost 300,000 times faster.

My PostgreSQL machine also has MySQL installed, so I ran the
MySQL version of the code on that machine for testing, a second
opinion if you like.  It leaked memory almost as bad as my
client's PHP/MySQL installation, but a little more slowly, 396
bytes or so per row.  The slower memory consumption enabled the
code to run to completion, barely:

  1 12149492
  2 12149972
  3 12150404
  4 12150800
...
 295284129087704
 295285129088100
 295286129088496
 295287129088892
295287

So is this just a difference in the programming quality of the 
database extensions for MySQL vs. PostgreSQL that one gobbles up
memory profusely, while the other one has only a slight, slow
leak?

I will try experimenting with Daniel's idea of unbuffered
queries, but my understanding is that while an unbuffered result
resource is in use, no other SQL transactions can be conducted.
Maybe I can get around that by using one MySQL connection for the
unbuffered query, and another separate MySQL connection for the
incidental SQL queries that I need to perform as I process each
record from the large dataset.

Jim

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



Re: [PHP] Why does this script run out of memory?

2011-10-28 Thread Jim Long
On Fri, Oct 28, 2011 at 02:57:02PM -0700, Tommy Pham wrote:
 On Fri, Oct 28, 2011 at 9:38 AM, Jim Long p...@umpquanet.com wrote:
 
  I'm running PHP 5.3.8 on FreeBSD 8.2 with MySQL 5.1.55.
 
 
 Jim,
 
 Installed from packages or standard port tree build?  Did you do any tweak
 for the ports build?  Any special compiler parameters in your make.conf?
 I've noticed that you used MySQL extensions.  Have you tried MySQLi to see
 if there's any difference?
 
 Regards,
 Tommy

MySQL server and PHP and extensions built from ports, without any
local tweaks.  Nothing very interesting in /etc/make.conf:

MASTER_SITE_FREEBSD=1
CPUTYPE?=p3
USA_RESIDENT=YES
NO_INET6=YES
WITHOUT_IPV6=YES
NO_I4B=true
NO_BLUETOOTH=true
NO_IPFILTER=true
NO_KERBEROS=true
NO_ATM=true # do not build ATM related programs and libraries
NOUUCP=true # do not build uucp related programs
NO_UUCP=true # do not build uucp related programs
NO_GAMES=true
NO_PROFILE=true
PERL_VERSION=5.10.1

Port options for php5-extensions are:

_OPTIONS_READ=php5-extensions-1.5
WITH_BCMATH=true
WITHOUT_BZ2=true
WITHOUT_CALENDAR=true
WITH_CTYPE=true
WITHOUT_CURL=true
WITHOUT_DBA=true
WITH_DOM=true
WITHOUT_EXIF=true
WITH_FILEINFO=true
WITH_FILTER=true
WITHOUT_FRIBIDI=true
WITH_FTP=true
WITHOUT_GD=true
WITHOUT_GETTEXT=true
WITHOUT_GMP=true
WITH_HASH=true
WITHOUT_ICONV=true
WITHOUT_IMAP=true
WITHOUT_INTERBASE=true
WITH_JSON=true
WITHOUT_LDAP=true
WITHOUT_MBSTRING=true
WITHOUT_MCRYPT=true
WITHOUT_MSSQL=true
WITH_MYSQL=true
WITHOUT_MYSQLI=true
WITHOUT_ODBC=true
WITHOUT_OPENSSL=true
WITHOUT_PCNTL=true
WITH_PDF=true
WITH_PDO=true
WITHOUT_PDO_SQLITE=true
WITH_PGSQL=true
WITH_POSIX=true
WITHOUT_PSPELL=true
WITHOUT_READLINE=true
WITHOUT_RECODE=true
WITH_SESSION=true
WITHOUT_SHMOP=true
WITH_SIMPLEXML=true
WITHOUT_SNMP=true
WITHOUT_SOAP=true
WITHOUT_SOCKETS=true
WITHOUT_SQLITE=true
WITHOUT_SQLITE3=true
WITHOUT_SYBASE_CT=true
WITHOUT_SYSVMSG=true
WITHOUT_SYSVSEM=true
WITHOUT_SYSVSHM=true
WITHOUT_TIDY=true
WITH_TOKENIZER=true
WITHOUT_WDDX=true
WITH_XML=true
WITH_XMLREADER=true
WITHOUT_XMLRPC=true
WITH_XMLWRITER=true
WITHOUT_XSL=true
WITHOUT_YAZ=true
WITHOUT_ZIP=true
WITHOUT_ZLIB=true

As Daniel suggested, using mysql_query_unbuffered works a treat,
at the expense of a small amount of additional programming
complexity.  In my prior work with Postgres, I found that it
would handle small or large datasets with equal ease, so I was
surprised to find that MySQL blew up given a sufficient number of
repeated calls to mysql_fetch_row();

Thank you for mentioning MySQLi.  Although it is alphabetically
adjacent in the documentation, it had never drawn my attention.
I'll build the PHP extension and take a look when time permits.

Jim

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



Re: [PHP] mysql_fetch_array() vs mysql_fetch_assoc() WAS: Re: [PHP] Why does this script run out of memory?

2011-10-28 Thread Jim Long
On Fri, Oct 28, 2011 at 06:19:56PM -0400, Daniel Brown wrote:
 On Fri, Oct 28, 2011 at 18:13, Paul Halliday paul.halli...@gmail.com wrote:
 
  Whats the difference between fetch_assoc and fetch_row?
 
  I use:
  while ($row = mysql_fetch_row($theQuery)) {
  ? ?doCartwheel;
  }
 
  on just under 300 million rows and nothing craps out. I have
  memory_limit set to 4GB though. Although, IIRC I pushed it up for GD
  not mysql issues.
 
  Same OS and php ver, MySQL is 5.1.48
 
 Please don't hijack other's threads to ask a question.  I've
 started this as a new thread to address this question.
 
 mysql_fetch_array() grabs all of the data and places it in a
 simple numerically-keyed array.
 
 By contrast, mysql_fetch_assoc() grabs it and populates an
 associative array.  This means that the column names (or aliases, et
 cetera) become the keys for the array.  With mysql_fetch_assoc(), you
 can still call an array key by number, but it's not vice-versa with
 mysql_fetch_array().

I'm not seeing any numeric keys in my mysql_fetch_assoc() arrays.

However, mysql_fetch_row (by default) does both: the array will be
indexed numerically from 0 to N-1 corresponding to the table's N
columns, and the array will also have string key indices which
correspond to the query's column names.  So by default,
mysql_fetch_row uses twice the amount of data, because each field
appears in the array twice.

var_dump( $row ) will show in graphic detail.


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



[PHP] SORT_NUMERIC

2003-02-28 Thread Jim Long
Hi,

Does anyone know how to make the flag sort_numeric work? 
Will it work with asort?  

asort ($numeric_array, SORT_NUMERIC);
I've tried this but it looks like it's having problems with the comma in
big numbers. 
I'm not absolutely sure, but it looks like it's ignoring everything
after a comma when it sorts.

TIA,
Jim Long

BTW: asort is the one I need as I must maintain the keys

JanetVal Wrote:

 sort() sorts by value but assigns new keys as numbers.
 asort() sorts by value, but keeps the same keys
 ksort() sorts by key.

THANKS !
-- 
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
| Jim Long Network - Web Design  |
| http://jimlong.net/web |
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
| Jim Long - Rep: Manhattan Dance Orchestra  |
| http://manhattandanceorchestra.com |
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

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



[PHP] strip comma from $value

2003-02-28 Thread Jim Long
Hi,

I've figured out the way to solve my problem is to get rid of the commas
before I sort.

Trying to use this:

//strip the commas---
foreach ($numeric_array as $key = $value) {
if (stristr($value, ,)){
//test to see if it worked
echo(comma striped); 
}
}

-- 

It passed the test but,
I'm doing something wrong because the commas are still there.

TIA,
Jim Long

Jim Long Wrote:
 Does anyone know how to make the flag sort_numeric work? 
 Will it work with asort?  
 
 asort ($numeric_array, SORT_NUMERIC);
 I've tried this but it looks like it's having problems with the comma in
 big numbers. 
 I'm not absolutely sure, but it looks like it's ignoring everything
 after a comma when it sorts.
 


 
 BTW: asort is the one I need as I must maintain the keys
 
 JanetVal Wrote:
 
  sort() sorts by value but assigns new keys as numbers.
  asort() sorts by value, but keeps the same keys
  ksort() sorts by key.
 
 THANKS !

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



[PHP] re: strip comma from $value

2003-02-28 Thread Jim Long
Hi,

Trying this:

//strip the commas from numeric array so it can sort properly---

foreach ($numeric_array as $key = $value) {
if (ereg_replace (, , , $value)){
echo(comma striped);
}
}

Does the same thing as before, echo's comma stripped, but does not
actually remove the commas

THANKS.. any other ideas?
Jim Long
--

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



[PHP] re: strip comma from $value

2003-02-28 Thread Jim Long
Hi,

Thanks to those who are helping me.

Matt,

Same result, echo's sucess, but commas are still out put in the $numeric_array.

Jim Long

Matt Wrote:

 foreach($numeric_array as $key = $value ) {
 if(strstr($value,,))
 {
 $value = ereg_replace(,,,$value);
 echo comma stripped;
 }
 }

Hugh Wrote:

 
 try ereg_replace(,,,$value);


Orignal post:

 Hi,

 I've figured out the way to solve my problem is to get rid of the commas
 before I sort.

 Trying to use this:

 //strip the commas---
 foreach ($numeric_array as $key = $value) {
 if (stristr($value, ,)){
 //test to see if it worked
 echo(comma striped);
 }
 }

 --

 It passed the test but,
 I'm doing something wrong because the commas are still there.

 TIA,
 Jim Long

 Jim Long Wrote:
  Does anyone know how to make the flag sort_numeric work?
  Will it work with asort?
 
  asort ($numeric_array, SORT_NUMERIC);
  I've tried this but it looks like it's having problems with the comma in
  big numbers.
  I'm not absolutely sure, but it looks like it's ignoring everything
  after a comma when it sorts.
 


 
  BTW: asort is the one I need as I must maintain the keys
 
  JanetVal Wrote:
 
   sort() sorts by value but assigns new keys as numbers.
   asort() sorts by value, but keeps the same keys
   ksort() sorts by key.
 
  THANKS !

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



[PHP] re: sort comma from $value

2003-02-28 Thread Jim Long
Hi,

Figured it out.

I needed to reset my output array $numeric_array.

//strip the commas from numeric array so it can sort properly---

foreach($numeric_array as $key = $value ) {
if(strstr($value,,))
{
$value = ereg_replace(,,, $value);
  $numeric_array[$key] = $value; //reset the output array
echo comma stripped;
}
}

Thanks again for all your help,

Jim Long

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



[PHP] sort mixed array

2003-02-27 Thread Jim Long
Hi,

I have an array with an html link as key and different $vars as val:

$list = array ( 
'a href=http://someplace.com/;some place/a' = $vendor1_total,
'a href=http://anotherplace.com/;another place/a' = $vendor2__total,
[snip] etc..

some vals are numeric and some vals are n/a
asort works to sort the array as a string, however, I need to sort the
numeric vals numeric, then have the n/a's go away or be output at the
bottom of the sort.

Please be clear and gentle.. I'm not schooled in php, just a tinkerer
trying to make this mess work.

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



[PHP] restating plea for help with sort

2003-02-27 Thread Jim Long
Hi,

I have an array with an html link as key and different $vars as val:

$list = array ( 
'a href=http://someplace.com/;some place/a' = $vendor1_total,
'a href=http://anotherplace.com/;another place/a' = $vendor2__total,
[snip] etc..

some vals are numeric and some vals are n/a
asort works to sort the array as a string, however, I need to sort the
numeric vals numeric, then have the n/a's go away or be output at the
bottom of the sort.

Please be clear and gentle.. I'm not schooled in php, just a tinkerer
trying to make this mess work.

Thanks In Advance,

Jim Long

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



[PHP] help with arrays

2003-02-27 Thread Jim Long
Hi,

Origninal Post:

 I have an array with an html link as key and different $vars as val:
 
 $list = array ( 
 'a href=http://someplace.com/;some place/a' = $vendor1_total,
 'a href=http://anotherplace.com/;another place/a' = $vendor2__total,
 [snip] etc..
 
 some vals are numeric and some vals are n/a
 asort works to sort the array as a string, however, I need to sort the
 numeric vals numeric, then have the n/a's go away or be output at the
 bottom of the sort.

Kevin Stone wrote: (THANKS)

 In order to sort the non n/a values you're going
 to have to build a separate array and perhaps this will simplfy the problem.
 So instead of what I suggested earlier.. loop the array and start TWO new
 arrays within the loop.  One array for n/a values and one for non-n/a
 values.  After the values are sorted to their separate arrays do
 asort($nonnavals); then concatonate the two arrays together using
 array_merge(), putting the n/a array second.


I can't fiqure out how to set up arrays inside the while loop.

Using this:

while (list ($key, $val) = each ($orderd_list)) {
if ($val == 'n/a'){
$no_plan_list = array ($key = $val);// need an array here for n/a 
}else{
$plan_list = array  ($key = $val);// need an array here for numeric 
vals
}
}


Thanks In Advance,
Jim Long

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



[PHP] separating value=n/a from array

2003-02-27 Thread Jim Long
Hi,

Thanks to those who have helped me.

Jason Lange Wrote:
 Try this code (tested):
 
 -- Begin Code --
 foreach ($list as $key = $value) {
  // If elements value is a numeral add to numeric_array
  if (is_numeric($value)) {
  $numeric_array[$key] = $value;
  // If value is not a numeral (presumably 'n/a') add to na_array
  } else {
  $na_array [$key] = $value;
  }
 }
 
 // Re-combine arrays placing all other values at the end
 $final_array = array_merge($numeric_array, $na_array);
 -- End Code --

This works.. THANKS!  HOWEVER:

I think PHP is seeing a comma, in a large number and think that it IS
NOT NUMERIC, because when I test a large number it put it in the $na_array

Do I need to add something to this code to stip the commas?

THANKS IN ADAVANCE AGAIN,
Jim Long


Kevin Stone wrote:
 To append my own answer.  In order to sort the non n/a values you're going
 to have to build a separate array and perhaps this will simplfy the problem.
 So instead of what I suggested below.. loop the array and start TWO new
 arrays within the loop.  One array for n/a values and one for non-n/a
 values.  After the values are sorted to their separate arrays do
 asort($nonnavals); then concatonate the two arrays together using
 array_merge(), putting the n/a array second.
 
 - Kevin
 
 - Original Message -
 From: Kevin Stone [EMAIL PROTECTED]
 To: [EMAIL PROTECTED]; [EMAIL PROTECTED]
 Sent: Thursday, February 27, 2003 12:04 PM
 Subject: Re: [PHP] restating plea for help with sort
 
 
 
Hmm well upon first glance looks like all you need to do is loop that
 
 array
 
and start a new array within the loop.  An If/Else construct checks If the
value is n/a then append to the back of the new array Else prepend to
 
 the
 
front of the new array.You append the values manually or use
array_push() and array_unshift().  Search the manual for more information.
Hope that helps.
- Kevin

- Original Message -
From: Jim Long [EMAIL PROTECTED]
To: [EMAIL PROTECTED]
Sent: Thursday, February 27, 2003 11:35 AM
Subject: [PHP] restating plea for help with sort



Hi,

I have an array with an html link as key and different $vars as val:

$list = array (
'a href=http://someplace.com/;some place/a' = $vendor1_total,
'a href=http://anotherplace.com/;another place/a' =
 
 $vendor2__total,
 
[snip] etc..

some vals are numeric and some vals are n/a
asort works to sort the array as a string, however, I need to sort the
numeric vals numeric, then have the n/a's go away or be output at the
bottom of the sort.

Please be clear and gentle.. I'm not schooled in php, just a tinkerer
trying to make this mess work.

Thanks In Advance,

Jim Long

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



[PHP] PHP SORT_NUMERIC

2003-02-27 Thread Jim Long
Hi,

I've got to sort the $numeric_arry

This doesn't seem to work:
sort ($numeric_array, SORT_NUMERIC);
I think its sorting $key

asort ($numeric_array, SORT_NUMERIC);
sorts as a string


THANKS AGAIN IN ADVANCE,
Jim Long

JanetVal Wrote:
 Well, as long as you know exactly what you are looking for, you can use 
 
   if ($value != n/a)) {
 
 Janet


Jason Lange Wrote:
 Try this code (tested):
 
 -- Begin Code --
 foreach ($list as $key = $value) {
  // If elements value is a numeral add to numeric_array
  if (is_numeric($value)) {
  $numeric_array[$key] = $value;
  // If value is not a numeral (presumably 'n/a') add to na_array
  } else {
  $na_array [$key] = $value;
  }
 }
 
 // Re-combine arrays placing all other values at the end
 $final_array = array_merge($numeric_array, $na_array);
 -- End Code --


Original Question:
I have an array with an html link as key and different $vars as val:

$list = array (
'a href=http://someplace.com/;some place/a' = $vendor1_total,
'a href=http://anotherplace.com/;another place/a' =
 
 $vendor2__total,
 
[snip] etc..

some vals are numeric and some vals are n/a
asort works to sort the array as a string, however, I need to sort the
numeric vals numeric, then have the n/a's go away or be output at the
bottom of the sort.

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



[PHP] connecting to db

2002-08-21 Thread Jim Long

Hi,

Just setting up a new db at a new domain and the connection script does
not work.
I've always been able to connect to a db using the following:

?php

  // Connect to the database server
  $dbcnx = mysql_connect(localhost,
   username, password);
  if (!$dbcnx) {
echo( PUnable to connect to the  .
  database server at this time./P );
exit();
  }



The host support suggests the following:

 Root as the mysql user and localhost as the server, unless you selected remote mysql
 connections from the web control panel.

BTW: I DID select remote mysql. Should I unselect it?

FMI (forgive my ignorance):

What connection script should I use?

Thanks So Much in Advance,
Jim Long

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




[PHP] defining separate form element within While statment

2002-08-04 Thread Jim Long

Hi,

I'm having trouble passing $the_score to edit_s1_php3:

// Display the scores
  while ( $row = mysql_fetch_array($result) ) {
echo( form . p . 
$row[last_name] .  ,  . $row[first_name] . /p . 
$row[s1] . input type='text' name='the_score' size='3'
.   
a href='edit_s1.php3?name=$name'edit score/a .br .
$row[s2] . input type='text' name='the_score' size='3'
.   
a href='edit_s2.php3?name=$name'edit score/a . br . 
$row[s3] . input type='text' name='the_score' size='3'
 .  
a href='edit_s3.php3?name=$name'edit score/a . br  . 
$row[s4] . input type='text' name='the_score' size='3'
.   
a href='edit_s4.php3?name=$name'edit score/a . br . 
$row[s5] . input type='text' name='the_score' size='3'
.   
a href='edit_s5.php3?name=$name'edit score/a . br . 
$row[s6] . input type='text' name='the_score' size='3'
.   
a href='edit_s6.php3?name=$name'edit score/a . br . 
$row[s7] . input type='text' name='the_score' size='3'
.   
a href='edit_s7.php3?name=$name'edit score/a . br . 
$row[s8] . input type='text' name='the_score' size='3'
.   
a href='edit_s8.php3?name=$name'edit score/a . br . 
$row[s9] . input type='text' name='the_score' size='3'
.   
a href='edit_s9.php3?name=$name'edit score/a . br . 
$row[s10] . input type='text' name='the_score'
size='3' .   
a href='edit_s10.php3?name=$name'edit score/a . br . 
$row[s11] . input type='text' name='the_score'
size='3' .   
a href='edit_s11.php3?name=$name'edit score/a . br . 
$row[s12] . input type='text' name='the_score'
size='3' .   
a href='edit_s12.php3?name=$name'edit score/a . br . 
$row[s13] . input type='text' name='the_score'
size='3' .   
a href='edit_s13.php3?name=$name'edit score/a . br . 
$row[s14] . input type='text' name='the_score'
size='3' .   
a href='edit_s14.php3?name=$name'edit score/a . br . 
$row[s15] . input type='text' name='the_score'
size='3' .   
a href='edit_s15.php3?name=$name'edit score/a. /form);
  }

I'm pretty sure it's because I don't have the form action defined.. however,
each input goes to a diffent action.  How do I do this?

Thanks In Advance,
Jim

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




[PHP] sort and maintain varible names

2002-04-29 Thread Jim Long

I have a basic sort:

 $orderd_list = array ($onec_rounded_total, $aps_rounded_total,
$bo_rounded_total, $cap3_rounded_total, $cap3_rounded_total,
$cap_rounded_total, $cfl_rounded_total, $ecg_rounded_total,
$ldd_rounded_total, $png_rounded_total, $spr_rounded_total,
$ist917_rounded_total, $tciac_rounded_total, $tcicl_rounded_total, 
$utl_rounded_total);
 sort ($orderd_list);
 reset ($orderd_list);
 while (list ($key, $val) = each ($orderd_list)) {
 echo [.$key.] = .$val.br\n;

It returns

[0] = 4.90
[1] = 4.99
[2] = 5.06
[3] = 5.60
[4] = 6.00
[5] = 6.00
[6] = 6.90
[7] = 6.90
[8] = 6.95
[9] = 7.00
[10] = 9.00
[11] = 10.50
[12] = 11.00
[13] = 11.26
[14] = 13.10

How do I maintain a varible name (for identification) in this output
list ie: 

onec = 4.90 //from the varible $onec_rounded_total
cfl = 4.99 
bo = 5.06
and so on... 

Thanks In Advance,
j
-- 
http://jimlong.net/web

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




[PHP] simplify if/then statement

2002-04-27 Thread Jim Long

Hi,

I've got the following statement for a state options menu.
How can this be expressed in a simple manner for all 52 states?


//choose state

if ($state == AL) {

// include class
 include(CalcAL.class.inc);
}

else if ($state == AR) {

// include class
 include(CalcAR.class.inc);
}

else if ($state == AZ) {

// include class
 include(CalcAZ.class.inc);
}

else{
echo Please select a State.br\n;
}

Thank You Very Much,

Jim Long
-- 
http://jimlong.net/web

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




Re: [PHP] simplify if/then statement

2002-04-27 Thread Jim Long

Using pull down for $state.

This works great!

 $incfilename = Calc.$state..class.inc;
 if (file_exists($incfilename))
 include($incfilename);
 else
 echo Please select a State.br\n;  

Thanks To Everyone, 

j
-- 
http://jimlong.net/web


 I'd do something similar to this, along with 
 putting all states in an array to make sure 
 $state is actually a state (in_array).  And 
 maybe you want to add guam :)
 
 a) be sure $state is set, else load default
 b) be sure $state is not bogus, else yell at them
 c) be sure the $state file exists, if so, include 
it else houston we have a problem

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




[PHP] re: passing inputs

2002-03-13 Thread Jim Long

THANKS TO ERIK PRICE !

You ablility to explain php concepts in simple langauge is exactly why I
signed up for this list! 

I will be deeply greatful for any other tutorials like this one on
passing imputs.

THANKS AGAIN,
Jim Long

Erik Price wrote:

 John,
 
 It seems that you're using two different conventions here.  This is 
 inconsistent, and confusing to me (at least, and possibly others trying 
 to help you).  Let me show you what I am talking about:


 Please don't be insulted if I make an assumption about what you know 
 about the use of variables within scripts, I'm going to do my best to 
 explain this and I can't know how much you know or don't.  Here's how it 
 works:

 On a script, you have access to any variable that you create within that 
 script.  Thus, if you create a variable named $inp, you can then echo 
 that variable or manipulate it in any way.  Like this:
 
 $inp = blue;   // this assigns the string blue to the $inp variable
 echo $inp;   // this echoes blue
 $outp = green;  // this assigns green (a string) to the $outp 
 variable
 $outp . $inp;   // this combines (concatenates) the two variables 
 together
  // and results in the string greenblue
 
 Okay, you probably already know all of that.  But my point is that these 
 variables are accessible to this particular script.  NOT TO OTHER 
 SCRIPTS.  If you need a variable to be accessible to another script, you 
 must pass the variable along.  There are a few ways to do this:
 
 etc..etc..etc..

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




[PHP] archive

2002-03-12 Thread Jim Long

Hello,

Is there a searchable archive of this PHP- general subscriber list?

Thanks in Advance,
Jim Long

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