简体   繁体   中英

PHP auto-populate with array/strings

Before I begin, 2 disclaimers:

1) On a scale of 1 to 10,000 my PHP knowledge is about a 4.3.
2) I am not certain I use the right coding vernacular, please forgive me if I call something by the wrong name (see disclaimer 1).

I have a PHP web page that has 100 similar items on it. There is a disabled checkbox followed by a title. This is repeated 100 times. Each line has a different name variable, value variable, if the box is checked or not, and title.

Currently, the HTML looks like this:

<p class="displayCheck">
                  <input type="checkbox" name="num1" value="nu1" checked> Title 1<br>
                  <input type="checkbox" name="num2" value="nu2"> Title 2<br>
                  <input type="checkbox" name="num3" value="nu3"> Title 3<br>
                  <input type="checkbox" name="num4" value="nu4" checked> Title 4<br>
                  <input type="checkbox" name="num5" value="nu5"> Title 5<br>
                  ... repeated 100 times (in groups of 10)
                </p>

I am trying to put this into a PHP array, I guess? I want to end up being able to change the title and if the box is checked or not. I figured I would try to slim down the code and use variables for the small differences.

Here is what I have:

$num = "2";
$nu = "2";
$Title = "2";
$isChecked = "checked";
$lineCode = '<input type="checkbox" disabled="disabled" name="num' . $num . '" value="nu' . $nu . '" ' . $isChecked . '> Title ' . $Title;

Then, on the page, I can use

<?php echo $lineCode; ?><br>

which will produce this

<input type="checkbox" disabled="disabled" name="num2" value="nu2" checked> Title 2<br>

Currently, what I am after is some way to reproduce the lineCode so that it populates the HTML with the code form the first code box above, I realize I need to put the $num, $nu, etc. into an array somehow so that they will produce "num1", "num2" , etc. as needed.

I further assume this will be a multidimensional array of array indexes (and one boolean - isChecked? maybe?)

While it has taken me about 2 hours to produce the $lineCode above, I fear I can't figure out anything that will work for what I am after.

I am not sure if it matters, but eventually, I will be editing each checkbox through PHP.

For example, I will visit a separate page on the site and it will change the value of $isChecked and $Title on an individual basis. For now, I would just like the population and coding to be correct to show the 100 lines.

You can try below:

<?php
$ele = array(
    array(
        'num' => 1,
        'nu' => 1,
        'title' => 1,
        'isChecked' => true
    ),
    array(
        'num' => 2,
        'nu' => 2,
        'title' => 2,
        'isChecked' => true
    ),
    array(
        'num' => 3,
        'nu' => 3,
        'title' => 3,
        'isChecked' => false
    )
);
foreach ($ele as $line)
    echo "<input type='checkbox' disabled='disabled' name='num" . $line['num'] . "' value='nu" . $line['num'] . "' " . ($line['isChecked'] == true ? "checked" : false) . "> Title" . $line['title'] . "<br>";
?>

Here, I am creating an associative array and then looping over it using a foreach loop to arrive at the final HTML.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM