Question

I hope somebody can help me, because i got an menu that is auto generated via my MySQL db.

Because i got the menu to work inside the website and with that i mean it works with "test.dk/about" but the a href is empty when it's going out of the website like "http://google.com"...

btw it's just a very simple UL LI menu no dropdown or something.

Here is my script

    static function build_menu()
{
    $result = mysql_query("SELECT * FROM menu");
    $menu = array();
    while ($row = mysql_fetch_assoc($result)) {
        if ($row["is_external"]) {
            $url = $row["url"];
        } else if (empty($row["is_external"])) {
            $url = get_page_url($row["page_id"]);
        }
        $menu[] = array("name" => $row["name"], "page_id" => $row["page_id"], "is_external" => $row["url"], "url" => $url);
    }
    return $menu;
}

static function get_page_url($page_id)
{
    $result = mysql_query("SELECT view_id FROM page WHERE id = '$page_id'");
    $result = mysql_fetch_assoc($result);
    $view_id = $result["view_id"];
    $result = mysql_query("SELECT listen_path FROM view WHERE id = '$view_id'");
    $result = mysql_fetch_assoc($result);
    $listen_path = $result["listen_path"];
    return $listen_path;
}

static function render()
{
    $result = mysql_query("SELECT * FROM menu"); ?>
    <div class="menu">
    <ul><?php while ($item = mysql_fetch_assoc($result)) { ?>
            <li><a href="<?php echo self::get_page_url($item["page_id"]) ?>"><?php echo $item["name"] ?></a>
            </li>                <?php } ?></ul></div><?php
}

How can i fix it, so it works both internal and external?

<div class="menu"> <ul> <li><a href="/">Homepage</a></li> <li><a href="/about">About</a></li> <li><a href="/Develop">Develop</a></li> <li><a href="">Support</a></li>

This should be <li><a href="http://our.umbraco.org/">Support</a></li>; </ul> </div>

Was it helpful?

Solution 3

I got it work after a while!

I simply just created an If else statement in the render function

static function render(){
    $menu_items = self::get();
    ?><div class="menu"><ul><?php while ($item = mysql_fetch_assoc($menu_items)) { ?>
            <li><a href="<?php

                if(empty($item["is_external"]))
                {
                    echo self::get_page_url($item["page_id"]);
                }

                else if($item["is_external"] = 1)
                {
                    echo $item["url"];
                }

                 ?>"><?php echo $item["name"] ?></a>
            </li>                <?php } ?></ul></div><?php
}

OTHER TIPS

You only check for an external link in the function build_menu(), but this function isn't called anywhere from your render() function.

The render() function only calls get_page_url() which doesn't distinguish between internal and external links.

Href parameter of external URL must start with protocol declaration, so with "http://" in your case.

So change your code in condition inside the function "build_menu", if the URL is external, add "http://" to it, something like this:

$url = 'http://'.$row["url"];

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top