Solved

HTML Select Box - How to improve load times when populating....

Posted on 2008-10-29
6
915 Views
Last Modified: 2012-08-14
Hi, Currently I have I have some PHP which reads a list of players from a player table in a mySql database. I create an array of players and poulate each of 2 HTML select boxes on the HTML Page.

This is so I can compare one player against another player. This works fine as you can see  
in the URL Link.

However my problem is that the number of players will grow to a few thousand and the loading of the SELECT boxes is slow. What is the best technique to perform a comparision like this. Some code would be great if you could provide this.

See below for the PHP Code and the HTML Code. The HTML Link will give understanding as to how it works.


---------------------------------------------------------------------------

Code Snippet A - Html for showing the two Select boxes. 

---------------------------------------------------------------------------

<td valign="top">Player 1:<br>

  <select name="player1" size="5" style="width: 150px;">

      <!--{html_options values=$player1_id selected=$player1_sel output=$player1_name}-->

  </select>

</td>

 

<td valign="top">Player 2:<br>

   <select name ="player2"  size="5" style="width: 150px;">

      <!--{html_options values=$player2_id selected=$player2_sel output=$player2_name}-->

   </select>

</td>

 

---------------------------------------------------------------------------

Code Snippet B - PHP Code to load values for the Select boxes

---------------------------------------------------------------------------

 

$sql_names = "SELECT playerName FROM qwscores_players ORDER by playerName ASC";

$sel_names = $db->get_results($sql_names);

 

$player1_name = array();

$player2_name = array();

 

foreach($sel_names as $row) {

    	array_push($player1_name, stripslashes($row["playerName"]));

     	array_push($player2_name, stripslashes($row["playerName"]));

}

 

 $tpl->assign("player1_name", $player1_name);

 $tpl->assign("player1_id", number_array($player1_name));

 $tpl->assign("player1_sel", $player1);

 

 $tpl->assign("player2_name", $player2_name);

 $tpl->assign("player2_id", number_array($player2_name));

 $tpl->assign("player2_sel", $player2);

 

  	 $player1_sql = $player1_name[$player1];

  	 $player2_sql = $player2_name[$player2];

Open in new window

0
Comment
Question by:9703403
  • 3
  • 2
6 Comments
 
LVL 6

Expert Comment

by:fourice
ID: 22832492
Why do you want all the players in one time?
Perhaps it is better to first choose the first letter (ie. f) and then load all names with a f? (of course seperate for both selectboxes).
0
 

Author Comment

by:9703403
ID: 22832586
Yeah I was thining of different ways of doing it. One was to have a filter where the user could limit players by time. I.e Only populate the Selection Box with players active within last 24 hours but some users will want to compare players as they scroll down through them.

Also I am not sure hwo I would go about coding that as you suggested anbd whether it would work well.
0
 
LVL 15

Accepted Solution

by:
OMC2000 earned 125 total points
ID: 22832870
I guess you mean potential problem with "select" rendering when there are too many options. If so, you could build complete HTML string on the server side and assign it to parent HTML element using JavaScript funtion called on load event. It used to work much faster regular way of select definition in HTML code.


<html>

<body onload="doLoad();">

<table>

<tr>

<td colspan=4>

<select id='lstSource' name='lstSource'>

</select>

</td>

</tr>

<tr>

<td colspan=4>

<select id='lstDest' name='lstDest'>

</select>

</td>

</tr>

</table> 

<script language="JavaScript">

var firstSelect = "<select id='lstSource' name='lstSource'>"

+"<option value='1'>1</option><option value='2'>2</option><option value='3'>3</option><option value='4'>4</option><option value='5'>5</option>"

+"</select>"; 

var secondSelect = "<select id='lstDest' name='lstDest'><option value='1'>1</option><option value='2'>2</option><option value='3'>3</option><option value='4'>4</option><option value='5'>5</option></select>"; 
 

function doLoad()

{

  obj = document.getElementById("lstDest").parentNode;

  obj.innerHTML = firstSelect;

  obj = document.getElementById("lstSource").parentNode;

  obj.innerHTML = secondSelect;

}

</script>

</body>

</html>

Open in new window

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:9703403
ID: 22833474
OMC2000: But the list I have is dynamic so I would need to read from DB and the load each time.
Is that possible with the code you have above. Not sure how that works compared to mine.
Cheers
0
 
LVL 15

Expert Comment

by:OMC2000
ID: 22839593
if you reload page you can fill these lists from the database during this process, it must not be a problem for modern servers to build such lists from the database

If you want to rebuild them without page reloading you could use one of dynamic page part filling technique
1. Ajax - assign to JS vars result of Ajax function execution and then reassign it to selection parent nodes
2. run separate PHP file whith parameters in the hidden frame and include into the code of that PHP file JS block, which assign newly constructed content of selection list to selection parent nodes
3. make dynamic source for JS script block. Use named SCRIPT block <script ID="reloadSels" ...>
and assign path to PHP file, which build js content and selection lists string from the database, to SRC property. When this file gets loaded browser executes it. This way you also can assign newly constructed content of selection list to selection parent nodes
0
 

Author Comment

by:9703403
ID: 22899837
Hey. Thanks for your suggestions. I still have not resolved this but am working through your suggestions. Will let you know how i get on. Thanks.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now