Solved

List reading correctly and then starts reading from bottom to top

Posted on 2011-03-04
6
200 Views
Last Modified: 2012-06-27
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
0
Comment
Question by:corterp
  • 3
  • 3
6 Comments
 
LVL 8

Accepted Solution

by:
rationalboss earned 500 total points
ID: 35035210
Use this:

for ($x=count($_POST['Sel'])-1;$x>=0;$x--) {
    $selected_value = $_POST['Sel'][$x];
    // other stuff
}
0
 

Author Comment

by:corterp
ID: 35035243
I am sorry but where would that go?
0
 
LVL 8

Expert Comment

by:rationalboss
ID: 35035317
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

0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

Author Comment

by:corterp
ID: 35035654
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.
0
 
LVL 8

Expert Comment

by:rationalboss
ID: 35035673
I did not touch anything aside from that line.
Could it be that it doesn't work in the first time?
0
 

Author Comment

by:corterp
ID: 35036021
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.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

806 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question