Re: Processing two post values

From: Date: Sun, 23 Oct 2005 03:31:56 +0000
Subject: Re: Processing two post values
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-224498@lists.php.net to get a copy of this message
...not quite sure what you're asking. Are you asking for help on how to process these variables? I wrote this off the top of my head... meaning that I did not test it... since I don't know if this was what you were asking... I just fiddled with it. Add this code and tell me if it prints out the sql statements the way you wanted BTW, I think your code is wrong here <form action="<?php action=<?php echo $frm['action']; ?>&property_id=<?php echo $_GET['property_id']; ?>" method="post"> should be <form action="<?php echo $frm['action'];?>&property_id=<?php echo $_GET['property_id']; ?>" method="post"> -Minuk ====BEGIN CODE==== $roomArray = array(); $roomArray['bedroom'] = array(); $roomArray['bathroom'] = array(); $roomArray['reception']=array(); /* * I am assuming all you have are 3 types of rooms, 'bedroom', 'bathroom', and 'reception' * * I am also assuming that the lowest room number is 1 and that the all the room values are of the following format : * * ROOMNAME_ROOMNUMBER_X * ROOMNAME_ROOMNUMBER_Y * * the format is strict, there must be an "_" after the room name and one after room number */ /* * Loop through post, and if the form name starts with a room name... then try to "decipher" the form name. * If appropriate, then add. */ foreach ($_POST as $key=>$value) { /*
    * see if the $key has "_" after the first character
    *  and another "_" after the first one
    *     * Search for the following
    *
    *  Valid form names are of
    *       *    CHARACTER + (1 or more letters) + "_" + NUMBER + (1 or more numbers) + "_" + (X or Y)
    *
    * Using explode, if we break a form name at the '_', the valid types should break into three pieces
    *    room name
    *    room number
    *    room dimension(x or y)
    */
$roomDataArray = explode('_', $key); if (count($roomDataArray)==3) {
         $roomName = $roomDataArray[0];            $roomNumber = $roomDataArray[1];
         $roomDimension = $roomDataArray[2];
         /*           * check to see if $roomName exists in $roomArray,
          *  $roomNumber is a number
          *  $roomDimension is 'X'  or  'Y'
          *  $_POST[$key] is a number > 0
          */
           if (isset($roomArray[$roomName])
            && is_numeric($roomNumber)
            && (strcasecmp($roomDimension, 'x') ==  0 || strcasecmp($roomDimension, 'y')==0)
            && is_numeric($_POST[$key])
            )          {
            if (!isset($roomArray[$roomName][$roomNumber]))                $roomArray[$roomName][$roomNumber] = array();
           $roomArray[$roomName][$roomNumber][$roomDimension] = $value;
            //By the way, $_POST[$key] = $value, in case I lost 'ya there
       }
} } /* * At this point, $roomArray should be of the following format * * $roomArray['bedroom'][1][x] = 10; * $roomArray['bedroom'][1][y] = 20; * . * . * . * etc. * * generate an insert sql statement PER ROOM */ echo 'sql commands <BR><BR>'; foreach ($roomArray as $roomName=>$roomNumberDataArray) foreach ($roomNumberDataArray as $roomNumber=>$roomDimensionArray)
      foreach ($roomDimensionArray as $roomDimension => $value)
      {
           $sql = 'insert into database.table("roomName", "roomNumber", "roomDimensionName", "roomDimensionValue") values ("'.$roomName.'", "'.$roomNumber.'", "'.$roomDimension.'", "'.$value.'");';
           echo $sql.'<BR>';
       }
echo 'done'; ====END CODE==== Shaun wrote:
Hi, I have a form on my site with a database driven amount of rooms for a property - bedrooms, bathrooms and receptions. For each room there will be a textfield allowing users to enter the x and y dimensions. For each room I need to insert the values to the database, however I cant think of a way to do this. All one can do with php is a foreach on the $_POST values whereas I need to process two at a time i.e. one insert of the x and y values per room. Here is the form I have created: <form action="<?php action=<?php echo $frm['action']; ?>&property_id=<?php echo $_GET['property_id']; ?>" method="post"> <table> <tr> <?php $i = 1; while( $i <= $frm["Number_Of_Bedrooms"] ){ ?> <tr> <td>Bedroom <?php echo $i ?> <td>X:<input name="bedroom_<?php echo $i ?>_x" type="text" /></td> <td>Y:<input name="bedroom_<?php echo $i ?>_y" type="text" /></td> </tr> <?php $i++; } $i = 1; while( $i <= $frm["Number_Of_Bathrooms"] ){ ?> <tr> <td>Bathroom <?php echo $i ?> <td>X:<input name="bathroom_<?php echo $i ?>_x" type="text" /></td> <td>Y:<input name="bathroom_<?php echo $i ?>_y" type="text" /></td> </tr> <?php $i++; } $i = 1; while( $i <= $frm["Number_Of_Receptions"] ){ ?> <tr> <td>Reception <?php echo $i ?> <td>X:<input name="reception_<?php echo $i ?>_x" type="text" /></td> <td>Y:<input name="reception_<?php echo $i ?>_y" type="text" /></td> </tr> <?php $i++; } ?> <tr> <td>&nbsp;</td> <td><input type="submit" name="Submit" value="<? echo $frm["submit_text"]; ?>"></td> <td>&nbsp;</td> </tr> </table> </form>


« previous php.general (#224498) next »