0

I am fetching some 1000 records precisely from MySQL using PHP in a drop-down box, data loads on page reload and it is showing in console too, but whenever I click data shows in drop-down really slowly/delay.

PS: Cannot opt for caching Redis or other.

SQL Query is like- 'select user from table' //users are 1000

PHP Script:

<select name="wname" id="Publication" class="form-control"  required>
              <option value=" ">Select Publication</option>

               <?php
               $select = "";
               while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { 
                 ?>

               <option value="<? echo $row['Id']; ?>"<? if($row['Id']==$select){ echo "selected"; } ?>>
                                     <? 
                                       
                                       echo $row['users']; 

                                       ?>
             
      <?php
    }
     ?>
        </option>
            </select>
kate moss
  • 416
  • 1
  • 5
  • 18
  • 1
    A 1000 options in a select dropdown is much. Your web browser might be struggling, not to speak about your users who would have to select 1 option out of a 1000. – KIKO Software Mar 24 '22 at 07:51
  • there are some data which needs to select from 1000 records, is there any option to perform a drop-down smoothly without any delay or lag! @KIKOSoftware – kate moss Mar 24 '22 at 07:56
  • 1
    Please read [Why shouldn't I use mysql_* functions in PHP?](https://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php?noredirect=1&lq=1) – brombeer Mar 24 '22 at 08:15
  • Can't help you speed up a `SELECT` that we can't see. – Rick James Mar 24 '22 at 21:35

1 Answers1

1

Can you use a different approach?

You must be render the dropdown as a search box. When your user starts typing, you make an ajax call to the DB, for example at start of 3rd chars.

E.g.

When your user type "sin" you populate with "sineverba" and "sinology", when user continues with "sine" you print only "sineverba" and so on.

sineverba
  • 5,059
  • 7
  • 39
  • 84
  • I understand but this approaches call Ajax on every key-up event right? – kate moss Mar 24 '22 at 07:58
  • 1
    This is not a bad idea. You could store the 1000 items in a javascript array and use that instead of an AJAX call. That way the browser doesn't need to render a dropdown with a 1000 items. – KIKO Software Mar 24 '22 at 08:08
  • Using an input with an attached datalist would be an easy option, no need to AJAX, lighter on DOM/browser since there's nothing to render. – Markus AO Mar 24 '22 at 09:01
  • @MarkusAO can I see or get any example please ? – kate moss Mar 24 '22 at 09:06
  • 1
    @archana it's a standard HTML feature, examples are everywhere if you search. https://developer.mozilla.org/en-US/docs/Web/HTML/Element/datalist – Markus AO Mar 24 '22 at 16:08