Solved

List reading correctly and then starts reading from bottom to top

Posted on 2011-03-04
6
202 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
[X]
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
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

733 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