1

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;
9
  • 4
    Are you sure that code is giving the error? Is it possible that you're running a different file? Commented Apr 26, 2011 at 16:42
  • 1
    to which line number / which echo error reffering to ? this peace of code is correct Commented Apr 26, 2011 at 16:42
  • That code works fine. Are you sure the error's occuring here? As well, any reason you're using the alternate block syntax instead of the usual {}? Commented Apr 26, 2011 at 16:47
  • the line number always refers to the line immediately following the nested while loop echo "</select></td>"; i switched because i've been trying everything to try to fix it. and i'm sure it's that code because i've moved around stuff and the line number changes with my edits. i didn't want to post the entire 115 lines. Commented Apr 26, 2011 at 16:50
  • I actually didn't know php supported a completely different syntax. Grr php is always giving everyone arbitrary choices to make your life more difficult. Commented Apr 26, 2011 at 16:50

2 Answers 2

2

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; ?>
Sign up to request clarification or add additional context in comments.

22 Comments

Short tags are not xml compliant and should be avoided.
The short tags for PHP don't ever get seen by the browser - how can they be non-compliant?
@tandu You've got that all wrong. Of course PHP code isn't XML complaint! Unnecessary downvote!
@Gary The documentation in php.ini suggests against using short tags: "Using short tags should be avoided when developing applications or libraries that are meant for redistribution, or deployment on PHP servers which are not under your control, because short tags may not be supported on the target server."
@Michael it only suggests against using them if you're distributing the code. If you're concerned about that replace <?= with <?php echo. that type of inline coding still makes for far more readable code.
|
0

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>"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.