Jump to content

DropDown List selection help with getting it alphabeticl order


junk4u

Recommended Posts

Okay,

 

Sorry about the last post didn't see how to close the or delete the post. My bad. What can I add to get my drop down to be in alphabetical order?

 

I've tried adding a clause to the sql statement, but I guess Im adding it in the wrong place as I keep getting errors or nothing changes.

 

<?php

if(!isset($_SESSION)) session_start();

$server = 'localhost';
$user = 'root';
$pass = '';
$dbase = 'competitors';

$table = 'zip_code';
$ar_cols = array('state_prefix', 'city', 'zip_code', 'locationtext');

$preid = 'slo_'; 
$col = $ar_cols[0];
$re_html = ''; 

if(isset($_POST['col']) && isset($_POST['wval'])) {
$col = trim(strip_tags($_POST['col']));
  $wval = "'".trim(strip_tags($_POST['wval']))."'";
}

$key = array_search($col, $ar_cols);  
$wcol = $key===0 ? $col : $ar_cols[$key-1];
$_SESSION['ar_cols'][$wcol] = isset($wval) ? $wval : $wcol;
  
$last_key = count($ar_cols)-1;
$next_col = $key<$last_key ? $ar_cols[$key+1] : '';

$conn = new mysqli($server, $user, $pass, $dbase);

if (mysqli_connect_errno()) { exit('Connect failed: '. mysqli_connect_error()); }

for($i=1; $i<=$key; $i++) {
  $ar_where[] = '`'.$ar_cols[$i-1].'`='.$_SESSION['ar_cols'][$ar_cols[$i-1]];
}

$where = isset($ar_where) ? ' WHERE '. implode($ar_where, ' AND ') : '';
$sql = "SELECT DISTINCT `$col` FROM `$table`".$where;

$result = $conn->query($sql);
if ($result->num_rows > 0) {
  $onchg = $next_col!==null ? " onchange=\"ajaxReq('$next_col', this.value);\"" : '';
if($col!=$ar_cols[$last_key]) $re_html = $col. ': <select name="'. $col. '"'. $onchg. '><option>- - -</option>';

  while($row = $result->fetch_assoc()) {
    if($col==$ar_cols[$last_key]) $re_html .= '<br/>'. $row[$col];
    else $re_html .= '<option value="'. $row[$col]. '">'. $row[$col]. '</option>';
  }

  if($col!=$ar_cols[$last_key]) $re_html .= '</select> '; 
}
else { $re_html = '0 results'; }

$conn->close();

if($col==$ar_cols[0]) {
  for($i=1; $i<count($ar_cols); $i++) {
    if($ar_cols[$i]===null) continue;
    if($i==$last_key) $re_html .= '<div id="'. $preid.$ar_cols[$i]. '"> </div>';
    else $re_html .= '<span id="'. $preid.$ar_cols[$i]. '"> </span>';
  }


  $re_html .= '<script type="text/javascript">var ar_cols = '.json_encode($ar_cols).'; var preid = "'. $preid. '";</script>';
}
else echo $re_html;
?>

Link to comment
Share on other sites

Running Wampserver

MySql 5.5.16

PHP 5.3.8

 

Okay,

 

Sorry about the last post didn't see how to close the or delete the post. My bad. What can I add to get my drop down to be in alphabetical order?

 

I've tried adding a clause to the sql statement, but I guess Im adding it in the wrong place as I keep getting errors or nothing changes.

 

 


if(!isset($_SESSION)) session_start();

$server = 'localhost';
$user = 'root';
$pass = '';
$dbase = 'competitors';

$table = 'zip_code';
$ar_cols = array('state_prefix', 'city', 'zip_code', 'locationtext');

$preid = 'slo_'; 
$col = $ar_cols[0];
$re_html = ''; 

if(isset($_POST['col']) && isset($_POST['wval'])) {
$col = trim(strip_tags($_POST['col']));
  $wval = "'".trim(strip_tags($_POST['wval']))."'";
}

$key = array_search($col, $ar_cols);  
$wcol = $key===0 ? $col : $ar_cols[$key-1];
$_SESSION['ar_cols'][$wcol] = isset($wval) ? $wval : $wcol;
  
$last_key = count($ar_cols)-1;
$next_col = $key<$last_key ? $ar_cols[$key+1] : '';

$conn = new mysqli($server, $user, $pass, $dbase);

if (mysqli_connect_errno()) { exit('Connect failed: '. mysqli_connect_error()); }

for($i=1; $i<=$key; $i++) {
  $ar_where[] = '`'.$ar_cols[$i-1].'`='.$_SESSION['ar_cols'][$ar_cols[$i-1]];
}

$where = isset($ar_where) ? ' WHERE '. implode($ar_where, ' AND ') : '';
$sql = "SELECT DISTINCT `$col` FROM `$table`".$where;

$result = $conn->query($sql);
if ($result->num_rows > 0) {
  $onchg = $next_col!==null ? " onchange=\"ajaxReq('$next_col', this.value);\"" : '';
if($col!=$ar_cols[$last_key]) $re_html = $col. ': <select name="'. $col. '"'. $onchg. '><option>- - -</option>';

  while($row = $result->fetch_assoc()) {
    if($col==$ar_cols[$last_key]) $re_html .= '<br/>'. $row[$col];
    else $re_html .= '<option value="'. $row[$col]. '">'. $row[$col]. '</option>';
  }

  if($col!=$ar_cols[$last_key]) $re_html .= '</select> '; 
}
else { $re_html = '0 results'; }

$conn->close();

if($col==$ar_cols[0]) {
  for($i=1; $i<count($ar_cols); $i++) {
    if($ar_cols[$i]===null) continue;
    if($i==$last_key) $re_html .= '<div id="'. $preid.$ar_cols[$i]. '"> </div>';
    else $re_html .= '<span id="'. $preid.$ar_cols[$i]. '"> </span>';
  }


  $re_html .= '<script type="text/javascript">var ar_cols = '.json_encode($ar_cols).'; var preid = "'. $preid. '";</script>';
}
else echo $re_html;
?>
[/quote]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.