Jump to content

Help with very basic programming problem


zatox123

Recommended Posts

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 by zatox123
Link to comment
Share on other sites

@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 by Psycho
Link to comment
Share on other sites

@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 ;)

Link to comment
Share on other sites

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 by zatox123
Link to comment
Share on other sites

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 by Ch0cu3r
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.