2 Replies Latest reply on Nov 24, 2008 10:30 AM by alice_data

    Looping SelectedItem values in HttpService to Update Several Rows in Database in Flex

    alice_data
      Hi,

      My client wanted me to create something that allows certain DataGrid to display to allow users to edit and update the database records. The only dilemma I am having here is how to actually create something from Flex's end to allow users update several rows of the database at once. Since everything in my datagrid would be defined as selectedItem whenever a user accesses the entry, how can I get Flex to recognize each of the updated variables while I update the database?

      Another question is, if I do something like dg2.selectedItem.id, it would only display the "selectedItem," and since I intend to loop every single one of the values as such in the datagrid to be updated, is it possible to do something like dg. .id? I tried to do this, and I kept on getting errors.

      If anyone could refer me to some examples on how to get this to start, it would be great.

      Thanks in advance.

      Alice

      This is the current code I have:

      <mx:HTTPService id="save_scenario" method="POST" url=" http://localhost/save_scenario.php" useProxy="false">
      <mx:request xmlns="">
      <scenario_name>{scenario_name}</scenario_name>
      <id_no>{dg2.selectedItem.id}</id_no>
      <region_name>{dg2.selectedItem.region_name}</region_name>
      <population>{dg2.selectedItem.population}</population>
      <market>{dg2.selectedItem.market}</market>
      </mx:request>
      </mx:HTTPService>
        • 1. Re: Looping SelectedItem values in HttpService to Update Several Rows in Database in Flex
          Zee Yang Level 1
          there might be others ways of doing it, but i would track the changes at the data source level.

          store your data set in an ArrayCollection. Bind your data grid to the ArrayCollection object as its data source.

          with the ArrayCollection, attach an event handler to the collectionChange event. you can use the handler to track which rows of data has been changed.

          http://livedocs.adobe.com/flex/3/langref/mx/collections/ArrayCollection.html
          • 2. Looping SelectedItem values in HttpService to Update Several Rows in Database in Flex
            alice_data Level 1
            Thanks, but I think I will be a little more explicit here about what I really wanted to know. My impression is that if I have a DataGrid and that I would like to allow users to edit certain entries within the actual grid and have all of it saved to the database, I could use a for loop and just update everything without paying attention to what has really been updated, but I have been struggling with how to pass it all to the variables side. Currently, I could update things one entry a time, by using the code I provided earlier, but is there a way of which I can have it all posted to the HTTPService in one go?

            Note: I tried using dg. .id_no in the for loop to iterate over the variables I intend to post, but I don't think I am allowed to do so, do I?

            Alice

            Attached is what I used in a PHP script to allow the code loop around the users' edits, how could I do the similar thing in Flex by assigning the index variable to something more dynamic?

            Attach Code

            <?php

            if(!isset($_POST['npop'])){
            // this is where the user will select how many pops to insert
            ?>

            <form method="post">
            <input type="text" name="npop" value="Type in how many post values">
            <input type="submit" value="Continue">
            </form>

            <?
            } else {
            if(!isset($_POST['pop'])){
            $pop = $_POST['npop'];
            // when the user has selected the N number of pops
            // its time to create the N input elements
            ?>
            <form method="post">
            <input type="hidden" name="npop" value="<?=$pop;?>">
            <input type="hidden" name="pop" value="0">

            <?
            for($i = 1;$i <= $pop; $i++){
            echo "<h3>Pop $i</h3>";

            // now comes the 5 input elements..
            // add them as u like naming them in the following pattern,
            // or whatever prefix
            ?>
            Market: <input type="text" name="a<?=$i;?>" value="0"><br />
            IM_accept: <input type="text" name="b<?=$i;?>" value="0"><br />
            IM_defer: <input type="text" name="c<?=$i;?>" value="0"/><br />
            CR_accept : <input type="text" name="d<?=$i;?>" value="0"><br />
            FC_accept : <input type="text" name="e<?=$i;?>" value="0"><br />
            <?
            }
            ?>
            <input type="Submit" value="Submit form">
            </form>
            <?
            } else {
            $pop = $_POST['npop'];

            // when the user has submitted the pops,
            // they will be inserted to N rows in the table.

            for($i = 1; $i <= $pop; $i++){
            $a = $_POST['a'.$i]; $b = $_POST['b'.$i]; $c = $_POST['c'.$i];
            $d = $_POST['d'.$i]; $e = $_POST['e'.$i];
            $sql = "insert INTO table VALUES('$a', '$b', '$c', '$d', '$e')" . "\n";
            echo $sql;
            $myFile = "testFile.txt";
            $fh = fopen($myFile, 'a') or die("can't open file");
            fwrite($fh, $sql);
            fclose($fh);

            }


            echo "POPs were recorded. Thank you";

            }
            }
            ?>