php-general Digest 15 Feb 2011 04:48:57 -0000 Issue 7182

Topics (messages 311278 through 311300):

Re: extract price by preg_match_all
        311278 by: Frank Arensmeier

Re: using BOTH GET and POST in the same page.
        311279 by: tedd
        311280 by: tedd
        311285 by: Paul M Foster

PHP -- using without installing
        311281 by: Steve Staples
        311283 by: Paul M Foster
        311286 by: Steve Staples
        311289 by: Richard Quadling

2 submit buttons.
        311282 by: Paul Halliday
        311284 by: Paul M Foster
        311287 by: Floyd Resler
        311288 by: Paul M Foster
        311291 by: tolga
        311292 by: Dmitrii Varvashenia
        311294 by: Dmitrii Varvashenia

code quest
        311290 by: Kirk Bailey
        311295 by: Richard Quadling
        311296 by: Kirk Bailey
        311297 by: Jim Lucas
        311298 by: Kirk Bailey

Howdy (new in here)
        311293 by: Brian Waters

Rate my (really) simple template class
        311299 by: Brian Waters
        311300 by: Paul M Foster

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 ---
<?php
if ( preg_match_all("/([0-9]+[,\.]{1}[0-9]{2})/", $data, $matches) ) {
        echo "Matches found:";
        echo "<pre>";
        print_r($matches);
        echo "</pre>";
} else {
        echo "Didn't find anything...";
}
?>

Untested.

/frank

14 feb 2011 kl. 15.05 skrev Tontonq Tontonq:

> example data:
> 
> <div class="picboxfooter">
>  <span class="newprice"><span class="productOldPrice">old price 829,00
> &euro;</span><br />your price 58,90 &euro; *</span>
> </div>
> 
> another :
>  <span class="newprice"> 9,90 &euro; *</span>
> 
> i want to extract 829,.00 & 58,90 from first source , 9,90 from the second
> 
> i tried many way like preg_match_all('/<span
> class="newprice">(\$[0-9,]+(\.[0-9]{2})?)<\/span>/',$data,$prices);
> it doesn't work


--- End Message ---
--- Begin Message ---
At 11:58 PM -0500 2/13/11, Paul M Foster wrote:
On Sun, Feb 13, 2011 at 02:25:45PM -0500, tedd wrote:

 > At 10:53 AM +0530 2/12/11, Ashim Kapoor wrote:
 > >Dear All,
 > >
> >I am reading "PHP5 and MySQL Bible". Chapter 7 of the book says that PHP can
 > >use GET and POST in the SAME page! Also it says that we can use the SAME
> >variables in GET and POST variable sets and that conflict resolution is done
 > >by variable_order option in php.ini Can some one write a small program to
> >illustrate the previous ideas? It is not clear to me as to how to implement
 > >this.
 > >
 > >Many thanks,
 > >Ashim.
 >
 > Ashim:
 >
 > What others have not addressed is that the form used to send
 > variables will send only GET OR POST method variables, but not both
 > at the same time.

-snip-

It sounds like you're saying this isn't possible, yet it is. So what am
I missing? Is there an error in my code?

Paul

--
Paul M. Foster
http://noferblatz.com


Paul:

What I am saying is your form is a POST-method-form and it will only send variables via the POST method. If you change the form's method to GET, then the variables from the form will be sent via the GET method.

Remember, there is ONLY ONE form being used per Submit action -- it can either be a POST method or a GET method, but it can not be both.

This does not mean that you can't send both POST and GET variables together via the same Submit action, but it does mean that the *form* will only send it's contents via one method or the other.

Here's a demo:

http://php1.net/c/post-get

Please note that the POST method form will send only POST data and the GET method form will send only GET data. However, you can force a POST method *form* via the action attribute value to also include a GET variable (see Submit POST w/GET) -- BUT -- that does not come from the form's input but rather from the action attribute. Do you see what I mean?

The code is shown.

Also, please note interaction between the GET/POST/REQUEST re the variable 'alfa' and you will get an idea of why REQUEST may pose a problem.

Cheers,

tedd

--
-------
http://sperling.com/

--- End Message ---
--- Begin Message ---
At 10:31 PM -0500 2/13/11, Robert Cummings wrote:
On 11-02-13 02:25 PM, tedd wrote:
At 10:53 AM +0530 2/12/11, Ashim Kapoor wrote:
Dear All,

I am reading "PHP5 and MySQL Bible". Chapter 7 of the book says that PHP can
use GET and POST in the SAME page! Also it says that we can use the SAME
variables in GET and POST variable sets and that conflict resolution is done
by variable_order option in php.ini Can some one write a small program to
illustrate the previous ideas?  It is not clear to me as to how to implement
this.

Many thanks,
Ashim.

Ashim:

What others have not addressed is that the form used to send
variables will send only GET OR POST method variables, but not both
at the same time.

Using REQUEST will show the values of the variables sent, but will
not show what method was used (not addressing COOKIE) and that is the
reason why it's not the best idea to use REQUEST.

Furthermore, as you point out, conflict resolution is done in
accordance with variable order as set in the php.ini file and that
can be different between different environments. As such, a script
can act differently and there in lies the problem.

Now, I have used scripts that may receive POST or GET variables and
act accordingly, but you will never (except possibly AJAX) have a
situation where a script will receive both sets of variables at the
same time. So, I don't think one can write a small simple script that
can demonstrate this.

This is terribly wrong... any drupal site (or probably any front controller based CMS) will use GET variables to route to the correct page which may have a form which will capture the POSTed data. I've seen POST and GET often in the same page.

Cheers,
Rob.

Rob:

Understood, but that is not what I am addressing. A form can only send data it has collected via it's declared method and the choices are POST or GET, but not both.

Please review the post I made to Paul on this subject -- that explains this concept better.

Cheers,

tedd

--
-------
http://sperling.com/

--- End Message ---
--- Begin Message ---
On Mon, Feb 14, 2011 at 12:30:56PM -0500, tedd wrote:

> >At 11:58 PM -0500 2/13/11, Paul M Foster wrote:
> >On Sun, Feb 13, 2011 at 02:25:45PM -0500, tedd wrote:
> >
> > > At 10:53 AM +0530 2/12/11, Ashim Kapoor wrote:
> > > >Dear All,
> > > >
> > > >I am reading "PHP5 and MySQL Bible". Chapter 7 of the book says
> >that PHP can
> > > >use GET and POST in the SAME page! Also it says that we can use
> the SAME
> > > >variables in GET and POST variable sets and that conflict
> >resolution is done
> > > >by variable_order option in php.ini Can some one write a small
> program to
> > > >illustrate the previous ideas?  It is not clear to me as to how
> >to implement
> > > >this.
> > > >
> > > >Many thanks,
> > > >Ashim.
> > >
> > > Ashim:
> > >
> > > What others have not addressed is that the form used to send
> > > variables will send only GET OR POST method variables, but not both
> > > at the same time.
> >
> >-snip-
> >
> >It sounds like you're saying this isn't possible, yet it is. So what am
> >I missing? Is there an error in my code?
> >
> >Paul
> >
> >--
> >Paul M. Foster
> >http://noferblatz.com
> 
> 
> Paul:
> 
> What I am saying is your form is a POST-method-form and it will only
> send variables via the POST method. If you change the form's method
> to GET, then the variables from the form will be sent via the GET
> method.
> 
> Remember, there is ONLY ONE form being used per Submit action -- it
> can either be a POST method or a GET method, but it can not be both.
> 
> This does not mean that you can't send both POST and GET variables
> together via the same Submit action, but it does mean that the *form*
> will only send it's contents via one method or the other.
> 
> Here's a demo:
> 
> http://php1.net/c/post-get
> 
> Please note that the POST method form will send only POST data and
> the GET method form will send only GET data. However, you can force a
> POST method *form* via the action attribute value to also include a
> GET variable (see Submit POST w/GET) -- BUT -- that does not come
> from the form's input but rather from the action attribute. Do you
> see what I mean?
> 
> The code is shown.

Understood. It sounded like you were saying you could only get back POST
or GET values from a page, which isn't true. The form itself, yes, can
only have one or the other method attribute.

> 
> Also, please note interaction between the GET/POST/REQUEST re the
> variable 'alfa' and you will get an idea of why REQUEST may pose a
> problem.

This is why I don't use the REQUEST variable.

Paul

-- 
Paul M. Foster
http://noferblatz.com


--- End Message ---
--- Begin Message ---
Hi!

I've been developing this stand alone application, found the webserver
that I am going to use (it is written in php) and all is good... on
windows.

I can get the php.exe and php-cgi.exe running no issue on windows
without "installing" them... even tested on 3 machines (developed on
Vista, sent to my friend on W7, and my GF on XP Home) and they all work
fine... now I was trying to port it over to linux, and I can't find
anything that I can use :(

Is there such a thing?  or will I have to have a pre-req of "php and
php-cgi must be installed on linux" disclaimer?

Any help, or pointers to where my google-fu has failed me would be
appreciated :)

TIA!

Steve


--- End Message ---
--- Begin Message ---
On Mon, Feb 14, 2011 at 12:32:51PM -0500, Steve Staples wrote:

> Hi!
> 
> I've been developing this stand alone application, found the webserver
> that I am going to use (it is written in php) and all is good... on
> windows.
> 
> I can get the php.exe and php-cgi.exe running no issue on windows
> without "installing" them... even tested on 3 machines (developed on
> Vista, sent to my friend on W7, and my GF on XP Home) and they all work
> fine... now I was trying to port it over to linux, and I can't find
> anything that I can use :(
> 
> Is there such a thing?  or will I have to have a pre-req of "php and
> php-cgi must be installed on linux" disclaimer?
> 
> Any help, or pointers to where my google-fu has failed me would be
> appreciated :)

I don't know if there's a web server under Linux which is written in
PHP. But I imagine you'd be hard pressed to find a Linux distribution
which does not come with several web servers and PHP either already
installed or easily installable. After all, most of the web is running
on Linux servers.

Paul

-- 
Paul M. Foster
http://noferblatz.com


--- End Message ---
--- Begin Message ---
On Mon, 2011-02-14 at 16:10 -0500, Paul M Foster wrote:
> On Mon, Feb 14, 2011 at 12:32:51PM -0500, Steve Staples wrote:
> 
> > Hi!
> > 
> > I've been developing this stand alone application, found the webserver
> > that I am going to use (it is written in php) and all is good... on
> > windows.
> > 
> > I can get the php.exe and php-cgi.exe running no issue on windows
> > without "installing" them... even tested on 3 machines (developed on
> > Vista, sent to my friend on W7, and my GF on XP Home) and they all work
> > fine... now I was trying to port it over to linux, and I can't find
> > anything that I can use :(
> > 
> > Is there such a thing?  or will I have to have a pre-req of "php and
> > php-cgi must be installed on linux" disclaimer?
> > 
> > Any help, or pointers to where my google-fu has failed me would be
> > appreciated :)
> 
> I don't know if there's a web server under Linux which is written in
> PHP. But I imagine you'd be hard pressed to find a Linux distribution
> which does not come with several web servers and PHP either already
> installed or easily installable. After all, most of the web is running
> on Linux servers.
> 
> Paul
> 
> -- 
> Paul M. Foster
> http://noferblatz.com
> 
> 

it's not that I dont want to have a webserver installed, it is a
standalone app, that doesn't require apache or any other httpd server
running... for people who want to run this on their windows, or linux,
or slackware servers and have no use for an "apache" server running for
just this.

i've got it working for windows, where i didn't have to install
anything, was just looking for something for a "*nix" distribution that
will do it too.

the webserver i am using, is 'nanserv', which works perfectly for what I
need it to do, and running under linux, it's very fast (windows is a bit
slow, but that's windows for ya)

I guess I am just going to have to say that having PHP installed is a
pre-req for the app (i just dont want to go installing software on
people's computers)


--- End Message ---
--- Begin Message ---
On 14 February 2011 21:50, Steve Staples <[email protected]> wrote:
> On Mon, 2011-02-14 at 16:10 -0500, Paul M Foster wrote:
>> On Mon, Feb 14, 2011 at 12:32:51PM -0500, Steve Staples wrote:
>>
>> > Hi!
>> >
>> > I've been developing this stand alone application, found the webserver
>> > that I am going to use (it is written in php) and all is good... on
>> > windows.
>> >
>> > I can get the php.exe and php-cgi.exe running no issue on windows
>> > without "installing" them... even tested on 3 machines (developed on
>> > Vista, sent to my friend on W7, and my GF on XP Home) and they all work
>> > fine... now I was trying to port it over to linux, and I can't find
>> > anything that I can use :(
>> >
>> > Is there such a thing?  or will I have to have a pre-req of "php and
>> > php-cgi must be installed on linux" disclaimer?
>> >
>> > Any help, or pointers to where my google-fu has failed me would be
>> > appreciated :)
>>
>> I don't know if there's a web server under Linux which is written in
>> PHP. But I imagine you'd be hard pressed to find a Linux distribution
>> which does not come with several web servers and PHP either already
>> installed or easily installable. After all, most of the web is running
>> on Linux servers.
>>
>> Paul
>>
>> --
>> Paul M. Foster
>> http://noferblatz.com
>>
>>
>
> it's not that I dont want to have a webserver installed, it is a
> standalone app, that doesn't require apache or any other httpd server
> running... for people who want to run this on their windows, or linux,
> or slackware servers and have no use for an "apache" server running for
> just this.
>
> i've got it working for windows, where i didn't have to install
> anything, was just looking for something for a "*nix" distribution that
> will do it too.
>
> the webserver i am using, is 'nanserv', which works perfectly for what I
> need it to do, and running under linux, it's very fast (windows is a bit
> slow, but that's windows for ya)
>
> I guess I am just going to have to say that having PHP installed is a
> pre-req for the app (i just dont want to go installing software on
> people's computers)
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

What you are looking for is php cli.
http://docs.php.net/manual/en/install.unix.commandline.php contains
scant information about this.

But, a common mechanism (and as a windows only user, I'm 50% guessing
here) in getting a "file" to run it's handler is to add a line to the
top of the file which the shell you are running uses to determine the
program responsible for handling it.

On Windows, this is a combination of the extension and the registry
(controlled by assoc and ftype). On *nix, the line at the top of the
script seems to to a similar thing.

#!/usr/bin/php

http://articles.sitepoint.com/article/php-command-line-1 seems like a
good place to start.


There may be some issue with the line. The install may be in a
different location or have a different name (#!/usr/local/bin/php-cgi
for example).

Richard.

-- 
Richard Quadling
Twitter : EE : Zend
@RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY

--- End Message ---
--- Begin Message ---
I have 2 buttons on a page:

if (isset($_POST['botton1'])) {dothing1();}
if (isset($_POST['button2'])) {dothing2();}

They both work as intended when I click on them. If however I click
within a text box and hit enter, they both fire.

Is there a way to stop this?

Thanks.

-- 
Paul Halliday
http://www.pintumbler.org

--- End Message ---
--- Begin Message ---
On Mon, Feb 14, 2011 at 03:35:02PM -0400, Paul Halliday wrote:

> I have 2 buttons on a page:
> 
> if (isset($_POST['botton1'])) {dothing1();}
> if (isset($_POST['button2'])) {dothing2();}
> 
> They both work as intended when I click on them. If however I click
> within a text box and hit enter, they both fire.
> 
> Is there a way to stop this?

Check your code. My experience has been that forms with multiple submits
will fire the *first* submit in the form when you hit Enter in a text
field or whatever. I just tested this and found it to be true.

Now, I'm doing this in Firefox on Linux. I suppose there could be
differences among browsers, but I suspect that the specs for HTML
mandate the behavior I describe.

Paul

-- 
Paul M. Foster
http://noferblatz.com


--- End Message ---
--- Begin Message ---
On Feb 14, 2011, at 4:18 PM, Paul M Foster wrote:

> On Mon, Feb 14, 2011 at 03:35:02PM -0400, Paul Halliday wrote:
> 
>> I have 2 buttons on a page:
>> 
>> if (isset($_POST['botton1'])) {dothing1();}
>> if (isset($_POST['button2'])) {dothing2();}
>> 
>> They both work as intended when I click on them. If however I click
>> within a text box and hit enter, they both fire.
>> 
>> Is there a way to stop this?
> 
> Check your code. My experience has been that forms with multiple submits
> will fire the *first* submit in the form when you hit Enter in a text
> field or whatever. I just tested this and found it to be true.
> 
> Now, I'm doing this in Firefox on Linux. I suppose there could be
> differences among browsers, but I suspect that the specs for HTML
> mandate the behavior I describe.
> 
> Paul
> 

If you don't mind using a little JavaScript you can test for which button 
should fire when enter is pressed.  How I would do it is to first add a hidden 
field and call it "buttonClicked".  Now, in the text field where you would like 
a button to fire if enter is pressed, at this to the tag: 
onkeyup="checkKey(this,event)".  For the JavaScript portion of it, do this:

function checkKey(element,evt) {
        var buttonClicked=""

        if(evt.keyCode=13) {
                if(element.name="field1") {
                        buttonClicked="button1"
                } else if(element.name=="field2") {
                        buttonClicked="button2"
                }
                if(buttonClicked) {
                        document.formName.buttonClicked.value=buttonClicked
                        document.formName.submit()
                }
        }
}

Hope that helps!

Take care,
Floyd


--- End Message ---
--- Begin Message ---
On Mon, Feb 14, 2011 at 05:15:11PM -0500, Floyd Resler wrote:

> 
> On Feb 14, 2011, at 4:18 PM, Paul M Foster wrote:
> 
> > On Mon, Feb 14, 2011 at 03:35:02PM -0400, Paul Halliday wrote:
> > 
> >> I have 2 buttons on a page:
> >> 
> >> if (isset($_POST['botton1'])) {dothing1();} if
> >> (isset($_POST['button2'])) {dothing2();}
> >> 
> >> They both work as intended when I click on them. If however I click
> >> within a text box and hit enter, they both fire.
> >> 
> >> Is there a way to stop this?
> > 
> > Check your code. My experience has been that forms with multiple
> > submits will fire the *first* submit in the form when you hit Enter
> > in a text field or whatever. I just tested this and found it to be
> > true.
> > 
> > Now, I'm doing this in Firefox on Linux. I suppose there could be
> > differences among browsers, but I suspect that the specs for HTML
> > mandate the behavior I describe.
> > 
> > Paul
> > 
> 
> If you don't mind using a little JavaScript you can test for which
> button should fire when enter is pressed.  How I would do it is to
> first add a hidden field and call it "buttonClicked".  Now, in the
> text field where you would like a button to fire if enter is pressed,
> at this to the tag: onkeyup="checkKey(this,event)".  For the
> JavaScript portion of it, do this:

Yeah, but you don't even have to go that far. Just put a print_r($_POST)
at the beginning of the file, and you'll see which button gets pressed.
It will show up in the POST array.

Paul

-- 
Paul M. Foster
http://noferblatz.com


--- End Message ---
--- Begin Message ---
15.02.2011 00:24, Paul M Foster yazmış:
On Mon, Feb 14, 2011 at 05:15:11PM -0500, Floyd Resler wrote:

On Feb 14, 2011, at 4:18 PM, Paul M Foster wrote:

On Mon, Feb 14, 2011 at 03:35:02PM -0400, Paul Halliday wrote:

I have 2 buttons on a page:

if (isset($_POST['botton1'])) {dothing1();} if
(isset($_POST['button2'])) {dothing2();}

They both work as intended when I click on them. If however I click
within a text box and hit enter, they both fire.

Is there a way to stop this?
Check your code. My experience has been that forms with multiple
submits will fire the *first* submit in the form when you hit Enter
in a text field or whatever. I just tested this and found it to be
true.

Now, I'm doing this in Firefox on Linux. I suppose there could be
differences among browsers, but I suspect that the specs for HTML
mandate the behavior I describe.

Paul

If you don't mind using a little JavaScript you can test for which
button should fire when enter is pressed.  How I would do it is to
first add a hidden field and call it "buttonClicked".  Now, in the
text field where you would like a button to fire if enter is pressed,
at this to the tag: onkeyup="checkKey(this,event)".  For the
JavaScript portion of it, do this:
Yeah, but you don't even have to go that far. Just put a print_r($_POST)
at the beginning of the file, and you'll see which button gets pressed.
It will show up in the POST array.

Paul

this could also work;
JS:
function send ( selectedtype )
{
document.formname.postingvalue.value = selectedtype ;
document.formname.submit() ;
}
html:
<form name=formname>
<input type="hidden" name="postingvalue" />
<a href="javascript:send('1')">Button 1</a></td>
<a href="javascript:send('2')">Button 2</a></td>
<a href="javascript:send('3')">Button 3</a></td>
php:
if($_POST['postingvalue']=="1")
{ code here } elseif ($_POST['postingvalue']=="1")
{ code 2 here } bla bla

--- End Message ---
--- Begin Message ---
2011/2/14 Paul Halliday <[email protected]>:
> if (isset($_POST['botton1'])) {dothing1();}
> if (isset($_POST['button2'])) {dothing2();}

Hello.

in html:
<input type="submit" value="Update" name="op">
<input type="submit" value="Checkout" name="op">

in PHP
if(isset($_POST['op'])){
 switch($_POST['op']){
  case 'Update':
   dothing1();
  break;

  case 'Checkout':
   dothing2();
  break;

  default:
   dohatehackersfunction(':)');
  break;
 }
}

PS: Don't use multiple input type="image" buttons - all IE's don't
send value attribute in POST - use CSS


-- 
WBR, Dmitrii
+375 29 60-LINUX, 25-LINUX, 40-LINUX
icq: 193-74-771
www.varvashenia.ru, www.seoder.ru

--- End Message ---
--- Begin Message ---
oh - I forgot - the first the submit will be the default

2011/2/15 Dmitrii Varvashenia <[email protected]>:


-- 
WBR, Dmitrii
+375 29 60-LINUX, 25-LINUX, 40-LINUX
icq: 193-74-771
www.varvashenia.ru, www.seoder.ru

--- End Message ---
--- Begin Message --- Now I have a situation. I need to take the code from my former home page and modify it to lay out a table (let's say 5 cells wide) and as many rows deep to contain all the items. Each item is the name of the directory, under which is an icon image from that directory, under which is a description file read from that directory. Here is my code thus far:

<?php
# The next several lines declare an array of directories which are NOT to be listed!#
$excludes[] = 'attachments'; #20
$excludes[] = 'data';
$excludes[] = 'include';
$excludes[] = 'resources';
$excludes[] = 'stats';
$excludes[] = '_private';
$excludes[] = '_vti_bin';
$excludes[] = '_vti_cnf';
$excludes[] = '_vti_log';
$excludes[] = '_vti_pvt';
$excludes[] = '_vti_txt'; #30
$excludes[] = '_vxi_txt';
$excludes[] = 'css';
$excludes[] = 'img';
$excludes[] = 'images';
$excludes[] = 'js';
$excludes[] = 'cgi';
$excludes[] = 'cgi-bin';
$excludes[] = 'ssfm';
$ls = scandir(dirname(__FILE__));
echo '<table border="1" cellspacing="5" cellpadding="0" bgcolor="E0E0E0"><tr align="center">'; #40 foreach ($ls as $d) { if (is_dir($d) && !preg_match('/^\./',basename($d)) &&!in_array(basename($d),$excludes))
 {
echo '<td>'.$d.'<br><a href="'.$d.'"><img src="/Categories/'.$d.'/thumb.png" border="5"></a><br>';
  include($d."/desc.txt");
  echo '</td>';
  };
};
echo '</tr></table>';
?>

Now I am stymied on changing this to add </tr><tr> at the right points in the structure. I am new to php, and welcome all suggestions and gainful comments.

--
end

Very Truly yours,
                 - Kirk Bailey,
                   Largo Florida

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


--- End Message ---
--- Begin Message ---
On 15 February 2011 00:53, Kirk Bailey <[email protected]> wrote:
> Now I have a situation. I need to take the code from my former home page and
> modify it to lay out a table (let's say 5 cells wide) and as many rows deep
> to contain all the items. Each item is the name of the directory, under
> which is an icon image from that directory, under which is a description
> file read from that directory. Here is my code thus far:
>
> <?php
> # The next several lines declare an array of directories which are NOT to be
> listed!#
> $excludes[] = 'attachments'; #20
> $excludes[] = 'data';
> $excludes[] = 'include';
> $excludes[] = 'resources';
> $excludes[] = 'stats';
> $excludes[] = '_private';
> $excludes[] = '_vti_bin';
> $excludes[] = '_vti_cnf';
> $excludes[] = '_vti_log';
> $excludes[] = '_vti_pvt';
> $excludes[] = '_vti_txt'; #30
> $excludes[] = '_vxi_txt';
> $excludes[] = 'css';
> $excludes[] = 'img';
> $excludes[] = 'images';
> $excludes[] = 'js';
> $excludes[] = 'cgi';
> $excludes[] = 'cgi-bin';
> $excludes[] = 'ssfm';
> $ls = scandir(dirname(__FILE__));
> echo '<table border="1" cellspacing="5" cellpadding="0" bgcolor="E0E0E0"><tr
> align="center">'; #40
> foreach ($ls as $d) {  if (is_dir($d) && !preg_match('/^\./',basename($d))
> &&!in_array(basename($d),$excludes))
>  {
>  echo '<td>'.$d.'<br><a href="'.$d.'"><img
> src="/Categories/'.$d.'/thumb.png" border="5"></a><br>';
>  include($d."/desc.txt");
>  echo '</td>';
>  };
> };
> echo '</tr></table>';
> ?>
>
> Now I am stymied on changing this to add </tr><tr> at the right points in
> the structure. I am new to php, and welcome all suggestions and gainful
> comments.

$ls = scandir(dirname(__FILE__));
echo '<table border="1" cellspacing="5" cellpadding="0" bgcolor="E0E0E0">'; #40
foreach ($ls as $d) {  if (is_dir($d) &&
!preg_match('/^\./',basename($d)) &&!in_array(basename($d),$excludes))
 {
 echo '<tr align="center"><td>', $d, '<br><a href="', $d, '"><img
src="/Categories/', $d, '/thumb.png" border="5"></a><br>';
 include($d."/desc.txt");
 echo '</td></tr>';
 };
};
echo '</table>';

maybe?

-- 
Richard Quadling
Twitter : EE : Zend
@RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY

--- End Message ---
--- Begin Message --- well, this ends the row after every cell. I am trying to get a row of 5 cells across, then end it and start a new row. If the routines stops before the end of the count of 5 due to lack of further directories, closing out the table following the loops will onclude a </tr> tag.


On 2/14/2011 8:30 PM, Richard Quadling wrote:
On 15 February 2011 00:53, Kirk Bailey<[email protected]>  wrote:
Now I have a situation. I need to take the code from my former home page and
modify it to lay out a table (let's say 5 cells wide) and as many rows deep
to contain all the items. Each item is the name of the directory, under
which is an icon image from that directory, under which is a description
file read from that directory. Here is my code thus far:

<?php
# The next several lines declare an array of directories which are NOT to be
listed!#
$excludes[] = 'attachments'; #20
$excludes[] = 'data';
$excludes[] = 'include';
$excludes[] = 'resources';
$excludes[] = 'stats';
$excludes[] = '_private';
$excludes[] = '_vti_bin';
$excludes[] = '_vti_cnf';
$excludes[] = '_vti_log';
$excludes[] = '_vti_pvt';
$excludes[] = '_vti_txt'; #30
$excludes[] = '_vxi_txt';
$excludes[] = 'css';
$excludes[] = 'img';
$excludes[] = 'images';
$excludes[] = 'js';
$excludes[] = 'cgi';
$excludes[] = 'cgi-bin';
$excludes[] = 'ssfm';
$ls = scandir(dirname(__FILE__));
echo '<table border="1" cellspacing="5" cellpadding="0" bgcolor="E0E0E0"><tr
align="center">'; #40
foreach ($ls as $d) {  if (is_dir($d)&&  !preg_match('/^\./',basename($d))
&&!in_array(basename($d),$excludes))
  {
  echo '<td>'.$d.'<br><a href="'.$d.'"><img
src="/Categories/'.$d.'/thumb.png" border="5"></a><br>';
  include($d."/desc.txt");
  echo '</td>';
  };
};
echo '</tr></table>';
?>

Now I am stymied on changing this to add</tr><tr>  at the right points in
the structure. I am new to php, and welcome all suggestions and gainful
comments.
$ls = scandir(dirname(__FILE__));
echo '<table border="1" cellspacing="5" cellpadding="0" bgcolor="E0E0E0">'; #40
foreach ($ls as $d) {  if (is_dir($d)&&
!preg_match('/^\./',basename($d))&&!in_array(basename($d),$excludes))
  {
  echo '<tr align="center"><td>', $d,'<br><a href="', $d, '"><img
src="/Categories/', $d, '/thumb.png" border="5"></a><br>';
  include($d."/desc.txt");
  echo '</td></tr>';
  };
};
echo '</table>';

maybe?


--
end

Very Truly yours,
                 - Kirk Bailey,
                   Largo Florida

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


--- End Message ---
--- Begin Message ---
On 2/14/2011 4:53 PM, Kirk Bailey wrote:
> Now I have a situation. I need to take the code from my former home page and
> modify it to lay out a table (let's say 5 cells wide) and as many rows deep to
> contain all the items. Each item is the name of the directory, under which is 
> an
> icon image from that directory, under which is a description file read from 
> that
> directory. Here is my code thus far:
> 
> <?php
> # The next several lines declare an array of directories which are NOT to be
> listed!#
> $excludes[] = 'attachments'; #20
> $excludes[] = 'data';
> $excludes[] = 'include';
> $excludes[] = 'resources';
> $excludes[] = 'stats';
> $excludes[] = '_private';
> $excludes[] = '_vti_bin';
> $excludes[] = '_vti_cnf';
> $excludes[] = '_vti_log';
> $excludes[] = '_vti_pvt';
> $excludes[] = '_vti_txt'; #30
> $excludes[] = '_vxi_txt';
> $excludes[] = 'css';
> $excludes[] = 'img';
> $excludes[] = 'images';
> $excludes[] = 'js';
> $excludes[] = 'cgi';
> $excludes[] = 'cgi-bin';
> $excludes[] = 'ssfm';
> $ls = scandir(dirname(__FILE__));
> echo '<table border="1" cellspacing="5" cellpadding="0" bgcolor="E0E0E0"><tr
> align="center">'; #40
> foreach ($ls as $d) {  if (is_dir($d) && !preg_match('/^\./',basename($d))
> &&!in_array(basename($d),$excludes))
>  {
>   echo '<td>'.$d.'<br><a href="'.$d.'"><img src="/Categories/'.$d.'/thumb.png"
> border="5"></a><br>';
>   include($d."/desc.txt");
>   echo '</td>';
>   };
> };
> echo '</tr></table>';
> ?>
> 
> Now I am stymied on changing this to add </tr><tr> at the right points in the
> structure. I am new to php, and welcome all suggestions and gainful comments.
> 

You want modulo math...

Check out this page

http://www.cmsws.com/examples/php/modulo_array_output.php

If you like the output, here is the source

http://www.cmsws.com/examples/php/modulo_array_output.phps

The bottom two examples show how you can use other HTML tags to present data.

Jim Lucas

--- End Message ---
--- Begin Message --- Very nice; I am leaning in the direction of doing it this way. Thank you!
:-)



On 2/14/2011 9:24 PM, Jim Lucas wrote:
On 2/14/2011 4:53 PM, Kirk Bailey wrote:
Now I have a situation. I need to take the code from my former home page and
modify it to lay out a table (let's say 5 cells wide) and as many rows deep to
contain all the items. Each item is the name of the directory, under which is an
icon image from that directory, under which is a description file read from that
directory. Here is my code thus far:

<?php
# The next several lines declare an array of directories which are NOT to be
listed!#
$excludes[] = 'attachments'; #20
$excludes[] = 'data';
$excludes[] = 'include';
$excludes[] = 'resources';
$excludes[] = 'stats';
$excludes[] = '_private';
$excludes[] = '_vti_bin';
$excludes[] = '_vti_cnf';
$excludes[] = '_vti_log';
$excludes[] = '_vti_pvt';
$excludes[] = '_vti_txt'; #30
$excludes[] = '_vxi_txt';
$excludes[] = 'css';
$excludes[] = 'img';
$excludes[] = 'images';
$excludes[] = 'js';
$excludes[] = 'cgi';
$excludes[] = 'cgi-bin';
$excludes[] = 'ssfm';
$ls = scandir(dirname(__FILE__));
echo '<table border="1" cellspacing="5" cellpadding="0" bgcolor="E0E0E0"><tr
align="center">'; #40
foreach ($ls as $d) {  if (is_dir($d)&&  !preg_match('/^\./',basename($d))
&&!in_array(basename($d),$excludes))
  {
   echo '<td>'.$d.'<br><a href="'.$d.'"><img src="/Categories/'.$d.'/thumb.png"
border="5"></a><br>';
   include($d."/desc.txt");
   echo '</td>';
   };
};
echo '</tr></table>';
?>

Now I am stymied on changing this to add</tr><tr>  at the right points in the
structure. I am new to php, and welcome all suggestions and gainful comments.

You want modulo math...

Check out this page

http://www.cmsws.com/examples/php/modulo_array_output.php

If you like the output, here is the source

http://www.cmsws.com/examples/php/modulo_array_output.phps

The bottom two examples show how you can use other HTML tags to present data.

Jim Lucas


--
end

Very Truly yours,
                 - Kirk Bailey,
                   Largo Florida

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


--- End Message ---
--- Begin Message ---
Hi everyone! I'm just starting out with PHP so I'm looking for a good
place to ask some questions, have my code critiqued (read: visciously
ripped apart), and generally BS about PHP and programming in general
(if that's OK). I know that PHP sometimes has a reputation for having
(some) lousy developers, and I'd like to avoid becoming one of those
people. To that end, I've subscribed here because I've always found
mailing lists to have much higher-quality discourse than the average
online foum.

Looking forward to participating!

- BW

P.S, The rules page on the web
(http://us2.php.net/reST/php-src/README.MAILINGLIST_RULES) is
currently broken, but this one works:
http://us2.php.net/reST/php-src/trunk_README.MAILINGLIST_RULES. Just a
heads up to whoever is in charge of that.

--- End Message ---
--- Begin Message ---
So I decided to write a template class in order to get myself going on
learning PHP. Of course I wrote the simplest thing possible:

class Template
{
        protected $template;
        protected $vars;

        public function __construct($template)
        {
                $this->template = $template;
        }

        public function __set($name, $value)
        {
                $this->vars[$name] = $value;
        }

        public function __get($name)
        {
                return $this->vars[$name];
        }

        public function __toString()
        {
                ob_start();
                eval('?>' . $this->template);
                return ob_get_clean();
        }
}

Which you can use, quite simply like this:

$tpl = new Template(file_get_contents('index.tpl.php'));

$tpl->title = 'Here\'s the title';
$tpl->text = 'Blah blah blah...';

echo $tpl;

I have a few questions though.

- First, I'm storing the template as an actual string, instead of just
a path to a template file, which means I'm using eval() instead of
require() in my __toString(). My thinking was that this would avoid
reading the template file twice in the case that __toString() gets
called multiple times. But will PHP handle this automagically if I do
in fact decide to store a path to a file, and call require() instead?

- Secondly, I noticed that in the constructor, it's not necessary to
initialize $vars to an empty array, and I haven't done so. I guess PHP
automatically initializes it the first time I set one of its elements
to a value. Is this okay, or is there a better way in the name of best
practices?

- Finally, I'd like to be able to limit what things can be accessed
from the scope of the template file. As it stands, if you have a
function named blowUpTheComputer() or a gobal variable called
$dontTouchThis, a template author can easily cause trouble. They can
also access any methods and properties of the Template class. How
would you go about restricting this?

Thanks a lot!

- BW

--- End Message ---
--- Begin Message ---
On Mon, Feb 14, 2011 at 09:52:51PM -0500, Brian Waters wrote:

> So I decided to write a template class in order to get myself going on
> learning PHP. Of course I wrote the simplest thing possible:
> 
> class Template
> {
>         protected $template;
>         protected $vars;
> 
>         public function __construct($template)
>         {
>                 $this->template = $template;
>         }
> 
>         public function __set($name, $value)
>         {
>                 $this->vars[$name] = $value;
>         }
> 
>         public function __get($name)
>         {
>                 return $this->vars[$name];
>         }
> 
>         public function __toString()
>         {
>                 ob_start();
>                 eval('?>' . $this->template);
>                 return ob_get_clean();
>         }
> }

This is *not* a simple class. You're using magic methods, etc.

> 
> Which you can use, quite simply like this:
> 
> $tpl = new Template(file_get_contents('index.tpl.php'));
> 
> $tpl->title = 'Here\'s the title';
> $tpl->text = 'Blah blah blah...';
> 
> echo $tpl;
> 
> I have a few questions though.
> 
> - First, I'm storing the template as an actual string, instead of just
> a path to a template file, which means I'm using eval() instead of
> require() in my __toString(). My thinking was that this would avoid
> reading the template file twice in the case that __toString() gets
> called multiple times. But will PHP handle this automagically if I do
> in fact decide to store a path to a file, and call require() instead?

Advice: don't use eval() this way. It's slow and dangerous. I'd suggest
simply reading your template file into a class variable in the
constructor. Considering this is a "template" class, you're likely to
need to read the whole thing in at some point anyway. Do it at the
beginning and avoid eval() and the like. Also, you're not obligating
your user to read in the file and pass it to you on the stack, which is
really an abuse of the stack if you can avoid it. And once read in, you
can "vet" it with various PHP functions to ensure it doesn't have
dangerous content when it is eventually output.

> 
> - Secondly, I noticed that in the constructor, it's not necessary to
> initialize $vars to an empty array, and I haven't done so. I guess PHP
> automatically initializes it the first time I set one of its elements
> to a value. Is this okay, or is there a better way in the name of best
> practices?

PHP is a language which doesn't require variables to be initialized
before use. That said, it's probably superior programming practice to do
so anyway. That way, you *know* what's in there when you accidentally
use the variable later (which you will).

> 
> - Finally, I'd like to be able to limit what things can be accessed
> from the scope of the template file. As it stands, if you have a
> function named blowUpTheComputer() or a gobal variable called
> $dontTouchThis, a template author can easily cause trouble. They can
> also access any methods and properties of the Template class. How
> would you go about restricting this?

Now you see the liability of eval(). If you're going to allow
user-defined and evaluated functions in your template, you're going to
have to live with the consequences. Fortunately, the worst thing that's
liable to happen is they hose the website, not the whole computer; PHP
runs with the permissions of the server, which are generally limited to
hacking up files in the web root directories.

Rather than evaluating user functions at runtime, I would instead allow
"hooks", where the user can perhaps "register" a function and you
include() or require() its file later. Drupal uses a scheme vaguely like
this. Of course, you're still stuck if the user is an idiot or
malicious.

Actually, I'd consider a template class like this overkill anyway,
unless you plan to build a templating language and interpreter. Instead,
simply set a variable to the template filename, and just include() it at
some point. Make the user initialize all the variables and read in all
the include files before you do this. They can then do anything they
like within the template, which is generally just a HTML file with some
PHP code in it.

And if you're truly new to PHP, I'd suggest just coding some forms with
PHP to handle the interaction in them as a first action. This will get
you used to the way PHP handles variables, POST and GET, and the like.
Simple stuff without classes, until you're fully comfortable with basic
PHP variable handling, built-in functions, etc.

Paul

-- 
Paul M. Foster
http://noferblatz.com


--- End Message ---

Reply via email to