Forum Moderators: coopster

Message Too Old, No Replies

Possible to assign php variable to html tag attribute?

         

singularityflow

3:07 am on Oct 23, 2007 (gmt 0)

10+ Year Member



I recently began teaching myself php but I am currently stumped by a problem I encountered. Unfortunately my efforts to search the web for a solution came up empty, so hopefully someone here can shed some light.

I am having trouble trying to assign a php variable to an html tag attribute. To clarify, an example is as follows:

<?php $rowtitles = array(1 => Pizza1, 2 => Pizza2, 3 => Pizza3)?>

<!-- Row of column headers -->
<tr>
<td>Pizza #</td>
<td>Pepperoni</td>
<td>Mushrooms</td>
<td>Pineapple</td>
</tr>

<!-- Display a row of checkboxes for each value in rowtitles -->
<?php for($i=0; $i < sizeof($rowtitles); $i++) {?>
<tr>
<td><?php print $rowtitles[$i];?></td>
<td><input name="pizza1[]" type="checkbox" value="?" <?php if(in_array($rowtitles[$i], $array1)) {?>checked="checked"<?php ;}?>"/></td>
<td><input name="pizza2[]" type="checkbox" value="?" <?php if(in_array($rowtitles[$i], $array2)) {?>checked="checked"<?php ;}?>"/></td>
<td><input name="pizza3[]" type="checkbox" value="?" <?php if(in_array($rowtitles[$i], $array3)) {?>checked="checked"<?php ;}?>"/></td>
</tr><br>
<?php }?>

This generates a table where users can select toppings for however many pizzas are in the rowtitles array. The if statements allow the checkboxes to remain checked if the form reloads due to an error during validation. My problem arises with the value attribute (indicated by the question marks in the code above). I would like to set value equal to “$rowtitles[$i]” so that I can store the arrays pizza1, pizza2, and pizza3 in a database to keep track of the user’s selections for future use. But I cannot figure out how to do this by combining php and html.

I have tried value="<?php $rowtitles[$i]?>" and value="<?php print $rowtitles[$i];?>" and some other variations but have been unsuccessful thus far. Is this even possible or should I be approaching this from a different angle? Unfortunately, my actual code may loop up to 50 or more times, so I hope there is a method that uses looping rather than having to individually name each checkbox value by hand.

Hopefully I have explained my question clearly enough. Thanks in advance for any advice, I appreciate any insight you can offer! Hopefully someone can help me out :)

Habtom

5:07 am on Oct 23, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



singularityflow, welcome to WebmasterWorld.

Replacing the question marks with <?php print $rowtitles[$i];?> should give you the results.

Habtom

singularityflow

6:37 am on Oct 23, 2007 (gmt 0)

10+ Year Member



Habtom,

Thanks for the welcome and answer to my question :)

It turns out my arrays were being reinitialized so they were turning up empty in my database even with the proper code in the value attribute, but if you hadn't confirmed the php was correct, I might have never saw that.

Thanks again for helping out a php newcomer!

Habtom

6:44 am on Oct 23, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You're welcome :)