Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9191137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:36:47+00:00 2026-06-17T20:36:47+00:00

I have a menu which get its items from database. I want to get

  • 0

I have a menu which get its items from database.
I want to get the menu id of clicked menu in following for each:

public function gen_menu($menuItems, $pId = 0)
{
    $menu       = '';
    $ulStart    = 0;
    $base_url   = base_url();
    $uri        = $this->uri->segment(1);
    foreach($menuItems as $row)
    {
        if($row->parent_id==$pId)
        {
            if($ulStart==0) { $menu .= "<UL>"; $ulStart++; }
                $url = $row->item_url;
                stripos($url,$base_url)===0 || $url==""?$base_url = base_url():$base_url="";
                {
                    if($row->external==1){
                        $menu .= '<LI><a href="'.$row->custom_url.'" title="'.$row->alt_title.'">'.$row->item_title.'</a>'.$this->gen_menu($menuItems, $row->item_id).'</LI>';
                    }
                    else{
                        $class = ($uri==$row->custom_url)?"class='selected'":"";
                        $menu .= '<LI '.$class.'><a href="'.site_url($row->custom_url).'/'.$row->item_id.'" title="'.$row->alt_title.'" onclick="'.$this->get_id($row->item_id).'">'.$row->item_title.'</a>'.$this->gen_menu($menuItems, $row->item_id).'</LI>';
                    }
                }
        }

    }
    if($ulStart!=0) { $menu .= "</UL>"; }
    return $menu;
}`

When I am using onclick in it returns the last id of table always.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T20:36:48+00:00Added an answer on June 17, 2026 at 8:36 pm

    Try this:

    $menu .= '<LI '.$class.'><a href="'.site_url($row->custom_url).'/'.$row->item_id.'" title="'.$row->alt_title.'" onclick="javascript:get_id('.$row->item_id.');">'.$row->item_title.'</a>'.$this->gen_menu($menuItems, $row->item_id).'</LI>';
    

    You had $row->item_id inside the quoted string, so it wasn’t expanded. You should have gotten a javascript error from that.

    UPDATE:

    The problem you’re having is that you’re confusing server-side and client-side actions. The code in this PHP script all runs on the server when it’s creating the page to send to the user. If you’re setting a session variable in the loop, the variable will contain the value from the last iteration. Putting PHP code inside onclick doesn’t run it when the user clicks, it runs it when the server is composing the onclick attribute to put in the HTML that gets sent to the browser.

    If you want to do something on the server when the user clicks, you need to put a Javascript function in the onclick attribute, and that function should use AJAX to call another PHP script.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have dropdown menu..which is dynamic.. How can get value of the last item
I have a menu item which has children menu items. When one of the
I have a menu item to which I add another item. Now I want
I have a menu which looks like this: When the user clicks on Recruitment,
Suppose we have a menu which presents the user with some options: Welcome: 1)
I have a jQuery menu which is build up by like this: <h3 id='300'
I have a menu div which has a dark background. Inside it, I have
I have a drop down menu which you can see here on jsfiddle .
I have a drop down menu which fills with some doc names. Now when
I have an extensive menu in which I would like to search for and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.