Solved

List reading correctly and then starts reading from bottom to top

Posted on 2011-03-04
6
198 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Do you want to insert HTML5 video into your site? This is the tutorial how to do so. What are the main advantages of HTML5 video? 1) Have good compression, good image quality, and low decode processor use. 2) It is royalty-free 3) It is easi…
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

705 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now