Reputation: 164
I am making a website in which I want a column on the left hand side to be a list of links. This column populated with links will be on every single web page of the entire site, and it will be updated with more links quite frequently. So the problem I need to solve is how to make it so that whenever I want to add a new link to my website's "link list", I don't have to manually add it in the HTML code in every single web page.
I know some Java but I'm completely unfamiliar with PHP. I did a bit of research and found the "include" declaration, and kind of slapped together something that does work, but I have no clue whether this is a good idea or not - as in if its bad code/bad style, so could I get some opinions on this please? Is it good code? Or am I going about it the wrong way?
The entire php file that contains the array, title "videos_array.php":
<?php
$videoArr = array(
"<li><a href=\"http://www.youtube.com/arbitraryvideo1\">Video 1</a></li>",
"<li><a href=\"http://www.youtube.com/arbitraryvideo2\">Video 2</a></li>",
"<li><a href=\"http://www.youtube.com/arbitraryvideo3\">Video 3</a></li>");
?>
And the web pages where I want to insert these values into look like this:
<html>
<head>
<!--All the necessary tags-->
</head>
<body>
<!--More Tags-->
<div id="link_column">
<?php include 'videos_array.php';
foreach($videoArr as $val){
echo $val;
}
?>
</div>
</body>
</html>
When referencing a php file with the include declaration, is it a relative path that I should be using, or are absolute and virtual references ok as well?
Thanks in advance for your input
Upvotes: 1
Views: 1591
Reputation: 165069
When referencing a php file with the include declaration, is it a relative path that I should be using, or are absolute and virtual references ok as well?
What you're doing is fine however there's some points to be wary of around PHP's include path.
The include path is a FIFO stack of server file-system paths that PHP will use as base directories when resolving paths provided in include
and require
statements (and probably some other things). The basic include path usually includes .
(or the current working directory). This is resolved as the directory containing the first (parent) PHP script executed. This can catch people out once you go a few includes deep.
What I find generally works best is to prefix include paths with the __DIR__
constant. This resolves to the parent directory of the current script.
For example, say you have the following directory structure
foo.php
bar.php
dir/baz.php
To include bar.php
from foo.php
, you can use
include __DIR__ . '/bar.php';
To include bar.php
from dir/baz.php
include __DIR__ . '/../bar.php';
You can also manipulate the include_path
configuration. For example (in foo.php
)
// add "dir" to the top of the include path stack
set_include_path(implode(PATH_SEPARATOR, [
realpath(__DIR__ . '/dir'),
get_include_path()]));
// now include baz.php
include 'baz.php'; // this works because we added "dir" to the include path
Upvotes: 1
Reputation: 40639
Your $videoArr
is used only once, so there is no need to use it in another php file
you can just echo
after declaring it like,
<?php
$videoArr = array(
"<li><a href=\"http://www.youtube.com/arbitraryvideo1\">Video 1</a></li>",
"<li><a href=\"http://www.youtube.com/arbitraryvideo2\">Video 2</a></li>",
"<li><a href=\"http://www.youtube.com/arbitraryvideo3\">Video 3</a></li>"
);
foreach($videoArr as $val){
echo $val;
}
?>
Even no need to create an array
if it is not used just echo
it as a string
like,
<?php
echo "<li><a href=\"http://www.youtube.com/arbitraryvideo1\">Video 1</a></li>",
"<li><a href=\"http://www.youtube.com/arbitraryvideo2\">Video 2</a></li>",
"<li><a href=\"http://www.youtube.com/arbitraryvideo3\">Video 3</a></li>";
?>
And in your index page
use it like,
<div id="link_column">
<?php include 'videos_array.php';?>
</div>
Upvotes: 1