php-general Digest 12 Mar 2011 06:21:46 -0000 Issue 7222

Topics (messages 311774 through 311793):

$_POST variable
        311774 by: Danny
        311775 by: Daniel Brown
        311777 by: Steve Staples
        311778 by: richard gray
        311779 by: Geoff Lane
        311781 by: Shawn McKenzie
        311783 by: richard gray
        311784 by: Jim Lucas
        311785 by: Shawn McKenzie
        311788 by: Kirk Bailey

Possible to pinpoint peak memory usage?
        311776 by: Daniel Hong
        311782 by: David Harkness
        311786 by: Daniel Hong

Re: [PHP-WEBMASTER] php with htmlspecialchars function
        311780 by: NetEmp

web site link request
        311787 by: Kirk Bailey

Zend Framework - getParam() Question
        311789 by: Dan Joseph
        311790 by: NetEmp
        311791 by: Midhun Girish

Re: Check for open file
        311792 by: Tommy Pham

Re: Delaying $(document).ready() in jQuery until php script finish
        311793 by: Tommy Pham

Administrivia:

To subscribe to the digest, e-mail:
        [email protected]

To unsubscribe from the digest, e-mail:
        [email protected]

To post to the list, e-mail:
        [email protected]


----------------------------------------------------------------------
--- Begin Message ---
Hi guys,

I have a form that has a long list of radio-bottons inside of it. The
radio-buttons are dynamically created via php and MySQL.

Here is an example of one of the radio buttons:

<input type="radio" name="<?php print ("radio_".$result_from_mysql) ; ?>" 
value="0">
<input type="radio" name="<?php print ("radio_".$result_from_mysql) ; ?>" 
value="1">

Now, when I submit this form to another page for processing, how would I "catch"
the above radio-button's $_POST name since I do not know the name, only that it
starts with "radio_" ?

Thank You

Danny

--- End Message ---
--- Begin Message ---
On Fri, Mar 11, 2011 at 14:28, Danny <[email protected]> wrote:
[snip!]
>
> Now, when I submit this form to another page for processing, how would I 
> "catch"
> the above radio-button's $_POST name since I do not know the name, only that 
> it
> starts with "radio_" ?

    One method is a foreach() loop.

    <?php
        foreach ($_POST as $k => $v) {
            if (preg_match('/^radio_/Ui',trim($k))) {
                echo $k.' is a match, and it\'s value is '.$v.'.<br/>'.PHP_EOL;
            }
        }
    ?>

-- 
</Daniel P. Brown>
Network Infrastructure Manager
http://www.php.net/

--- End Message ---
--- Begin Message ---
On Fri, 2011-03-11 at 21:28 +0200, Danny wrote:
> Hi guys,
> 
> I have a form that has a long list of radio-bottons inside of it. The
> radio-buttons are dynamically created via php and MySQL.
> 
> Here is an example of one of the radio buttons:
> 
> <input type="radio" name="<?php print ("radio_".$result_from_mysql) ; ?>" 
> value="0">
> <input type="radio" name="<?php print ("radio_".$result_from_mysql) ; ?>" 
> value="1">
> 
> Now, when I submit this form to another page for processing, how would I 
> "catch"
> the above radio-button's $_POST name since I do not know the name, only that 
> it
> starts with "radio_" ?
> 
> Thank You
> 
> Danny
> 

loop thought all the $_POST variables...

foreach($_POST as $key => $val)
{
        if(susbtr($key, 0, 7) === "radio_")
        {
                # this is what we're looking for
        }
}

crude, but works... I am sure there are many ways to look for it.

Steve


--- End Message ---
--- Begin Message ---
On 11/03/2011 20:28, Danny wrote:
Hi guys,

I have a form that has a long list of radio-bottons inside of it. The
radio-buttons are dynamically created via php and MySQL.

Here is an example of one of the radio buttons:

<input type="radio" name="<?php print ("radio_".$result_from_mysql) ; ?>" 
value="0">
<input type="radio" name="<?php print ("radio_".$result_from_mysql) ; ?>" 
value="1">

Now, when I submit this form to another page for processing, how would I "catch"
the above radio-button's $_POST name since I do not know the name, only that it
starts with "radio_" ?

foreach ($_POST as $k => $v) {
    if (substr($k,0,6) == 'radio_') {
        echo 'Name is -> ',$k,'<br />';
        // Do stuff...
    }
}

HTH
Rich

--- End Message ---
--- Begin Message ---
Hi Danny,

On Friday, March 11, 2011, 7:28:10 PM, you wrote:

> I have a form that has a long list of radio-bottons inside of it. The
> radio-buttons are dynamically created via php and MySQL.

> Here is an example of one of the radio buttons:

> <input type="radio" name="<?php print ("radio_".$result_from_mysql) ; ?>" 
> value="0">
> <input type="radio" name="<?php print ("radio_".$result_from_mysql) ; ?>" 
> value="1">

> Now, when I submit this form to another page for processing, how would I 
> "catch"
> the above radio-button's $_POST name since I do not know the name, only that 
> it

You could use foreach to iterate through the post variables until you
encounter a match:

foreach ($_POST as $key => $value){
    if (substr($key, 0, 6) == "radio_") {
       $buttonName = $key;
       $buttonValue = 4value;
       break 2;
    }
}

I haven't tried the above code, but I hope someone will correct my
efforts if I'm wrong.

-- 
Geoff Lane
Cornwall, UK


--- End Message ---
--- Begin Message ---
On 03/11/2011 01:28 PM, Danny wrote:
> Hi guys,
> 
> I have a form that has a long list of radio-bottons inside of it. The
> radio-buttons are dynamically created via php and MySQL.
> 
> Here is an example of one of the radio buttons:
> 
> <input type="radio" name="<?php print ("radio_".$result_from_mysql) ; ?>" 
> value="0">
> <input type="radio" name="<?php print ("radio_".$result_from_mysql) ; ?>" 
> value="1">
> 
> Now, when I submit this form to another page for processing, how would I 
> "catch"
> the above radio-button's $_POST name since I do not know the name, only that 
> it
> starts with "radio_" ?
> 
> Thank You
> 
> Danny

The most common and flexible way to do this sort of thing is to use
arrays instead:

<input type="radio" name="radio[<?php echo $result_from_mysql; ?>]"
value="0">
<input type="radio" name="radio[<?php echo $result_from_mysql; ?>]"
value="1">


Then:

foreach($_POST['radio'] as $key => $value) {
   echo "radio for $key is $value";
}
-- 
Thanks!
-Shawn
http://www.spidean.com

--- End Message ---
--- Begin Message ---
You could use foreach to iterate through the post variables until you
encounter a match:

foreach ($_POST as $key =>  $value){
     if (substr($key, 0, 6) == "radio_") {
        $buttonName = $key;
        $buttonValue = 4value;
        break 2;
     }
}

I haven't tried the above code, but I hope someone will correct my
efforts if I'm wrong.

given your code example -> 'break 2;' -- s/b just 'break;' ... 'break 2;' is to exit the outer loop of a nested loop which is not the case here.

Rich

--- End Message ---
--- Begin Message ---
On 3/11/2011 12:03 PM, Shawn McKenzie wrote:
> On 03/11/2011 01:28 PM, Danny wrote:
>> Hi guys,
>>
>> I have a form that has a long list of radio-bottons inside of it. The
>> radio-buttons are dynamically created via php and MySQL.
>>
>> Here is an example of one of the radio buttons:
>>
>> <input type="radio" name="<?php print ("radio_".$result_from_mysql) ; ?>" 
>> value="0">
>> <input type="radio" name="<?php print ("radio_".$result_from_mysql) ; ?>" 
>> value="1">
>>
>> Now, when I submit this form to another page for processing, how would I 
>> "catch"
>> the above radio-button's $_POST name since I do not know the name, only that 
>> it
>> starts with "radio_" ?
>>
>> Thank You
>>
>> Danny
> 
> The most common and flexible way to do this sort of thing is to use
> arrays instead:
> 
> <input type="radio" name="radio[<?php echo $result_from_mysql; ?>]"
> value="0">
> <input type="radio" name="radio[<?php echo $result_from_mysql; ?>]"
> value="1">
> 
> 
> Then:
> 
> foreach($_POST['radio'] as $key => $value) {
>    echo "radio for $key is $value";
> }

Your example would be good if the OP wanted checkbox'es.  But with radio
buttons, the whole point (most of the time) is to have the form only allow you
to have one of the radio input fields selected at any given time.  How you
showed it, it would not see the uniqueness of the radio button names, and
therefor allow more than one of the radio input fields to be selected at a time.

I would try something like this:

As long as this is correct:

<input type="radio" name="radio_<?php echo $result_from_mysql; ?>" value="0" 
/>Zero
<input type="radio" name="radio_<?php echo $result_from_mysql; ?>" value="1" 
/>One

Then I would do the following:

foreach ($_POST as $k => $v) {
  if ( strpos(trim($k), 'radio_') === 0 ) {
    echo $k.' is a match, and it\'s value is '.$v.'.<br/>'.PHP_EOL;
  }
}

Jim Lucas

--- End Message ---
--- Begin Message ---
On 03/11/2011 02:33 PM, Jim Lucas wrote:
> On 3/11/2011 12:03 PM, Shawn McKenzie wrote:
>> On 03/11/2011 01:28 PM, Danny wrote:
>>> Hi guys,
>>>
>>> I have a form that has a long list of radio-bottons inside of it. The
>>> radio-buttons are dynamically created via php and MySQL.
>>>
>>> Here is an example of one of the radio buttons:
>>>
>>> <input type="radio" name="<?php print ("radio_".$result_from_mysql) ; ?>" 
>>> value="0">
>>> <input type="radio" name="<?php print ("radio_".$result_from_mysql) ; ?>" 
>>> value="1">
>>>
>>> Now, when I submit this form to another page for processing, how would I 
>>> "catch"
>>> the above radio-button's $_POST name since I do not know the name, only 
>>> that it
>>> starts with "radio_" ?
>>>
>>> Thank You
>>>
>>> Danny
>>
>> The most common and flexible way to do this sort of thing is to use
>> arrays instead:
>>
>> <input type="radio" name="radio[<?php echo $result_from_mysql; ?>]"
>> value="0">
>> <input type="radio" name="radio[<?php echo $result_from_mysql; ?>]"
>> value="1">
>>
>>
>> Then:
>>
>> foreach($_POST['radio'] as $key => $value) {
>>    echo "radio for $key is $value";
>> }
> 
> Your example would be good if the OP wanted checkbox'es.  But with radio
> buttons, the whole point (most of the time) is to have the form only allow you
> to have one of the radio input fields selected at any given time.  How you
> showed it, it would not see the uniqueness of the radio button names, and
> therefor allow more than one of the radio input fields to be selected at a 
> time.

One radio button of the same name selected at a time yes.  From the OP's
code of two radios, one with value 0 and one with value 1, I assumed
these were to be a pair with the same name with only one able to be
successful.  This extends to arrays as well:

<input type="radio" name="radio[1111]" value="0">
<input type="radio" name="radio[1111]" value="1">

<input type="radio" name="radio[555]" value="0">
<input type="radio" name="radio[555]" value="1">

This works as expected, the same as the code I posted.

-- 
Thanks!
-Shawn
http://www.spidean.com

--- End Message ---
--- Begin Message ---


On 3/11/2011 2:43 PM, Geoff Lane wrote:

[snip]
You could use foreach to iterate through the post variables until you
encounter a match:

foreach ($_POST as $key =>  $value){
     if (substr($key, 0, 6) == "radio_") {
        $buttonName = $key;
        $buttonValue = 4value;
        break 2;
     }
}

I haven't tried the above code, but I hope someone will correct my
efforts if I'm wrong.

ok, now I am very new to php, so if i got this wrong be nice.

It APPEARS TO ME that you are setting a variable called buttonName to the extracted value stored in $key for each name in the post submission, and a variable named buttonValue for the item's value. THEM, you do the same thing again to the same destination variables for the next name/value pair, and so-on until they list of name/value pairs is exhausted. IF this understanding is correct, only the LAST name/value pair will emerge from the process intact; prior values will be obliterated. Would they not be better to append them to a single dimensioned array, which starts life as a null array? If I am getting this wrong, please administer wet mackerel therapy to my tired head and explain the facts.

--
end

Very Truly yours,
                 - Kirk Bailey,
                   Largo Florida

                       kniht
                      +-----+
                      | BOX |
                      +-----+
                       think


--- End Message ---
--- Begin Message ---
Hello,

Is it possible to pinpoint the location where the most memory was used? I
log memory_get_peak_usage() in sections of my code where I believe the most
memory would be consumed, but I haven't had luck with getting an accurate
picture. I also log the peak usage at the very end of the code. I can see
memory usage go up in places that I expected it to, but then things are
pretty much flat lined. However, the peak usage at the very end shows a much
higher number, sometimes it could be twice as large.

I'm also tracing with xdebug. At the beginning of the trace memory starts
off low, then it goes up as things are allocated. It also goes back down as
things are deallocated. But never do I see the numbers as high as what PHP
is reporting as the peak. It looks like xdebug traces at every step of
execution, shouldn't the memory usage shown in the trace be the actual usage
for every single step in the code? Why is PHP reporting that peak was much
higher?

Maybe I'm not understanding this stuff correctly?

Thanks,
daniel

--- End Message ---
--- Begin Message ---
On Fri, Mar 11, 2011 at 11:35 AM, Daniel Hong <[email protected]> wrote:

> Is it possible to pinpoint the location where the most memory was used?


Take a look at XHProf. [1] It will track memory and time of all function
calls. Paul Reinheimer created a GUI [2] to make dealing with the data
easier.

Peace,
David

[1] http://mirror.facebook.net/facebook/xhprof/doc.html
[2]
http://blog.preinheimer.com/index.php?/archives/355-A-GUI-for-XHProf.html

--- End Message ---
--- Begin Message ---
Nice. I will give XHProf a try. Thanks!

-daniel

On Fri, Mar 11, 2011 at 12:04 PM, David Harkness
<[email protected]>wrote:

> On Fri, Mar 11, 2011 at 11:35 AM, Daniel Hong <[email protected]>wrote:
>
>> Is it possible to pinpoint the location where the most memory was used?
>
>
> Take a look at XHProf. [1] It will track memory and time of all function
> calls. Paul Reinheimer created a GUI [2] to make dealing with the data
> easier.
>
> Peace,
> David
>
> [1] http://mirror.facebook.net/facebook/xhprof/doc.html
> [2]
> http://blog.preinheimer.com/index.php?/archives/355-A-GUI-for-XHProf.html
>
>

--- End Message ---
--- Begin Message ---
Well Lisa, that is exactly the way htmlspecialchars is supposed to work.

Here you could also display - Test - without using any function at all,
simply echo "<a href='test'>Test</a>"; and this should work well.

NetEmp

On Fri, Mar 11, 2011 at 11:51 PM, Daniel Brown <[email protected]> wrote:

> On Fri, Mar 11, 2011 at 13:07, Lisa Nguyen <[email protected]>
> wrote:
> > Hi
> >
> > I use one of your example to test the htmlspecialchars :
> >
> > <?php
> > $new = htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES);
> > echo $new; // &lt;a href=&#039;test&#039;&gt;Test&lt;/a&gt;
> > ?>
> >
> > On my browse, it display like below
> > <a href='test'>Test</a>
> >
> > Instead:
> > Test
> >
> > Please help me how display on browse only "Test", not like view source.
>
>    First, this isn't a support channel.  Please view
> http://php.net/support for a brief list of available support methods.
> The list you want is PHP General (CC'd on this email, and you can
> subscribe at http://php.net/mailinglists ).
>
>    Second, you're using htmlspecialchars(), which is converting the <
> and > to &lt; and &gt; respectively, which will cause the browser to
> display it as if it was source (though viewing the source of that
> would show you the entities).
>
>    Instead, check out strip_tags(), which is likely what you want:
>
>        http://php.net/strip_tags
>
> --
> </Daniel P. Brown>
> Network Infrastructure Manager
> http://www.php.net/
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

--- End Message ---
--- Begin Message --- May I have suggestions of websites I would do well to visit to improve my php skills? Links please, with a breif description if you would be so good.

--
end

Very Truly yours,
                 - Kirk Bailey,
                   Largo Florida

                       kniht
                      +-----+
                      | BOX |
                      +-----+
                       think


--- End Message ---
--- Begin Message ---
Hi Everyone,

Zend Framework getParam question....

I'm trying to get a value from the url...

I know how to grab:

http;//www.website.com/index/user/1

that's the index controller, $this->_getParam('user'); (value = 1)..

What I'd like to be able to grab is just off one thing from the url...
example..  I want to give an article a unique/clean url... so, when I go to:

http://www.website.com/article-clean-url

I can somehow grab that 'article-clean-url' as a value and use it for a
lookup in the database.

I've tried everything and search all over the place.  I can't find the
answer.  Can someone tell me how this is done?  Thanks...

-- 
-Dan Joseph

--- End Message ---
--- Begin Message ---
Hi Dan

One method for this is to use URL Rewriting (which can be implemented on
Apache using htaccess).

Through URL Rewriting you can first make the following URL:

http://www.website.com/article-clean-url

<http://www.website.com/article-clean-url>to internally behave as the
following:

http://www.website.com/index/user/ <http://www.website.com/index/user/1>1

<http://www.website.com/index/user/1>and then you can use the same getParam
method to grab the value and carry out further processing.

Hope this helps.

Cheers

NetEmp

On Sat, Mar 12, 2011 at 10:04 AM, Dan Joseph <[email protected]> wrote:

> Hi Everyone,
>
> Zend Framework getParam question....
>
> I'm trying to get a value from the url...
>
> I know how to grab:
>
> http;//www.website.com/index/user/1
>
> that's the index controller, $this->_getParam('user'); (value = 1)..
>
> What I'd like to be able to grab is just off one thing from the url...
> example..  I want to give an article a unique/clean url... so, when I go
> to:
>
> http://www.website.com/article-clean-url
>
> I can somehow grab that 'article-clean-url' as a value and use it for a
> lookup in the database.
>
> I've tried everything and search all over the place.  I can't find the
> answer.  Can someone tell me how this is done?  Thanks...
>
> --
> -Dan Joseph
>

--- End Message ---
--- Begin Message ---
You can also try routing in zend..
http://codeutopia.net/blog/2007/11/16/routing-and-complex-urls-in-zend-framework/

Midhun Girish



On Sat, Mar 12, 2011 at 11:16 AM, NetEmp <[email protected]> wrote:

> Hi Dan
>
> One method for this is to use URL Rewriting (which can be implemented on
> Apache using htaccess).
>
> Through URL Rewriting you can first make the following URL:
>
> http://www.website.com/article-clean-url
>
> <http://www.website.com/article-clean-url>to internally behave as the
> following:
>
> http://www.website.com/index/user/ <http://www.website.com/index/user/1>1
>
> <http://www.website.com/index/user/1>and then you can use the same
> getParam
> method to grab the value and carry out further processing.
>
> Hope this helps.
>
> Cheers
>
> NetEmp
>
> On Sat, Mar 12, 2011 at 10:04 AM, Dan Joseph <[email protected]> wrote:
>
> > Hi Everyone,
> >
> > Zend Framework getParam question....
> >
> > I'm trying to get a value from the url...
> >
> > I know how to grab:
> >
> > http;//www.website.com/index/user/1
> >
> > that's the index controller, $this->_getParam('user'); (value = 1)..
> >
> > What I'd like to be able to grab is just off one thing from the url...
> > example..  I want to give an article a unique/clean url... so, when I go
> > to:
> >
> > http://www.website.com/article-clean-url
> >
> > I can somehow grab that 'article-clean-url' as a value and use it for a
> > lookup in the database.
> >
> > I've tried everything and search all over the place.  I can't find the
> > answer.  Can someone tell me how this is done?  Thanks...
> >
> > --
> > -Dan Joseph
> >
>

--- End Message ---
--- Begin Message ---
On Fri, Mar 4, 2011 at 7:09 AM, Steve Staples <[email protected]> wrote:

>
> Depending on the size of the file, wouldn't this fall under the 2gb
> limitation on windows 32bit OS?  I ran into this problem on a project I
> was working on, and ended up switching to Python (but that is a WHOLE
> other conversation)
>
> just food for thought, since I am not sure of the size of files they are
> dealing with.
>
> Steve
>
>

Jumping in late.  I've been too busy.

Steve,

I think you're probably referring to FAT32 file system.  IIRC, NTFS
doesn't have that low limit. [1]

Regards,
Tommy

[1] http://technet.microsoft.com/en-us/library/cc938937.aspx

--- End Message ---
--- Begin Message ---
I think the OP is having both PHP & JS codes mixed and scattered all
over the page.  If chunked-encoding used without any ob*
implementation, then that's the problem he'll experience.

Richard,

I recommend to put the $(document).ready() and any JS scriptlets
within <body></body> tags at the very bottom of the HTML document just
right before </body>.  This would allow the mixed PHP/HTML to finish
without creating problems for your JS code(s).  The other solution is
implement output buffer using ob* functions.

Regards,
Tommy

--- End Message ---

Reply via email to