Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

List reading correctly and then starts reading from bottom to top

Posted on 2011-03-04
6
201 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Place text over image using CSS 6 48
CSS: How do I override in-line styling 11 25
How to create a table with buttons 3 21
IF statment In Powershell 12 15
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…

856 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