1

I am teaching myself PHP and am having an issue with a table I just created. I have my code and what is being displayed below. I have been sitting here for hours trying to figure it out but can't. If any one can explain why the table doesn't complete the while loop and everything after the the closing of the last row displays above the table...hopefully I'm clear enough for everyone or at least someone to understand.

php table error

As pointed out, I should include the code for those who are using screen readers.

<html>
<head>
    <link rel="stylesheet" type="text/css" href="freight.css">
    <!--<script type="text/javascript" src=""></script>-->
    <title>PHP Loops Example</title>
</head>
<body>
    <h3>PHP generated</h3>
    <table>
        <tr>
            <th>Distance</th>
            <th>Cost</th>
        </tr>   
        <?
            $distance = 50;
            while ($distance <= 250){
                echo "<tr>
                        <td>.$distance.</td>
                        <td>.($distance/10).</td>
                    </tr>";
                        // displays everything from the double quote (above) to the end of the whilte loop  
                $distance += 50;
            }
        ?>
    </table>
    <? echo "$distance"; ?> <!-- doesn't display -->
</body>
</html>
Mike
  • 11
  • 3
  • Please post code, errors, sample data or textual output here as plain-text, not as images that can be hard to read, can’t be copy-pasted to help test code or use in answers, and are hostile to those who use screen readers. You can edit your question to add the code in the body of your question. Use the `{}` button to format any blocks of code, or indent with four spaces for the same effect. – tadman Jul 26 '17 at 16:03
  • Try inspecting the HTML - do you see your full PHP code? If so, the dupe suggestion above is correct. Your concatenation of variables inside the loop is also wrong. – Qirel Jul 26 '17 at 16:07
  • try this inside while loop echo " ".$distance." ".($distance/10)." "; $distance += 50; – rowmoin Jul 26 '17 at 16:22

3 Answers3

0

when echoing out variables do not forget to close and re-open the quotation marks echo "hello".$variable." ending the hello";

TheHidden
  • 551
  • 1
  • 8
  • 20
0

Recommend you use the full php tag instead of enabling short tags on your server. If you use short tags, you make your code dependent on a configuration setting that may not always be available!

Also, check to be sure that your script is being parsed through PHP. In many installations, files named like xxx.html will not be parsed by PHP. Seeing PHP variable names (nor resolved values) in the output document is a symptom of this issue.

Ray Paseur
  • 2,106
  • 2
  • 13
  • 18
-2

PHP short tags aren't enabled on your server, so you can't use <? instead use <?php

or update your php.ini file to enable short tags

short_open_tag=On

Kevin P
  • 601
  • 3
  • 9
  • How do you know its that the short tags are disabled? It could be that PHP isn't running at all. – Qirel Jul 26 '17 at 16:05
  • Assuming PHP is running that's what the problem most likely is. – Kevin P Jul 26 '17 at 16:09
  • We don't know, it could be either ;-) In most cases, its that PHP isn't running (or the file is being accessed in a way that PHP doesn't run). Short-tags aren't recommended to use though. It could be that they're disabled, but we can't know for sure without any further info from OP ;-) – Qirel Jul 26 '17 at 16:11
  • by the looks at the image php is not running at all – Masivuye Cokile Jul 26 '17 at 16:12
  • Wow a lot of down votes. I've ran into issues with short tags being disabled in the past, banged my head against the wall for hours, the image is exactly what pages look like when short tags are disabled. – Kevin P Jul 26 '17 at 16:32