zatox123 Posted October 31, 2013 Share Posted October 31, 2013 (edited) Hello everyone, I am taking a php course and was given some practice problems to do but was not given any answers to check my work. I am VERY confused with what to do with Part 2 of this problem. For the first part I was supposed to code this bit of code which I all ready did. What I need help with is the second part... Here is the code I made that I am supposed to modify <?php $names=array('duck','chicken','turkey','axolotl'); $prices=array(7,5,18,200); print "<table>"; for ($i=0; $i<=3; $i++) { $item=$names[$i]; $cost=$prices[$i]; print "<tr><td><input type='radio' name='purchase' value='$i'> $item </td><td> $cost</td></tr>"; } print "</table>"; ?> The directions are "Modify my program listed above, to provide a correctly positioned caption row for the table with captions "Select", "Animal" and "Price" at the head of each column. I am really unsure of how to go about doing this. If anyone could help me out with some tips or just show me an answer so I can see how you did it I would greatly appreciate it. Edited October 31, 2013 by zatox123 Quote Link to comment https://forums.phpfreaks.com/topic/283479-help-with-very-basic-programming-problem/ Share on other sites More sharing options...
mentalist Posted October 31, 2013 Share Posted October 31, 2013 There's many ways to do it, could concatenate the arrays then do mod 4 on i and if i=4 add a tr, or as is write out to two strings then print at the end, use css divs and positioning, etc, etc... Quote Link to comment https://forums.phpfreaks.com/topic/283479-help-with-very-basic-programming-problem/#findComment-1456410 Share on other sites More sharing options...
Ch0cu3r Posted October 31, 2013 Share Posted October 31, 2013 It is asking you to modify the table so it includes the table headings Select, Animal and Price To add the table headings change print "<table>"; to print "<table> <tr> <th>Select</th> <th>Animal</th> <th>Price</th> </tr>"; Then add </td><td> before $item Quote Link to comment https://forums.phpfreaks.com/topic/283479-help-with-very-basic-programming-problem/#findComment-1456411 Share on other sites More sharing options...
Psycho Posted October 31, 2013 Share Posted October 31, 2013 (edited) @mentalis: I don't think that is what the instructions are asking for. you're describing the solution for data to flow across a row then create a new row after n records. What I am reading is that each row is a complete record with each column representing a different piece of data for that record. @zatox123, what I think you need to do is create what I refer to as a "header" row where each item in that row is a column header that describes the type of data in that column. For this you would use TH tags (e.g. "table header") instead of TD tags ("table data"). Additionally, the header should be created before the data - so it would be before the loop. Give it a try and get back to us with any problems/questions. EDIT: looks like Ch0c3r went ahead and gave you the solution. I was wanting you to try and figure it out yourself. Edited October 31, 2013 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/283479-help-with-very-basic-programming-problem/#findComment-1456412 Share on other sites More sharing options...
mentalist Posted October 31, 2013 Share Posted October 31, 2013 @mentalis: I don't think that is what the instructions are asking for. you're describing the solution for data to flow across a row then create a new row after n records. What I am reading is that each row is a complete record with each column representing a different piece of data for that record. ... Oops, assumed! Zatox, try that next Quote Link to comment https://forums.phpfreaks.com/topic/283479-help-with-very-basic-programming-problem/#findComment-1456413 Share on other sites More sharing options...
zatox123 Posted October 31, 2013 Author Share Posted October 31, 2013 thanks for the help guys! Quote Link to comment https://forums.phpfreaks.com/topic/283479-help-with-very-basic-programming-problem/#findComment-1456416 Share on other sites More sharing options...
zatox123 Posted October 31, 2013 Author Share Posted October 31, 2013 (edited) so would it end up looking like this? When i run the program it doesnt work lol so i think I messed something up. <?php $names=array('duck','chicken','turkey','axolotl'); $prices=array(7,5,18,200); print "<table> <tr> <th>Select</th> <th>Animal</th> <th>Price</th> </tr>”; for ($i=0; $i<=3; $i++) { </td><td> $item=$names[$i]; $cost=$prices[$i]; print "<tr><td><input type='radio' name='purchase' value='$i'> $item </td><td> $cost</td></tr>"; } print "</table>"; ?> Edited October 31, 2013 by zatox123 Quote Link to comment https://forums.phpfreaks.com/topic/283479-help-with-very-basic-programming-problem/#findComment-1456418 Share on other sites More sharing options...
Ch0cu3r Posted October 31, 2013 Share Posted October 31, 2013 (edited) Not quite you appear be using smart quotes here </tr>”; The ” should be a " You placed the </td><td> in the wrong place it needs to go before $item </td><td> $cost</td></tr>"; Your code should be <?php $names=array('duck','chicken','turkey','axolotl'); $prices=array(7,5,18,200); print "<table> <tr> <th>Select</th> <th>Animal</th> <th>Price</th> </tr>"; for ($i=0; $i<=3; $i++) { $item=$names[$i]; $cost=$prices[$i]; print "<tr><td><input type='radio' name='purchase' value='$i'> </td><td>$item </td><td> $cost</td></tr>"; } print "</table>"; ?> Edited October 31, 2013 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/283479-help-with-very-basic-programming-problem/#findComment-1456419 Share on other sites More sharing options...
zatox123 Posted October 31, 2013 Author Share Posted October 31, 2013 thanks for the help! really appreciate it Quote Link to comment https://forums.phpfreaks.com/topic/283479-help-with-very-basic-programming-problem/#findComment-1456435 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.