PDA

View Full Version : Reversed listbox data


h8ids
10-12-2006, 04:04 PM
Pulling data into a listbox.
The data keeps showing up "reversed"; the last record is showing up as the first option. But this only occurs in Firefox, IE works fine.

There is a reference to an external JS function (onclick="activeMajorRev()"), but it only activates a button.

The code has some JS mixed in to assist with user selection.
HTML first:

<form name="f1">
<div id="MajorEdit" name="MajorEdit" style='display: none'>
&nbsp;&nbsp;<b>Edit</b> a Major.
<input type="text" name="MajorEdit" id="MajorEdit" size="4" onclick="activeMajorRev()">
<input type="text" name="MajorEdit2" size="35" onclick="activeMajorRev()">
<input type="text" name="MajorEdit3" size="3"><br>
&nbsp;&nbsp;<input disabled type="submit" value="Save revisions" name="MajorRev" id="MajorRev" title="Store Major revisions" onclick="this.form.action='DBEditMajor.php'; resolveData();">
<p></div>
</form>


php with JS:
<script type="text/javascript">
var empMajorChoice = [
<?php //error_reporting(E_ALL);
$dbMajor = mysql_connect("localhost", "root", "");
mysql_select_db("newcourse",$dbMajor);
$resultMajor = mysql_query('SELECT Majors,MajorCode,Records FROM majors');
$count = 1;
$num = mysql_num_rows($resultMajor);
if ($num != 0) {
while ($rowMajor = mysql_fetch_array($resultMajor)) {
echo "['$rowMajor[0]','$rowMajor[1]','$rowMajor[2]']";
if($count < $num){
echo ",";
}
$count++;
}
}
mysql_free_result($resultMajor);
?>
];
//Collecting of paired data for Major presentation
for(var a=0;a<empMajorChoice.length;a++){
document.f1.MajorChoice[document.f1.MajorChoice.length] = new Option(empMajorChoice[a][0],empMajorChoice[a][1],empMajorChoice[a][2])
}
// Populating Major Edit fields
function testMajor(objMajor){
if(objMajor.selectedIndex!=0){
document.f1.MajorEdit.value=empMajorChoice[objMajor.selectedIndex][1]
document.f1.MajorEdit2.value=empMajorChoice[objMajor.selectedIndex][0]
document.f1.MajorEdit3.value=empMajorChoice[objMajor.selectedIndex][2]
} else {
document.f1.MajorEdit.value="";
document.f1.MajorEdit2.value="";
document.f1.MajorEdit3.value="";
}
}
</script>

Fumigator
10-12-2006, 04:27 PM
There's no telling what order the rows are getting selected in, because you don't have an ORDER BY clause on your query.

h8ids
10-12-2006, 04:41 PM
ORBER BY Still didn't resolve the problem.
The listbox is showing the data in order of last to first record.

ORDER BY sets the options into alphabetical order.

Will rebuild DB table to make sure it isn't corrupt.

Fumigator
10-12-2006, 05:06 PM
You're talking about "last" record and "first" record as if you were reading data from a sequential file, which you are not. You are selecting data from a database. The only way to guarantee your query will pull data out in a specific order is using the ORDER BY clause. Without it, the database will choose the most efficient way-- which may or may not be the order in which the data was inserted into the table. If you need the data in a particular order, you have to take measures to ensure that order. You can't assume anything.

h8ids
10-12-2006, 05:25 PM
I'd like them to show up in the listbox (top to bottom) as they do in the DB table (starting from Record 1 to Record ++)

Surprised to see Firefox sorting Record ++ to Record 1 while IE sorts it as expected. :(

GJay
10-12-2006, 06:00 PM
why are you generating javascript rather than HTML?

h8ids
10-12-2006, 07:24 PM
JS is being used to allow a user to view hidden fields and to lock edittable fields.

h8ids
10-12-2006, 08:10 PM
Adjusted a line of code to read:

document.f1.MajorChoice[document.f1.MajorChoice.length] = new Option(empMajorChoice[a][2],empMajorChoice[a][1],empMajorChoice[a][0],empMajorChoice[a][3])

Now the listbox works correctly in both IE and Firefox.
Weird.