tags:

views:

41

answers:

4

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); 
?>
A: 

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.

Col. Shrapnel
+1  A: 

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

Don Dickinson
Yes you are correct... thanks
dmschenk
A: 

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";
Mike
Thank you very much for the explainations!!! They were very helpful to me. I changed the active ='' to id='' and this worked well. I really appreciate the help
dmschenk
A: 

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); 
?>
iwanwan
Thanks for the help everyone.I got this to work fine with a couple of extra mods on my end. I added a MenuID column to pages table for referencing the menu table. it seemed more effecient to me now and with that a slight tweek to the code you posted I changed the row_rsDaTa['title'] refs to row_rsDaTa['menuID'] and all is happy!Thanks again
dmschenk