JakkkeM Posted December 26, 2012 Share Posted December 26, 2012 Hi Guys, I'm an amateur coder working on a huge project probably way above my capabilities and thus continue to run into problems. So, I seek your help! In my developing system, I'm making a dynamic website so you can control the front-end from the back-end. To edit the front-end you have a page Manage Layout which allows you to edit the HTML of the home page. Then you just type in my custom elements like, [menu], etc. I'm using preg_replace to change these into the scripts I require. The HTML of the homepage is contained in a database, then when it's executed on the homepage I do the preg replace etc. What I'm doing for my [menu] is: Now, the problem occurs. It executes this 100%, the preg_replace carries it out perfectly, BUT instead of it executing where I've placed it in the overall position on the page, it puts everything on the first line after the <body> tag... The preg_replace simply replaces [menu] with createMenu(); so I would've thought that my function would be executed wherever I had [menu]??? I'm not entirely sure what I've said is clear but lets hope so. Cheers. ABOVE:The menu is being placed on the first line, rather than in the div where I have [menu] (which is consequently to the right of the CrossFit St Marys Logo) Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted December 26, 2012 Share Posted December 26, 2012 Echoing output inside of a function will output that content at the point in the code where the function is called. In general, functions should return the result they produce, so that the function call is essentially replaced by the result the function produced. Quote Link to comment Share on other sites More sharing options...
Christian F. Posted December 26, 2012 Share Posted December 26, 2012 (edited) Another advantage to using return over echo is that you can arrange your functions however you like, without affecting the structure of the site. That means you can also change the structure of your site, without having to re-arrange your functions. For instance: If you found out you wanted a bread crumb on your page, and that it should go before the menu. However, the breadcrumb is relying upon some of the processing you've done in relation with the menu. If you've used echo inside the functions, you either have to duplicate that bit of code (which greatly increases the probability of bugs), move the breadcrumb to be after the menu, or do position magic with CSS to make it appear as if it was before the menu. With return, you just create the breadcrumb after the menu has been created, and then echo them out in their respective places in the code. Also, I see that you've used a query inside a loop in your code. This is strongly recommended against as it hurts performance like nothing else, and on top of that quite unnecessary. What you should do instead is to use one query with a JOIN statement, to fetch all of the data you need the first time around. Then loop through the result, and do whatever you want to do. PS: Please post your code as text, and use the [code][/code] tags around your code. Doing both helps make both your post and your code a lot easier to read. Edited December 26, 2012 by Christian F. Quote Link to comment Share on other sites More sharing options...
ignace Posted December 26, 2012 Share Posted December 26, 2012 (edited) You could use classes for this as that allows you to keep the identifier '[menu]' together with the HTML. class MenuGeneratorView { public function generate() { } public function getIdentifier() { return 'menu'; } } echo str_replace('['.$view->getIdentifier().']', $view->generate(), $str); Something else you can do is parse each [<identifier>] so that you allow parameters for example. [menu do="generate" direction="vertical" sort="asc"] public function generate($direction = 'horizontal', $sort = 'desc') { .. } [menu do="breadcrumb" direction="horizontal" sort="asc"] public function breadcrumb($direction = 'horizontal', $sort = 'asc') { .. } Edited December 26, 2012 by ignace Quote Link to comment Share on other sites More sharing options...
JakkkeM Posted December 26, 2012 Author Share Posted December 26, 2012 Another advantage to using return over echo is that you can arrange your functions however you like, without affecting the structure of the site. That means you can also change the structure of your site, without having to re-arrange your functions. For instance: If you found out you wanted a bread crumb on your page, and that it should go before the menu. However, the breadcrumb is relying upon some of the processing you've done in relation with the menu. If you've used echo inside the functions, you either have to duplicate that bit of code (which greatly increases the probability of bugs), move the breadcrumb to be after the menu, or do position magic with CSS to make it appear as if it was before the menu. With return, you just create the breadcrumb after the menu has been created, and then echo them out in their respective places in the code. Also, I see that you've used a query inside a loop in your code. This is strongly recommended against as it hurts performance like nothing else, and on top of that quite unnecessary. What you should do instead is to use one query with a JOIN statement, to fetch all of the data you need the first time around. Then loop through the result, and do whatever you want to do. PS: Please post your code as text, and use the tags around your code. Doing both helps make both your post and your code a lot easier to read.[/quote]The problem I face if I use a JOIN statement, is that for every link, it does the button again? echo "<h1>Test Page</h1>"; $query = mysql_query("SELECT * FROM `menus` LEFT JOIN `links` ON `menus`.`menu_id` = `links`.`assoc_menu`"); while($MENU = mysql_fetch_array($query, MYSQL_ASSOC)){ $return_menu = $MENU['title']." ".$MENU['link_title']."<br />"; echo $return_menu; } But now, for every link, it returns the title that link belongs too... So I'm not sure how to handle this because if I have 10 links for my first menu item, I'll have 10 buttons for that item? Quote Link to comment Share on other sites More sharing options...
JakkkeM Posted December 26, 2012 Author Share Posted December 26, 2012 You could use classes for this as that allows you to keep the identifier '[menu]' together with the HTML. class MenuGeneratorView { public function generate() { } public function getIdentifier() { return 'menu'; } } echo str_replace('['.$view->getIdentifier().']', $view->generate(), $str); Something else you can do is parse each [<identifier>] so that you allow parameters for example. [menu do="generate" direction="vertical" sort="asc"] public function generate($direction = 'horizontal', $sort = 'desc') { .. } [menu do="breadcrumb" direction="horizontal" sort="asc"] public function breadcrumb($direction = 'horizontal', $sort = 'asc') { .. } Unfortunately, and this is where the amateur part comes in, I don't know how to do anything you suggested. Haha. Quote Link to comment Share on other sites More sharing options...
Christian F. Posted December 26, 2012 Share Posted December 26, 2012 The problem I face if I use a JOIN statement, is that for every link, it does the button again? [sNIP] So I'm not sure how to handle this because if I have 10 links for my first menu item, I'll have 10 buttons for that item? Let me refer you to this post by Barand which shows how to prevent that problem. Quote Link to comment Share on other sites More sharing options...
JakkkeM Posted December 27, 2012 Author Share Posted December 27, 2012 Let me refer you to this post by Barand which shows how to prevent that problem. Thanks Quote Link to comment Share on other sites More sharing options...
JakkkeM Posted December 27, 2012 Author Share Posted December 27, 2012 Another advantage to using return over echo is that you can arrange your functions however you like, without affecting the structure of the site. That means you can also change the structure of your site, without having to re-arrange your functions. For instance: If you found out you wanted a bread crumb on your page, and that it should go before the menu. However, the breadcrumb is relying upon some of the processing you've done in relation with the menu. If you've used echo inside the functions, you either have to duplicate that bit of code (which greatly increases the probability of bugs), move the breadcrumb to be after the menu, or do position magic with CSS to make it appear as if it was before the menu. With return, you just create the breadcrumb after the menu has been created, and then echo them out in their respective places in the code. Also, I see that you've used a query inside a loop in your code. This is strongly recommended against as it hurts performance like nothing else, and on top of that quite unnecessary. What you should do instead is to use one query with a JOIN statement, to fetch all of the data you need the first time around. Then loop through the result, and do whatever you want to do. PS: Please post your code as text, and use the tags around your code. Doing both helps make both your post and your code a lot easier to read. The problem I had with return, was that instead of giving me all of my menus, it only returned the first one... :/ I'll test it with the new SQL format and see how I go. Quote Link to comment 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.