JakkkeM Posted December 28, 2012 Share Posted December 28, 2012 Hi all, I'm experiencing a lot of trouble trying to create a script for my website. I'm making a database dependant menu, and I've spent a couple of days doing trial and error much to no prevail. I'm using Twitter Bootstrap if anyone is familiar with it, and creating a drop down menu. The code for each of these buttons is: <div class="btn-group"> <a class="btn btn-inverse dropdown-toggle" data-toggle="dropdown" href="#"> WOD <span class="caret"></span> </a> <ul class="dropdown-menu"> <!-- dropdown menu links --> <li><a tabindex="-1" href="http://www.google.com.au">View the WOD</a></li> <li><a tabindex="-1" href="#">WOD Archives</a></li> <li class="divider"></li> <li><a tabindex="-1" href="#">Benchmarks</a></li> </ul> </div> So, what I need to be able to do is call each menu that I have in the database, (I have 5), determine if it's a drop down, and if it is, echo the links for it inside of the <ul class="dropdown-menu"> It seems simple to me, but I cannot get it right. Please help! Cheers. Quote Link to comment https://forums.phpfreaks.com/topic/272439-menu-script/ Share on other sites More sharing options...
Muddy_Funster Posted December 28, 2012 Share Posted December 28, 2012 your current PHP code? Quote Link to comment https://forums.phpfreaks.com/topic/272439-menu-script/#findComment-1401718 Share on other sites More sharing options...
JakkkeM Posted December 28, 2012 Author Share Posted December 28, 2012 If you REALLY want it: echo "<h1>Test Page</h1>"; // Fetch MENUs $fetch_menu = mysql_query("SELECT * FROM `menus` ORDER BY `stacking`"); // Function for LInks function linkCreate($menu_id){ // Fetch Links $fetch_links = mysql_query("SELECT * FROM `links` WHERE `assoc_menu` = '".$menu_id."'"); $num_links = mysql_num_rows($fetch_links); while($LINK = mysql_fetch_array($fetch_links, MYSQL_ASSOC)){ $return_link = "<li><a tabindex=\"-1\" href=\"#\">Health Assessment and Wavier</a></li>"; } return $num_links." ".$return_link; } while($MENU = mysql_fetch_array($fetch_menu, MYSQL_ASSOC)){ echo $MENU['title']."<br>"; echo linkCreate($MENU['menu_id']); } This comes close but only returns the first link... Probably because it's a function and I'm an idiot for using a function but hey. // Do the query for all the menus $query = mysql_query("SELECT * FROM `menus` LEFT JOIN `links` ON `menus`.`menu_id` = `links`.`assoc_menu`"); // Set some functions function createButton($btn_type, $btn_title, $TYPE, $standard_url){ if($TYPE == "1"){ // If it's a drop down execute: $button = "<a class=\"btn ".$btn_type." dropdown-toggle\" data-toggle=\"dropdown\" href=\"#\">".$btn_title." <span class=\"caret\"></span></a>"; }elseif($TYPE == "0"){ // If it's a standard button execute: $button = "<a class=\"btn ".$btn_type."\" href=\"".$standard_url."\">".$btn_title."</a>"; }else{ // If it's not specified, error: $button = "<a class=\"btn btn-warning\" href=\"#\">Button Error</a>"; } return $button; } function createDropdown(){ } // Set the $prev_title variable to prevent repeating the button $prev_title = ""; // Execute an array for all menus while($MENU = mysql_fetch_array($query, MYSQL_ASSOC)){ // If this is a new button - print the button if($MENU['title'] != $prev_title){ // If the titles are not the same, THIS IS THE BUTTON $return_menu = $MENU['title']."<br>"; }elseif($MENU['title'] == $prev_title){ // If the titles are the same, THIS IS A LINK. $return_menu = $MENU['link_title']."<br>"; } // Print whatever is necessary echo $return_menu; // Reset the prev_title for the next menu $prev_title = $MENU['title']; } This returns mostly OK, but removes the first link, and I can't put the links in the UL as required. Quote Link to comment https://forums.phpfreaks.com/topic/272439-menu-script/#findComment-1401727 Share on other sites More sharing options...
Muddy_Funster Posted December 28, 2012 Share Posted December 28, 2012 ok, last thing I'll need is your table structure and a few lines of sample data. PS. why would you think there was something wrong with using a function? Quote Link to comment https://forums.phpfreaks.com/topic/272439-menu-script/#findComment-1401730 Share on other sites More sharing options...
JakkkeM Posted December 28, 2012 Author Share Posted December 28, 2012 Well, I'm essentially trying to get the function to return multiple solutions when they're (to my understanding) designed to output one. Image of the Menu Table Image of the Link Table Hope that helps For the record - the table names are "menus" and "links" Quote Link to comment https://forums.phpfreaks.com/topic/272439-menu-script/#findComment-1401731 Share on other sites More sharing options...
timothyarden Posted December 28, 2012 Share Posted December 28, 2012 (edited) Hi JakkkeM, Functions can return multiple values in two ways; function functionName(){ //code return $value1, $value2; // etc - comes back in a array } // OR function functionName(){ //code $value1 = 'value1'; $value2 = 'value2'; $array = array($value1,$value2); return $array; } When you then recieve the result by doing something like: $function = functionName(); $function will be equal to that array returned just like it would equal a single value if you just returned a variable. If you need more help feel free to ask otherwise here are two links which you might find helpful: - http://www.w3schools.../php_arrays.asp (For Arrays) - http://www.w3schools...p_functions.asp (For Functions) W3Schools.com is a great place to learn; its where I learnt and also php.net is also helpful for learning about specific built in functions in PHP. Hope this helps. Timothy Arden PS Sorry I couldnt indent my code examples -> kept coming up with this '' so I just edited it and left it unidented Edited December 28, 2012 by timothyarden Quote Link to comment https://forums.phpfreaks.com/topic/272439-menu-script/#findComment-1401739 Share on other sites More sharing options...
JakkkeM Posted December 28, 2012 Author Share Posted December 28, 2012 Hi JakkkeM, Functions can return multiple values in two ways; function functionName(){ //code return $value1, $value2; // etc - comes back in a array } // OR function functionName(){ //code $value1 = 'value1'; $value2 = 'value2'; $array = array($value1,$value2); return $array; } When you then recieve the result by doing something like: $function = functionName(); $function will be equal to that array returned just like it would equal a single value if you just returned a variable. If you need more help feel free to ask otherwise here are two links which you might find helpful: - http://www.w3schools.../php_arrays.asp (For Arrays) - http://www.w3schools...p_functions.asp (For Functions) W3Schools.com is a great place to learn; its where I learnt and also php.net is also helpful for learning about specific built in functions in PHP. Hope this helps. Timothy Arden PS Sorry I couldnt indent my code examples -> kept coming up with this '' so I just edited it and left it unidented The only problem with this, is I don't have a set amount of values - in your example you used two, I have no set amount of links that could be assigned to a menu. Does that matter? Quote Link to comment https://forums.phpfreaks.com/topic/272439-menu-script/#findComment-1401740 Share on other sites More sharing options...
Muddy_Funster Posted December 28, 2012 Share Posted December 28, 2012 W3schools is a crap place to learn - you just won't know that untill you you're past using it. I wonder if the admins can set the forum to auto-ban anyone that links to it on here.... anyway, getting back on topic after that rather random piece of promotion. I really can't see what your using to link the formatting to the elements, so I added a couple comments into the code to highlight where you are likely going to need to add these. I basicly only changed your SELECT * (because it's naughty) and added in a small bit of code to refactor the results from the database into something that's more menu-maker-friendly here's the code I came up with - as pretty much always : it's untested and unlikely to work as is, but should be enough to get you moving on the right track. $sql = <<<SQL SELECT link_id, links.assoc_menu as assoc_menu, link_title, link_url, title, type, btn_type, dropdown, stacking, menus.assoc_menu as m_assoc_menu, url as menu_url FROM menus INNER JOIN links ON (links.assoc_menu = menus.menu_id) WHERE link_permissions = 1 SQL; $query = mysql_query($sql) or die(mysql_error()); $startList = mysql_fetch_assoc($query); $refactorList = array(); foreach($startList as $line){ $refatorList[$line['assoc_menu']][] = array( 'link_id' => $line['link_id'], 'link_title' => $line['link_title'], 'link_url' => $line['link_url'], 'title' => $line['link_title'], 'btn_type' => $line['btn_type'], 'dropdown' => $line['dropdown'], 'stacking' => $line['stacking'], 'm_assoc_menu' => $line['m_assoc_menu'], 'menu_url' => $line ['menu_url'] ); } foreach($refactorList as $menu){ echo '<div class="your-ui-menu-class">'.$menu['0']['title']."\n\r <ul id=\"{$menu['0']['title']}\"> \n\r"; // ^ echo to output each menu title and start the <ul> foreach($menu as $link){ echo "<li><a href=\"{$link['link_url']}\">{$link['link_title']}</a></li>\n\r"; // ^ echo to output each link within the menu } echo "</ul> \n\r </div> \n\r"; } Quote Link to comment https://forums.phpfreaks.com/topic/272439-menu-script/#findComment-1401741 Share on other sites More sharing options...
timothyarden Posted December 28, 2012 Share Posted December 28, 2012 Forgot to mention; with the returned array to get the info from it you can either use echo $array[$number]; // $number should be equal to 1 before the one you want as array indexes start at zero //example echo $array[0]; // echo's $value1 echo $array[1]; // echo's $value2 // or foreach($array as $value){ echo $value; } Timothy Quote Link to comment https://forums.phpfreaks.com/topic/272439-menu-script/#findComment-1401743 Share on other sites More sharing options...
timothyarden Posted December 28, 2012 Share Posted December 28, 2012 No it doesn't matter how many values - an array can have a practically infinite amount of values, just read through them using the code I gave above. Timothy Quote Link to comment https://forums.phpfreaks.com/topic/272439-menu-script/#findComment-1401745 Share on other sites More sharing options...
Muddy_Funster Posted December 28, 2012 Share Posted December 28, 2012 Forgot to mention; with the returned array to get the info from it you can either use echo $array[$number]; // $number should be equal to 1 before the one you want as array indexes start at zero //example echo $array[0]; // echo's $value1 echo $array[1]; // echo's $value2 Timothy that's completly useless for an associative array Quote Link to comment https://forums.phpfreaks.com/topic/272439-menu-script/#findComment-1401747 Share on other sites More sharing options...
JakkkeM Posted December 28, 2012 Author Share Posted December 28, 2012 Closer than I've ever gotten. But where the hell these values are coming from I have no idea? Image One - The Dropdown Image Two - The Buttons $sql = "SELECT `link_id`, `links`.`assoc_menu` as `assoc_menu`, `link_title`, `link_url`, `title`, `type`, `btn_type`, `dropdown`, `stacking`, `menus`.`assoc_menu` as `m_assoc_menu`, `url` as `menu_url` FROM `menus` INNER JOIN `links` ON (`links`.`assoc_menu` = `menus`.`menu_id`) WHERE `link_permissions` = '1'"; $query = mysql_query($sql) or die(mysql_error()); $startList = mysql_fetch_assoc($query); $refactorList = array(); foreach($startList as $line){ $refactorList[$line['assoc_menu']][] = array( 'link_id' => $line['link_id'], 'link_title' => $line['link_title'], 'link_url' => $line['link_url'], 'title' => $line['link_title'], 'btn_type' => $line['btn_type'], 'dropdown' => $line['dropdown'], 'stacking' => $line['stacking'], 'm_assoc_menu' => $line['m_assoc_menu'], 'menu_url' => $line ['menu_url'] ); } foreach($refactorList as $menu){ echo '<div class="btn-group"><a class="btn '.$menu['0']['btn_type'].'\n\r dropdown-toggle" data-toggle="dropdown" href="#">'.$menu['0']['title'].'<span class="caret"></span></a><ul class="dropdown-menu">'; // echo '<div class="your-ui-menu-class">'.$menu['0']['title']."\n\r <ul id=\"{$menu['0']['title']}\"> \n\r"; // ^ echo to output each menu title and start the <ul> foreach($menu as $link){ echo "<li><a href=\"{$link['link_url']}\">{$link['link_title']}</a></li>\n\r"; // ^ echo to output each link within the menu } echo "</ul> \n\r </div> \n\r"; } Quote Link to comment https://forums.phpfreaks.com/topic/272439-menu-script/#findComment-1401751 Share on other sites More sharing options...
timothyarden Posted December 28, 2012 Share Posted December 28, 2012 Yes, Sorry looked over his code earlier and didnt refresh myself before sending that. It should be $arrayName[$reference]. Example: $array = array('value1' => 'value', 'value 2' = 'value'); // and then echo $array['value1']; echo $array['value2']; As Muddy Funster said my original post was wrong and above is how you would reference to a value in associative array. You can have as many values in the array as you want as I said before. Hope this helps to clarify my mistake. Timothy Quote Link to comment https://forums.phpfreaks.com/topic/272439-menu-script/#findComment-1401752 Share on other sites More sharing options...
Muddy_Funster Posted December 28, 2012 Share Posted December 28, 2012 (edited) JakkkeM, can you post the rendered page source for those dropdowns? Edt: just noticed that you have \n\r inside single quotes - they will not be parsed properly unless they are in double quotes. you can delete them, they were just to add a lttle formating to the final page source to make it slightly easier to read. Edited December 28, 2012 by Muddy_Funster Quote Link to comment https://forums.phpfreaks.com/topic/272439-menu-script/#findComment-1401755 Share on other sites More sharing options...
JakkkeM Posted December 28, 2012 Author Share Posted December 28, 2012 <div class="btn-group"> <a class="btn btn-inverse dropdown-toggle" data-toggle="dropdown" href="#"> WOD <span class="caret"></span> </a> <ul class="dropdown-menu"> <!-- dropdown menu links --> <li><a tabindex="-1" href="http://www.google.com.au">View the WOD</a></li> <li><a tabindex="-1" href="#">WOD Archives</a></li> <li class="divider"></li> <li><a tabindex="-1" href="#">Benchmarks</a></li> </ul> </div> ?? Quote Link to comment https://forums.phpfreaks.com/topic/272439-menu-script/#findComment-1401756 Share on other sites More sharing options...
Muddy_Funster Posted December 28, 2012 Share Posted December 28, 2012 that....looks.....right? :/ Quote Link to comment https://forums.phpfreaks.com/topic/272439-menu-script/#findComment-1401757 Share on other sites More sharing options...
JakkkeM Posted December 28, 2012 Author Share Posted December 28, 2012 It's returning it absolutely fine! Just without the correct values in the there? I.e. my titles are not 1, V, ? etc... Quote Link to comment https://forums.phpfreaks.com/topic/272439-menu-script/#findComment-1401758 Share on other sites More sharing options...
Muddy_Funster Posted December 28, 2012 Share Posted December 28, 2012 try a print_r($refactorList) to check whats in the array. Quote Link to comment https://forums.phpfreaks.com/topic/272439-menu-script/#findComment-1401760 Share on other sites More sharing options...
JakkkeM Posted December 28, 2012 Author Share Posted December 28, 2012 Holy Moly... Quote Link to comment https://forums.phpfreaks.com/topic/272439-menu-script/#findComment-1401762 Share on other sites More sharing options...
Muddy_Funster Posted December 28, 2012 Share Posted December 28, 2012 hmm, ok, let's make a change and see what we get, change $startList = mysql_fetch_assoc($query); to while($result = mysql_fetch_assoc($query){ $startList[] = $result; } and try the print_r() again Quote Link to comment https://forums.phpfreaks.com/topic/272439-menu-script/#findComment-1401764 Share on other sites More sharing options...
JakkkeM Posted December 28, 2012 Author Share Posted December 28, 2012 Almost! So close! The last button - should be a link... Quote Link to comment https://forums.phpfreaks.com/topic/272439-menu-script/#findComment-1401780 Share on other sites More sharing options...
Muddy_Funster Posted December 28, 2012 Share Posted December 28, 2012 ok, so that's looking a bit healthier, go back to the original echoes and then show us the page source Quote Link to comment https://forums.phpfreaks.com/topic/272439-menu-script/#findComment-1401784 Share on other sites More sharing options...
Christian F. Posted December 28, 2012 Share Posted December 28, 2012 One little tip: Either use echo "<pre>"; before the print_r () call, or click on "View source" to see a formatted version of the output. Makes it a whole lot easier to actually read what it says. Quote Link to comment https://forums.phpfreaks.com/topic/272439-menu-script/#findComment-1401839 Share on other sites More sharing options...
JakkkeM Posted December 29, 2012 Author Share Posted December 29, 2012 (edited) <h1>Test Page</h1><div class="btn-toolbar"><div class="btn-group"><a class="btn btn-inverse dropdown-toggle" data-toggle="dropdown" href="#">View the WOD<span class="caret"></span></a><ul class="dropdown-menu"><li><a href="?page=home">View the WOD</a></li> <li><a href="?page=home">WOD Archives</a></li> <li><a href="?page=home">Benchmakrs</a></li> </ul> </div> <div class="btn-group"><a class="btn btn-inverse dropdown-toggle" data-toggle="dropdown" href="#">Who, What, When, Where and Why?<span class="caret"></span></a><ul class="dropdown-menu"><li><a href="?page=home">Who, What, When, Where and Why?</a></li> <li><a href="?page=home">Services and Pricing</a></li> <li><a href="?page=growth-of-sport">Growth of Sport</a></li> <li><a href="?page=what-is-crossfit">What is CrossFit?</a></li> </ul> </div> <div class="btn-group"><a class="btn btn-inverse dropdown-toggle" data-toggle="dropdown" href="#">What You Need To Know<span class="caret"></span></a><ul class="dropdown-menu"><li><a href="?page=need-to-know">What You Need To Know</a></li> <li><a href="?page=contacting-us">Contacting Us</a></li> <li><a href="?page=health-assessment-and-waiver">Health Assessment and Wavier</a></li> </ul> </div> <div class="btn-group"><a class="btn btn-inverse dropdown-toggle" data-toggle="dropdown" href="#">How it Works<span class="caret"></span></a><ul class="dropdown-menu"><li><a href="?page=how-it-works">How it Works</a></li> </ul> </div> </div>Array ( [1] => Array ( [0] => Array ( [link_id] => 1 [link_title] => View the WOD [link_url] => ?page=home [title] => View the WOD [btn_type] => btn-inverse [dropdown] => 1 [stacking] => 1 [m_assoc_menu] => 0 [menu_url] => ) [1] => Array ( [link_id] => 2 [link_title] => WOD Archives [link_url] => ?page=home [title] => WOD Archives [btn_type] => btn-inverse [dropdown] => 1 [stacking] => 1 [m_assoc_menu] => 0 [menu_url] => ) [2] => Array ( [link_id] => 3 [link_title] => Benchmakrs [link_url] => ?page=home [title] => Benchmakrs [btn_type] => btn-inverse [dropdown] => 1 [stacking] => 1 [m_assoc_menu] => 0 [menu_url] => ) ) [2] => Array ( [0] => Array ( [link_id] => 4 [link_title] => Who, What, When, Where and Why? [link_url] => ?page=home [title] => Who, What, When, Where and Why? [btn_type] => btn-inverse [dropdown] => 0 [stacking] => 2 [m_assoc_menu] => 0 [menu_url] => ?page=login ) [1] => Array ( [link_id] => 5 [link_title] => Services and Pricing [link_url] => ?page=home [title] => Services and Pricing [btn_type] => btn-inverse [dropdown] => 0 [stacking] => 2 [m_assoc_menu] => 0 [menu_url] => ?page=login ) [2] => Array ( [link_id] => 6 [link_title] => Growth of Sport [link_url] => ?page=growth-of-sport [title] => Growth of Sport [btn_type] => btn-inverse [dropdown] => 0 [stacking] => 2 [m_assoc_menu] => 0 [menu_url] => ?page=login ) [3] => Array ( [link_id] => 7 [link_title] => What is CrossFit? [link_url] => ?page=what-is-crossfit [title] => What is CrossFit? [btn_type] => btn-inverse [dropdown] => 0 [stacking] => 2 [m_assoc_menu] => 0 [menu_url] => ?page=login ) ) [3] => Array ( [0] => Array ( [link_id] => 8 [link_title] => What You Need To Know [link_url] => ?page=need-to-know [title] => What You Need To Know [btn_type] => btn-inverse [dropdown] => 1 [stacking] => 3 [m_assoc_menu] => 0 [menu_url] => ) [1] => Array ( [link_id] => 9 [link_title] => Contacting Us [link_url] => ?page=contacting-us [title] => Contacting Us [btn_type] => btn-inverse [dropdown] => 1 [stacking] => 3 [m_assoc_menu] => 0 [menu_url] => ) [2] => Array ( [link_id] => 10 [link_title] => Health Assessment and Wavier [link_url] => ?page=health-assessment-and-waiver [title] => Health Assessment and Wavier [btn_type] => btn-inverse [dropdown] => 1 [stacking] => 3 [m_assoc_menu] => 0 [menu_url] => ) ) [4] => Array ( [0] => Array ( [link_id] => 11 [link_title] => How it Works [link_url] => ?page=how-it-works [title] => How it Works [btn_type] => btn-inverse [dropdown] => 1 [stacking] => 4 [m_assoc_menu] => 0 [menu_url] => ) ) ) Edited December 29, 2012 by JakkkeM Quote Link to comment https://forums.phpfreaks.com/topic/272439-menu-script/#findComment-1401974 Share on other sites More sharing options...
JakkkeM Posted December 29, 2012 Author Share Posted December 29, 2012 That's the source code Quote Link to comment https://forums.phpfreaks.com/topic/272439-menu-script/#findComment-1401975 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.