cactus Posted April 8, 2011 Share Posted April 8, 2011 Hi, I have a menu that is generated from my mysql database and I want it on every page of my website as the main menu. However I can''t figure out how to do this. I managed to get it on the view article page by duplicating the my query like this: function viewArticle() { if ( !isset($_GET["articleId"]) || !$_GET["articleId"] ) { homepage(); return; } $result['article'] = Article::getById( (int)$_GET["articleId"] ); //single record $result['pageTitle'] = $result['article']->title . " | Widget News"; $results = array(); $all_data = Article::getList( HOMEPAGE_NUM_ARTICLES ); //multiple record $results['articles'] = $all_data['results']; $results['totalRows'] = $all_data['totalRows']; require( TEMPLATE_PATH . "/viewArticle.php" ); } However I can't figure out how to place it on for example the new article page. function newArticle() { $results = array(); $results['pageTitle'] = "New Article"; $results['formAction'] = "newArticle"; if ( isset( $_POST['saveChanges'] ) ) { // User has posted the article edit form: save the new article $article = new Article; $article->storeFormValues( $_POST ); $article->insert(); header( "Location: admin.php?status=changesSaved" ); } elseif ( isset( $_POST['cancel'] ) ) { // User has cancelled their edits: return to the article list header( "Location: admin.php" ); } else { // User has not posted the article edit form yet: display the form $results['article'] = new Article; require( TEMPLATE_PATH . "/admin/editArticle.php" ); } } Can anybody help me out i'm really struggling? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/233078-multiple-record-array/ Share on other sites More sharing options...
nethnet Posted April 8, 2011 Share Posted April 8, 2011 Put all of your functions that will be used across multiple scripts in an include file, something like 'functions.inc.php' and then include them at the start of every page. include "functions.inc.php"; That way you won't have to duplicate code. Quote Link to comment https://forums.phpfreaks.com/topic/233078-multiple-record-array/#findComment-1198687 Share on other sites More sharing options...
cactus Posted April 8, 2011 Author Share Posted April 8, 2011 I was going to do that once i'd figured out which section of the code would be duplicated on each page. Do you know which bit it would be, i've tried various ways and none of them are working? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/233078-multiple-record-array/#findComment-1198692 Share on other sites More sharing options...
cactus Posted April 8, 2011 Author Share Posted April 8, 2011 I tried to add in the following code to another page <div id="menu"> <ul id="headlines"> <?php foreach ( $results['articles'] as $article ) { ?> //THIS LINE <li> <a href=".?action=viewArticle&articleId=<?php echo $article->id?>"><?php echo htmlspecialchars( $article->title )?></a> </li> <?php } ?> </ul> </div> However I keep getting the following errors: Undefined index: articles and Invalid argument supplied for foreach() they appear on the line highlighted above can anybody help? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/233078-multiple-record-array/#findComment-1198712 Share on other sites More sharing options...
cactus Posted April 9, 2011 Author Share Posted April 9, 2011 Can someone please help me with the errors I am getting I don't understand how to fix them? Please. Thanks in anticipation Quote Link to comment https://forums.phpfreaks.com/topic/233078-multiple-record-array/#findComment-1199154 Share on other sites More sharing options...
spiderwell Posted April 9, 2011 Share Posted April 9, 2011 how often does a menu change on a website? , is it really necessary to keep it in the database?, thats one extra db call on every page you dont really need i use this as a common included file for a navigation on many sites i write/have written, obviously you would adapt the output to your needs: $navarray['Logout'] = 'login.php?action=logout'; $navarray['View Exhibitions'] = 'exhibitions.php'; $navarray['My Sales'] = 'mysales.php'; $navarray['View Images'] = 'images.php'; $navarray['Consigned Images'] = 'consigned.php'; $navstring = "<div class=\"shadetabs\"><ul>"; foreach ($navarray as $key => $value) { $navstring .= "<li"; if (strpos($_SERVER['PHP_SELF'], $value)) $navstring .= " class='selected'"; $navstring .= ">\r"; $navstring .= "<a href='" . $value . "'>"; $navstring .= $key; $navstring .= "</a>\r"; $navstring .= "</li>\r"; } $navstring .= $outputstr; $navstring .= "</ul></div>"; echo $navstring; this out the menu into a <ul> list and those have so many css interpretations they can look like anything Quote Link to comment https://forums.phpfreaks.com/topic/233078-multiple-record-array/#findComment-1199162 Share on other sites More sharing options...
cactus Posted April 9, 2011 Author Share Posted April 9, 2011 I am creating a cms to update the pages so the menu needs to be on each page to access the updated page, if that makes sense? I'm really sorry but I don't understand what you mean with the post you have just sent i'm a beginner at php and mysql and am finding it difficult to figure out this problem. It's the last section of my website that I'm having problems with and just need the error messages fixed. So is there any way that I can fix these problems to get the menu working on each page? I really hope someone can help. I'm really struggling, please Quote Link to comment https://forums.phpfreaks.com/topic/233078-multiple-record-array/#findComment-1199173 Share on other sites More sharing options...
cactus Posted April 9, 2011 Author Share Posted April 9, 2011 hello, sorry for constantly replying in my own post but I need to know how to get the menu that is generated from my database on every page. Is there even a way to do this? or link the current menu icon image to one of the posts from the database? Any ideas?? I can't figure it out. Thanks in anticipation Quote Link to comment https://forums.phpfreaks.com/topic/233078-multiple-record-array/#findComment-1199381 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.