4 Replies Latest reply on Aug 5, 2009 6:14 PM by Albert S.

    Dynamic list/menu update page

    Navstation

      Hi,

      I am using PHP, Mysql.  MySQL has different tables.  Created list/menu to show data off one of the table that works great and when I select the value and hit the updae button it updates the database correctly.  Problem I am having is that when I go to update the page again, it always defaults to the first value of the drop down list.    I need it to remember the value that was selected.  Hope this explains it well.

        • 1. Re: Dynamic list/menu update page
          Albert S. Level 3

          Hi,

           

          Sounds like you need to capture the first entry and turn it into a session so then when you go back you can call on that session.

          • 2. Re: Dynamic list/menu update page
            I-Evolve

            Not sure what you are trying to explain. I am having the same issue. Can this be described in a little more detial.

             

            What I have:

            <select name="cat_id" multiple="multiple" size="5" id="cat_id">
                      <?php
                      do {
                      ?>
                      <option value="<?php echo $row_listCategories['id']?>"><?php echo $row_listCategories['cat']?></option>
                      <?php
            } while ($row_listCategories = mysql_fetch_assoc($listCategories));
              $rows = mysql_num_rows($listCategories);
              if($rows > 0) {
                  mysql_data_seek($listCategories, 0);
                  $row_listCategories = mysql_fetch_assoc($listCategories);
              }
            ?>
                    </select>

            • 3. Re: Dynamic list/menu update page
              Günter Schenk Level 4

              I-Evolve wrote:

               

              Not sure what you are trying to explain. I am having the same issue. Can this be described in a little more detial.

              Your Dynamic List/Menu appears to be missing the "Select value aqual to:" definition:

               

              screen_dynamic_list.jpg

               

              Cheers,

              Günter

              • 4. Re: Dynamic list/menu update page
                Albert S. Level 3

                Hi,

                 

                So the first example I am posting is pretty much the same idea as what Gunter wrote, Gunter wrote the way to handle it in dreamweaver.

                This example is what happens in the code after the DW wizard does its thing.


                Right before it submits to the database you can create a session of the value.

                 


                $_SESSION['cat_id'] = $_POST['cat_id'];

                 


                Then use a string compare command to check against the session.

                 

                (If session cat_id is the same as the recordset cat_id value then echo that line as selected)

                 

                 

                 

                <?php if (!(strcmp($row_listCategories['id'], $_SESSION['cat_id']))) {echo "selected=\"selected\"";} ?>

                 

                 

                 

                Here it is included in your code sample:

                 


                <select name="cat_id" multiple="multiple" size="5" id="cat_id">
                          <?php do { ?>
                          <option value="<?php echo $row_listCategories['id']?>"<?php if (!(strcmp($row_listCategories['id'], $_SESSION['cat_id']))) {echo "selected=\"selected\"";} ?>><?php echo $row_listCategories['cat']?></option>
                          <?php
                                } while ($row_listCategories = mysql_fetch_assoc($listCategories));
                                  $rows = mysql_num_rows($listCategories);
                                  if($rows > 0) {
                                      mysql_data_seek($listCategories, 0);
                                      $row_listCategories = mysql_fetch_assoc($listCategories);
                                  }
                            ?>
                        </select>

                 

                 

                 

                Note that at the top of your page you will need to initiate the session:

                 


                session_start();

                 

                 

                NOTE: This is example takes note that you added the multiple="multiple" tag to your select. If you want it so someone selects two items then after the update you want to call on both select items. (so all they selected is still selected, not just the last item.).

                 

                    <select name="select[]" id="select" multiple="multiple">
                      <?php do { ?>
                      <?php if (is_array($_SESSION['cat_id'])) { ?>
                      <option value="<?php echo $row_listCategories['id']?>"<?php if (in_array($row_listCategories['id'], $_SESSION['cat_id'])) {echo "selected=\"selected\"";} ?>><?php echo $row_listCategories['cat']?></option>     
                      <?php } else { ?>
                      <option value="<?php echo $row_listCategories['id']?>"<?php if (!(strcmp($row_listCategories['id'], $_SESSION['cat_id']))) {echo "selected=\"selected\"";} ?>><?php echo $row_listCategories['cat']?></option>
                      <?php } ?>     
                          <?php
                                } while ($row_listCategories = mysql_fetch_assoc($listCategories));
                                  $rows = mysql_num_rows($listCategories);
                                  if($rows > 0) {
                                      mysql_data_seek($listCategories, 0);
                                      $row_listCategories = mysql_fetch_assoc($listCategories);
                                  }
                            ?>
                    </select>

                 

                You will probably have to make an adjustment to your database input query to handle the array $_POST['cat_id']. I didnt have time to test that part.