Help with calling values with values inside it's name

Hi everybody, I need help with something I’m trying to do and I’m pretty sure I’m not actually calling this the right way, I will try my best to explain it but if you have any questions, please feel free to ask to make sure it’s clear. Thanks.

Basically, I’m trying to print a variable value coming out from 2 variables.

Let’s say the variable I’m trying to print is called $value5, quite simple, but $value5 is not defined, it comes from a form that has multiple dynamic checkbox fields ($value1, $value2, $value3, etc.), the number of checkbox can vary from one form to another and I can know, depending on the opened form, how many of them there are (Form A has 3 checkboxes, Form B has 5 checkboxes, etc.)

Once the form is submitted, I want to dynamically recreate the values, by knowing the number of checkboxes, I can do a little while loop and I need to recreate the variable in the loop so I can save them. Something like this;

$maxcheckboxes = 4;
$loop = 1;
while ($loop < $maxcheckboxes)
{
print("value$loop <br />");
$loop++;
}

That would output this;

value1
value2
value3
value4

But actually, that is not I want, I want the value of the checkbox to be printed when checked, if the value is always ‘item_checked’ when checked, and that I check 1, 2 and 4, I want this to be printed;

item_checked
item_checked

item_checked

So basically, I want this;

$value1 -> item_checked
$value2 -> item_checked
$value3 -> 
$value4 -> item_checked

And not this;

$value1 -> value1
$value2 -> value2
$value3 -> value3
$value4 -> value4

Counting up variable names is mostly a serious alarm signal, but this a perfect chance for an exercise on Arrays. So your form must look something like

<input name="value1" />
<input name="value2" />
<input name="value3" />

but you can define a loose list:

<input name="checkbox[]" />
<input name="checkbox[]" />
<input name="checkbox[]" />

but give it a better name, so you know in a year what these should mean. So PHP will create a plain array from that when you submit the form (and only checked values are submitted anyway), so just have a look at it yourself:

var_dump($_POST['checkbox']);

Thanks for the answer, but my main issue is that the system I’m building will save data onto a MySQL database and it doesn’t matter which value it has, what matters, it’s whenever it’s checked or not.

Let say I load somebody profile and that I want to check which formation he has and that there’s 10 formation total.

In my database, I have the person profile with 10 columns at the end (f1,f2,f3,f4,f5,f6,f7,f8,f9,f10).

So basically, I need to recreate each variable (10) even if it’s not submitted (empty) because the checkbox1 refers to f1 in the database, if it’s not checked, I want to save nothing onto f1 column, if checkbox2 is checked, I want to save it’s value (let’s say ‘on’) onto f2 column and so on. So every checkbox has is counterpart in the database, that’s why I need different names and to match them, recreating values. And this is just an example, sometimes this has 3 checkbox, sometimes 8, etc.

Here is a basic sample of what I need to achieve;

$looper = 1;
$value = "value";
$value5 = "<br /><br />Hello World!!<br /><br />";

while ($looper <= '5')
{
print("$value$looper <br />");

$looper++;
}

it prints this;

value1
value2
value3
value4
value5

But what I need it to print, especially at 5, is the Hello World sentence from the recreated variable value construtected from $value and $looper. So basically value1 to value4 would be empty but recreated anyway and value5 would print this;

<br /><br />Hello World!!<br /><br />

Use an array name for the form field, with the array index being the incremental numerical value.

Next, only checked check-boxes are submitted. In the form processing code, you would loop over the range of possible values and test if the element in the submitted $_POST[‘the_field_name’] array isset().

Lastly, your database table design is bad. Do not create a sequence of name-numbered columns as the code needed to find, manipulate, or retrieve the data is overly complicated. You would instead store this type of data in a second table, with one row per data item, related back to the user through the user’s id.

And that’s where all the mess began… just like your counted form fields - you’re looking in the wrong direction, you try to work around a system that’s broken on the base:

If you would have stored all this properties within a relation, you could easily use theory of sets to build up any structure you want, example:

<?php

// prepare database
$pdo = new PDO("sqlite::memory:", "", "", [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$pdo->query("create table properties(name varchar(50))");
$pdo->query("insert into properties(name) values ('blueeyes'), ('redhair'), ('tenfingers')");
$pdo->query("create table userhasproperties (userid int, name varchar(50), value varchar(50))");

// get available properties to create form fields
$html = '';
foreach($pdo->query('select name from properties') as $property){
    $html .= '<input type="checkbox" name="properties['.$property['name'].']" value="yes" />'."\n";
}
echo $html;

// get all valid properties
$properties = $pdo->query('select name from properties')->fetchAll(PDO::FETCH_COLUMN, 0);
$_POST['properties'] = ['redhair', 'blueeyes', 'pirate']; // send from malicious user
var_dump($properties); // valid properties
var_dump(array_keys($_POST['properties'])); // given properties
var_dump(array_intersect($properties, $_POST['properties'])); // intermediate properties

// insert all properties
$insert = $pdo->prepare('insert into userhasproperties(userid, name, value) values(?,?,?)');
foreach (array_intersect($properties, $_POST['properties']) as $property){
    $insert->execute([1, $property, 'TRUE']);
}
var_dump($pdo->query('select * from userhasproperties')->fetchAll(PDO::FETCH_ASSOC));

no counting needed.

Sponsor our Newsletter | Privacy Policy | Terms of Service