2

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.

4
  • Try this: $lineCode = '<input type="checkbox" disabled="disabled" name="num[' . $num . ']" Commented Nov 28, 2019 at 11:23
  • @Benilson that doesn't do anything except add a bracket around the output? What am I missing with your suggestion? Commented Nov 28, 2019 at 15:53
  • In html, names with [] are array Commented Nov 28, 2019 at 16:02
  • Oh, gotcha! Wouldn't that only be useful if I was using an HTML form, though? To $POST information where the information was the same from various inputs? Sorry for the confusion, the checkboxes I am using are more visual aid than anything there is no end-user input. Commented Nov 28, 2019 at 16:39

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

So, in this instance then, I would still need the $ele for all 100 entries? It does work, and I can see how it will allow me to later be able to point to a specific line to make a change. However, there will also eventually be over 1,000 such lines. Is there a way to loop the $ele with an auto-increment or something? Either way, this is more practical than I have already, thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.