tags:

views:

103

answers:

8

I want to create a simple menu function which can call it example get_menu()

Here is my current code.

<?php 
$select = 'SELECT * FROM pages';
$query  = $db->rq($select);
while ($page = $db->fetch($query)) { 
$id = $page['id'];
$title = $page['title'];
?>
<a href="page.php?id=<?php echo $id; ?>" title="<?php echo $title; ?>"><?php echo $title; ?></a>
<?php } ?>

How to do that in?

function get_menu() {
}

Let me know.

A: 

just put function get_menu() { above your code and } below

Col. Shrapnel
+3  A: 

Here is the function for that:

function get_menu(&$db)
{
    $select = 'SELECT * FROM pages';
    $query  = $db->rq($select);
    $menu = '';

    while ($page = $db->fetch($query)) { 
    $id = $page['id'];
    $title = $page['title'];

    $menu .= '<a href="page.php?id=' . $id . '" &title="' . $title .'></a>'
    }

    return $menu;
}

.

Some Quick Corrections In Your Script:

  • You were missing = after id
  • You were missing & after title

Suggestion:

You can give your menu links a class and style as per your menu needs :)

Sarfraz
if you reset $menu in the while-loop, this won't work as expected. you have to reset it before... and because of this is a function and there is nothing before it, you don't need to reset it...
oezi
hehe, good point. but an array for the future template use would be better.
Col. Shrapnel
@oezi: fixed even before reading your comment, thanks anyways :)
Sarfraz
@oezi not reset but define
Col. Shrapnel
thanks sarfraz. Fatal error: Call to a member function rq() on a non-object in D:\Web\xampp\htdocs\mini\index.php on line 74 how to fix the rq() ?
bob
@bob: simply put ` global $db;` in the function, see my updated answer. Thanks
Sarfraz
thetaiko
@thetaiko: Thanks for pointing that :)
Sarfraz
thank Sarfraz! work like a charm cheer!~~
bob
@bob: You are welcome :)
Sarfraz
+1  A: 

now here you already have a mistake:

<a href="page.php?id=<?php echo $id; ?>" title="<?php echo $title; ?>"><?php echo $title; ?></a>

notice the = after id

you can't be too careful

Alexander
A: 

or like this??

function get_menu( $title, $id ) {
    $menu = '';

    $menu .= '<a href="page.php?id' . $id . '" title="' . $title .'></a>'

    echo $menu;
}
------------------------
   $select = 'SELECT * FROM pages';
    $query  = $db->rq($select);
    while ($page = $db->fetch($query)) { 
        $id = $page['id'];
        $title = $page['title'];

        get_menu($title, $id );
    }
Hanseh
+1  A: 

First, separate the part where you're doing something, and the one used to display things.

Second, the alternative syntax looks better for the display part.

<?php
function get_menu(){
  $items = array();
  $select = 'SELECT * FROM pages';
  $query  = $db->rq($select);
  while ($page = $db->fetch($query)) { 
    $items[] = $page['id'];
  }
  return $items;
}

$menuItems = get_menu();
?>
<ul>
<?php foreach($menuItems as $item): ?>
  <li><a href="page.php?id=<?php echo $item['id']; ?>" title="<?php echo $item['title']; ?>"><?php echo $item['title']; ?></a></li>
<?php endforeach;?>
</ul>
Arkh
+3  A: 

get_menu() has to get reference to $db somehow. Probably the best and easiest way is to pass that reference as parameter:

function get_menu(MyDatabaseHandler $db) {
  // code proposed by Sarfraz here
}
Crozin
+1 for passing in the `$db`
thetaiko
Or you could call global $db
Psytronic
@Psytronic: `global` is a pure evil and everyone who uses it should be... - global is the worst solution anyway... ;)
Crozin
still a solution though :p but yes, it has it's many flaws
Psytronic
+2  A: 
function getMenu()
{
    $select = 'SELECT FROM pages';
    $query  = $db->rq($select);
    $menu = new Array;

    while ($page = $db->fetch($query)) { 
       $menu[] = '<a href="page.php?id=' . $page['id'] . '&title=' . $page['title'] .'"></a>';
    }

    return $menu;
}
Neo
A: 

The code Sarfraz posted is going to create invalid anchor tags (i.e. links). They'll also be missing names. Here is the shorter/faster version:

function get_menu($db)
{
    $result = $db->rq('SELECT id,title FROM pages');
    $menu = '';

    while ($page = $db->fetch($result))
    { 
        $id = $page['id'];
        $title = $page['title'];

        $menu .= "<a href='page.php?id={$id}&title={$title}'>{$title}</a>\n";
    }

    return $menu;
}

To use that do this:

echo get_menu($db);

The error you were getting was probably resulting from not passing the database connection to the function.

NOTE: It's generally not a good idea to show database ID numbers to the user in the interest of security; slugs are much better for identifying pages and are SEO friendly. Also, there shouldn't be any need to pass the page title to page.php because if you've got the ID you can get that when you need it from the database. Here's the code with this in mind:

function get_menu($db)
{
    $result = $db->rq('SELECT id,title FROM pages');
    $menu = '';

    while ($page = $db->fetch($result))
    { 
        $menu .= "<a href='page.php?id={$page['id']}'>{$page['title']}</a>\n";
    }

    return $menu;
}
Skrat