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.