/    Sign up×
Community /Pin to ProfileBookmark

Does ForEach Loop Take Only Original names of Arrays ?

Php Experts,

I got this array:

Step 1

[code]
//$radio_button_gender_options = array(‘Male’,’Female’,’Male To Female’,’Female To Male’);
$radio_button_gender_options[]=’Male’;
$radio_button_gender_options[]=’Female’;
$radio_button_tos_options = array(‘Yes’,’No’);
[/code]

Step 2
And I did this:

[code]
$value = $form_question_label;
$value_1 = str_replace(“”,”_”,”$value”); //Replaced Spaces to Underscores.
$value_2 = strtolower(“$value_1”); //Replaced Upper Case to Lower Case.

//$radio_button_gender_options = array(‘Male’,’Female’,’Male To Female’,’Female To Male’);
$current_radio_button_options = “$” . “radio_button_” . “$value_2” . “_options”;
$current_radio_button_option = “$” . “radio_button_” . “$value_2” . “_option”;
[/code]

Step 3
Therefore, this should work as the Array Name is correct:

[code]
foreach($current_radio_button_options as $current_radio_button_option) //Loop through the whole appropriate ‘Radio Buttons’ array.
{
echo “<input type=”radio” id=”$current_radio_button_option” name=”$value_2″ value=”$current_radio_button_option”>
<label_for=”$radio_button_gender_option”>$form_question_label</label><br>”;
}
[/code]

But I get error:
**Warning: Invalid argument supplied for foreach() in C:xampphtdocstestsearch_2.php on line 130**

Line 130 is this:

[code]
foreach($current_radio_button_options as $current_radio_button_option) //Loop through the whole appropriate ‘Radio Buttons’ array.
[/code]

It seems php does not recognise this variable as array:

[code]
$current_radio_button_options
[/code]

It should. Look at step 2 above.
The original Array is actually this:

[code]
//$radio_button_gender_options = array(‘Male’,’Female’,’Male To Female’,’Female To Male’);
$radio_button_gender_options[]=’Male’;
$radio_button_gender_options[]=’Female’;
$radio_button_gender_options[]=’Male To Female’;
$radio_button_gender_options[]=’Female To Male’;
$radio_button_tos_options = array(‘Yes’,’No’);
[/code]

But remember at step 2, I did this:

[code]
//$radio_button_gender_options = array(‘Male’,’Female’,’Male To Female’,’Female To Male’);
$current_radio_button_options = “$” . “radio_button_” . “$value_2” . “_options”;
$current_radio_button_option = “$” . “radio_button_” . “$value_2” . “_option”;
[/code]

So it is like say, the array is called “tom”.
But instead of looping (foreach) through Tom, I am doing it like this:

bert = tom
Now loop foreach thru ‘bert’.
Now, doesn’t foreach loop work that way ? Do you have to give the original Array Name or what ?

Why am I doing it like this ? There is a reason for it. Not pasting here the full code as it is too long. If you look at the full code then you will see I have to do it like this.

to post a comment

10 Comments(s)

Copy linkTweet thisAlerts:
@developer_webauthorApr 29.2020 — Folks,

Here is an update...
<i>
</i>foreach($current_radio_button_options as $current_radio_button_option) //Loop through the whole appropriate 'Radio Buttons' array.
{
echo "&lt;input type="radio" id="$current_radio_button_option" name="$value_2" value="$current_radio_button_option"&gt;
&lt;label_for="$current_radio_button_option"&gt;$current_radio_button_option&lt;/label&gt;&lt;br&gt;";
}


I get same error again ...

**Warning: Invalid argument supplied for foreach() in C:xampphtdocstestsearch_2.php on line 130**

Line 130 is this:
<i>
</i>foreach($current_radio_button_options as $current_radio_button_option) //Loop through the whole appropriate 'Radio Buttons' array.


The error is saying that, in the foreach loop, the value before the "as" is not recognised.

meaning this is not recognised:
<i>
</i>$current_radio_button_options

If that is so, then it should not create the value after the "as". Should not create this:
<i>
</i>$current_radio_button_option


But it does. And it successfully echoes it to ! Look:

[upl-image-preview url=https://www.webdeveloper.com/assets/files/2020-04-29/1588169437-709513-foreach-invalid.png]

Zoom in and see ...

[upl-image-preview url=https://www.webdeveloper.com/assets/files/2020-04-29/1588169720-563547-foreach-invalid.png]

Can you see the "male", "female", etc. options in the radio button options ?

Well, they got created by:

**$current_radio_button_option**

Question is, how did the foreach manage to create that particular variable from this following variable which the foreach keeps saying invalid argument ?

**$current_radio_button_options**
Copy linkTweet thisAlerts:
@NogDogApr 29.2020 — The first argument to foreach() ($current_radio_button_options in this case) must be an array (or an iterable object). It looks like you are just making it a string that _looks_ like a variable name.
Copy linkTweet thisAlerts:
@developer_webauthorApr 29.2020 — @NogDog#1617957

Ok. So how to fix it ? Show me a code sample so it doesn't look like a string anymore.

I just played around like this but no luck:
<i>
</i>foreach(array $current_radio_button_options as $current_radio_button_option) //Loop through the whole appropriate 'Radio Buttons' array.
{
echo "&lt;input type="radio" id="$current_radio_button_option" name="$value_2" value="$current_radio_button_option"&gt;
&lt;label_for="$current_radio_button_option"&gt;$current_radio_button_option&lt;/label&gt;&lt;br&gt;";
}


No luck with this either:
<i>
</i>foreach($current_radio_button_options[] as $current_radio_button_option) //Loop through the whole appropriate 'Radio Buttons' array.
{

Trying to force php to not take the first value of the foreach as a string.

Or should I be fixing this instead:
<i>
</i>$current_radio_button_options = "$" . "radio_button_" . "$value_2" . "_options";
$current_radio_button_option = "$" . "radio_button_" . "$value_2" . "_option";


If so, how ? So that the value of the variables are not taken as string ?
Copy linkTweet thisAlerts:
@NogDogApr 29.2020 — > @developer_web#1617973 Or should I be fixing this instead:

> $current_radio_button_options = "$" . "radio_button_" . "$value_2" . "_options";

> $current_radio_button_option = "$" . "radio_button_" . "$value_2" . "_option";


I have no idea why you are doing that, I'm afraid, nor what the expected result should be. Perhaps you want to use "variable variables" (usually another "code smell").
<i>
</i>$current_radio_button_options = ${"radio_button_" . "$value_2" . "_options"};

Even if that "fixes" it, you're probably better served by doing something like using array-style names in your HTML form names, and then you can actually loop on them in your PHP code.

Not a great article, but first usable one my googling found: https://code-boxx.com/post-array-html-form/
Copy linkTweet thisAlerts:
@developer_webauthorMay 02.2020 — @NogDog#1617975

This should explain things to you.

Look, I want the php to produce a form like this:
<i>
</i>&lt;label for="first_name"&gt;First Name&lt;/label&gt;
&lt;input type="text" placeholder="Type your First Name here ..." name="first_name" required [A-Za-z0-9] autocorrect=off&gt;


Note the label. It is 'First Name'. Capital letters are the first letter of each word.

Now note the 'name="first_name" '. No capitals. Space replaced by underscore.

So, instead of having one array for the label and another for the "form item names" like so ..
<i>
</i>$form_labels = array('First Name', 'Surname')
$form_item_names = array('first_name', 'surname')


Just using one array only:
<i>
</i>$form_labels = array('First Name', 'Surname')


So, instead of having a gun and a bullet (2 items) to make the weapon, I'm just using the gun to make the bullet out of that too by cutting portion of the gun and chisselling it here and there and shaping & forming the bullet.

I am just having one array "$form_labels" and producing the "names=" of each form item from the $form_labels" array values by non-capitalising the values and replacing the spaces with the underscores.

Here's a better explanation ....

God didn't create all males from Adam only while all females from Eve. He didn't create all humans from 2 items (2 humans).

Instead, he created even Eve from Adam and then all the descendants from both of them. Note, he started off with one item: Adam. And not 2 items: Adam & Eve.

I'm doing the same.

And no. Originally, I did not have any of this in mind (Adam & Gun example) but I just thought of these 2 examples on the spot here to explain things to you.

In short, I prefer to play around. Experiment. Create things out of as many 'less items' as possible. For fun.

Look below NogDog. This is something I was doing 6 months back on another project. It is put on hold. Note again, I creating the form labels and names (name='value') from arrays.

Once the current project is finished, I will continue this old one that I put on hold few months back. You were helping me on that old one too. Here's a glimpse of that old unfinished project.
<i>
</i>&lt;?php

//'USER' Form.
$form_questions_labels = array('First Name','Middle Name','Surname','Gender','Age Range','Education','Personal Email','About Me','Home Country');
$form_questions_required_labels = array('First Name','Surname','Gender','Age Range','Personal Email','About Me','Home Country');

//TEXT BOX LABELS.
$form_questions_textboxes_labels = array('First Name','Middle Name','Surname','Education','Personal Email');
//TEXT AREA LABELS.
$form_questions_textareas_labels = array('About Me');
//RADIO BUTTON LABELS.
$form_questions_radio_buttons_labels = array('Gender');
//DROPDOWN LABELS.
$form_questions_dropdowns_labels = array('Age Range','Home Country');

//RADIO BUTTONS AND THEIR OPTIONS
//'Gender' Options
$form_radio_button_number_[0] = array('Male','Female');

//DROPDOWNS AND THEIR OPTIONS
//'Age Range' Options
$form_dropdown_number_[0] = array('Select here','Under 16','16-20','21-25','26-30','31-35','36-40','41-45','46-50','51-55','56-60','61-65','66-70','71-75','76-80','81-85','86-90','91-95','96-100','100+');
//'Home Country' Options
$form_dropdown_number_[1] = array('Select here','Australia','Canada','New Zealand','USA','UK');

//Creates the New/Edit Record Webform.
//Since this webform is used multiple times in this file, best to make a function that is easily reusable to call the webform.
function renderForm($first_name = '', $middle_name = '', $surname = '', $gender = '', $age_range = '', $education = '', $marital_status = '', $working_status = '', $profession = '', $work_title = '', $personal_email = '', $about_me = '', $home_area = '', $home_town = '', $home_neighbourhood = '', $home_council = '', $home_borough = '', $home_city = '', $home_county = '', $home_district = '', $home_region = '', $home_state = '', $home_country = '', $error = '', $id = '')
{
//'USER' Form.
$form_questions_labels = array('First Name','Middle Name','Surname','Gender','Age Range','Education','Personal Email','About Me','Home Country');
$form_questions_required_labels = array('First Name','Surname','Gender','Age Range','Personal Email','About Me','Home Country');

<i> </i>//TEXT BOX LABELS.
<i> </i>$form_questions_textboxes_labels = array('First Name','Middle Name','Surname','Education','Personal Email');
<i> </i>
<i> </i>//TEXT AREA LABELS.
<i> </i>$form_questions_textareas_labels = array('About Me');
<i> </i>
<i> </i>//RADIO BUTTON LABELS.
<i> </i>$form_questions_radio_buttons_labels = array('Gender');
<i> </i>
<i> </i>//DROPDOWN LABELS.
<i> </i>$form_questions_dropdowns_labels = array('Age Range','Home Country');

<i> </i>//RADIO BUTTONES AND THEIR OPTIONS
<i> </i>//'Gender' Options
<i> </i>$form_radio_button_number_[0] = array('Male','Female');

<i> </i>//DROPDOWNS AND THEIR OPTIONS
<i> </i>//'Age Range' Options
<i> </i>$form_dropdown_number_[0] = array('Select here','Under 16','16-20','21-25','26-30','31-35','36-40','41-45','46-50','51-55','56-60','61-65','66-70','71-75','76-80','81-85','86-90','91-95','96-100','100+');
<i> </i>//'Home Country' Options
<i> </i>$form_dropdown_number_[1] = array('Select here','Australia','Canada','New Zealand','USA','UK');
<i> </i>?&gt;
<i> </i>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;
<i> </i>&lt;html&gt;
<i> </i>&lt;head&gt;
<i> </i>&lt;title&gt;Form&lt;/title&gt;
<i> </i>&lt;/head&gt;
<i> </i>&lt;body&gt;
<i> </i>
<i> </i>&lt;div class="container"&gt;
<i> </i>&lt;form action="" method="post"&gt;
<i> </i>
<i> </i>&lt;?php
<i> </i>foreach($form_questions_labels as $value)
<i> </i>{
<i> </i> $required_current_count_match = 'False';
<i> </i> $textbox_current_count_match = 'False';
<i> </i> $textarea_current_count_match = 'False';
<i> </i> $radio_button_current_count_match = 'False';
<i> </i> $dropdown_current_count_match = 'False';
<i> </i>
<i> </i> $current_value_matched = 'False';
<i> </i>
<i> </i> //Check if current Question is a * REQUIRED Question or not.
<i> </i> //There are 7 required questions: $form_questions_required_labels = array().
<i> </i> $required_current_count='0'; $required_count_end = '7'; $required_current_count_match = 'False';
<i> </i> while($value != $form_questions_required_labels[$required_current_count]||$required_current_count != $required_count_end)
<i> </i> {
<i> </i> if($value == "$form_questions_required_labels[$required_current_count]")
<i> </i> {
<i> </i> $current_required_label_number = $required_current_count;
<i> </i> $required_current_count_match = 'True';
<i> </i> $required_current_count = $required_count_end;
<i> </i>
<i> </i> $current_value_matched = 'TRUE';
<i> </i> }
<i> </i> else
<i> </i> {
<i> </i> $required_current_count++;
<i> </i> }
<i> </i> }
<i> </i> if($current_value_matched != 'TRUE')
<i> </i> {
<i> </i> //Check if current Question's input type is a TextBox or not.
<i> </i> //There are 5 TextBox questions: $form_questions_textboxes_labels = array().
<i> </i> $textbox_current_count='0'; $textbox_count_end = '5'; $textbox_current_count_match = 'False';
<i> </i> while($textbox_current_count != $textbox_count_end)
<i> </i> {
<i> </i> if($value == "$form_questions_textboxes_labels[$textbox_current_count]")
<i> </i> {
<i> </i> $current_textbox_label_number = $textbox_current_count;
<i> </i> $textbox_current_count_match = 'True';
<i> </i> $textbox_current_count = $textbox_count_end;
<i> </i>
<i> </i> $current_value_matched = 'TRUE';
<i> </i> }
<i> </i> else
<i> </i> {
<i> </i> $textbox_current_count++;
<i> </i> }
<i> </i> }
<i> </i> }
<i> </i> if($current_value_matched != 'TRUE')
<i> </i> {
<i> </i> //Check if current Question's input type is a TextArea or not.
<i> </i> //There is 1 TextArea question: $form_questions_textareas_labels = array().
<i> </i> $textarea_current_count='0'; $textarea_count_end = '1'; $textarea_current_count_match = 'False';
<i> </i> while($textarea_current_count != $textarea_count_end)
<i> </i> {
<i> </i> if($value == "$form_questions_textareas_labels[$textarea_current_count]")
<i> </i> {
<i> </i> $current_textarea_label_number = $textarea_current_count;
<i> </i> $textarea_current_count_match = 'True';
<i> </i> $textarea_current_count = $textarea_count_end;
<i> </i>
<i> </i> $current_value_matched = 'TRUE';
<i> </i> }
<i> </i> else
<i> </i> {
<i> </i> $textarea_current_count++;
<i> </i> }
<i> </i> }
<i> </i> }
<i> </i> if($current_value_matched != 'TRUE')
<i> </i> {
<i> </i> //Check if current Question's input type is a RadioButton or not.
<i> </i> //There are 3 RadioButton questions: $form_questions_radio_buttons_labels = array().
<i> </i> $radio_button_current_count='0'; $radio_button_count_end = '1'; $radio_button_current_count_match = 'False';
<i> </i> while($radio_button_current_count != $radio_button_count_end)
<i> </i> {
<i> </i> if($value == "$form_questions_radio_buttons_labels[$radio_button_current_count]")
<i> </i> {
<i> </i> $current_radio_button_number = $radio_button_current_count;
<i> </i> $radio_button_current_count_match = 'True';
<i> </i> $radio_button_current_count = $radio_button_count_end;
<i> </i>
<i> </i> $current_value_matched = 'TRUE';
<i> </i> }
<i> </i> else
<i> </i> {
<i> </i> $radio_button_current_count++;
<i> </i> }
<i> </i> }
<i> </i> }
<i> </i> if($current_value_matched != 'TRUE')
<i> </i> {
<i> </i> //Check if current Question's input type is a DropDown or not.
<i> </i> //There are 3 DropDown questions: $form_questions_dropdowns_labels = array().
<i> </i> $dropdown_current_count='0'; $dropdown_count_end = '2'; $dropdown_current_count_match = 'False';
<i> </i> while($dropdown_current_count != $dropdown_count_end)
<i> </i> {
<i> </i> if($value == "$form_questions_dropdowns_labels[$dropdown_current_count]")
<i> </i> {
<i> </i> $current_drop_down_number = $dropdown_current_count;
<i> </i> $dropdown_current_count_match = 'True';
<i> </i> $dropdown_current_count = $dropdown_count_end;
<i> </i>
<i> </i> $current_value_matched = 'TRUE';
<i> </i> }
<i> </i> else
<i> </i> {
<i> </i> $dropdown_current_count++;
<i> </i> }
<i> </i> }
<i> </i> }
<i> </i>
<i> </i> //If current Question's input type is NOT a REQUIRED one and is a TextBox.
<i> </i> if($required_current_count_match == 'False' &amp;&amp; $textbox_current_count_match == 'True')
<i> </i> {
<i> </i> //Need to replace 'Spaces' with '_'. And need to replace Upper Cases with Lower Cases as the 'name=" attributes are in lower cases with Under Scores instead of Spaces. Eg. Form Question Label 'Marital Status" is in form input field attribute "name='marital_status' or "id"='marital_status'".
<i> </i> $value_1 = str_replace(" ","_","$value"); //Replaced Spaces to Underscores.
<i> </i> $value_2 = strtolower($value_1); //Replaced Upper Cases to Lower Cases.
<i> </i> ?&gt;
<i> </i> &lt;div class="row"&gt;
<i> </i> &lt;div class="col-25"&gt;
<i> </i> &lt;label for="&lt;?php echo $value_2;?&gt;"&gt;&lt;b&gt;&lt;?php echo $value;?&gt;: &lt;/b&gt;&lt;/label&gt;
<i> </i> &lt;/div&gt;
<i> </i> &lt;div class="col-75"&gt;
<i> </i> &lt;input type="text" name="&lt;?php echo $value_2;?&gt;" value="&lt;?php echo ${$value_2}; ?&gt;"/&gt;
<i> </i> &lt;/div&gt;
<i> </i> &lt;/div&gt;
<i> </i> &lt;?php
<i> </i> }
<i> </i> //If current Question's input type is NOT a REQUIRED one and is a TextArea.
<i> </i> elseif($required_current_count_match == 'False' &amp;&amp; $textarea_current_count_match == 'True')
<i> </i> {
<i> </i> //Need to replace 'Spaces' with '_'. And need to replace Upper Cases with Lower Cases as the 'name=" attributes are in lower cases with Under Scores instead of Spaces. Eg. Form Question Label 'Marital Status" is in form input field attribute "name='marital_status' or "id"='marital_status'".
<i> </i> $value_1 = str_replace(" ","_","$value"); //Replaced Spaces to Underscores.
<i> </i> $value_2 = strtolower($value_1); //Replaced Upper Cases to Lower Cases.
<i> </i> ?&gt;
<i> </i> &lt;div class="row"&gt;
<i> </i> &lt;div class="col-25"&gt;
<i> </i> &lt;label for="&lt;?php echo $value_2;?&gt;"&gt;&lt;b&gt;&lt;?php echo $value;?&gt;: &lt;/b&gt;&lt;/label&gt;
<i> </i> &lt;/div&gt;
<i> </i> &lt;div class="col-75"&gt;
<i> </i> &lt;textarea name="&lt;?php echo $value_2;?&gt;" rows="10" cols="30"&gt;
<i> </i> &lt;?php
<i> </i> echo "Write about yourself here.";
<i> </i> ?&gt;
<i> </i> &lt;/textarea&gt;
<i> </i> &lt;/div&gt;
<i> </i> &lt;/div&gt;
<i> </i> &lt;?php
<i> </i> }
<i> </i> //If current Question's input type is NOT a REQUIRED one and is a RadioButton.
<i> </i> elseif($required_current_count_match == 'False' &amp;&amp; $radio_button_current_count_match == 'True')
<i> </i> {
<i> </i> //Need to replace 'Spaces' with '_'. And need to replace Upper Cases with Lower Cases as the 'name=" attributes are in lower cases with Under Scores instead of Spaces. Eg. Form Question Label 'Marital Status" is in form input field attribute "name='marital_status' or "id"='marital_status'".
<i> </i> $value_1 = str_replace(" ","_","$value"); //Replaced Spaces to Underscores.
<i> </i> $value_2 = strtolower($value_1); //Replaced Upper Cases to Lower Cases.
<i> </i> ?&gt;
<i> </i> &lt;div class="row"&gt;
<i> </i> &lt;div class="col-25"&gt;
<i> </i> &lt;label for="&lt;?php echo $value_2;?&gt;"&gt;&lt;b&gt;&lt;?php echo $value;?&gt;: &lt;/b&gt;&lt;/label&gt;
<i> </i> &lt;/div&gt;
<i> </i> &lt;div class="col-75"&gt;
<i> </i> &lt;?php
<i> </i> foreach($form_radio_button_number_[$current_radio_button_number] as $value_3)
<i> </i> {
<i> </i> if($id != '' &amp;&amp; $value_3 == ${$value_2})
<i> </i> {
<i> </i> //Even &lt;?php echo "$value_3" is fine as both "$value_3" and "${$value_2}" hold same value. ${$value_2} is from database."
<i> </i> ?&gt;
<i> </i> &lt;input type="radio" name="&lt;?php echo $value_2;?&gt;" value="&lt;?php echo ${$value_2};?&gt;" checked&gt;&lt;?php echo ${$value_2};?&gt;
<i> </i> &lt;?php
<i> </i> }
<i> </i> else
<i> </i> {
<i> </i> ?&gt;
<i> </i> &lt;input type="radio" name="&lt;?php echo $value_2;?&gt;" value="&lt;?php echo $value_3;?&gt;"&gt;&lt;?php echo $value_3;?&gt;
<i> </i> &lt;?php
<i> </i> }
<i> </i> }
<i> </i> ?&gt;
<i> </i> &lt;/div&gt;
<i> </i> &lt;/div&gt;
<i> </i> &lt;?php
<i> </i> }
<i> </i> //If current Question's input type is NOT a REQUIRED one and is a DropDown.
<i> </i> elseif($required_current_count_match == 'False' &amp;&amp; $dropdown_current_count_match == 'True')
<i> </i> {
<i> </i> //Need to replace 'Spaces' with '_'. And need to replace Upper Cases with Lower Cases as the 'name=" attributes are in lower cases with Under Scores instead of Spaces. Eg. Form Question Label 'Marital Status" is in form input field attribute "name='marital_status' or "id"='marital_status'".
<i> </i> $value_1 = str_replace(" ","_","$value"); //Replaced Spaces to Underscores.
<i> </i> $value_2 = strtolower($value_1); //Replaced Upper Cases to Lower Cases.
<i> </i> ?&gt;
<i> </i> &lt;div class="row"&gt;
<i> </i> &lt;div class="col-25"&gt;
<i> </i> &lt;label for="&lt;?php echo $value_2;?&gt;"&gt;&lt;b&gt;&lt;?php echo $value;?&gt;: &lt;/b&gt;&lt;/label&gt;
<i> </i> &lt;/div&gt;
<i> </i> &lt;div class="col-75"&gt;
<i> </i> &lt;select id="&lt;?php echo $value_2;?&gt;" name="&lt;?php echo $value_2;?&gt;"&gt;
<i> </i> &lt;?php
<i> </i> foreach($form_dropdown_number_[$current_drop_down_number] as $value_3)
<i> </i> {
<i> </i> if($id != '' &amp;&amp; $value_3 == ${$value_2})
<i> </i> {
<i> </i> //Even &lt;?php echo "$value_3" is fine as both "$value_3" and "${$value_2}" hold same value. ${$value_2} is from database."
<i> </i> ?&gt;
<i> </i> &lt;option value="&lt;?php echo ${$value_2};?&gt;" selected&gt;&lt;?php echo ${$value_2};?&gt;&lt;/option&gt;
<i> </i> &lt;?php
<i> </i> }
<i> </i> else
<i> </i> {
<i> </i> ?&gt;
<i> </i> &lt;option value="&lt;?php echo $value_3;?&gt;"&gt;&lt;?php echo $value_3;?&gt;&lt;/option&gt;
<i> </i> &lt;?php
<i> </i> }
<i> </i> }
<i> </i> ?&gt;
<i> </i> &lt;/select&gt;
<i> </i> &lt;/div&gt;
<i> </i> &lt;/div&gt;
<i> </i> &lt;?php
<i> </i> }
<i> </i> //If current Question's input type is a REQUIRED one and is a TextBox.
<i> </i> if($required_current_count_match == 'True' &amp;&amp; $textbox_current_count_match == 'True')
<i> </i> {
<i> </i> //Need to replace 'Spaces' with '_'. And need to replace Upper Cases with Lower Cases as the 'name=" attributes are in lower cases with Under Scores instead of Spaces. Eg. Form Question Label 'Marital Status" is in form input field attribute "name='marital_status' or "id"='marital_status'".
<i> </i> $value_1 = str_replace(" ","_","$value"); //Replaced Spaces to Underscores.
<i> </i> $value_2 = strtolower($value_1); //Replaced Upper Cases to Lower Cases.
<i> </i> ?&gt;
<i> </i> &lt;div class="row"&gt;
<i> </i> &lt;div class="col-25"&gt;
<i> </i> &lt;label for="&lt;?php echo $value_2;?&gt;"&gt;&lt;b&gt;&lt;?php echo $value;?&gt;: *&lt;/b&gt;&lt;/label&gt;
<i> </i> &lt;/div&gt;
<i> </i> &lt;div class="col-75"&gt;
<i> </i> &lt;input type="text" name="&lt;?php echo $value_2;?&gt;" value="&lt;?php echo ${$value_2}; ?&gt;"/&gt;
<i> </i> &lt;/div&gt;
<i> </i> &lt;/div&gt;
<i> </i> &lt;?php
<i> </i> }
<i> </i> //If current Question's input type is a REQUIRED one and is a TextArea.
<i> </i> elseif($required_current_count_match == 'True' &amp;&amp; $textarea_current_count_match == 'True')
<i> </i> {
<i> </i> //Need to replace 'Spaces' with '_'. And need to replace Upper Cases with Lower Cases as the 'name=" attributes are in lower cases with Under Scores instead of Spaces. Eg. Form Question Label 'Marital Status" is in form input field attribute "name='marital_status' or "id"='marital_status'".
<i> </i> $value_1 = str_replace(" ","_","$value"); //Replaced Spaces to Underscores.
<i> </i> $value_2 = strtolower($value_1); //Replaced Upper Cases to Lower Cases.
<i> </i> ?&gt;
<i> </i> &lt;div class="row"&gt;
<i> </i> &lt;div class="col-25"&gt;
<i> </i> &lt;label for="&lt;?php echo $value_2;?&gt;"&gt;&lt;b&gt;&lt;?php echo $value;?&gt;: *&lt;/b&gt;&lt;/label&gt;
<i> </i> &lt;/div&gt;
<i> </i> &lt;div class="col-75"&gt;
<i> </i> &lt;textarea name="&lt;?php echo $value_2;?&gt;" rows="10" cols="30"&gt;
<i> </i> &lt;?php
<i> </i> if ($id != '') //This $id is 'post id' &amp; 'user id'.
<i> </i> {
<i> </i> echo ${$value_2};
<i> </i> }
<i> </i> else
<i> </i> {
<i> </i> echo "Write about yourself here.";
<i> </i> }
<i> </i> ?&gt;
<i> </i> &lt;/textarea&gt;
<i> </i> &lt;/div&gt;
<i> </i> &lt;/div&gt;
<i> </i> &lt;?php
<i> </i> }
<i> </i> //If current Question's input type is a REQUIRED one and is a RadioButton.
<i> </i> elseif($required_current_count_match == 'True' &amp;&amp; $radio_button_current_count_match == 'True')
<i> </i> {
<i> </i> //Need to replace 'Spaces' with '_'. And need to replace Upper Cases with Lower Cases as the 'name=" attributes are in lower cases with Under Scores instead of Spaces. Eg. Form Question Label 'Marital Status" is in form input field attribute "name='marital_status' or "id"='marital_status'".
<i> </i> $value_1 = str_replace(" ","_","$value"); //Replaced Spaces to Underscores.
<i> </i> $value_2 = strtolower($value_1); //Replaced Upper Cases to Lower Cases.
<i> </i> ?&gt;
<i> </i> &lt;div class="row"&gt;
<i> </i> &lt;div class="col-25"&gt;
<i> </i> &lt;label for="&lt;?php echo $value_2;?&gt;"&gt;&lt;b&gt;&lt;?php echo $value;?&gt;: *&lt;/b&gt;&lt;/label&gt;
<i> </i> &lt;/div&gt;
<i> </i> &lt;div class="col-75"&gt;
<i> </i> &lt;?php
<i> </i> foreach($form_radio_button_number_[$current_radio_button_number] as $value_3)
<i> </i> {
<i> </i> if($id != '' &amp;&amp; $value_3 == ${$value_2})
<i> </i> {
<i> </i> //Even &lt;?php echo "$value_3" is fine as both "$value_3" and "${$value_2}" hold same value. ${$value_2} is from database."
<i> </i> ?&gt;
<i> </i> &lt;input type="radio" name="&lt;?php echo $value_2;?&gt;" value="&lt;?php echo ${$value_2};?&gt;" checked&gt;&lt;?php echo ${$value_2};?&gt;
<i> </i> &lt;?php
<i> </i> }
<i> </i> else
<i> </i> {
<i> </i> ?&gt;
<i> </i> &lt;input type="radio" name="&lt;?php echo $value_2;?&gt;" value="&lt;?php echo $value_3;?&gt;"&gt;&lt;?php echo $value_3;?&gt;
<i> </i> &lt;?php
<i> </i> }
<i> </i> }
<i> </i> ?&gt;
<i> </i> &lt;/div&gt;
<i> </i> &lt;/div&gt;
<i> </i> &lt;?php
<i> </i> }
<i> </i> //If current Question's input type is a REQUIRED one and is a DropDown.
<i> </i> elseif($required_current_count_match == 'True' &amp;&amp; $dropdown_current_count_match == 'True')
<i> </i> {
<i> </i> //Need to replace 'Spaces' with '_'. And need to replace Upper Cases with Lower Cases as the 'name=" attributes are in lower cases with Under Scores instead of Spaces. Eg. Form Question Label 'Marital Status" is in form input field attribute "name='marital_status' or "id"='marital_status'".
<i> </i> $value_1 = str_replace(" ","_","$value"); //Replaced Spaces to Underscores.
<i> </i> $value_2 = strtolower($value_1); //Replaced Upper Cases to Lower Cases.
<i> </i> ?&gt;
<i> </i> &lt;div class="row"&gt;
<i> </i> &lt;div class="col-25"&gt;
<i> </i> &lt;label for="&lt;?php echo $value_2;?&gt;"&gt;&lt;b&gt;&lt;?php echo $value;?&gt;: *&lt;/b&gt;&lt;/label&gt;
<i> </i> &lt;/div&gt;
<i> </i> &lt;div class="col-75"&gt;
<i> </i> &lt;select id="&lt;?php echo $value_2;?&gt;" name="&lt;?php echo $value_2;?&gt;"&gt;
<i> </i> &lt;?php
<i> </i> foreach($form_dropdown_number_[$current_drop_down_number] as $value_3)
<i> </i> {
<i> </i> if($id != '' &amp;&amp; $value_3 == ${$value_2})
<i> </i> {
<i> </i> //Even &lt;?php echo "$value_3" is fine as both "$value_3" and "${$value_2}" hold same value. ${$value_2} is from database."
<i> </i> ?&gt;
<i> </i> &lt;option value="&lt;?php echo ${$value_2};?&gt;" selected&gt;&lt;?php echo ${$value_2};?&gt;&lt;/option&gt;
<i> </i> &lt;?php
<i> </i> }
<i> </i> else
<i> </i> {
<i> </i> ?&gt;
<i> </i> &lt;option value="&lt;?php echo $value_3;?&gt;"&gt;&lt;?php echo $value_3;?&gt;&lt;/option&gt;
<i> </i> &lt;?php
<i> </i> }
<i> </i> }
<i> </i> ?&gt;
<i> </i> &lt;/select&gt;
<i> </i> &lt;/div&gt;
<i> </i> &lt;/div&gt;
<i> </i> &lt;?php
<i> </i> }
<i> </i>}
<i> </i>?&gt;
<i> </i>&lt;input type="submit" name="submit" value="Submit" /&gt;
<i> </i>&lt;/form&gt;
<i> </i>&lt;/div&gt;

<i> </i>&lt;/body&gt;
<i> </i>&lt;/html&gt;
&lt;?php
}
?&gt;


Here is the code you helped me on ... Talking about the old project that I put on hold.
<i>
</i>&lt;?php

/* CODES FROM NogDog:
https://www.webdeveloper.com/d/387048-which-of-these-is-not-equal-to-operator/15
https://www.webdeveloper.com/d/387053-fatal-error-cannot-use-isset-on-the-result-of-an-expression
*/

$form_questions = array(array('label' =&gt; 'First Name','required' =&gt; true,'type' =&gt; 'text','name' =&gt; 'first_name'),
array('label' =&gt; 'About Me','required' =&gt; false,'type' =&gt; 'textarea','name' =&gt; 'about_me') // etc....);

foreach($form_questions as $question) {
echo "&lt;label for='{$question['name']}'&gt;{$question['label']}";
if($question['required']) {
echo "*";
}
echo "&lt;/label&gt;n";
// some if/else or switch/case statement here based on $question['type']
// to control what form element type gets output...
}

?&gt;

&lt;?php
if(isset($form_questions_required_labels[$required_current_count] &amp;&amp; $required_current_count &amp;&amp; $required_count_end))
if(isset($form_questions_required_labels[$required_current_count]) &amp;&amp; $required_current_count &amp;&amp; $required_count_end)
?&gt;

Again, this file is not complete yet.

Just fiddling around with php and playing around. That is all.
Copy linkTweet thisAlerts:
@developer_webauthorMay 02.2020 — NogDog,

Actually, you deserve a better explanation.

Since I got your curiosity flared then I might aswell get your interest flared. That way, I get the most out of you and your other mod colleagues can join in with me when you get tired and prefer to take a break and help other forum members.

This is what I am doing in this current project which is similar to the old project mentioned in my previous post and so forget my previous post and it's code and concentrate on this post's code ...

I will create an admin panel say, admin_panel.php. Again intend to use the "create everything out of 1 item" approach as much as possible.

On the Admin Panel, you will design the Signup & Login form using blocktext input field.

Let us imagine we are designing the Signup page signup.php right now.

Note, only one input field here and it a blocktext. No text field to ask for names, no radio buttons to ask for genders (male,female options), no checkbox to ask if users agree to TOS or not (yes, no options), nothing else. Just the blocktext. Just Adam. No Eve. No Satan. ;)

So now you are at admin_panel.php and it shows you a single blocktext on the page.

Blocktext would look something like this:
<i>
</i>&lt;label for="form_question_labels"&gt;Form Question Labels:&lt;/label&gt;
&lt;textarea&gt;
&lt;/textarea&gt;

So, in the blocktext, you will input the form labels. Something like this:

First Name

Middle Name

Surname

Gender

Age range

TOS

Note: TOS = Terms of Service.

We will later on create form items based on these inputs.

Now, when you click the SUBMIT button. You complete section 1.

Then, another page would load on your screen showing you section 2.

Or better, the same page would load. So 1st section url will be this:

admin_panel.php?step=1

And 2nd section url will be this:

admin_panel.php?step=2

I will create conditions so the php checks the url to understand which "section" to load on screen.

step_1()

step_2()

And so on.

Ofcourse, I will add $_SESSIONS['steps_complete'] too so if the admin does not fill-in section 1 and tries filling in section 2 by directly going to 'admin_panel.php?step=2' and bypassing 'admin_panel.php?step=1' then he would be redirected to section 1.

Meaning if he initially visits:

admin_panel.php?step=2

then the script will check if this is TRUE or not:

$_
SESSIONS['steps_complete'] = '1';

if not, then Admin will be redirected to

admin_panel.php?step=1.

Don't worry about these small things. I understand these basics of "security" (even though I am a beginner programmer) so Admin doesn't jump steps and build an incomplete form.

Now on step 2, again you will see a single blocktext. Something like this:
<i>
</i>&lt;label for="form_question_labels_required"&gt;Form Question Labels Required:&lt;/label&gt;
&lt;textarea&gt;
&lt;?php echo $form_questions_labels;?&gt;
&lt;/textarea&gt;


Here, you just delete the non-required question labels. Click SUBMIT. Move-onto step 3:

admin_panel.php?step=3.

Step 3 would generate input fields based on your Form Question Labels. Page will show you something like this for you to select the input types of each of your Form Question Label ...
<i>
</i>&lt;label for="first_name_input_type":&lt;/label&gt;
&lt;select id="first_name_input_type" name="first_name_input_type"&gt;
&lt;option value="text_field"&gt;Text Field&lt;/option&gt;
&lt;option value="block_text"&gt;BlockText&lt;/option&gt;
&lt;option value="radio_button"&gt;Radio Button&lt;/option&gt;
&lt;option value="drop_down"&gt;Drop Down&lt;/option&gt;
&lt;/select&gt;
&lt;label for="gender_input_type":&lt;/label&gt;
&lt;select id="gender_input_type" name="gender_input_type"&gt;
&lt;option value="text_field"&gt;Text Field&lt;/option&gt;
&lt;option value="block_text"&gt;BlockText&lt;/option&gt;
&lt;option value="radio_button"&gt;Radio Button&lt;/option&gt;
&lt;option value="drop_down"&gt;Drop Down&lt;/option&gt;
&lt;/select&gt;
&lt;label for="tos_input_type":&lt;/label&gt;
&lt;select id="tos_input_type" name="tos_input_type"&gt;
&lt;option value="text_field"&gt;Text Field&lt;/option&gt;
&lt;option value="block_text"&gt;BlockText&lt;/option&gt;
&lt;option value="radio_button"&gt;Radio Button&lt;/option&gt;
&lt;option value="drop_down"&gt;Drop Down&lt;/option&gt;
&lt;/select&gt;


Now imagine you selected "text field" for "First Name", "Middle Name", "Surname".

And selected "radio button" for "Gender".

And selected "dropdown" for "TOS".

Then when you click the SUBMIT button, php will partially generate the signup.php. That page will hold arrays something like this for the time being until further inputs by the Admin during form designing process ....

<i>
</i>$form_questions_labels[]='First Name';
$form_questions_labels[]='Middle Name';
$form_questions_labels[]='Surname';
$form_questions_labels[]='Gender';
$form_questions_labels[]='Age_Range';
$form_questions_labels[]='Tos';

$form_questions_labels_required[]='First Name';
$form_questions_labels_required[]='Surname';
$form_questions_labels_required[]='Gender';
$form_questions_labels_required[]='Age_Range';
$form_questions_labels_required[]='Tos';

$text_fields_labels = array('First Name','Middle Name','Surname');

$radio_buttons_labels = array('Gender');
$dropdowns_labels = array('Tos');


Can you see, the arrays in the signup.php has differentiated the form input types ?

It has assigned the input type to each Form Question Label.

At this point, the signup.php just holds these arrays. No other php code as of yet. More code will be generated in this page based on Admin inputs in admin_panel.php in the upcoming steps.

Note at this point, the radio button labels been generated but not their options. In our example, a radio button has been assigned the label (Question Label) "Gender" but Admin has not been prompted for the options yet. This will come later.

Also note at this point, the dropdown labels been generated but not their options. In our example, a dropdown has been assigned the label (Question Label) "Tos" but Admin has not been prompted for the options yet. This will come later.

(NOTE: At this step, mysql tbl "users" would be created with cols matching your Form Question Labels. Form questions that you Admin created).

On Step 4, admin_panel.php will check the radio buttons array and note the array values ...

<i>
</i>$radio_buttons_labels = array('Gender');


And it will check for dropdowns array and note the array values ...

<i>
</i>$dropdowns_labels = array('Tos');


In this case, it finds "Gender" in the radio_buttons array and will now understand these are radio button questions (labels) and it must prompt for their options. Likewise for the dropdown array.

And so, it will now show you Admin something like this in the admin_panel.php?step =4:

Step 4 will show Admin something like this ...
<i>
</i>&lt;label for="radio_button_gender_options"&gt;Radio Button Gender Options:&lt;/label&gt;
&lt;textarea&gt;
&lt;/textarea&gt;
&lt;br&gt;
&lt;br&gt;
&lt;label for="dropdown_tos_options"&gt;Dropdown Tos Options:&lt;/label&gt;
&lt;textarea&gt;
&lt;/textarea&gt;


Let us say, in the blocktext representing the radio button, you input:

Male

Female

Let us say, in the blocktext representing the dropdown, you input:

Yes

No

And clicked the SUBMIT button.

It will add the following on the signup.php:
<i>
</i>$radio_button_gender_options[]='Male';
$radio_button_gender_options[]='Female';


or this:

<i>
</i>$radio_button_gender_options = array('Male','Female');


And it will add this:
<i>
</i>$dropdown_options = array('Yes','No');



At this stage, further code will be added to signup.php. Something like this ...
<i>
</i>&lt;?php
foreach($form_questions_labels as $form_question_label) //Loop through the whole 'Form Questions' array.
{
//Check if current 'Form Item' is a 'Text Field' or not.
if(in_array("$form_question_label",$text_fields_labels)) //Current 'Form Item' proved to be a 'Text Field'.
{
//Check if current 'Form Item' (Text Field) is a 'required' one or not.
if(in_array("$form_question_label",$form_questions_labels_required))//Current 'Form Item' (Text Field) proved to be a 'required' one.
{
//Added '*' (asterisk) to indicate the 'Text Field' is a 'required' one.
echo "&lt;label for="$form_question_label"&gt;$form_question_label *:&lt;/label&gt;
&lt;input type="text" name="$form_question_label" placeholder="$form_question_label"&gt;&lt;br&gt;";
}
else
{
//Added no '*' (asterisk) to indicate the 'Text Field' is NOT a 'required' one.
echo "&lt;label for="$form_question_label"&gt;$form_question_label:&lt;/label&gt;
&lt;input type="text" name="$form_question_label" placeholder="$form_question_label"&gt;&lt;br&gt;";
}
}

As you can see, php loops through the $form_questions_labels array using the foreach loop.

It then loops through the $text_field_labels array using the foreach loop to see whether the item is a text field or not. If it is then it loops through the $form_questions_labels_required array. if it finds a match then it assumes the text field here is a required one. Else not.

Let's say, during the foreach loop inside the "text_field_labels" array it finds no match for the $form_question_label current item, then it will move-onto the "radio_button_labels" array. If it finds a match then it will assume the current form item from the $form_question_label is a radio button and then it will check the current form item's label against the $form_questions_required array again (like last time). If it finds a match then it assumes the radio button is a required one. Else not.

However, while foreach looping through the $radio_button_labels array, it finds no match then it will move onto the $dropdown_labels array.

And so on.

You get the picture.

Now remember, based on the Admin's inputs on the admin_panel.php, the arrays of each type of form input items get created ? Remember at first this got generated in signup.php ?

<i>
</i>$radio_buttons_labels = array('Gender','Tos');


And then on the next step, Admin was prompted for the "Gender" radio button's options ?

And based on his input the following array got created ...

<i>
</i>$radio_button_gender_options[]='Male';
$radio_button_gender_options[]='Female';


Now, I need php to loop through the "$radio_button_labels() and grab the values.

And then foreach loop through the array named after the values.

I mean, it should first foreach loop through this one:
<i>
</i>$radio_buttons_labels = array('Gender','Tos');

And grab the values "Gender" and "Tos".

And then it should foreach loop through the "gender" array and then the "tos" array.

Note, the "Gender" gets converted to small letters first to "gender" before searching for the array.

that means, php should not search for this array:
<i>
</i>$radio_buttons_Gender_Options;


But search for this one:

<i>
</i>$radio_buttons_gender_Options;


The array values should be converted by replacing CAPITALS with small letters and replacing spaces with underscores. When you see codes like this in my following codes then understand I am doing the string conversions here for the "label for=" and "name=" values. Ok ?
<i>
</i>$value = $form_question_label;
$value_1 = str_replace("","_","$value"); //Replaced Spaces to Underscores.
$value_2 = strtolower("$value_1"); //Replaced Upper Case to Lower Case.


Look here for example ...
<i>
</i>//Check if current 'Form Item' is a 'Radio Button' or not.
if(in_array("$form_question_label",$radio_buttons_labels)) //Current 'Form Item' proved to be a 'Radio Button'.
{
//Check if current 'Form Item' (Radio Button) is a 'required' one or not.
if(in_array("$form_question_label",$form_questions_labels_required))//Current 'Form Item' (Radio Button) proved to be a 'required' one.
{
//Added '*' (asterisk) to indicate the 'Radio Button' is a 'required' one.
echo "&lt;label for="$form_question_label"&gt;$form_question_label *:&lt;/label&gt;";
}
else
{
//Added no '*' (asterisk) to indicate the 'Radio Button' is NOT a 'required' one.
echo "&lt;label for="$form_question_label"&gt;$form_question_label:&lt;/label&gt;";
}

<i> </i> $value = $form_question_label;
<i> </i> $value_1 = str_replace("","_","$value"); //Replaced Spaces to Underscores.
<i> </i> $value_2 = strtolower("$value_1"); //Replaced Upper Case to Lower Case.


Remember, we need to convert as we can't have this ...
<i>
</i>&lt;input type="radio" id="Gender" name="Gender" value="Gender"&gt;
&lt;label_for="$current_radio_button_option"&gt;Gender&lt;/label&gt;&lt;br&gt;";


We need output like this ...
<i>
</i>&lt;input type="radio" id="gender" name="gender" value="gender"&gt;
&lt;label_for="$current_radio_button_option"&gt;Gender&lt;/label&gt;&lt;br&gt;";


Note the "name=" and "id=". Note the "value=". Latter can contain CAPITALS and spaces.

Now NogDog,

Ypu want to know what all the fuss is about .....
<i>
</i>$current_radio_button_options = "$" . "radio_button_" . "$value_2" . "_options";
$current_radio_button_option = "$" . "radio_button_" . "$value_2" . "_option";


Let me explain.

Remember we had this array listing all the radio button question labels but not the radio button options labels ?
<i>
</i>$radio_buttons_labels = array('Gender','Tos');

Remember, from $radio_buttons_labels values we had to create arrays naming the arrays after the values ?

We had to create these ...
<i>
</i>//$radio_button_gender_options = array('Male','Female');
$radio_button_gender_options[]='Male';
$radio_button_gender_options[]='Female';

$radio_button_tos_options = array('Yes','No');


Remember, we create more arrays from the values of earlier arrays and we do this by using the foreach loop.

I tried doing the same for the radio button options but I get error.

This doesn't work:
<i>
</i>&lt;?php
foreach($form_questions_labels as $form_question_label) //Loop through the whole 'Form Questions' array.
{
//Check if current 'Form Item' is a 'Text Field' or not.
if(in_array("$form_question_label",$text_fields_labels)) //Current 'Form Item' proved to be a 'Text Field'.
{
//Check if current 'Form Item' (Text Field) is a 'required' one or not.
if(in_array("$form_question_label",$form_questions_labels_required))//Current 'Form Item' (Text Field) proved to be a 'required' one.
{
//Added '*' (asterisk) to indicate the 'Text Field' is a 'required' one.
echo "&lt;label for="$form_question_label"&gt;$form_question_label *:&lt;/label&gt;
&lt;input type="text" name="$form_question_label" placeholder="$form_question_label"&gt;&lt;br&gt;";
}
else
{
//Added no '*' (asterisk) to indicate the 'Text Field' is NOT a 'required' one.
echo "&lt;label for="$form_question_label"&gt;$form_question_label:&lt;/label&gt;
&lt;input type="text" name="$form_question_label" placeholder="$form_question_label"&gt;&lt;br&gt;";
}
}
//Check if current 'Form Item' is a 'Radio Button' or not.
if(in_array("$form_question_label",$radio_buttons_labels)) //Current 'Form Item' proved to be a 'Radio Button'.
{
//Check if current 'Form Item' (Radio Button) is a 'required' one or not.
if(in_array("$form_question_label",$form_questions_labels_required))//Current 'Form Item' (Radio Button) proved to be a 'required' one.
{
//Added '*' (asterisk) to indicate the 'Radio Button' is a 'required' one.
echo "&lt;label for="$form_question_label"&gt;$form_question_label *:&lt;/label&gt;";
}
else
{
//Added no '*' (asterisk) to indicate the 'Radio Button' is NOT a 'required' one.
echo "&lt;label for="$form_question_label"&gt;$form_question_label:&lt;/label&gt;";
}

<i> </i> $value = $form_question_label;
<i> </i> $value_1 = str_replace("","_","$value"); //Replaced Spaces to Underscores.
<i> </i> $value_2 = strtolower("$value_1"); //Replaced Upper Case to Lower Case.
<i> </i> foreach($radio_button_{$value_2}_options as $radio_button_{$value_2}_option) //Loop through the whole appropriate 'Radio Buttons' array.
<i> </i> {
<i> </i> echo "&lt;input type="radio" id="$current_radio_button_option" name="$value_2" value="$current_radio_button_option"&gt;
<i> </i> &lt;label_for="$current_radio_button_option"&gt;$current_radio_button_option&lt;/label&gt;&lt;br&gt;";
<i> </i> }
<i> </i>}
}


**Warning: Invalid argument supplied for foreach() in C:xampphtdocstestsearch_2.php on line**

Problem line is this:

<i>
</i>foreach($radio_button_{$value_2}_options as $radio_button_{$value_2}_option) //Loop through the whole appropriate 'Radio Buttons' array.


Anyway, let us switch that line to this and see what happens:

<i>
</i>foreach($radio_button_$value_2_options as $radio_button_{$value_2}_option) //Loop through the whole appropriate 'Radio Buttons' array.


**Parse error: syntax error, unexpected '$value_2_options' (T_VARIABLE) in C:xampphtdocstestsearch_2.php on line**

No luck. Context was this ...
<i>
</i>&lt;?php
foreach($form_questions_labels as $form_question_label) //Loop through the whole 'Form Questions' array.
{
//Check if current 'Form Item' is a 'Text Field' or not.
if(in_array("$form_question_label",$text_fields_labels)) //Current 'Form Item' proved to be a 'Text Field'.
{
//Check if current 'Form Item' (Text Field) is a 'required' one or not.
if(in_array("$form_question_label",$form_questions_labels_required))//Current 'Form Item' (Text Field) proved to be a 'required' one.
{
//Added '*' (asterisk) to indicate the 'Text Field' is a 'required' one.
echo "&lt;label for="$form_question_label"&gt;$form_question_label *:&lt;/label&gt;
&lt;input type="text" name="$form_question_label" placeholder="$form_question_label"&gt;&lt;br&gt;";
}
else
{
//Added no '*' (asterisk) to indicate the 'Text Field' is NOT a 'required' one.
echo "&lt;label for="$form_question_label"&gt;$form_question_label:&lt;/label&gt;
&lt;input type="text" name="$form_question_label" placeholder="$form_question_label"&gt;&lt;br&gt;";
}
}
//Check if current 'Form Item' is a 'Radio Button' or not.
if(in_array("$form_question_label",$radio_buttons_labels)) //Current 'Form Item' proved to be a 'Radio Button'.
{
//Check if current 'Form Item' (Radio Button) is a 'required' one or not.
if(in_array("$form_question_label",$form_questions_labels_required))//Current 'Form Item' (Radio Button) proved to be a 'required' one.
{
//Added '*' (asterisk) to indicate the 'Radio Button' is a 'required' one.
echo "&lt;label for="$form_question_label"&gt;$form_question_label *:&lt;/label&gt;";
}
else
{
//Added no '*' (asterisk) to indicate the 'Radio Button' is NOT a 'required' one.
echo "&lt;label for="$form_question_label"&gt;$form_question_label:&lt;/label&gt;";
}

<i> </i> $value = $form_question_label;
<i> </i> $value_1 = str_replace("","_","$value"); //Replaced Spaces to Underscores.
<i> </i> $value_2 = strtolower("$value_1"); //Replaced Upper Case to Lower Case.
<i> </i> foreach($radio_button_$value_2_options as $radio_button_{$value_2}_option) //Loop through the whole appropriate 'Radio Buttons' array.
<i> </i> {
<i> </i> echo "&lt;input type="radio" id="$current_radio_button_option" name="$value_2" value="$current_radio_button_option"&gt;
<i> </i> &lt;label_for="$current_radio_button_option"&gt;$current_radio_button_option&lt;/label&gt;&lt;br&gt;";
<i> </i> }
<i> </i>}
}



Let's try this now ...
<i>
</i>foreach($current_radio_button_options as $current_radio_button_option) //Loop through the whole appropriate 'Radio Buttons' array.


But that won't work unless we define $current_radio_button_options and $current_radio_button_option first.

So, let's try defining them first:
<i>
</i> //Check if current 'Form Item' is a 'Radio Button' or not.
if(in_array("$form_question_label",$radio_buttons_labels)) //Current 'Form Item' proved to be a 'Radio Button'.
{
//Check if current 'Form Item' (Radio Button) is a 'required' one or not.
if(in_array("$form_question_label",$form_questions_labels_required))//Current 'Form Item' (Radio Button) proved to be a 'required' one.
{
//Added '*' (asterisk) to indicate the 'Radio Button' is a 'required' one.
echo "&lt;label for="$form_question_label"&gt;$form_question_label *:&lt;/label&gt;";
}
else
{
//Added no '*' (asterisk) to indicate the 'Radio Button' is NOT a 'required' one.
echo "&lt;label for="$form_question_label"&gt;$form_question_label:&lt;/label&gt;";
}

<i> </i> $value = $form_question_label;
<i> </i> $value_1 = str_replace("","_","$value"); //Replaced Spaces to Underscores.
<i> </i> $value_2 = strtolower("$value_1"); //Replaced Upper Case to Lower Case.
<i> </i>
<i> </i> $current_radio_button_options = "$" . "radio_button_" . "$value_2" . "_options";
<i> </i> $current_radio_button_option = "$" . "radio_button_" . "$value_2" . "_option";
<i> </i>
<i> </i> /* THIS DIDN'T WORK
<i> </i> $current_radio_button_options = "$" . "radio_button_" . "{$value_2}" . "_options";
<i> </i> $current_radio_button_option = "$" . "radio_button_" . "{$value_2}" . "_option";
<i> </i> */
<i> </i>
foreach($current_radio_button_options as $current_radio_button_option) //Loop through the whole appropriate 'Radio Buttons' array.
{
echo "&lt;input type="radio" id="$current_radio_button_option" name="$value_2" value="$current_radio_button_option"&gt;
&lt;label_for="$current_radio_button_option"&gt;$current_radio_button_option&lt;/label&gt;&lt;br&gt;";
}
}
}


Warning: Invalid argument supplied for foreach() in C:xampphtdocstestsearch_2.php on line ...

Warning: Invalid argument supplied for foreach() in C:xampphtdocstestsearch_2.php on line ...

Nope! Ain't working. Let's switch this:
<i>
</i>$current_radio_button_options = "$" . "radio_button_" . "$value_2" . "_options";
$current_radio_button_option = "$" . "radio_button_" . "$value_2" . "_option";


to this:

<i>
</i>$current_radio_button_options = "$" . "radio_button_" . "{$value_2}" . "_options";
$current_radio_button_option = "$" . "radio_button_" . "{$value_2}" . "_option";


Nope! Same error:

Warning: Invalid argument supplied for foreach() in C:xampphtdocstestsearch_2.php on line ...

Warning: Invalid argument supplied for foreach() in C:xampphtdocstestsearch_2.php on line ...

Here is the full code NogDog:

<i>
</i>&lt;?php
$form_questions_labels[]='Title';
$form_questions_labels[]='First Name';
$form_questions_labels[]='Middle Name';
$form_questions_labels[]='Surname';
$form_questions_labels[]='Gender';
$form_questions_labels[]='Tos';

$form_questions_labels_required[]='Title';
$form_questions_labels_required[]='First Name';
$form_questions_labels_required[]='Surname';
$form_questions_labels_required[]='Gender';
$form_questions_labels_required[]='Tos';

$text_fields_labels = array('First Name','Middle Name','Surname');

$radio_buttons_labels = array('Gender','Tos');
//$radio_button_gender_options = array('Male','Female','Male To Female','Female To Male');
$radio_button_gender_options[]='Male';
$radio_button_gender_options[]='Female';
$drop_down_tos_options = array('Yes','No');
?&gt;
&lt;form action="" method="post" enctype="plain/text"&gt;

&lt;?php
foreach($form_questions_labels as $form_question_label) //Loop through the whole 'Form Questions' array.
{
//Check if current 'Form Item' is a 'Text Field' or not.
if(in_array("$form_question_label",$text_fields_labels)) //Current 'Form Item' proved to be a 'Text Field'.
{
//Check if current 'Form Item' (Text Field) is a 'required' one or not.
if(in_array("$form_question_label",$form_questions_labels_required))//Current 'Form Item' (Text Field) proved to be a 'required' one.
{
//Added '*' (asterisk) to indicate the 'Text Field' is a 'required' one.
echo "&lt;label for="$form_question_label"&gt;$form_question_label *:&lt;/label&gt;
&lt;input type="text" name="$form_question_label" placeholder="$form_question_label"&gt;&lt;br&gt;";
}
else
{
//Added no '*' (asterisk) to indicate the 'Text Field' is NOT a 'required' one.
echo "&lt;label for="$form_question_label"&gt;$form_question_label:&lt;/label&gt;
&lt;input type="text" name="$form_question_label" placeholder="$form_question_label"&gt;&lt;br&gt;";
}
}
//Check if current 'Form Item' is a 'Radio Button' or not.
if(in_array("$form_question_label",$radio_buttons_labels)) //Current 'Form Item' proved to be a 'Radio Button'.
{
//Check if current 'Form Item' (Radio Button) is a 'required' one or not.
if(in_array("$form_question_label",$form_questions_labels_required))//Current 'Form Item' (Radio Button) proved to be a 'required' one.
{
//Added '*' (asterisk) to indicate the 'Radio Button' is a 'required' one.
echo "&lt;label for="$form_question_label"&gt;$form_question_label *:&lt;/label&gt;";
}
else
{
//Added no '*' (asterisk) to indicate the 'Radio Button' is NOT a 'required' one.
echo "&lt;label for="$form_question_label"&gt;$form_question_label:&lt;/label&gt;";
}

<i> </i> $value = $form_question_label;
<i> </i> $value_1 = str_replace("","_","$value"); //Replaced Spaces to Underscores.
<i> </i> $value_2 = strtolower("$value_1"); //Replaced Upper Case to Lower Case.
<i> </i>
<i> </i> $current_radio_button_options = "$" . "radio_button_" . "$value_2" . "_options";
<i> </i> $current_radio_button_option = "$" . "radio_button_" . "$value_2" . "_option";
<i> </i>
<i> </i> /*
<i> </i> $current_radio_button_options = "$" . "radio_button_" . "{$value_2}" . "_options";
<i> </i> $current_radio_button_option = "$" . "radio_button_" . "{$value_2}" . "_option";
<i> </i> */
<i> </i> if($radio_button_gender_options = $current_radio_button_options)
<i> </i> {
<i> </i> echo "They are the same ARRAY";
<i> </i> }
<i> </i>
<i> </i> foreach($current_radio_button_options as $current_radio_button_option) //Loop through the whole appropriate 'Radio Buttons' array.
<i> </i> {
<i> </i> echo "&lt;input type="radio" id="$current_radio_button_option" name="$value_2" value="$current_radio_button_option"&gt;
<i> </i> &lt;label_for="$current_radio_button_option"&gt;$current_radio_button_option&lt;/label&gt;&lt;br&gt;";
<i> </i> }
<i> </i>}
}


NOTE: I haven't started working on admin_panel.php yet. Struggling with signup.php. As I don't want to create a signup page the traditional way. I need to create the form fields, their labels, their names (name='blah'), IDs (id='blah') radio button and drop down option values (value='') from array values using the foreach loop.

In short, from our example, the signup page should have these array lines only signifying the properties of the form items:
<i>
</i>$form_questions_labels[]='Title';
$form_questions_labels[]='First Name';
$form_questions_labels[]='Middle Name';
$form_questions_labels[]='Surname';
$form_questions_labels[]='Gender';
$form_questions_labels[]='Tos';

$form_questions_labels_required[]='Title';
$form_questions_labels_required[]='First Name';
$form_questions_labels_required[]='Surname';
$form_questions_labels_required[]='Gender';
$form_questions_labels_required[]='Tos';

$text_fields_labels = array('First Name','Middle Name','Surname');

$radio_buttons_labels = array('Gender','Tos');
//$radio_button_gender_options = array('Male','Female');
$radio_button_gender_options[]='Male';
$radio_button_gender_options[]='Female';
$dropdown_tos_options = array('Yes','No');



And php should generate the form and it's form fields and their properties based on the array values above.

NogDog,

I am having trouble from this point onwards:
<i>
</i>//Check if current 'Form Item' is a 'Radio Button' or not.
if(in_array("$form_question_label",$radio_buttons_labels)) //Current 'Form Item' proved to be a 'Radio Button'.
{
Copy linkTweet thisAlerts:
@developer_webauthorMay 04.2020 — @NogDog,

Did you get your answer to your last post's question ? :)
Copy linkTweet thisAlerts:
@developer_webauthorMay 06.2020 — I fixed the problem of this script on another thread:

https://www.webdeveloper.com/d/389834-notice-array-to-string-conversion-in/3

Check the last 2 threads over there.

This code fixed it.

Check it up on your xampp/lampp/wampp:
<i>
</i>&lt;!DOCTYPE HTML"&gt;
&lt;html&gt;

&lt;head&gt;
&lt;meta name="viewport" content="width-device=width, initial-scale=1"&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;?php
$form_questions_labels[]='Title';
$form_questions_labels[]='First Name';
$form_questions_labels[]='Middle Name';
$form_questions_labels[]='Surname';
$form_questions_labels[]='Gender';
$form_questions_labels[]='Age_Range';
$form_questions_labels[]='Marital_Status';
$form_questions_labels[]='Working_Status';
$form_questions_labels[]='Tos';

$form_questions_labels_required[]='Title';
$form_questions_labels_required[]='First Name';
$form_questions_labels_required[]='Surname';
$form_questions_labels_required[]='Gender';
$form_questions_labels_required[]='Age_Range';
$form_questions_labels_required[]='Marital_Status';
$form_questions_labels_required[]='Working_Status';
$form_questions_labels_required[]='Tos';

$text_fields_labels = array('First Name','Middle Name','Surname');
$radio_buttons_labels = array('Gender','Tos');
$drop_downs_labels = array('Marital Status','Working Status');
/*
$i=1;
$options_radio_button_[$i] = array('Male','Female','Male To Female','Female To Male');
$i=2;
$options_radio_button_[$i] = array('Yes','No');
*/

$i=1;
$options_drop_down_[$i] = array('Single','Married','Divorced','Widow');
$i=2;
$options_drop_down_[$i] = array('Selfemployed','Employed','Unemployed');

//Gender Options
$i=1;
$options_radio_button_[$i][]='Male';
$options_radio_button_[$i][]='Female';
$options_radio_button_[$i][]='Male To Female';
$options_radio_button_[$i][]='Female To Male';
$total_options_radio_button_[$i] = count($options_radio_button_[$i]);//4
echo "Line 55: $total_options_radio_button_[$i]&lt;br&gt;";
//Tos Options
$i=2;
$options_radio_button_[$i][]='Yes';
$options_radio_button_[$i][]='No';
$total_options_radio_button_[$i] = count($options_radio_button_[$i]);//2
echo "Line 61: $total_options_radio_button_[$i]&lt;br&gt;";
//Marital Status Options
$i=1;
$options_drop_down_[$i][]='Single';
$options_drop_down_[$i][]='Married';
$options_drop_down_[$i][]='Divorced';
$options_drop_down_[$i][]='Widow';
//Working Status Options
$i=2;
$options_drop_down_[$i][]='Selfemployed';
$options_drop_down_[$i][]='Employed';
$options_drop_down_[$i][]='Unemployed';

$total_form_questions_labels = 9;
$total_form_questions_labels_required = 8;
$total_text_fields_labels = 3;
$total_radio_buttons_labels = count($radio_buttons_labels);//2
echo "Line 78: $total_radio_buttons_labels&lt;br&gt;";

?&gt;
&lt;form action="" method="post" enctype="plain/text"&gt;

&lt;?php

foreach($form_questions_labels as $form_question_label) //Loop through the whole 'Form Questions' array.
{
$value = $form_question_label;
$value_1 = str_replace(" ","_","$value"); //Replaced Spaces to Underscores.
$value_2 = strtolower("$value_1"); //Replaced Upper Case to Lower Case.

<i> </i>//Check if current 'Form Item' is a 'Text Field' or not.
<i> </i>if(in_array("$form_question_label",$text_fields_labels)) //Current 'Form Item' proved to be a 'Text Field'.
<i> </i>{
<i> </i> //Check if current 'Form Item' (Text Field) is a 'required' one or not.
<i> </i> if(in_array("$form_question_label",$form_questions_labels_required))//Current 'Form Item' (Text Field) proved to be a 'required' one.
<i> </i> {
<i> </i> //Added '*' (asterisk) to indicate the 'Text Field' is a 'required' one.
<i> </i> echo "&lt;label for="$value_2"&gt;$form_question_label *:&lt;/label&gt;
<i> </i> &lt;input type="text" name="$value_2" placeholder="$form_question_label"&gt;&lt;br&gt;";
<i> </i> }
<i> </i> else
<i> </i> {
<i> </i> //Added no '*' (asterisk) to indicate the 'Text Field' is NOT a 'required' one.
<i> </i> echo "&lt;label for="$value_2"&gt;$form_question_label:&lt;/label&gt;
<i> </i> &lt;input type="text" name="$value_2" placeholder="$form_question_label"&gt;&lt;br&gt;";
<i> </i> }
<i> </i>}
<i> </i>//Check if current 'Form Item' is a 'Radio Button' or not.
<i> </i>if(in_array("$form_question_label",$radio_buttons_labels)) //Current 'Form Item' proved to be a 'Radio Button'.
<i> </i>{
<i> </i> //Check if current 'Form Item' (Radio Button) is a 'required' one or not.
<i> </i> if(in_array("$form_question_label",$form_questions_labels_required))//Current 'Form Item' (Radio Button) proved to be a 'required' one.
<i> </i> {
<i> </i> //Added '*' (asterisk) to indicate the 'Radio Button' is a 'required' one.
<i> </i> echo "&lt;label for="$value_2"&gt;$form_question_label *:&lt;/label&gt;&lt;br&gt;";
<i> </i> }
<i> </i> else
<i> </i> {
<i> </i> //Added no '*' (asterisk) to indicate the 'Radio Button' is NOT a 'required' one.
<i> </i> echo "&lt;label for="$value_2"&gt;$form_question_label:&lt;/label&gt;&lt;br&gt;";
<i> </i> }
<i> </i> /* I GET ARRAY TO STRING CONVERSION ERROR
<i> </i> for($i = 0; $i &lt;= $total_radio_buttons_labels; $i++)
<i> </i> {
<i> </i> if($form_question_label == $radio_buttons_labels[$i]) //eg. 'Gender'.
<i> </i> {
<i> </i> for ($ii = 1; $ii &lt;= $total_options_radio_button_[$ii]; $ii++)
<i> </i> {
<i> </i> echo "&lt;input type="radio" id="$options_radio_button_[$ii]" name="$value_2" value="$options_radio_button_[$ii]"&gt;
<i> </i> &lt;label_for="$options_radio_button_[$ii]"&gt;$options_radio_button_[$ii]&lt;/label&gt;&lt;br&gt;";
<i> </i> }
<i> </i> }
<i> </i> }
<i> </i> */
<i> </i> /* I GET ARRAY TO STRING CONVERSION ERROR
<i> </i> for($i = 0; $i &lt;= $total_radio_buttons_labels; $i++)
<i> </i> {
<i> </i> if($form_question_label == $radio_buttons_labels[$i]) //eg. 'Gender'.
<i> </i> {
<i> </i> for ($ii = 1; $ii &lt;= $total_options_radio_button_[$ii]; $ii++)
<i> </i> {
<i> </i> echo $options_radio_button_number = "$options_radio_button_[$ii]";
<i> </i> echo "&lt;input type="radio" id="$options_radio_button_number" name="$value_2" value="$options_radio_button_number"&gt;
<i> </i> &lt;label_for="$options_radio_button_number"&gt;$options_radio_button_number&lt;/label&gt;&lt;br&gt;";
<i> </i> }
<i> </i> }
<i> </i> }
<i> </i> */
<i> </i> //THIS WORKED OVER THE ABOVE TWO 'FOR LOOPS'.
<i> </i> $i = 0;
<i> </i> foreach($radio_buttons_labels as $radio_button_label) //$radio_buttons_labels = ('Gender','Tos');
<i> </i> {
<i> </i> if($form_question_label == $radio_button_label) //eg. 'Gender'.
<i> </i> {
<i> </i> $i++;
<i> </i> foreach($options_radio_button_[$i] as $option_radio_button_[$i])
<i> </i> {
<i> </i> echo "&lt;input type="radio" id="$option_radio_button_[$i]" name="$value_2" value="$option_radio_button_[$i]"&gt;
<i> </i> &lt;label_for="$option_radio_button_[$i]"&gt;$option_radio_button_[$i]&lt;/label&gt;&lt;br&gt;";
<i> </i> }
<i> </i> }
<i> </i> $i++;
<i> </i> }
<i> </i>}
}
?&gt;


NogDog, answer my previous posts and then close this ticket. But do answer as I need answers.

Thanks!
×

Success!

Help @developer_web spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...