php-general Digest 18 Mar 2006 20:38:11 -0000 Issue 4023

Topics (messages 232177 through 232194):

Re: PHP, SQL, AJAX, JS and populating a SelectBox?
        232177 by: Weber Sites LTD
        232180 by: Greg Beaver
        232181 by: Manuel Amador (Rudd-O)
        232182 by: Greg Beaver
        232183 by: Manuel Amador (Rudd-O)
        232184 by: Greg Beaver
        232185 by: Manuel Amador (Rudd-O)
        232186 by: dave

Updating a single line in a file
        232178 by: smr78
        232188 by: Curt Zirzow

Multiple inheritance: a technique
        232179 by: Manuel Amador (Rudd-O)

no newline after "?>" in the resulting HTML
        232187 by: Rostislav Krasny
        232189 by: Brady Mitchell
        232190 by: Rasmus Lerdorf
        232191 by: Adrian
        232192 by: Rasmus Lerdorf
        232193 by: James Benson
        232194 by: Chris Shiflett

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:
        php-general@lists.php.net


----------------------------------------------------------------------
--- Begin Message ---
Why invent the wheel?

http://developer.ebusiness-apps.com/technologies/webdevelopment/codeandcompo
nents/ebawebcombov3/default.htm

Sincerely 
 
berber 
 
Visit the Weber Sites Today, 
To see where PHP might take you tomorrow. 
PHP code examples : http://www.weberdev.com 
PHP & MySQL Forums : http://www.weberforums.com
Free Uptime Monitor : http://uptime.weberdev.com
SEO Data Monitor http://seo.weberdev.com
 

-----Original Message-----
From: Daevid Vincent [mailto:[EMAIL PROTECTED] 
Sent: Saturday, March 18, 2006 4:43 AM
To: php-general@lists.php.net
Subject: [PHP] PHP, SQL, AJAX, JS and populating a SelectBox?

I need to dynamically update a select box with results from a SQL database
using AJAX, but I can't find a single example of how to do this.

Basically I have a text input field, and a select box. 
As someone types in the input field,
I want the select box to fill in the results of matches.

I can fill in a <DIV> (as per the ten million examples out there) and that's
all fine and dandy, but way too simplistic for what I need.

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

--- End Message ---
--- Begin Message ---
Daevid Vincent wrote:
> I need to dynamically update a select box 
> with results from a SQL database using AJAX, 
> but I can't find a single example of how to do this.
> 
> Basically I have a text input field, and a select box. 
> As someone types in the input field, 
> I want the select box to fill in the results of matches.
> 
> I can fill in a <DIV> (as per the ten million examples out there)
> and that's all fine and dandy, but way too simplistic for what I need.

The best way to do this is indeed to put the entire select in a div, and
to replace the innerHTML of that div with the html for the select.
Always do as much processing as possible on the server side, or your
application will become interminably slow both to load and to run.

In my testing, I've found that the latency over high speed internet of
passing the entire select is exactly the same as it is from my local
machine.  When I used to pass an array of data and repopulate using
javascript DOM, it was slow as molasses, and I would occasionally have
weird timeouts.

Don't try to be smart when you can be simple :)

Greg

--- End Message ---
--- Begin Message ---
Greg Beaver wrote:

Daevid Vincent wrote:
I need to dynamically update a select box with results from a SQL database using AJAX, but I can't find a single example of how to do this.

Basically I have a text input field, and a select box. As someone types in the input field, I want the select box to fill in the results of matches.

I can fill in a <DIV> (as per the ten million examples out there)
and that's all fine and dandy, but way too simplistic for what I need.

The best way to do this is indeed to put the entire select in a div, and
to replace the innerHTML of that div with the html for the select.
But that is not DOM.  Another way to do this is:
- run your XMLHttpRequest
- have your server-side AJAX target script spit a newline-separated text file, with IDs and names such as:
1 XYZ
2 JWV
3 Something
- once the response is on the client, break the text file down with string manipulation functions/methods (split() comes to mind), perform a document.getElementByID("yourselect") and append document.createElement("option")'s to it, obviously setting the value properties on each element, and appending a document.createTextNode("your option text") into every option you create.

Always do as much processing as possible on the server side, or your
application will become interminably slow both to load and to run.

In my testing, I've found that the latency over high speed internet of
passing the entire select is exactly the same as it is from my local
machine.  When I used to pass an array of data and repopulate using
javascript DOM, it was slow as molasses, and I would occasionally have
weird timeouts.
It shouldn't have been slow. DOM manipulation is fast. But you need to remember to instantiate new objects, add children objects first (in the example, the text nodes and options), and add the parent objects to your document then. Otherwise, I can see how your application could get slow.

Don't try to be smart when you can be simple :)
I'd advise against this, and I'd also advise you to look up a JavaScript-usable serialization microformat for data coming from the server (XML is kind of unwieldy for this). Look for JSON on google.

Greg


--- End Message ---
--- Begin Message ---
Manuel Amador (Rudd-O) wrote:

> Greg Beaver wrote:
>
>> Daevid Vincent wrote:
>>  
>>
>>> I need to dynamically update a select box with results from a SQL
>>> database using AJAX, but I can't find a single example of how to do
>>> this.
>>>
>>> Basically I have a text input field, and a select box. As someone
>>> types in the input field, I want the select box to fill in the
>>> results of matches.
>>>
>>> I can fill in a <DIV> (as per the ten million examples out there)
>>> and that's all fine and dandy, but way too simplistic for what I need.
>>>   
>>
>>
>> The best way to do this is indeed to put the entire select in a div, and
>> to replace the innerHTML of that div with the html for the select.
>>  
>>
> But that is not DOM.  Another way to do this is:

It's also not Java or Fortran, but as with these argument, that is
irrelevant information - it is valid javascript, and as we all know, the
"J" in ajax ain't DOM, it's javascript.

> - run your XMLHttpRequest
> - have your server-side AJAX target script spit a newline-separated
> text file, with IDs and names such as:
> 1 XYZ
> 2 JWV
> 3 Something
> - once the response is on the client, break the text file down with
> string manipulation functions/methods (split() comes to mind), perform
> a document.getElementByID("yourselect") and append
> document.createElement("option")'s to it, obviously setting the value
> properties on each element, and appending a
> document.createTextNode("your option text") into every option you create.

This is far too complicated.  You don't need 50 lines of code to convert
from server-side data to HTML when the browser does it for you (and far
more efficiently) with this code:

var someCallback = {
    ajaxfunc: function(res) {
       document.getElementById('blah').innerHTML = res;
    }
}

>
>> Always do as much processing as possible on the server side, or your
>> application will become interminably slow both to load and to run.
>>
>> In my testing, I've found that the latency over high speed internet of
>> passing the entire select is exactly the same as it is from my local
>> machine.  When I used to pass an array of data and repopulate using
>> javascript DOM, it was slow as molasses, and I would occasionally have
>> weird timeouts.
>>  
>>
> It shouldn't have been slow.  DOM manipulation is fast.  But you need
> to remember to instantiate new objects, add children objects first (in
> the example, the text nodes and options), and add the parent objects
> to your document then.  Otherwise, I can see how your application
> could get slow.

I hate to burst your bubble, but I am an experienced developer, and do
know how to use DOM.  My application used these exact techniques, and
was simply slower.  The internal rendering code for a browser is just
simply far faster than javascript DOM will ever be.

Another truth is self-evident: all browsers have uniformly implemented
the setting of innerHTML for far longer than the implementation of DOM
has been uniform, and your chances of encountering a bug in a particular
browser implementation are much slimmer.

>
>> Don't try to be smart when you can be simple :)
>>
> I'd advise against this, and I'd also advise you to look up a
> JavaScript-usable serialization microformat for data coming from the
> server (XML is kind of unwieldy for this).  Look for JSON on google. 

You advise against simplicity?

First off, any good programmer will tell you that simplicity is always
the first thing to look for in an application, as complex algorithms are
almost always best implemented with simple (and clever) code, but this
is a tirade for another time.  Second, what makes you think I don't
transfer the HTML using JSON?

I use http://pear.php.net/HTML_AJAX for the actual ajax details, and it
has several serialization options, JSON by default.

First off, let's examine why one would use ajax in the first place. 
It's not to be "correct" or rigorous - it is to streamline the user
experience.  Ajax of course introduces other concerns such as the danger
of overloading the server with more HTTP requests than traditional apps,
but this is easily fixed by ensuring that examples like the "type in
text box and populate select" have some appropriate limits on how often
it actually sends the requests for data.

If you still don't believe me, take a straw poll of the most proficient
programmers to see how they are using ajax in real-world time-critical
applications.

Greg

--- End Message ---
--- Begin Message ---
Greg Beaver wrote:

This is far too complicated.  You don't need 50 lines of code to convert
from server-side data to HTML when the browser does it for you (and far
more efficiently) with this code:

var someCallback = {
   ajaxfunc: function(res) {
      document.getElementById('blah').innerHTML = res;
   }
}
You know you're oversimplifying matters. What if the returned markup is invalid? What if the server experienced an HTTP 500 error? Why should anyone want to program their application to return 'tag soup' instead of formally defining interfaces via standards? That's asking for maintenance nightmares.


I hate to burst your bubble, but I am an experienced developer, and do
know how to use DOM.  My application used these exact techniques, and
was simply slower.  The internal rendering code for a browser is just
simply far faster than javascript DOM will ever be.

Another truth is self-evident: all browsers have uniformly implemented
the setting of innerHTML for far longer than the implementation of DOM
has been uniform, and your chances of encountering a bug in a particular
browser implementation are much slimmer.
I'm not talking about using DOM because it's more formal. I'm talking about using DOM and formally defined interfaces for maintenance cost reasons. Tag soup is harder to debug and automate/instrument for tests. Tag soup is a nightmare. Sure, for quickies it can't be beaten. But try to figure out what the hell you (or one of your engineers) did six months ago without a formally specified way of returning values...

Don't try to be smart when you can be simple :)

I'd advise against this, and I'd also advise you to look up a
JavaScript-usable serialization microformat for data coming from the
server (XML is kind of unwieldy for this). Look for JSON on google.

You advise against simplicity?
No.  I'm advising against the false trap of initial simplicity.

First off, any good programmer will tell you that simplicity is always
the first thing to look for in an application, as complex algorithms are
almost always best implemented with simple (and clever) code, but this
is a tirade for another time.  Second, what makes you think I don't
transfer the HTML using JSON?
If you did that, you would be gaining nothing. Whatever you use as return value format, it should be easily parsable markup and should be formally specified (standards of course should be preferred). HTML is not XML and it's not as easily parsed as XML.

I use http://pear.php.net/HTML_AJAX for the actual ajax details, and it
has several serialization options, JSON by default.

First off, let's examine why one would use ajax in the first place. It's not to be "correct" or rigorous - it is to streamline the user
experience.  Ajax of course introduces other concerns such as the danger
of overloading the server with more HTTP requests than traditional apps,
but this is easily fixed by ensuring that examples like the "type in
text box and populate select" have some appropriate limits on how often
it actually sends the requests for data.
That statement I agree with.

If you still don't believe me, take a straw poll of the most proficient
programmers to see how they are using ajax in real-world time-critical
applications.
I've seen real-world AJAX applications succumb to complexity. I'm just suggesting something to manage complexity which should be fairly obvious now.

Greg

--- End Message ---
--- Begin Message ---
Manuel Amador (Rudd-O) wrote:
> Greg Beaver wrote:
> 
>> This is far too complicated.  You don't need 50 lines of code to convert
>> from server-side data to HTML when the browser does it for you (and far
>> more efficiently) with this code:
>>
>> var someCallback = {
>>    ajaxfunc: function(res) {
>>       document.getElementById('blah').innerHTML = res;
>>    }
>> }
>>  
>>
> You know you're oversimplifying matters.  What if the returned markup is
> invalid?  What if the server experienced an HTTP 500 error?  Why should
> anyone want to program their application to return 'tag soup' instead of
> formally defining interfaces via standards?  That's asking for
> maintenance nightmares.

1) what if the returned markup is invalid?

Well, the browser renders it oddly, and through the fact that your
server-side code is straightforward filling in an html template with
actual values, all you need to do is view the source (with the firefox
web debugging extension you can view generated source) or to add an
"alert(res)" in there.

2) HTTP 500?

This should be handled internally by the ajax mechanism (HTML_AJAX does
handle this)

3) tag soup?

I hardly think a <select> tag qualifies as "tag soup" but "tag soup" is
of course a non-definite term anyways :).

Greg

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

1) what if the returned markup is invalid?

Well, the browser renders it oddly, and through the fact that your
server-side code is straightforward filling in an html template with
actual values, all you need to do is view the source (with the firefox
web debugging extension you can view generated source) or to add an
"alert(res)" in there.
Now, how do you automate that in a testing suite? Going beyond, how do you set up unit (or other types of) tests in a cheap, straightforward way? How do you assure quality of an application built like this?

2) HTTP 500?

This should be handled internally by the ajax mechanism (HTML_AJAX does
handle this)
That's good.

3) tag soup?

I hardly think a <select> tag qualifies as "tag soup" but "tag soup" is
of course a non-definite term anyways :).
Well, returning an JSON or XML document certainly is more structured than using a simple SELECT and several OPTION HTML tags. The point here is, precisely, decoupling implementation from interface.

Sure, your solution mostly works. But what I still wonder is, is it the best solution in terms of software quality?

Greg

--- End Message ---
--- Begin Message ---
hi, i used
http://www.ajaxfreaks.com/tutorials/6/3.php?topic_id=73&hl=104#104
it works just fine, i implemented to one of my web, so far so good. Hope
it's what u are looking for

Daevid Vincent wrote:
> I need to dynamically update a select box 
> with results from a SQL database using AJAX, 
> but I can't find a single example of how to do this.
> 
> Basically I have a text input field, and a select box. 
> As someone types in the input field, 
> I want the select box to fill in the results of matches.
> 
> I can fill in a <DIV> (as per the ten million examples out there)
> and that's all fine and dandy, but way too simplistic for what I need.
> 

--- End Message ---
--- Begin Message ---
Hi,
What is the best method to update a single line in a text file?
I have a file made of identifiers, that is pointed on by a htaccess file and 
used by a server to give access to a web site.
The file content is like this :
login1:pass1\r\n
login2:pass2\r\n
....
loginn:passn\r\n
loginn1:passn1\r\n
....
lastlogin:lastpass\r\n

This file can be modified in three ways
update a single line when a user updates its profile
delete a single line when the webmaster makes a user inactive
append a line when the webmaster makes a user active

the difficulties are :
there is not the same number of users and lines in this file,
we dont know at which line are the identifiers of a user,
when updating identifiers, we must keep new line characters at the end.

So what are the best functions to use to read and rewrite the file?

file() which puts all the content in an array, including the new line 
characters? (if so, we need to use array_search() or array_keys() to find 
where are the identifiers

fread() or file_get_contents() which puts all the content in a string? I 
tried this way and use eregi_replace() to find where are the identifiers, 
replace them by new value. But sometimes, I get errors where the new line 
characters are suppressed between two users identifiers or where an 
identifier is repeated like this :

loginx:passx\r\n
loginx:passx\r\n
loginn:passnloginn1:passn1\r\n
loginn2:passn2\r\n

Here is my code :
<? php
//reading the actual file content
$length=filesize($filename);
$fp=fopen($filename,"r");
$str=fread($fp,$length);
fclose($fp);

//in case of updating
$str=eregi_replace($oldlogin.":".$oldpass,$newlogin.":".$newpass,$str);
//in case of deleting
$str=eregi_replace($oldlogin.":".$oldpass,"",$str);

//rewriting the file
$handle = fopen($filename,"w+")
fwrite($handle,$str,strlen($str))

//in case of inserting a new user

$str=$newlogin.":".$newpass."\r\n";
$handle = fopen($filename,"a")
fwrite($handle,$str,strlen($str))
?>
I know in "deleting" case, the eregi_replace pattern is so that the new line 
characters will not be removed, but this is not a problem

I'll try to use file() function and array_keys() and let you know.
Thanks

--- End Message ---
--- Begin Message ---
On Sat, Mar 18, 2006 at 11:01:07AM +0100, smr78 wrote:
> Hi,
> What is the best method to update a single line in a text file?
> I have a file made of identifiers, that is pointed on by a htaccess file and 
> used by a server to give access to a web site.
> The file content is like this :
> login1:pass1\r\n
> login2:pass2\r\n
> ....
> loginn:passn\r\n
> loginn1:passn1\r\n
> ....
> lastlogin:lastpass\r\n

Why dont you use a database for this?  You will run into race
conditions at some point.

If you need a file based database take a look at sqlite.

Curt.
-- 
cat .signature: No such file or directory

--- End Message ---
--- Begin Message ---
Hello, everyone.

I'm (by nature) a Python coder, and (by fiat) a PHP one. Yes, I code in Python for fun, and PHP for work. Despite that, I'm extremely thankful for the efforts poured into PHP. It's a great language.

Except for one thing.  It lacks multiple inheritance.

But PHP 5 has the runkit extension. While it does not allow users to get the full power of multiple inheritance, it does allow a limited (yet quite effective) form of it.

I've created a technique that exploits the power of PHP and runkit to simulate multiple inheritance, and I'd like to share it with you:

http://rudd-o.com/archives/2006/03/18/revisiting-multiple-inheritance-in-php/

If you find any inaccuracies or have anything to comment, feel free to use the page's commenting mechanisms.

Have a great day!

   Rudd-O

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

I've tried to ask the following question on internals@lists.php.net but
has been pointed out that that mailing list isn't appropriate. So I'm
resinding it here.

I am a newbie to PHP. I've installed a php5-cgi-5.1.2_1 from FreeBSD
ports collection and access it by FastCGI protocol from nginx HTTP
server. I've tried an example of "Hello World" PHP script from the
following tutorial page:

http://www.php.net/manual/en/tutorial.firstpage.php

This is how the resulting HTML code is expected to look like, according
to the tutorial:

<html>
 <head>
 <title>PHP Test</title>
 </head>
 <body>
 <p>Hello World</p>
 </body>
</html>

And following is the HTML code I've got:

<html>
 <head>
 <title>PHP Test</title>
 </head>
 <body>
 <p>Hello World</p></body>
</html>

Why there is no newline afer " <p>Hello World</p>" ?
Is it a PHP bug or the tutorial should be updated?

P.S. I'm not subscribed to this list, please Cc your reply.

--- End Message ---
--- Begin Message ---
> Why there is no newline afer " <p>Hello World</p>" ?
> Is it a PHP bug or the tutorial should be updated?

The tutorial is fine.

The sample code mentioned:

<html>
 <head>
  <title>PHP Test</title>
 </head>
 <body>
 <?php echo '<p>Hello World</p>'; ?>
</body>
</html> 

Contains a line break after the <p>Hello World</p> is echoed .
 
Check your code, if you don't have that line break, PHP is not going to create 
it for you.

Brady

--- End Message ---
--- Begin Message ---
Brady Mitchell wrote:
Why there is no newline afer " <p>Hello World</p>" ?
Is it a PHP bug or the tutorial should be updated?

The tutorial is fine.

The sample code mentioned:

<html>
 <head>
  <title>PHP Test</title>
 </head>
 <body>
 <?php echo '<p>Hello World</p>'; ?>
</body>
</html>
Contains a line break after the <p>Hello World</p> is echoed .
Check your code, if you don't have that line break, PHP is not going to create it for you.

That's actually not true. A line break after a closing ?> is ignored. This is to make it possible to have something like this:

<?php /* do something */ ?>
<html>
...
</html>

And have that opening <html> tag be on the first line of the file.

Another reason is for include files. If you include a file that ends with ?><newline> then you normally don't want that newline. Having a newline output for each file you include doesn't make much sense.

So yes, technically the tutorial is wrong.

-Rasmus

--- End Message ---
--- Begin Message ---
Is there a way to circumvent this?
My template engine compiles templates to PHP files and this "feature"
makes the output html code look awful sometimes.

--- End Message ---
--- Begin Message ---
Adrian wrote:
Is there a way to circumvent this?
My template engine compiles templates to PHP files and this "feature"
makes the output html code look awful sometimes.

Nope. Put in an extra newline after ?> if you need them, or put a \n at the end of the last echo inside the PHP block.

-Rasmus

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


Rostislav Krasny wrote:
Hi,

I've tried to ask the following question on internals@lists.php.net but
has been pointed out that that mailing list isn't appropriate. So I'm
resinding it here.

I am a newbie to PHP. I've installed a php5-cgi-5.1.2_1 from FreeBSD
ports collection and access it by FastCGI protocol from nginx HTTP
server. I've tried an example of "Hello World" PHP script from the
following tutorial page:

http://www.php.net/manual/en/tutorial.firstpage.php

This is how the resulting HTML code is expected to look like, according
to the tutorial:

<html>
 <head>
 <title>PHP Test</title>
 </head>
 <body>
 <p>Hello World</p>
 </body>
</html>

And following is the HTML code I've got:

<html>
 <head>
 <title>PHP Test</title>
 </head>
 <body>
 <p>Hello World</p></body>
</html>

Why there is no newline afer " <p>Hello World</p>" ?
Is it a PHP bug or the tutorial should be updated?

P.S. I'm not subscribed to this list, please Cc your reply.





I get the same results, I doubt it's a bug, the person who wrote the article maybe never tried what it would output since they probably know how the echo construct works, if you want a line break you should change it to:-


<?php echo "<p>Hello World</p>\n"; ?>

or:-

<?php echo "<p>Hello World</p>" . PHP_EOL; ?>

or:-

<?php echo "<p>Hello World</p>
"; ?>




James

--- End Message ---
--- Begin Message ---
Rostislav Krasny wrote:
Why there is no newline afer "<p>Hello World</p>"?
Is it a PHP bug or the tutorial should be updated?

I discuss this here: http://shiflett.org/archive/151

It's a feature of PHP that has some advantages and disadvantages, and it's not likely to change (consistency has merit, regardless of what you think of the behavior).

Hope that helps.

Chris

--- End Message ---

Reply via email to