在PHP方法中格式化HTML表格的正确方法[关闭]

I seem to be having trouble formatting tables for my Search Form.

What i need is proper formatting to display when the search form returns results.

Quick Question: is this the proper way to format Tables using the HTML in PHP method...... i seem to be off by a little.


 "<table>";
echo "<td>";
 echo "<tr>";
 echo "<tr>";

echo "<th><div class='searchresults'>"   .   $title  .  "</th></div>"; 
echo "<br/>";
echo "<th><div class='searchresults'>" .  $model .  "</th></div>";
echo "<br/>";
echo  "<div class='image'><img src =" . $image . "></table></div></a>";
echo "<br/>";

echo "</tr>";
echo "</td>";
echo "</table>";
echo "</div>";
    echo"
    <table>

    <tr>


    <td class='searchresults'>"   .   $title  .  "</td> 

    <td class='searchresults'>" .  $model .  "</td>

    <td class='image'><img src =" . $image . "></td>

    </tr>

    </table>
    ";

I think this is correct way!!

I think I would go this way:

<?php
    $title = 'title';
    $model = 'model';
    $image = 'image';
?>
<div>
    <table>
        <tr>
            <th>
                <div class='searchresults'><?php echo $title; ?></div>
            </th>           
            <th>
                <div class='searchresults'><?php echo $model; ?></div></th>
            <th>
                <div class='image'><img src="<?php echo $image; ?>" /></div>
            </th>
        </tr>
    </table>
</div>

If the html-markup is bigger than the one shown I would consider to use a template enginge. There plenty of these which can be used with PHP (e.g. smarty or mustache)

Start from scratch. This code is messy lacks any sort of HTML structure.

Whether you wrote it yourself or you have adapted someone else's code, you need to completely rewrite it.


"<tables>";

Is this being echoed? What is this string?


echo "<tr>";
echo "<tr>";

You have too many opening <tr> tags. Rows shouldn't be contained within rows.


echo "<th><div class='searchresults'>"   .   $title  .  "</th></div>";

This isn't chronological. <th><div>content</th></div> - you should make it <th><div>content</div></th>, keeping the div contained within the th. But you shouldn't really be using a div inside the th at all - use CSS to edit the style of that cell.


echo "<br/>";

Why is this here? You don't need this. If you want the cell to be taller, change the CSS.


echo "<th><div class='searchresults'>" .  $model .  "</th></div>";

Check your order here too.


echo "<br/>";

Again, not necessary.


echo  "<div class='image'><img src =" . $image . "></table></div></a>";

So much wrong here.

  • This div isn't part of the table.
  • The img src isn't contained within double OR single quotes so is invalid HTML.
  • Why are you closing the table here? </table> should be at the end of the table.
  • What is the </a> tag closing? There's no opening <a> tag anywhere in your code. It's just bizarre.

echo "<br/>";

Again, not necessary.


"</tr>";
"</td>";
"</table>";
"</div>";

There's no coherent order to any of this. They're not part of an echo or print command, they're not chronological, what is the </div> closing?