Page 1 of 1

Li element selection.

Posted: Fri Oct 16, 2009 8:06 am
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.

Re: Li element selection.

Posted: Fri Oct 16, 2009 8:21 am
by Mark Baker

Code: Select all

 
echo '<ul>
    <li><a href="main.php?location="'.$row["locationName"].'">'.$row["locationName"].'</a></li></ul>';
 

Re: Li element selection.

Posted: Fri Oct 16, 2009 10:07 am
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

Re: Li element selection.

Posted: Fri Oct 16, 2009 10:25 am
by Mark Baker
What does the html look like if you do a "View Source"?

Re: Li element selection.

Posted: Fri Oct 16, 2009 10:29 am
by desperado

Code: Select all

 
echo '<ul><li><a href="main.php?location=' . $row['locationName'] . '">' . $row['locationName'] . '</a></li></ul>';
 

Re: Li element selection.

Posted: Fri Oct 16, 2009 10:41 am
by Mark Baker
You're right desperado, I managed to snunk an extra " in there. Well spotted!

Re: Li element selection.

Posted: Fri Oct 16, 2009 11:15 am
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

Re: Li element selection.

Posted: Sat Oct 17, 2009 4:50 am
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