Page 1 of 1

Select Boxes

Posted: Tue Feb 01, 2005 1:31 pm
by johndoe132
Hello all,
I'm running an app which controls newspaper delivery. A customer screen has four select boxes for each day of the week and each list is pulled from a mysql database. All the Monday to Friday boxes are the same.
The problem is that the lists are pretty long and loading all the boxes takes too much time. This is the code I'm using to populate each box, which is run as an include:
<?php
$result = mysql_query("SELECT * FROM papers WHERE type='daily' ORDER BY paper",$db);
if ($myrow = mysql_fetch_array($result)) {
do {
$paper = $myrow["paper"];
$option="<option>";
printf("$option$paper<br>");
} while ($myrow = mysql_fetch_array($result));
} else {
echo "Sorry, no records were found!";
}
?>
Can anyone suggest a way to make this more efficient and speed up the loading?
Cheers,
Paul.

Posted: Tue Feb 01, 2005 2:43 pm
by timvw
you could try outputbuffering..

Posted: Tue Feb 01, 2005 2:49 pm
by johndoe132
Sorry, I'm unfamiliar with output buffering.
How would I use it here?
Thanks

Posted: Tue Feb 01, 2005 2:53 pm
by timvw
have a look at the examples in the manual
http://www.php.net/out-control

Posted: Tue Feb 01, 2005 6:04 pm
by feyd
if the boxes are exactly the same, why not store off all the options into a variable, then just output it again, with a new name?

From your code, it looks like you perform the same query multiple times, this is a waste of database time and resources most of the time.

Posted: Tue Feb 01, 2005 6:34 pm
by thegreatone2176
i really dont understand what your code is doing but i think this may fix it ... its similar to what i posted about the counting values problem

Code: Select all

<?php
$link = "SELECT * FROM papers WHERE type='daily' ORDER BY paper";
$result = mysql_query($link); 
while ($myrow = mysql_fetch_row($result)) &#123; 
$paper = "<option>" . $myrow&#1111;"paper"] . '<br>'; 
printf("$paper");
&#125; 
if (!$myrow) &#123;
echo "Sorry, no records were found!"; 
&#125; 
?>
Im pretty sure that would cover what you are trying to do if not just delete my post.

Posted: Wed Feb 02, 2005 1:01 am
by timvw
thegreatone2176 wrote:

Code: Select all

while ($myrow = mysql_fetch_row($result)) &#123; &#125; 
if (!$myrow) &#123; echo "Sorry, no records were found!"; &#125; 
?>
i would not update my code to that :p

first you fetch rows untill there aren't any left.
then you test if there are still rows left (which is always false)
thus you always display a sorry message

Posted: Wed Feb 02, 2005 4:28 am
by johndoe132
Feyd,
You may be on to something here! Could you post a little example please?
Cheers

Posted: Wed Feb 02, 2005 8:46 am
by timvw
an example implementation would look like:

Code: Select all

session_start();
if (!isset($_SESSION&#1111;'list'])) // might want to have other conditions
&#123;
      $list = '<list>';
      ..
       while ($row = mysql_fetch_assoc($row))
      &#123;
           $list .= '<option>' . $row&#1111;'whatever'];
      &#125;
      $_SESSION&#1111;'list'] = $list;
&#125;
echo $list
now each script/function that needs the list, could look in $_SESSION['list'] which will speed up things (provided you're not storing all your session data in a database :p

Posted: Wed Feb 02, 2005 11:47 am
by pickle
1) Take out the middle-men like $link and $paper
2) Change printf() to echo(). While I could find no documentation, I think echo has less overhead than printf, because it doesn't check for formatting at all, and it doesn't return anything.
3) Don't use ("), use ('). PHP parses anything in (") and looks for variables. Using (') removes that parsing, and just dumps the string
4) You should have a closing "</option>" tag, and you don't need the "<br />";
5) I don't think your if statement will work. The while loop will loop through the result set until $myrow is false. Then, your if statement will evaluate and output the error. This will always happen. Using mysql_num_rows() will evaluate independantly of the current row.

Code: Select all

<?php
 $result = mysql_query('SELECT * FROM papers WHERE type=`daily` ORDER BY paper');
 while ($myrow = mysql_fetch_row($result)) 
 &#123;
   echo ('<option>'.$myrow&#1111;'paper'].'</option>');
 &#125;
 if(!mysql_num_rows($result))
 &#123;
   echo 'Sorry, no records were found!';
 &#125;
?>