0

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.

0

8 Answers 8

3

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 :)

Sign up to request clarification or add additional context in comments.

6 Comments

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...
hehe, good point. but an array for the future template use would be better.
@oezi: fixed even before reading your comment, thanks anyways :)
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: simply put ` global $db;` in the function, see my updated answer. Thanks
|
3

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
}

1 Comment

@Psytronic: global is a pure evil and everyone who uses it should be... - global is the worst solution anyway... ;)
1

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

Comments

1

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>

Comments

1

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;
}

Comments

0

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

Comments

0

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 );
    }

Comments

0
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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.