Parse error: syntax error, unexpected T_ECHO

浪子不回头ぞ 提交于 2020-01-30 08:51:11

问题


I've been working on something for the past few days but this one bit of code perpetually throws an unexpected T_ECHO. My friends can't seem to find anything wrong with it and I'm at the edge of my patience. Even with the nested while loop removed it still throws an error and I switched to the while: endwhile; syntax as well and I'm still getting it. I'm sure the answer is staring me in the face but I probably can't see it.

  while ($row = mysql_fetch_array($result, MYSQL_ASSOC)):        
            echo "<tr>";
              echo "<td>". $row["site_description"] ."</td>";
              echo "<td>". $row["url"] ."</td>";
              echo "<td><select>";
                while ($roar = mysql_fetch_array($categories, MYSQL_ASSOC)):
                  echo "<option value=\"". $roar["category"] ."\">". $roar["category"] ."</option>";
                endwhile;
              echo "</select></td>";
            echo "</tr>";
          endwhile;

回答1:


You could use short tags to make this far more readable and likely less error prone.

<?php while ($row = mysql_fetch_array($result, MYSQL_ASSOC)): ?>
    <tr>
        <td><?= $row["site_description"] ?></td>
        <td><?= $row["url"] ?></td>
        <td>
            <select>
            <?php while ($roar = mysql_fetch_array($categories, MYSQL_ASSOC)): ?>
                <option><?= $roar["category"] ?></option>
            <?php endwhile; ?>
            </select>
        </td>
    </tr>
<?php endwhile; ?>



回答2:


Try this...

I think it may have been falling over because once you've parsed the categories resultset, for the first site, it comes up with nothing the next time you call mysql_fetch_array. So, doing this upfront and popping the results into a variable fixes that.

I've added "\t" and "\n" to the strings to make the html format prettily in view source.

    $result = mysql_query("select * from tmp_sites");
$categories = mysql_query("select * from tmp_cats");
$select_options = "";
while ($roar = mysql_fetch_array($categories, MYSQL_ASSOC)):
                  $select_options .= "\t\t<option value=\"". $roar["category"] ."\">". $roar["category"] ."</option>\n";
                endwhile;
echo "<table>";
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)):
            echo "\t<tr>\n";
              echo "\t\t<td>". $row["site_description"] ."</td>\n";
              echo "\t\t<td>". $row["url"] ."</td>\n";
              echo "\t\t<td><select>\n";
                echo $select_options;
              echo "\t\t</select></td>\n";
            echo "\t</tr>\n";
          endwhile;
echo "</table>"


来源:https://stackoverflow.com/questions/5793500/parse-error-syntax-error-unexpected-t-echo

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!