Create a Dynamic Form of Array Results

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Grandong
Forum Commoner
Posts: 65
Joined: Thu Jul 03, 2014 12:35 pm
Location: Indonesian
Contact:

Create a Dynamic Form of Array Results

Post by Grandong »

please help me,
I have an array like:

Code: Select all

Array
(
    [diagnostic] => Array
        (
            [status] => 200
            [elapsetime] => 0.0449
            [memoryusage] => 5.39MB
            [unix_timestamp] => 1404715619
            [confirm] => success
            [lang] => id
            [currency] => IDR
        )

    [output_type] => array
    [required] => Array
        (
            [separator] => Array
                (
                    [mandatory] => 1
                    [type] => text
                    [example] => 
                    [FieldText] => Informasi Kontak yang Dapat Dihubungi
                    [category] => separator
                )

            [conSalutation] => Array
                (
                    [mandatory] => 1
                    [type] => combobox
                    [example] => Mr
                    [FieldText] => Titel
                    [category] => contact
                    [resource] => Array
                        (
                            [0] => Array
                                (
                                    [id] => Mr
                                    [name] => Tuan
                                )

                            [1] => Array
                                (
                                    [id] => Mrs
                                    [name] => Nyonya
                                )

                            [2] => Array
                                (
                                    [id] => Ms
                                    [name] => Nona
                                )

                        )

                )

            [conFirstName] => Array
                (
                    [mandatory] => 1
                    [type] => textbox
                    [example] => Jane
                    [FieldText] => Nama Depan
                    [category] => contact
                )

            [conLastName] => Array
                (
                    [mandatory] => 1
                    [type] => textbox
                    [example] => Wacob
                    [FieldText] => Nama Belakang
                    [category] => contact
                )

            [conPhone] => Array
                (
                    [mandatory] => 1
                    [type] => textbox
                    [example] => Jane
                    [FieldText] => No. Telepon
                    [category] => contact
                )

            [separator_adult1] => Array
                (
                    [mandatory] => 1
                    [type] => text
                    [example] => 
                    [FieldText] => Penumpang Dewasa 1
                    [category] => separator
                )

            [titlea1] => Array
                (
                    [mandatory] => 1
                    [type] => combobox
                    [example] => Mr
                    [FieldText] => Titel
                    [category] => adult1
                    [resource] => Array
                        (
                            [0] => Array
                                (
                                    [id] => Mr
                                    [name] => Tuan
                                )

                            [1] => Array
                                (
                                    [id] => Mrs
                                    [name] => Nyonya
                                )

                            [2] => Array
                                (
                                    [id] => Ms
                                    [name] => Nona
                                )

                        )

                )

            [firstnamea1] => Array
                (
                    [mandatory] => 1
                    [type] => textbox
                    [example] => Jane
                    [FieldText] => Nama Depan
                    [category] => adult1
                )

            [lastnamea1] => Array
                (
                    [mandatory] => 1
                    [type] => textbox
                    [example] => wacob
                    [FieldText] => Nama Belakang
                    [category] => adult1
                )

            [ida1] => Array
                (
                    [mandatory] => 0
                    [type] => textbox
                    [example] => 143243434323443
                    [FieldText] => Nomor Kartu Identitas (KTP/Paspor/Lainnya)
                    [category] => adult1
                )

            [separator_adult2] => Array
                (
                    [mandatory] => 1
                    [type] => text
                    [example] => 
                    [FieldText] => Penumpang Dewasa 2
                    [category] => separator
                )

            [titlea2] => Array
                (
                    [mandatory] => 1
                    [type] => combobox
                    [example] => Mr
                    [FieldText] => Titel
                    [category] => adult2
                    [resource] => Array
                        (
                            [0] => Array
                                (
                                    [id] => Mr
                                    [name] => Tuan
                                )

                            [1] => Array
                                (
                                    [id] => Mrs
                                    [name] => Nyonya
                                )

                            [2] => Array
                                (
                                    [id] => Ms
                                    [name] => Nona
                                )

                        )

                )

            [firstnamea2] => Array
                (
                    [mandatory] => 1
                    [type] => textbox
                    [example] => Jane
                    [FieldText] => Nama Depan
                    [category] => adult2
                )

            [lastnamea2] => Array
                (
                    [mandatory] => 1
                    [type] => textbox
                    [example] => wacob
                    [FieldText] => Nama Belakang
                    [category] => adult2
                )

            [ida2] => Array
                (
                    [mandatory] => 0
                    [type] => textbox
                    [example] => 143243434323443
                    [FieldText] => Nomor Kartu Identitas (KTP/Paspor/Lainnya)
                    [category] => adult2
                )

        )

    [departures] => Array
        (
            [flight_id] => 28977522
            [airlines_name] => SRIWIJAYA
            [flight_number] => SJ-272
            [flight_date] => 2014-07-09
            [price_value] => 1125400.00
            [count_adult] => 2
            [count_child] => 0
            [count_infant] => 0
            [timestamp] => 2014-07-07 13:46:29
            [price_adult] => 562700.00
            [price_child] => 0.00
            [price_infant] => 0.00
            [simple_departure_time] => 05:45
            [simple_arrival_time] => 08:35
            [stop] => Langsung
            [long_via] => 
            [full_via] => CGK - DPS (05:45 - 08:35)
            [need_baggage] => 0
            [duration] => 1 j 50 m
            [image] => http://www.master18.tiket.com/images/tiket2/icon_sriwijaya_2.jpg
            [flight_infos] => Array
                (
                    [flight_info] => Array
                        (
                            [0] => Array
                                (
                                    [flight_number] => SJ-272
                                    [departure_city] => CGK
                                    [arrival_city] => DPS
                                    [simple_departure_time] => 05:45
                                    [simple_arrival_time] => 08:35
                                )

                        )

                )

        )

    [login_status] => false
    [token] => ed78d6b5e4ce52c2ab374e03653cf0459de0aa60
)
and this my html php :

Code: Select all

<?php $result = $json_results['required']['separator']; ?>
 <h3><p align="center"><?= $result["FieldText"]; ?></p></h3>
 
<form class="form-horizontal" role="form">
  <div class="form-group">
    <label for="conSalutation" name="conSalutation" class="col-sm-2 control-label">Title</label>
    <div class="col-sm-2">
   <select class="form-control">
    <?php foreach($json_results['required']['conSalutation']['resource'] as $result): ?> 
  <option id="<?= $result["id"]; ?>" name="<?= $result["name"]; ?>"><?= $result["id"]; ?></option>
  <?php endforeach; ?>
  </select>

    </div>
  </div>
  <div class="form-group">
    <label for="conFirstName" class="col-sm-2 control-label">Nama Depan</label>
    <div class="col-sm-7">        
        <input type="textbox" class="form-control" id="conFirstName" name="conFirstName" placeholder="">        
    </div>
  </div>
    <div class="form-group">
    <label for="conLastName" class="col-sm-2 control-label">Nama Belakang</label>
    <div class="col-sm-7">
        <input type="textbox" class="form-control" id="conLastName" name="conLastName" placeholder="">
    </div>
  </div>
      <div class="form-group">
    <label for="conPhone" class="col-sm-2 control-label">No. Telepon</label>
    <div class="col-sm-7">
        <input type="textbox" class="form-control" id="conPhone" name="conPhone" placeholder="">
    </div>
    </div>
      <div class="form-group">
    <label for="conEmailAddress" class="col-sm-2 control-label">Email</label>
    <div class="col-sm-7">
        <input type="email" class="form-control" id="conEmailAddress" name="conEmailAddress" placeholder="">
    </div>

  </div>
</form>

    <hr>
      <h3><p align="center">Informasi Penumpang</p></h3>
<form class="form-horizontal" role="form">
  <div class="form-group">
    <label for="titlea1" name="titlea1" class="col-sm-2 control-label">Title</label>
    <div class="col-sm-2">
   <select class="form-control">
    <?php foreach($json_results['required']['titlea1']['resource'] as $result): ?> 
  <option id="<?= $result["id"]; ?>" name="<?= $result["name"]; ?>"><?= $result["id"]; ?></option>
    <?php endforeach; ?>
  </select>

    </div>
  </div>
  <div class="form-group">
    <label for="firstnamea1" class="col-sm-2 control-label">Nama Depan</label>
    <div class="col-sm-7">
        <input type="text" class="form-control" id="firstnamea1" name="firstnamea1" placeholder="">
    </div>
  </div>
    <div class="form-group">
    <label for="lastnamea1" class="col-sm-2 control-label">Nama Belakang</label>
    <div class="col-sm-7">
        <input type="text" class="form-control" id="lastnamea1" name="lastnamea1" placeholder="">
    </div>
  </div>
      <div class="form-group">
    <label for="ida1" class="col-sm-2 control-label">Nomor Kartu Identitas (KTP/Paspor/Lainnya)</label>
    <div class="col-sm-7">
        <input type="textbox" class="form-control" id="ida1" name="ida1" placeholder="">
    </div></div>
    <div class="form-group ">
  <label for="tanggal" class="col-sm-2 control-label">Tanggal Lahir</label>
  <div class="col-sm-7">
  <input class="form-control" name="birthdatea1" id="datetimepicker" type="text" >
                        <script type="text/javascript">
                            jQuery('#datetimepicker').datetimepicker({  
                            format:'Y-m-d',
                            startDate:'1945-01-01',                            
                            maxDate:'+1970/01/02',
                            timepicker:false                            
                            });
                            
                        </script>
    </div></div>
    <div class="form-group">
    <label for="dcheckinbaggagea11" name="dcheckinbaggagea11" class="col-sm-2 control-label">Bagasi</label>
    <div class="col-sm-7">
   <select class="form-control">
    <?php foreach($json_results['required']['dcheckinbaggagea11']['resource'] as $result): ?> 
  <option id="<?= $result["id"]; ?>" name="<?= $result["name"]; ?>"><?= $result["name"]; ?></option>
    <?php endforeach; ?>
  </select>

    </div>
  </div>
        <button type="submit" class="btn btn-success">Lanjutkan</button>

  </div>
</form>
But My html php code not Dynamic Form Field
how can I create a dynamic form appropriate in the show from the array?
Newbie The Passion for Learning
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Create a Dynamic Form of Array Results

Post by Christopher »

It looks like the array has the information you need. Just loop through the fields and generate the HTML for each form field based on the info in the array. A simple way would be to have a switch() statement for each form field type. Better would be to create a class for each form field type and use those to render the form.

There are a number of good libraries available to do this in the major frameworks.
(#10850)
User avatar
Grandong
Forum Commoner
Posts: 65
Joined: Thu Jul 03, 2014 12:35 pm
Location: Indonesian
Contact:

Re: Create a Dynamic Form of Array Results

Post by Grandong »

Christopher wrote:It looks like the array has the information you need. Just loop through the fields and generate the HTML for each form field based on the info in the array. A simple way would be to have a switch() statement for each form field type. Better would be to create a class for each form field type and use those to render the form.

There are a number of good libraries available to do this in the major frameworks.
Please, Can You Give Me Code Exemple ?
Newbie The Passion for Learning
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Create a Dynamic Form of Array Results

Post by Celauran »

User avatar
Grandong
Forum Commoner
Posts: 65
Joined: Thu Jul 03, 2014 12:35 pm
Location: Indonesian
Contact:

Re: Create a Dynamic Form of Array Results

Post by Grandong »

thx very much
but i'am not familiar using cakephp

and more problem has been solved using :

Code: Select all

<?php if (empty($json_results['required']['dcheckinbaggagea11'])):{ ?>
  
<?php } ?>
<?php else: ?>

      <div class="form-group">
    <label for="dcheckinbaggagea11" name="dcheckinbaggagea11" class="col-sm-2 control-label">Bagasi</label>
    <div class="col-sm-7">
   <select class="form-control">
    <?php foreach($json_results['required']['dcheckinbaggagea11']['resource'] as $result): ?> 
  <option id="<?= $result["id"]; ?>" name="<?= $result["name"]; ?>"><?= $result["name"]; ?></option>
    <?php endforeach; ?>
  </select>
    </div>
  </div>

<?php endif; ?>
For Dynamic Field

BUT .. i don't undestand about :
if user select 2 , then display 2 form :banghead:
Newbie The Passion for Learning
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Create a Dynamic Form of Array Results

Post by Celauran »

I wasn't suggesting you use CakePHP, simply demonstrating how they've implemented what I thought you were asking, which would provide considerably more abstraction than what you've just posted, though also introduces significantly more complexity.
User avatar
Grandong
Forum Commoner
Posts: 65
Joined: Thu Jul 03, 2014 12:35 pm
Location: Indonesian
Contact:

Re: Create a Dynamic Form of Array Results

Post by Grandong »

Celauran wrote:I wasn't suggesting you use CakePHP, simply demonstrating how they've implemented what I thought you were asking, which would provide considerably more abstraction than what you've just posted, though also introduces significantly more complexity.
Problem Has Been Solved...
Thx Very Much Celauran..
But, can i ask again ? :D

i have array like :

Code: Select all

Array
(
    [diagnostic] => Array
        (
            [status] => 200
            [elapsetime] => 0.0473
            [memoryusage] => 5.41MB
            [unix_timestamp] => 1404836195
            [confirm] => success
            [lang] => id
            [currency] => IDR
        )

    [output_type] => array
    [required] => Array
        (
            [separator] => Array
                (
                    [mandatory] => 1
                    [type] => text
                    [example] => 
                    [FieldText] => Informasi Kontak yang Dapat Dihubungi
                    [category] => separator
                )

            [conSalutation] => Array
                (
                    [mandatory] => 1
                    [type] => combobox
                    [example] => Mr
                    [FieldText] => Titel
                    [category] => contact
                    [resource] => Array
                        (
                            [0] => Array
                                (
                                    [id] => Mr
                                    [name] => Tuan
                                )

                            [1] => Array
                                (
                                    [id] => Mrs
                                    [name] => Nyonya
                                )

                            [2] => Array
                                (
                                    [id] => Ms
                                    [name] => Nona
                                )

                        )

                )

            [conFirstName] => Array
                (
                    [mandatory] => 1
                    [type] => textbox
                    [example] => Jane
                    [FieldText] => Nama Depan
                    [category] => contact
                )

            [conLastName] => Array
                (
                    [mandatory] => 1
                    [type] => textbox
                    [example] => Wacob
                    [FieldText] => Nama Belakang
                    [category] => contact
                )

            [conPhone] => Array
                (
                    [mandatory] => 1
                    [type] => textbox
                    [example] => Jane
                    [FieldText] => No. Telepon
                    [category] => contact
                )

            [separator_adult1] => Array
                (
                    [mandatory] => 1
                    [type] => text
                    [example] => 
                    [FieldText] => Penumpang Dewasa 1
                    [category] => separator
                )

            [titlea1] => Array
                (
                    [mandatory] => 1
                    [type] => combobox
                    [example] => Mr
                    [FieldText] => Titel
                    [category] => adult1
                    [resource] => Array
                        (
                            [0] => Array
                                (
                                    [id] => Mr
                                    [name] => Tuan
                                )

                            [1] => Array
                                (
                                    [id] => Mrs
                                    [name] => Nyonya
                                )

                            [2] => Array
                                (
                                    [id] => Ms
                                    [name] => Nona
                                )

                        )

                )

            [firstnamea1] => Array
                (
                    [mandatory] => 1
                    [type] => textbox
                    [example] => Jane
                    [FieldText] => Nama Depan
                    [category] => adult1
                )

            [lastnamea1] => Array
                (
                    [mandatory] => 1
                    [type] => textbox
                    [example] => wacob
                    [FieldText] => Nama Belakang
                    [category] => adult1
                )

            [ida1] => Array
                (
                    [mandatory] => 0
                    [type] => textbox
                    [example] => 143243434323443
                    [FieldText] => Nomor Kartu Identitas (KTP/Paspor/Lainnya)
                    [category] => adult1
                )

            [birthdatea1] => Array
                (
                    [mandatory] => 1
                    [type] => datetime
                    [example] => 1990-01-01
                    [FieldText] => Tanggal Lahir
                    [category] => adult1
                )

            [passportnationalitya1] => Array
                (
                    [mandatory] => 1
                    [type] => combobox
                    [example] => id
                    [FieldText] => Kewarganegaraan
                    [category] => adult1
                    [resource] => http://api.master18.tiket.com/general_api/listCountry
                )

            [separator_adult2] => Array
                (
                    [mandatory] => 1
                    [type] => text
                    [example] => 
                    [FieldText] => Penumpang Dewasa 2
                    [category] => separator
                )

            [titlea2] => Array
                (
                    [mandatory] => 1
                    [type] => combobox
                    [example] => Mr
                    [FieldText] => Titel
                    [category] => adult2
                    [resource] => Array
                        (
                            [0] => Array
                                (
                                    [id] => Mr
                                    [name] => Tuan
                                )

                            [1] => Array
                                (
                                    [id] => Mrs
                                    [name] => Nyonya
                                )

                            [2] => Array
                                (
                                    [id] => Ms
                                    [name] => Nona
                                )

                        )

                )

            [firstnamea2] => Array
                (
                    [mandatory] => 1
                    [type] => textbox
                    [example] => Jane
                    [FieldText] => Nama Depan
                    [category] => adult2
                )

            [lastnamea2] => Array
                (
                    [mandatory] => 1
                    [type] => textbox
                    [example] => wacob
                    [FieldText] => Nama Belakang
                    [category] => adult2
                )

            [ida2] => Array
                (
                    [mandatory] => 0
                    [type] => textbox
                    [example] => 143243434323443
                    [FieldText] => Nomor Kartu Identitas (KTP/Paspor/Lainnya)
                    [category] => adult2
                )

            [birthdatea2] => Array
                (
                    [mandatory] => 1
                    [type] => datetime
                    [example] => 1990-01-01
                    [FieldText] => Tanggal Lahir
                    [category] => adult2
                )

            [passportnationalitya2] => Array
                (
                    [mandatory] => 1
                    [type] => combobox
                    [example] => id
                    [FieldText] => Kewarganegaraan
                    [category] => adult2
                    [resource] => http://api.master18.tiket.com/general_api/listCountry
                )

        )

    [departures] => Array
        (
            [flight_id] => 28978150
            [airlines_name] => CITILINK
            [flight_number] => QG-9631
            [flight_date] => 2014-07-11
            [price_value] => 1207600.00
            [count_adult] => 2
            [count_child] => 0
            [count_infant] => 0
            [timestamp] => 2014-07-08 23:15:45
            [price_adult] => 603800.00
            [price_child] => 0.00
            [price_infant] => 0.00
            [simple_departure_time] => 04:55
            [simple_arrival_time] => 08:05
            [stop] => Langsung
            [long_via] => 
            [full_via] => CGK - BPN (04:55 - 08:05)
            [need_baggage] => 0
            [duration] => 2 j 10 m
            [image] => http://www.master18.tiket.com/images/tiket2/icon_citilink_2.jpg
            [flight_infos] => Array
                (
                    [flight_info] => Array
                        (
                            [0] => Array
                                (
                                    [flight_number] => QG-9631
                                    [departure_city] => CGK
                                    [arrival_city] => BPN
                                    [simple_departure_time] => 
                                    [simple_arrival_time] => 
                                )

                        )

                )

        )

    [login_status] => false
    [token] => ed78d6b5e4ce52c2ab374e03653cf0459de0aa60
)
How To Get and Display titlea1
cz in my code this error Array to string conversion

This My Php Code

Code: Select all

<?= $json_results['required']['titlea'.$i]; ?>
Thx B4
Newbie The Passion for Learning
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Create a Dynamic Form of Array Results

Post by Christopher »

Grandong wrote:How To Get and Display titlea1
cz in my code this error Array to string conversion

This My Php Code

Code: Select all

<?= $json_results['required']['titlea'.$i]; ?>
Thx B4
What is $i ?
(#10850)
User avatar
Grandong
Forum Commoner
Posts: 65
Joined: Thu Jul 03, 2014 12:35 pm
Location: Indonesian
Contact:

Re: Create a Dynamic Form of Array Results

Post by Grandong »

This is Before Code:

Code: Select all

<?php $n=$json_results['departures']['count_adult']; ?>
<?php for($i=1;$i<=$n;$i++): { ?>
titlea genrate by API, if user select 1 then titlea is titlea1
Newbie The Passion for Learning
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Create a Dynamic Form of Array Results

Post by Celauran »

Look at the JSON output; ['required']['titlea1'] is an array. What are you trying to do?
User avatar
Grandong
Forum Commoner
Posts: 65
Joined: Thu Jul 03, 2014 12:35 pm
Location: Indonesian
Contact:

Re: Create a Dynamic Form of Array Results

Post by Grandong »

Celauran wrote:Look at the JSON output; ['required']['titlea1'] is an array. What are you trying to do?
I want to displat titlea1 in :

Code: Select all

<input type="textbox" class="form-control" id="titlea1" name="titlea1" placeholder=""> 
this possible ?
Newbie The Passion for Learning
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Create a Dynamic Form of Array Results

Post by Celauran »

The JSON presents an array of people and seems to suggest a combo box be used. I don't really understand the data structure of the resources array, though, so I don't know that I can suggest much.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Create a Dynamic Form of Array Results

Post by Christopher »

Grandong wrote:This is Before Code:

Code: Select all

<?php $n=$json_results['departures']['count_adult']; ?>
<?php for($i=1;$i<=$n;$i++): { ?>
titlea genrate by API, if user select 1 then titlea is titlea1
Yes, but the error message says nothing about that. It is an array to string conversion error. Are you sure the line you posted is where the error occurred? Have you dumped $json_results to see what exactly you are attempting to echo?
(#10850)
User avatar
Grandong
Forum Commoner
Posts: 65
Joined: Thu Jul 03, 2014 12:35 pm
Location: Indonesian
Contact:

Re: Create a Dynamic Form of Array Results

Post by Grandong »

Probelm Solved

Thx Very Much :D
Newbie The Passion for Learning
Post Reply