PHP active page code - I can't figure out parse error
I'm trying to build an active page menu with PHP and MySQL and am having a difficult time fixing the error. In the while statement I have an if statement that is giving me fits. Basically I think I'm saying that "thispage" is equal to the "title" based on pageID and as the menu is looped through if "thispage" is equal to "title" then echo id="active". Thanks
<?php
mysql_select_db($database_db_connection, $db_connection);
$query_rsDaTa = "SELECT * FROM pages WHERE pagesID = 4";
$rsDaTa = mysql_query($query_rsDaTa, $db_connection) or die(mysql_error());
$row_rsDaTa = mysql_fetch_assoc($rsDaTa);
$totalRows_rsDaTa = mysql_num_rows($rsDaTa);
$query_rsMenu = "SELECT * FROM menu WHERE online = 1 ORDER BY menuPos ASC";
$rsMenu = mysql_query($query_rsMenu, $db_connection) or die(mysql_error());
$thisPage = ($row_rsDaTa['title']);
?>
<link href="../css/MainStyle.css" rel="stylesheet" type="text/css" />
<h2><?php echo $thisPage; ?></h2>
<div id="footcontainer">
<ul id="footlist">
<?php
while($row_rsMenu = mysql_fetch_assoc($rsMenu)) {
echo (" <li" . <?php if ($thisPage==$row_rsDaTa['title']) echo id="active"; ?> . "<a href=\"../" . $row_rsMenu['menuURL'] . "\">" . $row_rsMenu['menuName'] . "</开发者_高级运维a></li>\n");
}
echo "</ul>\n";
?>
</div>
<?php
mysql_free_result($rsMenu);
mysql_free_result($rsDaTa);
?>
kind of a big, hairy line. i think you need to make it a little easier by splitting it into more than one line. also, what is this part of your line supposed to do?
echo id="active";
do you mean echo " id=\"active\" ";
note i added a space before "id" because you don't have one after the LI
Parse errors can be located by consecutive removing various blocks of code.
Remove some portions of your code and see, if error persists.
Say, you can temporarily remove html part. If error got eliminated - it's in this part. Now you can divide this part on smaller blocks and so on. Thus you can locate an erroneous line pretty close.
Also, the error message usually contains some vital information on the error.
First, you have <?php
nested inside another <?php
. This causes:
syntax error, unexpected '<'
Let's remove both <?php
and ?>
. Now I see that you want you output the id, but you don't tell PHP that it's a string. Wrap it in single quotes so that echo id="active";
becomes echo ' id="active"';
With this out of the way, you can't contatenate an if statement just like that:
echo (" <li" . if ($thisPage==$row_rsDaTa['title']) echo ' id="active"'; ...
You might want to introduce a variable that will store the string id="active"
if you're on the current page.
$id = '';
if ($thisPage==$row_rsDaTa['title']) {
$id = ' id="active"';
}
This piece of code might look like this when rewritten:
while($row_rsMenu = mysql_fetch_assoc($rsMenu)) {
$active = '';
if ($thisPage==$row_rsDaTa['title']) {
$id = ' id="active"';
}
echo (" <li" . $id . "<a href=\"../" . $row_rsMenu['menuURL'] . "\">" .
$row_rsMenu['menuName'] . "</a></li>\n");
}
echo "</ul>\n";
Try this :
<?php
mysql_select_db($database_db_connection, $db_connection);
$query_rsDaTa = "SELECT * FROM pages WHERE pagesID = 4";
$rsDaTa = mysql_query($query_rsDaTa, $db_connection) or die(mysql_error());
$row_rsDaTa = mysql_fetch_assoc($rsDaTa);
$totalRows_rsDaTa = mysql_num_rows($rsDaTa);
$query_rsMenu = "SELECT * FROM menu WHERE online = 1 ORDER BY menuPos ASC";
$rsMenu = mysql_query($query_rsMenu, $db_connection) or die(mysql_error());
$thisPage = ($row_rsDaTa['title']);
?>
<link href="../css/MainStyle.css" rel="stylesheet" type="text/css" />
<h2><?php echo $thisPage; ?></h2>
<div id="footcontainer">
<ul id="footlist">
<?php
while($row_rsMenu = mysql_fetch_assoc($rsMenu)) {
$id = ($thisPage==$row_rsDaTa['title']) ? "id='active'" : "";
echo "<li " . $id . "<a href='../" . $row_rsMenu['menuURL'] . "' >" . $row_rsMenu['menuName'] . "</a></li>\n";
}
?>
</ul>
</div>
<?php
mysql_free_result($rsMenu);
mysql_free_result($rsDaTa);
?>
精彩评论