Jump to content

How do you include frequently used code in another file?


Go to solution Solved by rocky48,

Recommended Posts

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! :confused:

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.

  • 4 weeks later...

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.

  • Solution

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);
?>

 

..... 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.

  • 3 weeks later...

 

You think?

 

How much testing did you do?

 

 

  • mysqli_num_rows() returns a number (which may be 0) and not a boolean true/false result

You 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

 

 


 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.