Foreach iteration display


#1

My task:

I am trying to create a row of images, which will allow me to select the one I want to use as the main display image for the item I am linking from a database.

I am doing this by outputting a row of images, and then underneath each will be a form radio input. If there is an image that is the current main image, then it will show as checked.

If I select a different radio input, then I will hit a submit button to change to the new main image. (Right now I want to do this all in PHP, but I do realize I can get more seamless interaction with Javascript – such as eliminating the need to hit a submit button).

I have the code for all the above set up, however in order to set up the names in the option input for posting, I want to iterate them. I’m not sure how to show the iteration value in a foreach loop. I reviewed the PHP manual on iterators, but I’m not sure how to apply the information shown there (http://php.net/manual/en/class.iterator.php).

Before I continue to spend more hours trying to figure this out, could someone please let me know if I am on the right path, or if there is a different way of doing this?

Here is what I have so far. The iteration# is just a placeholder for when I figure out what code I need to put there.

                    foreach ($result as $row) {
                        echo '<td align = "center">';

                            // Main Image (show as checked)
                            if ($row["mini_imag_id"] == $row["imag_id"]) {
                                echo '<input type="radio" name="image' . iteration# . '" value="y" checked>';
                            }
                            
                            // Alternative Images
                            else {
                                echo '<input type="radio" name="image' . iteration# . '" value="y">';
                            }
                        echo '</td>';
                    }

In order for this to work, my current thinking is:

  • I will need to retrieve the total number of data sets, and post with my submit button (likely via a hidden input).
  • In my PHP variables section at the top of my code, I will have a loop that will assign variables to the posted information. I imagine the code will look something like this
$maxRow = $_POST["maxRow"];

while($x <= $maxRow) {
     ${'image'.$x} = $_POST["image.$x"];
}

I do feel that there should be a way to do this with an array instead, but I don’t believe there is a way to assign an array name to the input name. In example:

echo '<input type="radio" name="image(' . iteration# . ')'" value="y">';

If any of this made any sense and someone could help point me to the right path, that would be much appreciated.


#2

Post an SQL dump of your DB so I have something to work with. You can also PM it to me.


#3

You are making this overly complicated. There’s no need for the maxRow value, concatenation, a loop inside the form processing code, or variables ending in numbers.

All the radio buttons for a ‘group’ need to have the same name attribute, which you are doing, i.e. ‘image’. Next, set the value attribute to be the image id. When the form is submitted, the submitted value for the radio button group field name, $_POST[‘image’], will be the id of the image for the checked radio button.


#4

Yeah, I’m pretty good at doing that :slight_smile:

Thanks. I have not used radio buttons before, and I clear how I thought they worked when I read about them, was not how they work. Pretty simple and straightforward actually – thanks for the reality check :slight_smile: