0
\$\begingroup\$

I am building a dynamic site. All data will be served through an index.php in my webroot, using jquery to update the content. In webroot, I have a folder 'i/' which contains the files I want to include. The main include files contain code which will be used on every page: globals.php, globals.css, globals.js, etc. plus library files, such as jquery.js. Then there are secondary includes that are used depending on the section or topic that has been loaded: topic1.php, topic1.css, topic1.js, etc.

I have written the following php function to call all the files that need to be declared in the head (stylesheets and javascript for now, but wanted to make it easy to add more filetypes in the future), allowing globals.php to be included before the html tag (and will probably include topics.php from inside globals.php). However, I'm not really sure if this is a very efficient function, or if there is a better way to do what I'm trying to:

function getIncludes($topic){
    $type=array("css","js");
    $library=array("js"=>array("jquery"));
    $scope=array("globals",$topic);
    foreach($type as $ext){
        $includes=array();
        if(array_key_exists($ext, $library)){
            foreach ($library[$ext] as $file){
                array_push($includes, $file);
            }
        }
        foreach($scope as $file){
            array_push($includes, $file);
        }
        foreach($includes as $file){
            if(file_exists('i/'.$file.'.'.$ext)){
                if($ext=="css"){
                    echo '<link rel="stylesheet" type="text/css" href="i/'.$file.'.'.$ext.'" />';
                }
                if($ext=="js"){
                    echo '<script type="text/javascript" src="i/'.$file.'.'.$ext.'"></script>';
                }
            }
        }
    }
}

Is this a good way of achieving my goal, or can I do better?

\$\endgroup\$

migrated from stackoverflow.com Jul 28 '12 at 1:42

This question came from our site for professional and enthusiast programmers.

  • \$\begingroup\$ Your function has a syntax error, and it doesn't output anything. What's the desired output? \$\endgroup\$ – nickb Jul 28 '12 at 1:01
  • \$\begingroup\$ Thanks for pointing out the syntax error, it's fixed now. I had missed an opening bracket on one of the foreach statements. The code is supposed to look for all script files which need to be linked to in the <head> (.js and .css for now), and write the html code for each one. \$\endgroup\$ – bee.catt Jul 28 '12 at 3:06
  • \$\begingroup\$ Oh, and it won't output anything unless it's run from a folder containing a subfolder named "i" which contains at least one .js or .css file named either "globals" or a name passed to the $topic variable. \$\endgroup\$ – bee.catt Jul 28 '12 at 3:12
  • \$\begingroup\$ something is also wrong with my multidimensional array reference ($library["js"]["jquery"]), but I can't figure out what it is. The script works for globals/$topic.css/js, but it's not including jquery.js, and I think it's because I'm not referencing it properly. \$\endgroup\$ – bee.catt Jul 28 '12 at 14:45
  • \$\begingroup\$ Ok, I figured out the problem with the multi-dimensional array (I needed to use array_key_exists instead of in_array), and the whole code now works as intended. However, I'd still like to make sure this is actually a good way of gathering and adding includes. \$\endgroup\$ – bee.catt Jul 28 '12 at 15:56
2
\$\begingroup\$

Don't make your directory structure so vague. Coming into this with no knowledge of your system how am I supposed to know what the "i" directory is? Just call it includes. Also, it is usually a good idea to separate your files based on their types so that they are easier to find. There are many common project directory structures out there. For instance I'm using something similar to Zend's. It looks similar to this.

/root
    /application
        /configs
        /models
        /views
        /controllers
    /data
        /cache
        /logs
    /public
        /js
        /css
        /images

I hope those file names don't indicate that you are actually using globals... There are much better ways to get "global" scope variables without ever using global. Sessions for instance. Globals, in my opinion, should eventually be deprecated. They are an old feature that has been proven faulty and replaced.

array_push() is another old function. Though there is nothing wrong with it, the preferred way to do it now is like so.

$includes[] = $file;

What you are trying to do is usually done with templates rather than PHP. For instance, you'd have a single HTML file with these includes in it already then dynamically change the contents with PHP. For those files that use the $topic variable, you would just perform a check to make sure it exists before including it via PHP. So a very basic template might look like this.

<html>
<head>
<title></title>
<script type="text/javascript" src="i/globals.js"></script>
<link rel="stylesheet" type="text/css" href="i/globals.css" />

<!-- SPECIFIC INCLUDES -->

<?php if( is_file( "i/$topic.js" ) : ?>
<script type="text/javascript" src="i/<?php echo $topic.js; ?>"></script>
<?php endif; ?>

<?php if( is_file( "i/$topic.css" ) : ?>
<link rel="stylesheet" type="text/css" href="i/<?php echo $topic.css; ?>" />
<?php endif; ?>

<!-- SPECIFIC INCLUDES -->

</head>
<body>
<?php if( is_file( "i/$topic.php" ) ) { include "i/$topic.php"; } ?>
</body>
</html>
\$\endgroup\$
  • \$\begingroup\$ thanks for all the input, I've actually been brainstorming a reworking of my framework, so these are some good tips. No, I am not using "globals" here in the sense of built in php globals, but rather as my own code which is meant to be used globally, that is, on every page. \$\endgroup\$ – bee.catt Jul 31 '12 at 17:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.