Razvan Baba
Razvan Baba

Reputation: 155

Translate HTML form request to php array

I have a html form with 3 selector:

a. Room -> 1, 2, 3, 4, 5, 6
b. Adults -> 1, 2, 3, 4, 5, 6
c. Childs -> 0, 1, 2, 3, 4, 5

THe php arrays that i need to get looks like: Example 1 room with 2 adults

$rooms[] = array(array("paxType" => "Adult"), array("paxType" => "Adult")); 

Example 2 rooms ( one room is with two adults and the second room si with 2 adults an one child

$rooms[] = array(array("paxType" => "Adult"), array("paxType" => "Adult"));
$rooms[] = array(array("paxType" => "Adult"), array("paxType" => "Adult"), array("paxType" =>"Child", "age" => 8)); 

The variables that i receive from the form are as below:

$City= $_POST['City']; - text
$CheckIN= $_POST['CheckIN']; - text (date)
$CheckOUT= $_POST['CheckOUT']; - text (date)
$Rooms= $_POST['Rooms']; - selector (1,2,3,4,5,6)
$Adults= $_POST['Adults']; - selector (1,2,3,4,5,6)
$Childs= $_POST['Childs']; - selector (0,1,2,3,4,5) 

Form is workink fine for text and date input fields. How can i translate the html form request to get into the a bove look like php arrays. Thank you for your time.

Entire code is:

// create SOAP client object
  $client = new SoapClient("http://www.bookingassist.ro/test/book.wsdl", array('trace' => 1));

  try {
      function addPaxType($type = null, $amount = 0)
{
    $pax = array();
    for ($i = 0; $i < amount; $i++)
    {
       array_push($pax, array('paxType' => $type));
    }
    return $pax;
}  

$RoomsLength = 1;//count($_POST['Rooms']);
$Rooms = array();

//iterate over all rooms
for ($i = 0; $i < $RoomsLength ; $i++)
{
    $Rooms[$i] = array();

    if ( count($Adults) > 0)
    {
        //use a function to add adults to room
        array_push($Rooms[$i] , addPaxType('Adults', count($Adults)));
    }
    if (count($Childs) > 0)
    {
        array_push($Rooms[$i], addPaxType('Childs', count($Childs)));
    }
} 

      $filters = array();
      $filters[] = array("filterType" => "hotelStar", "filterValue" => "3", "4", "5");

      $filters[] = array("filterType" => "resultLimit", "filterValue" => "7");

      // make getAvailableHotel request (start search)
      $checkAvailability = $client->getAvailableHotel("gfdgdfgVNhTzA4MVZ3Y2hjTkt3QXNHSXZRYUZOb095Qg==", "RHMK", "2015-03-30", "2015-04-12", "EUR", "RO", "false", $rooms, $filters);
  }
  catch (SoapFault $exception) {
      echo $exception->getMessage();
      exit;
  }

Upvotes: 2

Views: 175

Answers (2)

Mouser
Mouser

Reputation: 13294

You need a for lus for this. Based upon the data on this page. I cannot define in which rooms the children and adults are and the age of the children based upon the supplied data. I can however show you an attempt to make such an array.

function addPaxType($type = null, $amount = 0)
{
    $pax = array();
    for ($i = 0; $i < amount; $i++)
    {
       array_push($pax, array('paxType' => $type));
    }
    return $pax;
}  

$RoomsLength = count($_POST['Rooms']);
$Rooms = array();

//iterate over all rooms
for ($i = 0; $i < $RoomsLength ; $i++)
{
    $Rooms[$i] = array();

    if ( count($Adults) > 0)
    {
        //use a function to add adults to room
        array_push($Rooms[$i] , addPaxType('adult', count($Adults)));
    }
    if (count($Childs) > 0)
    {
        array_push($Rooms[$i], addPaxType('child', count($Childs)));
    }
}   

When two rooms are selected and two adults this will output:

 $Rooms[0] --> [ [paxType => adult], [paxType => adult] ];
 $Rooms[1] --> [ [paxType => adult], [paxType => adult] ];

Upvotes: 1

J_E_Roseto
J_E_Roseto

Reputation: 11

You can get an array setting in the name input method on the html form post

<br />Room: <input type="text" **name="somearray[]"** required/></br>

After that you can do some like this

$room=$_POST['somearray'];

forgive my bad English!

Upvotes: 1

Related Questions