views:

76

answers:

3
+1  Q: 

PHP "dynamic" Menu

I am just starting PHP (as in today). I want to create a customizable menu using a jquery script that can have a variable amount of items.

I am getting an error when i run this. The error is:

Parse error: syntax error, unexpected T_VARIABLE in /home/s0urc3/public_html/files01/menu.php on line 5

Thanks to Chase for his answer

index.php:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
<head>
<?PHP
$script_url="http://files01.s0urc3.ismywebsite.com/jquery/nagging-menu/nagging-menu.js";
$menu_css="http://files01.s0urc3.ismywebsite.com/jquery/nagging-menu/style.css";
$links = array(
    array("url" => "http://www.something1.com", "label" => "something"),
    array("url" => "http://www.something2.com", "label" => "something2"),
    array("url" => "http://www.something3.com", "label" => "something3"),
);
include("menu.php");
?>
<meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"/>
<title></title>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon"/>
<link rel="stylesheet" type="text/css" href=".css"/>
</head>
<body>
<?=writeMenu($links, $menu_css, $script_url)?>
</body>
</html>

menu.php:

<?
function writeMenu($links, $script_url, $menu_css){
$menu = '<link href=\"$menu_css\" type=\"text/css\">'
    $menu = '<div id="navi">';
    $menu .= '<div id="menu" class="default">';
    $menu .= '<ul>';

    foreach ($links as $item) {
        $menu .= "<li><a href=\"".$item['url']."\">".$item['label']."</a></li>";
    }

    $menu .= "</ul>";
    $menu .= "</div>";
    $menu .= "<script type=\"text/javascript\" src=\"http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js\" charset=\"utf-8\"></script>";
    $menu .= "<script type=\"text/javascript\" src=$script_url charset=\"utf-8\"></script>";

    return $menu;
}
?>

Thanks to Chase for his Re-script

+1  A: 

Let me say I am not too familiar with jquery, so I will only comment on your php use.

First off, you will need to use quotes around string like this: print("<li><a href=$link_1_url>$link_1_label</a></li>")

Then there is the question why you are using a switch like this and then copying the same thing and adding some. You can easily do it as follows instead:

if ($items  >= 1)
{
     // print line 1
}

if ($items >= 2)
{
     // print line 2
}
if ($items >= 3)
{
     // print line 3
}

This will make sure you don't have to copy the same thing over and over again. The same thing can be done with a switch as below, but this code is harder to understand:

$out = "";
switch ($items)
{
case 3:
   $out = "line3" . $out;
case 2:
   $out = "line2" . $out;
case 1:
   $out = "line1" . $out;
   print($out);
   break;
}

If you are wondering how that works, take a good look and keep in mind that I have only one break statement. This is just harder to understand and less clear, though, so it's just not recommended. However, as the only thing you are changing each time is a number, you can use the for-loop, which was made for just that purpose:

for ($i = 0; $i < $items; $i++)
{
   print("line " . $i);
}

Now you see, that's a lot shorter and easier, yet very clear.

edit: I was missing one thing: the long line of urls you had up there. One thing to learn when you program is to keep some neat whitespace, it's barely even clear that we are talking about a function here. Take a look at my code and yours... mine is readable while yours is not and that's all due to the whitespace I inserted and you didn't... Anyway, you probably you to use an array:

function printMenu ($urls)
{
    foreach ($urls as $url)
    {
        print("<a href='" . $url . "'>Link!</a>");
    }
}

// Now you can do:
printMenu(array("url1", "url2", "url3"));
Jasper
A: 
<?

$links = array(
    array("url" => "http://www.something1.com", "label" => "something"),
    array("url" => "http://www.something2.com", "label" => "something2"),
    array("url" => "http://www.something3.com", "label" => "something3"),
);


function writeMenu($links){

    $menu = '<div id="navi">';
    $menu .= '<div id="menu" class="default">';
    $menu .= '<ul>';

    foreach ($links as $item) {
        $menu .= "<li><a href=\"".$item['url']."\">".$item['label']."</a></li>";
    }

    $menu .= "</ul>";
    $menu .= "</div>";
    $menu .= "<script type=\"text/javascript\" src=\"http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js\" charset=\"utf-8\"></script>";
    $menu .= "<script type=\"text/javascript\" src=$script_url charset=\"utf-8\"></script>";

    return $menu;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<head>
  <meta name="generator" content="HTML Tidy for Mac OS X (vers 14 February 2006), see www.w3.org">
  <title></title>
</head>
<body>
<?=writeMenu($links)?>
</body>
</html>
Chase
A: 

Ok so i looked over the code so generously supplied by chase an solved my own issue. :D

here is the code of both menu.php and index.php

Menu.php:

<!--
PHP menu by ellisgeek
$email = '[email protected]';
$URL = 'http://s0urc3.ismywebsite.com'
Original code by chase on StackOverflow.com
-->
<?
function writeMenu($links, $css){
echo  '<link rel="stylesheet" type="text/css" href="$css" media="screen"/>';
echo  '<div id="navi"><div id="menu" class="fixed"><ul class=""> ';


foreach ($links as $item) {
    echo "<li><a href=".$item['url'].">".$item['label']."</a></li>";
}

echo  "</ul>";
echo  "</div>";
echo  "</div>";
}
?>

Index.php:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
<head>
<?
$css="style.css";
$links = array(
    array("url" => "http://www.something1.com", "label" => "something"),
    array("url" => "http://www.something2.com", "label" => "something2"),
    array("url" => "http://www.something3.com", "label" => "something3"),
);
include("menu.php");
?>
<meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"/>
<title></title>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon"/>
<link rel="stylesheet" type="text/css" href=".css"/>
</head>
<body>
<?=writeMenu($links, $menu_css, $script_url)?>
<p>Lorum Lorum Lorum Lorum Lorum Lorum Lorum Lorum Lorum Lorum Lorum Lorum Lorum Lorum Lorum Lorum Lorum Lorum Lorum Lorum Lorum Lorum</p>
<!--Add lotsa these-->
</body>
</html>

this will write a menu using a ul and a few div's feel free to copy n paste just don't remove the credit comment please.

ellisgeek