Li element selection.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
yorkshireflatcap
Forum Newbie
Posts: 5
Joined: Fri Oct 16, 2009 7:52 am

Li element selection.

Post by yorkshireflatcap »

I have the following code:

Code: Select all

 
while ($result = display_tree($num))
{
        echo"<ul>
        <li>";
    while ($row = mysql_fetch_array($result))
    {
        if ($row["menuID"] == 0)
        {
            echo "<a href='main.php'>".$row["locationName"]."</a>"; // output county
        }
        else
        {
            echo "<ul>
            <li><a href='main.php'>".$row["locationName"]."</a></li></ul>"; // output location - based in county
         }
    }
    echo"</li></ul>";
    $num++;
    mysql_free_result($result); 
}
 
All this code does is create a menu list with named locations - London, Coventry etc. The CSS code is tucked away.
However, what I'd like to achieve is to be able to select the menu item and save it so that I can do the following:

Code: Select all

 
            echo "<ul>
            <li><a href='main.php'?$locationName>".$row["locationName"]."</a></li></ul>";
 
All this code does is to select the last menu item. I've used onClick within the <href...> tag but again nothing happens!

How do I capture the menu selection so that I can post it back to the page using main.php?location

Many thanks.
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Li element selection.

Post by Mark Baker »

Code: Select all

 
echo '<ul>
    <li><a href="main.php?location="'.$row["locationName"].'">'.$row["locationName"].'</a></li></ul>';
 
yorkshireflatcap
Forum Newbie
Posts: 5
Joined: Fri Oct 16, 2009 7:52 am

Re: Li element selection.

Post by yorkshireflatcap »

Hi Mark,

I've done exactly as you've stated :

Code: Select all

 
echo '<ul>
      <li><a href="main.php?location="'.$row["locationName"].'">'.$row["locationName"].'</a></li></ul>';
 
but the location comes up as blank.

Any suggestions?

Regards

John
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Li element selection.

Post by Mark Baker »

What does the html look like if you do a "View Source"?
User avatar
desperado
Forum Commoner
Posts: 46
Joined: Wed Dec 10, 2008 8:49 am

Re: Li element selection.

Post by desperado »

Code: Select all

 
echo '<ul><li><a href="main.php?location=' . $row['locationName'] . '">' . $row['locationName'] . '</a></li></ul>';
 
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Li element selection.

Post by Mark Baker »

You're right desperado, I managed to snunk an extra " in there. Well spotted!
yorkshireflatcap
Forum Newbie
Posts: 5
Joined: Fri Oct 16, 2009 7:52 am

Re: Li element selection.

Post by yorkshireflatcap »

Thanks desperado you sorted out the issue for me.

However, Mark's solution to have a look at the source pin pointed the problem exactly and that was a missing double quotes.

Many thanks to both of you.

Regards

John
yorkshireflatcap
Forum Newbie
Posts: 5
Joined: Fri Oct 16, 2009 7:52 am

Re: Li element selection.

Post by yorkshireflatcap »

One more question - How do I get it to save the user's menu selection? OK, I have the following code :

Code: Select all

 
while ($result = display_tree($num))
{
    echo"<ul>
        <li>";
        while ($row = mysql_fetch_array($result))
        {
            if ($row["menuID"] == 0)
            {
                echo "<a href='index.php'>".$row["locationName"]."</a>"; /* County name */
            }
            else
            {
                echo '<ul><li><a href="index.php?location=' . $row['locationName'] . '">' . $row['locationName'] .'</a></li></ul>'; /* location within county */
            }
        }
        echo"   </li>
                </ul>";
        $num++;
        mysql_free_result($result); 
}
 
This code works in the way that as the user moves the mouse pointer down the menu items, the location is being reflected correctly - index.php?location=<location>.

However, when I do the following code:

Code: Select all

 
if (isset($_GET['location']))
{
   // do something
}
 
It's always true - not what I want!

What I'd like to happen is that when the user clicks a location in the menu list, the location var is then set and then I can perform the following logic:

Code: Select all

 
if (isset($_GET['location']))
{
   if (location == "Luton")
   {
      // perform code to create new page within index.php
   }
}
 
As you can probably gather, I am new at PHP.... well last time I did PHP was 10+ years ago!!

Regards

John
Post Reply