Link to home
Start Free TrialLog in
Avatar of corterp
corterp

asked on

List reading correctly and then starts reading from bottom to top

To anyone that can help.

I have the following code to read selected items in a list/menu and add to a database in the order i select from top to bottom or selecting all from top to bottom.

<script type="text/javascript">
      function selectAll() {
            var Form=document.getElementById('frmSelectDemo');
            var Sel=document.getElementById('Sel');
            if(Sel.options.length) {
                  for (var optionIndex=0; optionIndex < Sel.options.length; optionIndex++) {
                        Sel.options[optionIndex].selected=true;
                        }
                  }
            Form.submit();
      }    
</script>


<form id="frmSelectDemo" method="post" action="PLB.php">
<div id="selection">
        <?php
            $search = "/" . $_SESSION['SelA'] . "/";
            $search=str_replace("+", " ", $search);
            $sql = "SELECT * FROM Movies WHERE Filename LIKE '%$search%' ORDER BY Title ASC LIMIT 0 , 50000";
            $Recordset1 = mysql_query($sql, $connAdmin) or die(mysql_error());
            $row_Recordset1 = mysql_fetch_assoc($Recordset1);
            $totalRows_Recordset1 = mysql_num_rows($Recordset1);
        ?>
        <?php
            echo ("<select multiple size=22 name='Sel[]' id='Sel' class='full Sel' style='width:375px;')'>");
            $sitename = $row_Recordset1["Title"];
            echo ("<option value='$sitename'>$sitename</option>");
            while ($row = mysql_fetch_array($Recordset1)) {
                $sitename = $row["Title"];
                echo ("<option value='$sitename'>$sitename</option>");
            }
            echo ("</select>")
        ?>
    <?php }; ?>
</div>
<div id="AddA">
      <input type="button" value="Select All &amp; Save To Database" onClick="selectAll();" />
      <input type="submit" value="Save Selection To Database" />
</div>
</form>


<?php
      if (isset($_POST['Sel']) && is_array($_POST['Sel']))
      {
            $Username = $_SESSION['Username'];
            $L1 = $_SESSION['SelM1'];
            foreach ($_POST['Sel'] as $selected_value)
            {
            $sql = "SELECT * FROM PL WHERE Username='$Username' AND PLTitle='$L1' AND PLItem='$selected_value'";
            $Recordset1 = mysql_query($sql, $connAdmin) or die(mysql_error());
            $row_Recordset1 = mysql_fetch_assoc($Recordset1);
            $totalRows_Recordset1 = mysql_num_rows($Recordset1);
                  if ($totalRows_Recordset1 < 1) {
                        mysql_query("INSERT INTO PL (Username, PLTitle, PLItem)
                        VALUES ('$Username', '$L1', '$selected_value')");
                  }
            }
            mysql_query("COMMIT");
<?php echo "<script type='text/javascript'>window.location = './PLB.php'</script>";
}?>

The problem I am having is it will work fine but then start adding them from the bottom up with either selected just a few or all.

I need it to always read from index 0 and go from top to bottom.

Any help would be greatly appreciated
ASKER CERTIFIED SOLUTION
Avatar of rationalboss
rationalboss

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of corterp
corterp

ASKER

I am sorry but where would that go?
I'm sorry, instead of:
 foreach ($_POST['Sel'] as $selected_value)

(tip: next time use the [ code ]  tag so there are line numbers)

<script type="text/javascript">
      function selectAll() {
            var Form=document.getElementById('frmSelectDemo');
            var Sel=document.getElementById('Sel');
            if(Sel.options.length) {
                  for (var optionIndex=0; optionIndex < Sel.options.length; optionIndex++) {
                        Sel.options[optionIndex].selected=true;
                        }
                  }
            Form.submit();
      }    
</script>


<form id="frmSelectDemo" method="post" action="PLB.php">
<div id="selection">
        <?php
            $search = "/" . $_SESSION['SelA'] . "/";
            $search=str_replace("+", " ", $search);
            $sql = "SELECT * FROM Movies WHERE Filename LIKE '%$search%' ORDER BY Title ASC LIMIT 0 , 50000";
            $Recordset1 = mysql_query($sql, $connAdmin) or die(mysql_error());
            $row_Recordset1 = mysql_fetch_assoc($Recordset1);
            $totalRows_Recordset1 = mysql_num_rows($Recordset1);
        ?>
        <?php
            echo ("<select multiple size=22 name='Sel[]' id='Sel' class='full Sel' style='width:375px;')'>");
            $sitename = $row_Recordset1["Title"];
            echo ("<option value='$sitename'>$sitename</option>");
            while ($row = mysql_fetch_array($Recordset1)) {
                $sitename = $row["Title"];
                echo ("<option value='$sitename'>$sitename</option>");
            }
            echo ("</select>")
        ?>
    <?php }; ?>
</div>
<div id="AddA">
      <input type="button" value="Select All &amp; Save To Database" onClick="selectAll();" />
      <input type="submit" value="Save Selection To Database" />
</div>
</form>


<?php
      if (isset($_POST['Sel']) && is_array($_POST['Sel']))
      {
            $Username = $_SESSION['Username'];
            $L1 = $_SESSION['SelM1'];
            for ($x=count($_POST['Sel'])-1;$x>=0;$x--)
            {
            $selected_value = $_POST['Sel'][$x];
            $sql = "SELECT * FROM PL WHERE Username='$Username' AND PLTitle='$L1' AND PLItem='$selected_value'";
            $Recordset1 = mysql_query($sql, $connAdmin) or die(mysql_error());
            $row_Recordset1 = mysql_fetch_assoc($Recordset1);
            $totalRows_Recordset1 = mysql_num_rows($Recordset1);
                  if ($totalRows_Recordset1 < 1) {
                        mysql_query("INSERT INTO PL (Username, PLTitle, PLItem)
                        VALUES ('$Username', '$L1', '$selected_value')");
                  }
            }
            mysql_query("COMMIT");
<?php echo "<script type='text/javascript'>window.location = './PLB.php'</script>";
}?>

Open in new window

Avatar of corterp

ASKER

I tried as you suggested and it works with some quirks, the Select All  and Save to Database button no longer works, only if I manually choose and add.  Also when I add some items and then select more, they are added to the top of what has been selected, not the bottom.

Thank you for all your help.
I did not touch anything aside from that line.
Could it be that it doesn't work in the first time?
Avatar of corterp

ASKER

Your right, I messed up when I was trying to figure out where the code went.  It is back to working, now just trying to figure out why it appears on top of the others in the database and not below when i add new items.

Thank you for all your help.