0

i have a problem in showing two columns which is reff_number and Name in a combobox/select box, and now i can only display one column with it, if i have 9000 customer(in bahasa = pelanggan) , it will take an effort to find for user, and it will be easy if not only name but sho reff_number, please help me, thanks before

select name="cmbclient">
<option value="blank"></option>
<?php
  $datasql="SELECT * FROM pelanggan ORDER BY name;
  $dataQry=mysql_<select name="cmbclient">
  <option value="BLANK"> </option>
    <?php
      $dataSql = "SELECT * FROM pelanggan ORDER BY name";
      $dataQry = mysql_query($dataSql, $koneksidb) or die ("Gagal Query".mysql_error());
      while ($dataRow = mysql_fetch_array($dataQry)) {
        if ($dataRow['reff_number']== $_POST['cmbclient']) {
            $cek = " selected";
        } else { $cek=""; }
        echo "<option value='$dataRow[reff_number]' $cek>$dataRow[name]</option>";
      }
      $sqlData ="";
      ?>
</select>
3
Contributors
9
Replies
43
Views
1 Year
Discussion Span
Last Post by rayearth_1
0

Try:

<select name="cmbclient">
  <option value=""></option>
<?php
      /* select only the fields you need.  Doing SELECT * gets you all the fields on the table, even the one you don't need! */
      $dataSql = 'SELECT reff_number, name FROM pelanggan ORDER BY name';
      $dataQry = mysql_query($dataSql, $koneksidb) or die ("Gagal Query".mysql_error());
      while ($dataRow = mysql_fetch_assoc($dataQry))
      {
        if ($dataRow['reff_number'] !== $_POST['cmbclient'])
        {
            echo '<option value="',htmlentities($dataRow['reff_number'],ENT_QUOTES),'">', htmlentities($dataRow['name'],ENT_QUOTES), '</option>',PHP_EOL;
        }
        else
        { 
            echo '<option value="',htmlentities($dataRow['reff_number'],ENT_QUOTES),'" selected>', htmlentities($dataRow['name'],ENT_QUOTES), '</option>',PHP_EOL;
        }
      }
      mysql_free_result($dataQry);
?>
</select>
1

A great little tool I've used many times:

https://twitter.github.io/typeahead.js/examples/

However, never tried it on 9000+ rows though.

If you join your values "id + ' ' + name", then you can search the query on either field.

Also: https://github.com/bassjobsen/Bootstrap-3-Typeahead

BTW - 9000+ rows is a lot to query everytime you change a character. You may consider storing data (if it's not very "volatile") in localStorage (DOM). You should be able to store around 3Mb of data (at least) for each domain on any compatible browser. This will allow you to run blindlingly fast queries vs. round trip ajax affairs.

Edited by diafol

0

to hielo, thanks for your fast responsed, i have implemented it, but it stil the same , stil display ony the name, not with the reff_number, any advise for me, thx before

0

Try:

<select name="cmbclient">
  <option value=""></option>
<?php
      /* select only the fields you need.  Doing SELECT * gets you all the fields on the table, even the one you don't need! */
      $dataSql = 'SELECT reff_number, name FROM pelanggan ORDER BY name';
      $dataQry = mysql_query($dataSql, $koneksidb) or die ("Gagal Query".mysql_error());
      while ($dataRow = mysql_fetch_assoc($dataQry))
      {
        if ($dataRow['reff_number'] !== $_POST['cmbclient'])
        {
            echo '<option value="',htmlentities($dataRow['reff_number'],ENT_QUOTES),'">', htmlentities($dataRow['name'] . ' (' . $dataRow['reff_number'] . ')',ENT_QUOTES), '</option>',PHP_EOL;
        }
        else
        { 
            echo '<option value="',htmlentities($dataRow['reff_number'],ENT_QUOTES),'" selected>', htmlentities($dataRow['name'] . ' (' . $dataRow['reff_number'] . ')',ENT_QUOTES), '</option>',PHP_EOL;
        }
      }
      mysql_free_result($dataQry);
?>
</select>
0

Here's how I'd do it with Ajax:

//get_users.php
if($searchRef = filter_input(INPUT_GET,"search",FILTER_VALIDATE_INT))
{
  $filter = "WHERE `reff_number` = ?";
}else{
  $searchRef = $_GET['search'];
  $filter = "WHERE `name` = ? ORDER BY `name`";
}

$sql = "SELECT `reff_number`, `name` FROM `pelanggan` " . $filter . " LIMIT 10";
//Using PDO instead of deprecated mysql functions
$db = new PDO("mysql:host=localhost;dbname=mydb","root","");
$stmt = $db->prepare($sql);
$stmt->execute([$searchRef]);
$json = json_encode($stmt->fetchAll(PDO::FETCH_NUM));
echo $json;

Then the page itself...

<!doctype html>
<html lang="en">
<head>
</head>
<body>
<form>
  <input id="searchbox" name="searchbox" />
  <select id=myselect" name="myselect"></select>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
  var searchTerm;

  $("#searchbox").change(function() {
    //You can modify this to run on any integer or when text is e.g. more than 3 chars
    searchTerm = $(this).val();
    if(searchTerm.length > 0) runAjax();
  });

  function runAjax(){      {
    $.getJSON(
        "get_users.php", { search: searchTerm }
    ).done(function(json){
      $('#myselect').empty();
      $.each(json, function(i, value) {
        $('#myselect').append($('<option>').text(value[1]).attr('value', value[0]));
      });
    });
  }
</script>
</body>
</html>

Note no PHP within the html page. Helps to keep your parts separated. PHP used simply to retrieve data - it doesn't get involved with spitting out HTML. This is not tested and there are probably bugs - just to give an idea of what you could do. Notice the LIMIT in the SQL too - you'd probably want to stop a full read of all the records (9000?) once you got the first "x" number of results.

Edited by diafol

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.