Select Boxes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
johndoe132
Forum Newbie
Posts: 13
Joined: Thu Sep 30, 2004 5:09 am

Select Boxes

Post 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.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

you could try outputbuffering..
johndoe132
Forum Newbie
Posts: 13
Joined: Thu Sep 30, 2004 5:09 am

Post by johndoe132 »

Sorry, I'm unfamiliar with output buffering.
How would I use it here?
Thanks
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

have a look at the examples in the manual
http://www.php.net/out-control
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
thegreatone2176
Forum Contributor
Posts: 102
Joined: Sun Jul 11, 2004 1:27 pm

Post 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.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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
johndoe132
Forum Newbie
Posts: 13
Joined: Thu Sep 30, 2004 5:09 am

Post by johndoe132 »

Feyd,
You may be on to something here! Could you post a little example please?
Cheers
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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;
?>
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply