php-general Digest 10 Aug 2011 18:34:59 -0000 Issue 7438

2011-08-10 Thread php-general-digest-help

php-general Digest 10 Aug 2011 18:34:59 - Issue 7438

Topics (messages 314459 through 314463):

Re: Using function prototypes in code
314459 by: Stuart Dallas
314460 by: Tim Streater
314461 by: David Harkness
314462 by: Simon J Welsh

text insertion
314463 by: Chris Stinemetz

Administrivia:

To subscribe to the digest, e-mail:
php-general-digest-subscr...@lists.php.net

To unsubscribe from the digest, e-mail:
php-general-digest-unsubscr...@lists.php.net

To post to the list, e-mail:
php-gene...@lists.php.net


--
---BeginMessage---
On 10 Aug 2011, at 02:10, Frank Thynne wrote:

 In the interest of clarity and maintainability I would like to be able
 to write code that makes it clear what kind of arguments a function
 expects and what it returns.
 
 This is what I tried:
 
 function integer int_func(string $s) {
 // does something like, say, converting five to 5
 }
 
 There are two problems:
 1 The appearance of a type name before the function name is treated as
 a syntax error
 2 Even if I forget about declaring the return type and code it instead
 as
 
 function int_func(string $s) {
 ...
 }
 
 I get a run-time error when I call the function with a string. (eg
 $var = int_func(five);) The error message saysCatchable fatal
 error: Argument 1 passed to int_func() must be an instance of string,
 string given.
 
 It seems that basic data types cannot be specified in ths way although
 (intstances of) classes can. I have successfully used the technique to
 catch run-time errors of wrong object types when testing, but am
 surprised that I can't use it to trap unexpected basic types - or at
 least to document what is expected.
 
 To confuse me a bit further, I can't find a definitive list of the
 basic type names. For example, is it integer or int?

The manual says...

Type Hints can only be of the object and array (since PHP 5.1) type. 
Traditional type hinting with int and string isn't supported.

http://php.net/language.oop5.typehinting

-Stuart

-- 
Stuart Dallas
3ft9 Ltd
http://3ft9.com/---End Message---
---BeginMessage---
On 10 Aug 2011 at 02:10, Frank Thynne frank.thy...@gmail.com wrote: 

 In the interest of clarity and maintainability I would like to be able
 to write code that makes it clear what kind of arguments a function
 expects and what it returns.

So add the appropriate comments to your functions.

 This is what I tried:

 function integer int_func(string $s) {
  // does something like, say, converting five to 5
 }

 There are two problems:
 1 The appearance of a type name before the function name is treated as
 a syntax error
 2 Even if I forget about declaring the return type and code it instead
 as

 function int_func(string $s) {
 ...
 }

 I get a run-time error when I call the function with a string. (eg
 $var = int_func(five);) The error message saysCatchable fatal
 error: Argument 1 passed to int_func() must be an instance of string,
 string given.

Why are you doing this when the documentation clearly states that this is not 
how it works. Did you not read up about it first?

 It seems that basic data types cannot be specified in ths way although
 (intstances of) classes can. I have successfully used the technique to
 catch run-time errors of wrong object types when testing, but am
 surprised that I can't use it to trap unexpected basic types - or at
 least to document what is expected.

This is PHP, not FORTRAN IV.

Personally I see it as a great step forward that for the most part, I don't 
have to bother.

--
Cheers  --  Tim
---End Message---
---BeginMessage---
On Tue, Aug 9, 2011 at 6:10 PM, Frank Thynne frank.thy...@gmail.com wrote:

 function integer int_func(string $s) {
  // does something like, say, converting five to 5
 }


As Stuart pointed out, type-hinting currently only works for classes and
arrays. Scalar type-hinting is planned for the future, but for now you're
left with enforcing it in the function itself. You can create a class with
static helpers to make this easier if you're going to do it frequently.

As for documentation, go with the PHPDoc standard:

/**
 * Converts a spelled-out number to the equivalent integer.
 *
 * @param string $s must be a spelled out number, e.g. five rather than
5.
 * @return int
 */
function int_func($s) { ... }

To confuse me a bit further, I can't find a definitive list of the
 basic type names. For example, is it integer or int?


I use int and bool with the rest spelled out which works with casting.

Peace,
David
---End Message---
---BeginMessage---
On 10/08/2011, at 1:10 PM, Frank Thynne wrote:

 To confuse me a bit further, I can't find a definitive list of the
 basic type names. For example, is it integer or int?

Both. 
http://php.net/manual/en/language.types.type-juggling.php#language.types.typecasting
---
Simon Welsh
Admin of http://simon.geek.nz/

---End Message---
---BeginMessage---
How do 

Re: [PHP] Using function prototypes in code

2011-08-10 Thread Tim Streater
On 10 Aug 2011 at 02:10, Frank Thynne frank.thy...@gmail.com wrote: 

 In the interest of clarity and maintainability I would like to be able
 to write code that makes it clear what kind of arguments a function
 expects and what it returns.

So add the appropriate comments to your functions.

 This is what I tried:

 function integer int_func(string $s) {
  // does something like, say, converting five to 5
 }

 There are two problems:
 1 The appearance of a type name before the function name is treated as
 a syntax error
 2 Even if I forget about declaring the return type and code it instead
 as

 function int_func(string $s) {
 ...
 }

 I get a run-time error when I call the function with a string. (eg
 $var = int_func(five);) The error message saysCatchable fatal
 error: Argument 1 passed to int_func() must be an instance of string,
 string given.

Why are you doing this when the documentation clearly states that this is not 
how it works. Did you not read up about it first?

 It seems that basic data types cannot be specified in ths way although
 (intstances of) classes can. I have successfully used the technique to
 catch run-time errors of wrong object types when testing, but am
 surprised that I can't use it to trap unexpected basic types - or at
 least to document what is expected.

This is PHP, not FORTRAN IV.

Personally I see it as a great step forward that for the most part, I don't 
have to bother.

--
Cheers  --  Tim

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

Re: [PHP] Using function prototypes in code

2011-08-10 Thread David Harkness
On Tue, Aug 9, 2011 at 6:10 PM, Frank Thynne frank.thy...@gmail.com wrote:

 function integer int_func(string $s) {
  // does something like, say, converting five to 5
 }


As Stuart pointed out, type-hinting currently only works for classes and
arrays. Scalar type-hinting is planned for the future, but for now you're
left with enforcing it in the function itself. You can create a class with
static helpers to make this easier if you're going to do it frequently.

As for documentation, go with the PHPDoc standard:

/**
 * Converts a spelled-out number to the equivalent integer.
 *
 * @param string $s must be a spelled out number, e.g. five rather than
5.
 * @return int
 */
function int_func($s) { ... }

To confuse me a bit further, I can't find a definitive list of the
 basic type names. For example, is it integer or int?


I use int and bool with the rest spelled out which works with casting.

Peace,
David


Re: [PHP] Using function prototypes in code

2011-08-10 Thread Simon J Welsh
On 10/08/2011, at 1:10 PM, Frank Thynne wrote:

 To confuse me a bit further, I can't find a definitive list of the
 basic type names. For example, is it integer or int?

Both. 
http://php.net/manual/en/language.types.type-juggling.php#language.types.typecasting
---
Simon Welsh
Admin of http://simon.geek.nz/


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



[PHP] text insertion

2011-08-10 Thread Chris Stinemetz
How do I preserve text formatting when text is inserted into a database table?

For example: I am using a textarea to allow users to leave comments
into the database and I am using:

' . mysql_real_escape_string($_POST['store_comments']) . ',   to
prevent SQL injection;

But when I call the data with php from the database the format it was
inserted is not preserved. Is there a way to make sure when a user
adds new paragraphs or indentation it will be preserved?

Thank you,

Chris

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



Re: [PHP] text insertion

2011-08-10 Thread Ashley Sheridan


Chris Stinemetz chrisstinem...@gmail.com wrote:

How do I preserve text formatting when text is inserted into a database
table?

For example: I am using a textarea to allow users to leave comments
into the database and I am using:

' . mysql_real_escape_string($_POST['store_comments']) . ',  to
prevent SQL injection;

But when I call the data with php from the database the format it was
inserted is not preserved. Is there a way to make sure when a user
adds new paragraphs or indentation it will be preserved?

Thank you,

Chris

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

Are you sure its not preserved? When you output text in a browser, by default 
its output as html, not plain text. Html ignores extraneous whitespace, and 
doesn't use a monospaced font, so formatting text into columns in a textarea 
won't work either. Look at the html source code to see what is actually being 
output.

Thanks,
Ash
http://www.ashleysheridan.co.uk
--
Sent from my Android phone with K-9 Mail. Please excuse my brevity.

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



Re: [PHP] text insertion

2011-08-10 Thread Chris Stinemetz
 Are you sure its not preserved? When you output text in a browser, by default 
 its output as html, not plain text. Html ignores extraneous whitespace, and 
 doesn't use a monospaced font, so formatting text into columns in a textarea 
 won't work either. Look at the html source code to see what is actually being 
 output.

Thanks Ashley

Source is showing indentation and new paragaraphs just as I inserted
the text. How can I get HTML output to show same format?


Chris

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



Re: [PHP] text insertion

2011-08-10 Thread Shaun Farrell
You could try using markdown too. 

Sent from my iPhone

On Aug 10, 2011, at 3:57 PM, Ashley Sheridan a...@ashleysheridan.co.uk wrote:

 
 
 Chris Stinemetz chrisstinem...@gmail.com wrote:
 
 How do I preserve text formatting when text is inserted into a database
 table?
 
 For example: I am using a textarea to allow users to leave comments
 into the database and I am using:
 
 ' . mysql_real_escape_string($_POST['store_comments']) . ',to
 prevent SQL injection;
 
 But when I call the data with php from the database the format it was
 inserted is not preserved. Is there a way to make sure when a user
 adds new paragraphs or indentation it will be preserved?
 
 Thank you,
 
 Chris
 
 -- 
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php
 
 Are you sure its not preserved? When you output text in a browser, by default 
 its output as html, not plain text. Html ignores extraneous whitespace, and 
 doesn't use a monospaced font, so formatting text into columns in a textarea 
 won't work either. Look at the html source code to see what is actually being 
 output.
 
 Thanks,
 Ash
 http://www.ashleysheridan.co.uk
 -- 
 Sent from my Android phone with K-9 Mail. Please excuse my brevity.
 
 -- 
 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] text insertion

2011-08-10 Thread Daniel P. Brown
:On Wed, Aug 10, 2011 at 16:02, Chris Stinemetz
chrisstinem...@gmail.com wrote:
 Are you sure its not preserved? When you output text in a browser, by 
 default its output as html, not plain text. Html ignores extraneous 
 whitespace, and doesn't use a monospaced font, so formatting text into 
 columns in a textarea won't work either. Look at the html source code to see 
 what is actually being output.

 Thanks Ashley

 Source is showing indentation and new paragaraphs just as I inserted
 the text. How can I get HTML output to show same format?

Use HTML 'pre' tags:

pre?php echo $your_content; ?/pre

-- 
/Daniel P. Brown
Dedicated Servers, Cloud and Cloud Hybrid Solutions, VPS, Hosting
(866-) 725-4321
http://www.parasane.net/

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



RE: [PHP] regex or 'tidy' script to fix broken ? tags and introspection of variables

2011-08-10 Thread Daevid Vincent
 -Original Message-
 From: Camilo Sperberg [mailto:unrea...@gmail.com]
 Sent: Tuesday, August 09, 2011 5:27 PM
 
 For the first one, it may be that zend studio does have an internal script
 to do the job. Check the general preferences tab, template stuff. 

Nope. Nothing there. Those templates are for when you create new blurbs of
code, not modifying existing code.

There is a formatter however, sadly it doesn't have an option to force these
(you'd think that would be the perfect place to do this too huh.) In fact, I
posted this here:

http://forums.zend.com/viewtopic.php?f=59t=19173#p59348

 Please note that ?= is also valid and should be replaced to ?php echo
instead.

Yeah, I don't like that style. I prefer the ?= $foo ? version. It's
shorter, cleaner and easier to read.

Many people mistakenly think that short version is going to be deprecated
away. It is not. The PHP Devs have already clarified only the ? version
is, not this one.

http://www.php.net/manual/en/ini.core.php#ini.short-open-tag

 Also the short if version 1 == 1 ? True : false should be replaced if
i'm correct.

You are not. ;-)

The Ternary operator statement would never go away. It is a standard
comparison operator in pretty much any language and would be completely
stupid of the PHP Devs to deviate that far from the norm.

http://php.net/manual/en/language.operators.comparison.php

Plus I love that operator and use it quite frequently. However, I use it
like this just for clarity:

echo your result is .((1 == 1) ? 'true' : 'false').'br';

 Second question: zend studio displays all variables used by a script by
 clicking the arrow next to te file name. 

I've used ZS for 4+ years now, and comicaly have never even used those
little down arrows next to a file. HAHAH! Good to know. Although it is a
little strange as they seem to only be where you use a = assignment. It
doesn't know about - or other instances of that variable (like if you
echo it or something). But still could prove useful.

 If you want to display it in runtime, you can: print_r($GLOBALS);

Whoa nelly! That prints out WAAY too much information ;-)

But thanks. Not sure why I didn't think of that one. Maybe because at one
time I did use it, got sensory overload from the amount of data it spews to
the page, and then blocked it out of my mind for future use. :)


ÐÆ5ÏÐ
There are only 11 types of people in this world. Those that think binary
jokes are funny, those that don't, and those that don't know binary.
--

 Sent from my iPhone 5 Beta [Confidential use only]
 
 On 09-08-2011, at 19:40, Daevid Vincent dae...@daevid.com wrote:
 
  I've inherited a bunch of code and the previous developers have done two
  things that are really bugging me and I want to clean up.
 
  [a] They use short-tag ? instead of ?php. Anyone have some good
  search/replace style Regex (ideally for ZendStudio/Eclipse) that will
run
  through all the files in the project and fix those? There are lots of
 cases
  to account for such as a space after the ? or nospace or a newline or
even
  other text (which are all valid cases).
 
  [b] The other thing they do use use register_globals in the php.ini
file.
 Is
  there a good way to see all the variables that a page uses? Something I
 can
  print at the bottom of the page on my dev box - ideally with enough
  introspection to know where that variable originated from, and then I
can
  start converting things to $_GET, $_POST,  $_SESSION, $_COOKIE, etc.
 


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



Re: [PHP] text insertion

2011-08-10 Thread Chris Stinemetz

    Use HTML 'pre' tags:

        pre?php echo $your_content; ?/pre


I just tried that and that puts all the text on a single line.


echo 'tr class=topic-post
td class=user-poststrong' . $posts_row['first_name'] . ' ' .
$posts_row['last_name'] . ' ' . date('m-d-Y h:iA',
strtotime($posts_row['store_date'])) . '/strong/td
/tr
tr class=visit
td class=post-contentBroad Band test results:/strong ' .
$posts_row['store_tptest'] . 'br/br/pre'
.$posts_row['store_comments'] . '/pre/td
/tr';

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



Re: [PHP] text insertion

2011-08-10 Thread hdedeyan
how about 

echo nl2br($your_content);



- Original Message -
From: Chris Stinemetz chrisstinem...@gmail.com
Date: Wednesday, August 10, 2011 5:09 pm
Subject: Re: [PHP] text insertion
To: Daniel P. Brown daniel.br...@parasane.net
Cc: Ashley Sheridan a...@ashleysheridan.co.uk, PHP General 
php-general@lists.php.net

 
 Use HTML 'pre' tags:
 
 pre?php echo $your_content; ?/pre
 
 
 I just tried that and that puts all the text on a single line.
 
 
 echo 'tr class=topic-post
 td class=user-poststrong' . $posts_row['first_name'] 
 . ' ' .
 $posts_row['last_name'] . ' ' . date('m-d-Y h:iA',
 strtotime($posts_row['store_date'])) . '/strong/td
 /tr
 tr class=visit
 td class=post-contentBroad Band test results:/strong 
 ' .
 $posts_row['store_tptest'] . 'br/br/pre'
 .$posts_row['store_comments'] . '/pre/td
 /tr';
 
 -- 
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php
 


Re: [PHP] text insertion

2011-08-10 Thread Chris Stinemetz
No luck. Thanks.

On Aug 10, 2011 4:17 PM, hdede...@videotron.ca wrote:

 how about

 echo nl2br($your_content);




 - Original Message -
 From: Chris Stinemetz chrisstinem...@gmail.com
 Date: Wednesday, August 10, 2011 5:09 pm
 Subject: Re: [PHP] text insertion
 To: Daniel P. Brown daniel.br...@parasane.net
 Cc: Ashley Sheridan a...@ashleysheridan.co.uk, PHP General 
php-general@lists.php.net

  
  Use HTML 'pre' tags:
  
  pre?php echo $your_content; ?/pre
  
 
  I just tried that and that puts all the text on a single line.
 
 
  echo 'tr class=topic-post
  td class=user-poststrong' . $posts_row['first_name']
  . ' ' .
  $posts_row['last_name'] . ' ' . date('m-d-Y h:iA',
  strtotime($posts_row['store_date'])) . '/strong/td
  /tr
  tr class=visit
  td class=post-contentBroad Band test results:/strong
  ' .
  $posts_row['store_tptest'] . 'br/br/pre'
  .$posts_row['store_comments'] . '/pre/td
  /tr';
 
  --
  PHP General Mailing List (http://www.php.net/)
  To unsubscribe, visit: http://www.php.net/unsub.php
 


Re: [PHP] text insertion

2011-08-10 Thread Daniel P. Brown
On Wed, Aug 10, 2011 at 17:37, Chris Stinemetz chrisstinem...@gmail.com wrote:
 No luck. Thanks.

Per list rules, please don't top-post.

If the situation you're describing is accurate and correct, then
pre is indeed what you want.

-- 
/Daniel P. Brown
Dedicated Servers, Cloud and Cloud Hybrid Solutions, VPS, Hosting
(866-) 725-4321
http://www.parasane.net/

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



Re: Re: [PHP] text insertion

2011-08-10 Thread Tim Streater
On 10 Aug 2011 at 22:39, Daniel P. Brown daniel.br...@parasane.net wrote: 

 On Wed, Aug 10, 2011 at 17:37, Chris Stinemetz chrisstinem...@gmail.com
 wrote:
 No luck. Thanks.

Per list rules, please don't top-post.

If the situation you're describing is accurate and correct, then
 pre is indeed what you want.

But can you put a pre/pre inside a table cell? (I don't know that you 
can't, but it seems an odd thing to do).

--
Cheers  --  Tim

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

Re: Re: [PHP] text insertion

2011-08-10 Thread Ashley Sheridan


Tim Streater t...@clothears.org.uk wrote:

On 10 Aug 2011 at 22:39, Daniel P. Brown daniel.br...@parasane.net
wrote:

 On Wed, Aug 10, 2011 at 17:37, Chris Stinemetz
chrisstinem...@gmail.com
 wrote:
 No luck. Thanks.

Per list rules, please don't top-post.

If the situation you're describing is accurate and correct, then
 pre is indeed what you want.

But can you put a pre/pre inside a table cell? (I don't know that
you can't, but it seems an odd thing to do).

--
Cheers  --  Tim

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

First thing that jumps out at me is that the pre tags are apparently ignored. 
This suggests one of those ridiculous reset.css files being used, which uses 
css to style every Danny element the same. If its set to override the 
whitespace setting in a pre tag then that could cause the issue you are 
seeing, as I've never known pre tags to fail like that.

Thanks,
Ash
http://www.ashleysheridan.co.uk
--
Sent from my Android phone with K-9 Mail. Please excuse my brevity.

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



Re: Re: [PHP] text insertion

2011-08-10 Thread Tim Streater
On 10 Aug 2011 at 22:07, Chris Stinemetz chrisstinem...@gmail.com wrote: 


    Use HTML 'pre' tags:

        pre?php echo $your_content; ?/pre


 I just tried that and that puts all the text on a single line.

You could write the string into another textarea, which you could make readonly 
for this purpose.

--
Cheers  --  Tim

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

Re: [PHP] regex or 'tidy' script to fix broken ? tags and introspection of variables

2011-08-10 Thread Camilo Sperberg

On 10-08-2011, at 16:54, Daevid Vincent wrote:

 -Original Message-
 From: Camilo Sperberg [mailto:unrea...@gmail.com]
 Sent: Tuesday, August 09, 2011 5:27 PM
 
 For the first one, it may be that zend studio does have an internal script
 to do the job. Check the general preferences tab, template stuff. 
 
 Nope. Nothing there. Those templates are for when you create new blurbs of
 code, not modifying existing code.
 
 There is a formatter however, sadly it doesn't have an option to force these
 (you'd think that would be the perfect place to do this too huh.) In fact, I
 posted this here:
 
 http://forums.zend.com/viewtopic.php?f=59t=19173#p59348

That is sad to hear, I have never done the same thing you are now, but I 
thought it could help.

 
 Many people mistakenly think that short version is going to be deprecated
 away. It is not. The PHP Devs have already clarified only the ? version
 is, not this one.
 
 http://www.php.net/manual/en/ini.core.php#ini.short-open-tag
 

I had no idea, I thought short tags also implied short if-else, short echo and 
so on, good to know that, thanks for the clarification ;)

 
 Second question: zend studio displays all variables used by a script by
 clicking the arrow next to te file name. 
 
 I've used ZS for 4+ years now, and comicaly have never even used those
 little down arrows next to a file. HAHAH! Good to know. Although it is a
 little strange as they seem to only be where you use a = assignment. It
 doesn't know about - or other instances of that variable (like if you
 echo it or something). But still could prove useful.

Eclipse/Zend Studio is so full of options that you can miss a lot of them. 
Another downside of the little arrow thing is that it doesn't recognize arrays, 
which is obvious because arrays can only be read on runtime, well, same thing 
as objects.

 
 If you want to display it in runtime, you can: print_r($GLOBALS);
 
 Whoa nelly! That prints out WAAY too much information ;-)
 
 But thanks. Not sure why I didn't think of that one. Maybe because at one
 time I did use it, got sensory overload from the amount of data it spews to
 the page, and then blocked it out of my mind for future use. :)

Yeah I know it displays a lot of information, but I use it sometimes and it's 
the only way I know to display all variables, arrays and stuff in a neat and 
nice way. You can also install xDebug and use the included debugger in ZS, but 
sometimes using a simple print_r is faster.

Greetings :)

___
Mi blog
CHW
Mi Twitter


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



[PHP] Problem with inserting numbers...

2011-08-10 Thread Jason Pruim
So here I am attempting to generate some numbers to be inserted into a 
database... eventually they will make up a phone number (Which I've emailed 
about before and know about the bad ideas with it.. But it's the customer :)) 

Here is the code I am working with:

?PHP
function number_pad($number,$n) {
return str_pad((int) $number,$n,0,STR_PAD_LEFT);
}

$SQL = SELECT * FROM Test WHERE `areacode` = '907' AND `prefix` = '200' LIMIT 
5;

$result = mysql_query($SQL);
//$num = ;
//foreach ($result as $key = $value) {
//echo $key . - . $value . - . number_pad($num, 4) . BR;
//$num++;
//}

while ($num != 1) {
while($row = mysql_fetch_assoc($result)) {
$padnum = number_pad($num, 4);
echo $row['areacode'] . - . $row['prefix'] . - . $padnum . BR;
$num++;
}


}

?

basically all I'm trying to do is generate the last 4 digits starting at  
and going up to . for testing purposes I'm just echoing back but will 
eventually insert the complete number back into the database as a 7 digit 
string.

The error I'm getting is:

Fatal error: Maximum execution time of 30 seconds exceeded in 
/home/pruimpho/public_html/jason/dev/clients/flewid/Phone/config/phoneareacodes.php
 on line25

which is where the second while starts...

Does anyone know a better way to do this? 

I'm off to finish searching google while waiting for some good news :)

Thanks Everyone!


Jason Pruim
pru...@gmail.com




Re: [PHP] Problem with inserting numbers...

2011-08-10 Thread Jason Pruim
On Aug 10, 2011, at 9:36 PM, MUAD SHIBANI wrote:

 Basically you can increase time limit for this file to handle your request by 
 using 
 set_time_limit function ... 

Hi Maud,

Looked into set_time_limit and even tried running it with a value of 120 but 
it didn't help...

I'm affraid I have a problem with the way the 2 while loops are interacting and 
creating a infinite loop... 

BUT... I'm thinking there must be a better way... Maybe a while with a foreach? 
Now I'm just thinking outloud :)

Jason Pruim
pru...@gmail.com

Re: [PHP] Problem with inserting numbers...

2011-08-10 Thread Chris Stinemetz

 basically all I'm trying to do is generate the last 4 digits starting at  
 and going up to . for testing purposes I'm just echoing back but will 
 eventually insert the complete number back into the database as a 7 digit 
 string.

 The error I'm getting is:

 Fatal error: Maximum execution time of 30 seconds exceeded in 
 /home/pruimpho/public_html/jason/dev/clients/flewid/Phone/config/phoneareacodes.php
  on line25


I'm not sure the to while loops is the best control structure.

Why not try the following and see if it gets you the results you want?

__untested__

while($row = mysql_fetch_assoc($result)) {
if ($num != 1)  $padnum = number_pad($num, 4)
{
echo $row['areacode'] . - . $row['prefix'] . - . $padnum . BR;
$num++;
}

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



Re: [PHP] Problem with inserting numbers...

2011-08-10 Thread 李白|字一日
when

 $num++;

executed

$num will never be '1';

you may change the while loop to

while ($num  1) {
   while ($row = ...


2011/8/11 Jason Pruim pru...@gmail.com

 On Aug 10, 2011, at 9:36 PM, MUAD SHIBANI wrote:

  Basically you can increase time limit for this file to handle your
 request by using
  set_time_limit function ...

 Hi Maud,

 Looked into set_time_limit and even tried running it with a value of 120
 but it didn't help...

 I'm affraid I have a problem with the way the 2 while loops are interacting
 and creating a infinite loop...

 BUT... I'm thinking there must be a better way... Maybe a while with a
 foreach? Now I'm just thinking outloud :)

 Jason Pruim
 pru...@gmail.com



Re: [PHP] Problem with inserting numbers...

2011-08-10 Thread Ken Robinson

At 09:22 PM 8/10/2011, Jason Pruim wrote:
So here I am attempting to generate some numbers to be inserted into 
a database... eventually they will make up a phone number (Which 
I've emailed about before and know about the bad ideas with it.. But 
it's the customer :))


Here is the code I am working with:

?PHP
function number_pad($number,$n) {
return str_pad((int) $number,$n,0,STR_PAD_LEFT);
}

$SQL = SELECT * FROM Test WHERE `areacode` = '907' AND `prefix` = 
'200' LIMIT 5;


$result = mysql_query($SQL);

while ($num != 1) {
while($row = mysql_fetch_assoc($result)) {
$padnum = number_pad($num, 4);
echo $row['areacode'] . - . $row['prefix'] . - . 
$padnum . BR;

$num++;
}


}

?


Try to avoid putting a database query in a loop. In the query only 
ask for the fields you are going to use, it's faster. Why use your 
own function to pad a string, when there is a built-in function?


Try this code (untested):
?php
$nums = range(0,);
$q = SELECT areacode, prefix FROM Test WHERE `areacode` = '907' AND 
`prefix` = '200' LIMIT 5;

$rs = mysql_query($q);
while ($row = mysql_fetch_assoc($rs)) {
foreach ($nums as $n) {
echo 
sprintf('%03d-%03d-%04d',$row['areacode'],$row['prefix'],$n) br\n;

}
}
?

Ken 



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



[PHP] concatenating

2011-08-10 Thread Chris Stinemetz
Is it possible to concatenate a string and an element from a
mysql_fetch_assoc array? I haven't had much luck searching google.

Such as concatenating results with ' . $posts_row['store_tptest'] .
' so that if there are no elements returned nothing will be displayed?

Thank you,

Chris

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



Re: [PHP] concatenating

2011-08-10 Thread Negin Nickparsa
read the manual
http://www.php.net/manual/en/ref.strings.php
A comprehensive concatenation function, that works with array and strings

?php
function str_cat() {
  $args = func_get_args() ;

  // Asserts that every array given as argument is $dim-size.
  // Keys in arrays are stripped off.
  // If no array is found, $dim stays unset.
  foreach($args as $key = $arg) {
if(is_array($arg)) {
  if(!isset($dim))
$dim = count($arg) ;
  elseif($dim != count($arg))
return FALSE ;
  $args[$key] = array_values($arg) ;
}
  }

  // Concatenation
  if(isset($dim)) {
$result = array() ;
for($i=0;$i$dim;$i++) {
  $result[$i] = '' ;
  foreach($args as $arg)
$result[$i] .= ( is_array($arg) ? $arg[$i] : $arg ) ;
}
return $result ;
  } else {
return implode($args) ;
  }
}
?

A simple example :

?php
str_cat(array(1,2,3), '-', array('foo' = 'foo', 'bar' = 'bar', 'noop' =
'noop')) ;
?

will return :
Array (
  [0] = 1-foo
  [1] = 2-bar
  [2] = 3-noop
)

More usefull :

?php
$myget = $_GET ; // retrieving previous $_GET values
$myget['foo'] = 'b a r' ; // changing one value
$myget = str_cat(array_keys($myget), '=', array_map('rawurlencode',
array_values($myget))) ;
$querystring = implode(ini_get('arg_separator.output'), $myget)) ;
?

will return a valid querystring with some values changed.

Note that ?php str_cat('foo', '', 'bar') ; ? will return 'foobar',
while ?php str_cat(array('foo'), '', 'bar') ; ? will return array(0 =
foobar)
*t0russ at gmail dot com* 14-Jun-2005
05:38http://www.php.net/manual/en/ref.strings.php#53834
to kristin at greenaple dot on dot ca:
thanx for sharing.
your function in recursive form proved to be slightly faster and it returns
false (as it should) when the character is not found instead of number 0:
?php
function strnposr($haystack, $needle, $occurance, $pos = 0) {
return ($occurance2)?strpos($haystack, $needle,$pos):strnposr($haystack
,$needle,$occurance-1,strpos($haystack, $needle, $pos) + 1);
}
?


Re: [PHP] concatenating

2011-08-10 Thread Ken Robinson

At 12:03 AM 8/11/2011, Chris Stinemetz wrote:

Is it possible to concatenate a string and an element from a
mysql_fetch_assoc array? I haven't had much luck searching google.

Such as concatenating results with ' . $posts_row['store_tptest'] .
' so that if there are no elements returned nothing will be displayed?


Sure, do something like this:

?php
  echo ($posts_row['store_tptest'] != '')?results 
{$posts_row['stor_tptest']}:'';

?

Ken 



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