Web Analytics Made Easy -
StatCounter Reversed listbox data - CodingForum

Announcement

Collapse
No announcement yet.

Reversed listbox data

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Reversed listbox data

    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:
    Code:
    <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:
    Code:
    <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>

  • #2
    There's no telling what order the rows are getting selected in, because you don't have an ORDER BY clause on your query.

    Comment


    • #3
      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.
      Last edited by h8ids; Oct 12, 2006, 12:47 PM.

      Comment


      • #4
        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.

        Comment


        • #5
          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.

          Comment


          • #6
            why are you generating javascript rather than HTML?
            My thoughts on some things: http://codemeetsmusic.com
            And my scrapbook of cool things: http://gjones.tumblr.com

            Comment


            • #7
              JS is being used to allow a user to view hidden fields and to lock edittable fields.

              Comment


              • #8
                Adjusted a line of code to read:
                Code:
                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.

                Comment

                Working...
                X