3

Is there any better way to do this code and dont ask why it's in echo's my brother did it that way not sure why but its slow i do want the echo

echo "<table id=\"vote\">";
echo "<th colspan=\"11\">Vote now!</th>";
echo "<tr>";
echo "<td><form action=\"vote.php?id=$id&value=\"0\"\" method=\"post\"><input name=\"value\" value=\"0%\" type=\"submit\"/></form></td>";
echo "<td><form action=\"vote.php?id=$id&value=\"10\"\" method=\"post\"><input name=\"value\" value=\"10%\" type=\"submit\"/></form></td>";
echo "<td><form action=\"vote.php?id=$id&value=\"20\"\" method=\"post\"><input name=\"value\" value=\"20%\" type=\"submit\"/></form></td>";
echo "<td><form action=\"vote.php?id=$id&value=\"30\"\" method=\"post\"><input name=\"value\" value=\"30%\" type=\"submit\"/></form></td>";
echo "<td><form action=\"vote.php?id=$id&value=\"40\"\" method=\"post\"><input name=\"value\" value=\"40%\" type=\"submit\"/></form></td>";
echo "<td><form action=\"vote.php?id=$id&value=\"50\"\" method=\"post\"><input name=\"value\" value=\"50%\" type=\"submit\"/></form></td>";
echo "<td><form action=\"vote.php?id=$id&value=\"60\"\" method=\"post\"><input name=\"value\" value=\"60%\" type=\"submit\"/></form></td>";
echo "<td><form action=\"vote.php?id=$id&value=\"70\"\" method=\"post\"><input name=\"value\" value=\"70%\" type=\"submit\"/></form></td>";
echo "<td><form action=\"vote.php?id=$id&value=\"80\"\" method=\"post\"><input name=\"value\" value=\"80%\" type=\"submit\"/></form></td>";
echo "<td><form action=\"vote.php?id=$id&value=\"90\"\" method=\"post\"><input name=\"value\" value=\"90%\" type=\"submit\"/></form></td>";
echo "<td><form action=\"vote.php?id=$id&value=\"100\"\" method=\"post\"><input name=\"value\" value=\"100%\" type=\"submit\"/></form></td>";
echo "</tr>";
echo "</table>";
4

2 回答 2

2

Try a loop (I took the liberty of changing one of the echo to use single quotes (') instead of double quotes (") to get rid of all the escaping (\). I also replaced all your echo with a single variable (which sound be marginally faster).

$myForms = "";
$myForms .= "<table id=\"vote\">";
$myForms .= "<th colspan=\"11\">Vote now!</th>";
$myForms .= "<tr>";
for ($i = 0; $i < 110; $i += 10) {
    $myForms .= '<td><form action="vote.php?id=' . $id . '&value=' . $i . '" method="post">
                         <input name="value" value="' . $i . '%" type="submit"/>
                     </form>
                 </td>';
}
$myForms .= "</tr>";
$myForms .= "</table>";

echo $myForms;
于 2013-03-30T06:13:17.413 回答
2

I'd suggest you seperate HTML and PHP code to make it easier to manage and using for loop to DRY your code:

<table id="vote">
<th colspan="11">Vote now!</th>";
<tr>
<?php
    for($i=0; $i <= 100; $i+=10) {
        <td><form action="vote.php?id=' . $id . '&value=' . $i . '" method="post"> <input name="value" value="' . $i . '%" type="submit"/> </form> </td>;
    }
?>
<tr>
</table>
于 2013-03-30T06:19:39.080 回答