Code Comments
Programming Forum and web based access to our favorite programming groups.I'm working on a complete rewrite of a now ancient word-association
program I use for writing ideas, song lyrics, etc.
I've written a simple database class I'm only using about two commands
out of at the moment, but I must be overlooking something already,
because the browser acts as if I've written something deeply or
infinitely recursive.
Database info - there are 3984 rows that include a word field that's
varchar 255, and an additional int 11 field for an ID that I never
used and defaults to 0. The word field is Indexed. The whole thing is
125kb.
Next we have index.php, manually requesting two words from the
database
include_once('idea.db.class.php');
<? $words = $connection->getWord('2'); ?>
<DIV ID="display" NAME="display">
<? for($i=0;$i<$words;$i++) { echo '<DIV ID="'.$i.'" NAME="'.$i.'"
CLASS="element">'.$words[$i].'</DIV>'; } ?>
</DIV>
in idea.db.class.php, we have:
<?php
class dbConnect {
//some database variables here...
function dbConnect() {
$this->connection = @mysql_pconnect($this->server,$this->user,$this-
>password)or die($this->error2);
$this->dbSelect = mysql_select_db($this->database)or die ($this-
>error1);
}
function getWord($limit) {
$query = "SELECT word FROM $this->wordTable ORDER BY rand() LIMIT
$limit";
$result = mysql_query($query,$this->connection);
while ($data=mysql_fetch_array($result)) {
$word[] = $data['word'];
}
return $word;
}
// snip - other functions, etc
}
?>
This results in Firefox and IE both coughing and sputtering a bit
while returning the words, but usually we can get as far as seeing
them returned to the window. The browser status bar then suggests
however that it's still requesting data from the server, and maybe 5
seconds later it locks up permanently.
I've tried removing RAND(), I've obviously backed off the number of
requests to just two words, stripped everything else (javascript etc)
out of my index file...I'm stumped at what else it could be...the
pconnect? the default function in the class? Gremlins?
-Adam
Post Follow-up to this messagehas to be a recursion problem. Memory and CPU usage on the browser go through the roof.
Post Follow-up to this messagehedgomatic wrote:
> <? $words = $connection->getWord('2'); ?>
> <? for($i=0;$i<$words;$i++) { } ?>
> function getWord($limit) {
> while ($data=mysql_fetch_array($result)) {
> $word[] = $data['word'];
> }
> return $word;
> }
$word will contain an array so try
foreach($words as $word){}
or
for($i=0;$-<count($words);$i++;){}
Arjen
Post Follow-up to this message>> for($i=0;$i<$words;$i++) { }
Try to see what happens if $words evaluates to 0 or less...
Best regards.
Post Follow-up to this messageit's 7am and I doubt I'll get many responses, so why don't we make
this a lovely lil usenet example of how to solve your own %#&king
problem ;]
I've determined it's definitely the for() loop, which explains why it
feels like a recursion problem. Commenting that out the page loads
fine. But why? it's just a normal for loop....
first let's make sure there's nothing wrong with our array...
$words = Array('test','test2');
for($i=0;$i<$words;$i++) { echo '<DIV ID="'.$i.'" NAME="'.$i.'"
CLASS="element">'.$words[$i].'</DIV>'; }
oof, and remembering i need count() in there helps a bit, ugh, it's
early...
$words = Array('test','test2');
for($i=0;$i<count($words);$i++) { echo '<DIV ID="'.$i.'" NAME="'.$i.'"
CLASS="element">'.$words[$i].'</DIV>'; }
but somehow I'm still having a problem. wha?
well, trying this works. loads instantly--
for($i=0;$i<count($words);$i++) {
//echo '<DIV ID="'.$i.'" NAME="'.$i.'" CLASS="element">'.
$words[$i].'</DIV>';
echo 'test';
}
as does using a here doc instead:
for($i=0;$i<count($words);$i++) {
echo <<<END
<DIV ID="$i" NAME="$i" CLASS="element">$words[$i]</DIV>
END;
}
What? now I'm really
. Why would a here doc work but not
single quotes? let's undo back to the single quotes and double-check
to verify that's still really a problem...
hmm. loaded fine that time, maybe the page hadn't finished uploading
when I hit the URL earlier. fair enough. Let's try slowly putting
everything back in...
<? $words = $connection->getWord('2');
for($i=0;$i<count($words);$i++) {
echo '<DIV ID="'.$i.'" NAME="'.$i.'" CLASS="element">'.
$words[$i].'</DIV>';
}
ding!
Thanks usenet, I always feel better talking to you.
Post Follow-up to this messagehedgomatic schreef: > it's 7am and I doubt I'll get many responses, so why don't we make > this a lovely lil usenet example of how to solve your own %#&king > problem ;] > Hedgomatic, Why do you doubt you'll get many responses? It is not 7 am in the rest of the world. You must be from USA.... Regards, Erwin Moller
Post Follow-up to this messagehedgomatic wrote:
> I'm working on a complete rewrite of a now ancient word-association
> program I use for writing ideas, song lyrics, etc.
>
> I've written a simple database class I'm only using about two commands
> out of at the moment, but I must be overlooking something already,
> because the browser acts as if I've written something deeply or
> infinitely recursive.
>
> Database info - there are 3984 rows that include a word field that's
> varchar 255, and an additional int 11 field for an ID that I never
> used and defaults to 0. The word field is Indexed. The whole thing is
> 125kb.
>
>
> Next we have index.php, manually requesting two words from the
> database
>
> include_once('idea.db.class.php');
> <? $words = $connection->getWord('2'); ?>
>
> <DIV ID="display" NAME="display">
> <? for($i=0;$i<$words;$i++) { echo '<DIV ID="'.$i.'" NAME="'.$i.'"
> CLASS="element">'.$words[$i].'</DIV>'; } ?>
> </DIV>
>
> in idea.db.class.php, we have:
> <?php
> class dbConnect {
>
> //some database variables here...
>
> function dbConnect() {
> $this->connection = @mysql_pconnect($this->server,$this->user,$this-
> $this->dbSelect = mysql_select_db($this->database)or die ($this-
> }
>
> function getWord($limit) {
> $query = "SELECT word FROM $this->wordTable ORDER BY rand() LIMIT
> $limit";
> $result = mysql_query($query,$this->connection);
> while ($data=mysql_fetch_array($result)) {
> $word[] = $data['word'];
> }
> return $word;
> }
>
> // snip - other functions, etc
> }
> ?>
>
>
> This results in Firefox and IE both coughing and sputtering a bit
> while returning the words, but usually we can get as far as seeing
> them returned to the window. The browser status bar then suggests
> however that it's still requesting data from the server, and maybe 5
> seconds later it locks up permanently.
>
>
> I've tried removing RAND(), I've obviously backed off the number of
> requests to just two words, stripped everything else (javascript etc)
> out of my index file...I'm stumped at what else it could be...the
> pconnect? the default function in the class? Gremlins?
>
> -Adam
>
As others mentioned -
for($i=0;$i<$words;$i++)
$words is an array, not an integer. You can use
for($i=0;$i<count($words);$i++)
or, better yet, foreach().
If you had all errors being displayed, you should have gotten a notice
in your browser. In your development system's php.ini file you should
always have:
error_reporting=E_ALL // or E_ALL | E_STRICT)
display_errors=on
Also, don't use persistent connections. Unless you have a site running
several hundred connections per second, it will hurt you more than it
will help. Just use mysql_connect().
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Post Follow-up to this messageIt's not that I'm oblivious to the existence of other countries as most Americans, some groups are just more frequented by GMT-5 and -8 than others. I didn't mean to imply you don't exist :] On Apr 3, 7:12=A0am, Erwin Moller < Since_humans_read_this_I_am_spammed_too_ m...@spamyourself.com> wrote: > hedgomatic schreef: > > > Hedgomatic, > > Why do you doubt you'll get many responses? > It is not 7 am in the rest of the world. > You must be from USA.... > > Regards, > Erwin Moller
Post Follow-up to this message> $words is an array, not an integer. =A0You can use > for($i=3D0;$i<count($words);$i++) > or, better yet, foreach(). aye, lame mistake :] I may need to enumerate, so I went with for(). that and I've always heard foreach() was slightly slower than for() when iterating through arrays, because logically foreach doesn't know its end until it gets there. foreach is supposed to be more readable, but I've never really had a problem reading for()s. *shrug* > error_reporting=3DE_ALL =A0// or E_ALL | E_STRICT) > display_errors=3Don my paid host understandably doesn't allow ini_set(). I should really test stuff like this locally. > Also, don't use persistent connections. =A0Unless you have a site running > several hundred connections per second, it will hurt you more than it > will help. =A0Just use mysql_connect(). eventually it probably will be a whole ton of connections. I'm basically looking at a web version of something like wordpad, with the ability to seed random words into the text. Difference being you format whole words, not individual letters, and you can drag and drop them around. it depends on how many users, but I s'pose it's easy enough to swap the letter p out till we need it.
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.