-1

I am having a hard time trying to create a form using php that takes information from a multi-dimensional array(database isn't allowed for this assignment) to create the shopping cart feature like on ebay or amazon. The user is only supposed to be able to select one item to purchase, then pass the information to the next form in the process. I am trying to re-create a feature that I did in VB.net, using a nested loop to generate the repetitive controls for the form, in this case radio buttons.

    <?php
                $itemNumber = 0;
                foreach( $items as $item ) {
                    $itemNumber++;
                    echo "<tr height=80px>";

                        //i think the problem is in the nested loop below// 

                        foreach( $item as $key => $value ){
                            $myThumbnail = generateThumbnail("$itemNumber".".jpg");

                            echo "<td width = 50%><input type="radio" name="itemName" id="$itemNumber" value="itemName" setChecked("itemName", "itemName") /></td>";
                            echo "<td width=50%>$myThumbnail</td>";
                        }

                    echo "</tr>";

                }

                ?>

I have ran my code through a syntax checker but the checker is very limited. I have tried for going on six days now to find my errors. My instructor for this class hasn't returned any emails. I have searched both stackoverflow and PHPfreaks for possible solutions but all options I found use databases and/or hard code all the controls instead of creating them dynamically so as to make as many as needed no matter how big or small the array is. If more code is needed I will add upon request.

Ira Nelson
  • 13
  • 5
  • The syntax error is in first echo in foreach loop... You need to escape quotes around HTML attributes, or replace them by single quotes – pavel Nov 04 '17 at 17:53

1 Answers1

0

The problem is you are not escaping the quotes in the first echo correctly, try(using \ to escape):

echo "<td width = 50%><input type=\"radio\" name=\"itemName\" id=\"$itemNumber\" value=\"itemName\" setChecked(\"itemName\", \"itemName\") /></td>";

Alternatively you can use concatenation and ' for the string like:

echo '<td width = 50%><input type="radio" name="itemName" id="' . $itemNumber . '" value="itemName" setChecked("itemName", "itemName") /></td>';
mega6382
  • 9,211
  • 17
  • 48
  • 69
  • 1
    That made the page show up but not the radio buttons but is still a win to me, so thank you. There is little to no help being given in this class. The instructor says the class is, for the most part, a self taught class due to it not being a foundation class. Normally this isn't a problem, but I find the book to be seriously lacking, and the instructor is all but unreachable. – Ira Nelson Nov 04 '17 at 18:20