Jump to content

wrox php needs help agian


Go to solution Solved by boompa,

Recommended Posts

I have two questions; One: I have looked at this code for two days and can not find why it will not run. Can someone tell me what is wrong?  Two I think this wrox php is moving to fast for me as a newbie. Can anyone recommend a book that maybe starts out a little slower? 

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Fibonacci sequence</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<meta http-equiv="content-type" content="application/xhtml+xml; charset=UTF-8"/>
<meta http-equiv="content-style-type" content="text/css"/>
<link href="common.css" rel="stylesheet" type="text/css"/>
<style type="text/css">
th{text-align:left; background-color: #999;}
th, td{padding: 0.4em;}
tr.alt td{background: #ddd;}
</style>
</head>
<body>
<h2>Fibonacci sequence</h2>
<table cellspacing="0" border="0" style="width: 20em; border: 1px solid #666;">
<tr>
<th>Sequence #</th> <th>Value</th>
</tr>
<tr>
<td>F<sub>0</sub></td>
<td>0</td>
</tr>
<tr class="alt">
<td>F<sub>1</sub></td>
<td>1</td>
</tr>
<?php
$iterations = 10;
$num1 = 0;
$num2 = 1;
for($i=2; $i <= $iterations; $i++) { $sum = $num1 + $num2; $num1 = $num2; $num2 = $sum; ?>
<tr<?php if($i % 2 != 0) echo 'class="alt"' ?>>
<td>F<sub><?php echo $i?></sub></td>
<td><?php echo $num2?></td>
</tr> }
 
</table> 
</body>
</html>

 

I am not sure I did that code thing right?

Link to comment
https://forums.phpfreaks.com/topic/281261-wrox-php-needs-help-agian/
Share on other sites

Plus, you have lines of code that aren't closed with semi-colons. I don't know if there is some requirement of WROX to write code like that - but that's a terrible format in my opinion.

 

I think this looks and works much better.

<?php

$iterations = 10;
$num1 = 0;
$num2 = 1;
$output = '';
for($i=0; $i<=$iterations; $i++)
{
    $class = ($i%2) ? 'alt' : '';
    $sum = $num1 + $num2;

    $output .= "<tr class=\"{$class}\">";
    $output .= "<td>F<sub>{$i}</sub></td>";
    $output .= "<td>{$sum}</td>\n";
    $output .= "</tr>\n";

    $num1 = $num2;
    $num2 = $sum;
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Fibonacci sequence</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <meta http-equiv="content-type" content="application/xhtml+xml; charset=UTF-8"/>
    <meta http-equiv="content-style-type" content="text/css"/>
    <link href="common.css" rel="stylesheet" type="text/css"/>
    <style type="text/css">
        th{text-align:left; background-color: #999;}
        th, td{padding: 0.4em;}
        tr.alt td{background: #ddd;}
    </style>
</head>
<body>
    <h2>Fibonacci sequence</h2>
    <table cellspacing="0" border="0" style="width: 20em; border: 1px solid #666;">
        <tr><th>Sequence #</th><th>Value</th></tr>
        <?php echo $output; ?>
    </table>
</body>
</html>
Edited by Psycho

the missing php tags would have resulted in a fatal php parse error. do you have php's error_reporting set to E_ALL and display_errors set to ON in your php.ini (putting these settings in your script won't show fatal parse errors) so that php will help you by reporting and displaying all the errors it detects?

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.