rocky48 Posted August 3, 2015 Share Posted August 3, 2015 I have a menu that is used in all of my web pages and if I add something it means that I have to change the code on all of the the pages, which is time consuming. I have not written any code yet, but I just want some advice on how to go about this problem. Most of the web pages I have looked at are not very helpful. Some suggest jScript, but I am not very conversant with this language. Any help would be appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/297608-how-do-you-include-frequently-used-code-in-another-file/ Share on other sites More sharing options...
QuickOldCar Posted August 3, 2015 Share Posted August 3, 2015 include() the file/script Quote Link to comment https://forums.phpfreaks.com/topic/297608-how-do-you-include-frequently-used-code-in-another-file/#findComment-1517945 Share on other sites More sharing options...
mac_gyver Posted August 3, 2015 Share Posted August 3, 2015 I have a menu that is used in all of my web pages and if I add something it means that I have to change the code on all of the the pages it sounds like you need to use/write a content management system, where the different content making up the pages is stored in a database table, the menu is dynamically produced based on the content that's been stored in the database table, and you have one physical page (index.php) that dynamically outputs each different logical page, when it gets requested with a page identifier as part of the url. Quote Link to comment https://forums.phpfreaks.com/topic/297608-how-do-you-include-frequently-used-code-in-another-file/#findComment-1517946 Share on other sites More sharing options...
Guest MrLeN Posted August 28, 2015 Share Posted August 28, 2015 Let's say you have a page called index.php What you can do is create a file called menu.php and put it in the same directory. inside menu php use this code: <a href="index.php">Home</a> | <a href="about.php">About</a> Inside index.php use this code: include_once 'menu.php'; Note: This only works with php filed not html files. Quote Link to comment https://forums.phpfreaks.com/topic/297608-how-do-you-include-frequently-used-code-in-another-file/#findComment-1519828 Share on other sites More sharing options...
Solution rocky48 Posted September 5, 2015 Author Solution Share Posted September 5, 2015 I have written a database driven menu system. At first I forgot that all the files had to have a PHP extension, but now it all works OK. Here is the code for those who may be interested: <?php include ("connect_menu_LOCAL.php"); $conn = get_db_conn_menu(); $menu_items['Url']=""; $menu_sql = "SELECT Url FROM ipad"; $menu_res = mysqli_query($conn, $menu_sql) or die(mysqli_error($conn)); if (mysqli_num_rows($menu_res) === false) { echo "<p>".mysqli_error($menu_res)."</p>"; } else { while($menu_items = mysqli_fetch_array($menu_res)){ $item=$menu_items['Url']; echo $item; } } //free results mysqli_free_result($menu_res); //close connection to MySQL mysqli_close($conn); ?> Quote Link to comment https://forums.phpfreaks.com/topic/297608-how-do-you-include-frequently-used-code-in-another-file/#findComment-1520298 Share on other sites More sharing options...
Barand Posted September 5, 2015 Share Posted September 5, 2015 ..... but now it all works OK. You think? How much testing did you do? if (mysqli_num_rows($menu_res) === false) { echo "<p>".mysqli_error($menu_res)."</p>"; } mysqli_num_rows() returns a number (which may be 0) and not a boolean true/false result mysqli_error() tales the $conn object as its parameter, not the result object. getting no records returned is not an error, so there would be no message to echo in that event. You need to take a look in the manual more often. Quote Link to comment https://forums.phpfreaks.com/topic/297608-how-do-you-include-frequently-used-code-in-another-file/#findComment-1520305 Share on other sites More sharing options...
doni49 Posted September 20, 2015 Share Posted September 20, 2015 You think? How much testing did you do? mysqli_num_rows() returns a number (which may be 0) and not a boolean true/false resultYou need to take a look in the manual more often. That's not actually correct. The following was copied from the link below. When converting to boolean, the following values are considered FALSE:◦ the boolean FALSE itself ◦ the integer 0 (zero) ◦ the float 0.0 (zero) ◦ the empty string, and the string "0" ◦ an array with zero elements ◦ an object with zero member variables (PHP 4 only) ◦ the special type NULL (including unset variables) ◦ SimpleXML objects created from empty tags Every other value is considered TRUE (including any resource). http://php.net/manual/en/language.types.boolean.php#language.types.boolean.casting Quote Link to comment https://forums.phpfreaks.com/topic/297608-how-do-you-include-frequently-used-code-in-another-file/#findComment-1521217 Share on other sites More sharing options...
Barand Posted September 20, 2015 Share Posted September 20, 2015 He used === false which means it must be the same value and type (boolean false, not 0, or any of the others that evaluate to false) Quote Link to comment https://forums.phpfreaks.com/topic/297608-how-do-you-include-frequently-used-code-in-another-file/#findComment-1521222 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.