Jump to content

Using a PHP script in an HTML document


nebulous

Recommended Posts

I'll begin by defining the degree of my ignorance -- it is great. I'm not a Web developer or any type of programmer at all. I can use DreamWeaver to create pages, I can often figure out how to tweak "borrowed" code to my needs, and I am not shy in asking people more knowledgeable than I to help me when I need it. I lease some hosted web server space at http://www.jbryant.org and mostly use it to learn and play. It runs on n Apache server. I have several blogs (all of them Blogger/Blogspot blogs that I host on my site), some of them password protected and private. I'm nothing like an active blogger. I'm more a small-time hobbiest that is most interested in the technology itself and am so busy that I have never taken the time to really learn to program. Beyond all that, I'm teachable, I pick up on things pretty well, and I'm programming-aware. OK, does that define me adequately?

 

Regarding PHP, I'm completely new. I've done mostly Javascript stuff. My site does support PHP 5, and for the first time I needed to do something that PHP can do but JS can't. My entire site is in HTML.

 

Here's my dilemma, with hopefully no more background than is necessary. Trust me -- this is a PHP question. What follows is relevent:

Blogger recently upgraded its stuff to allow the use of labels/categories in its blogs. You can add labels to your posts and if you are using a new Blogger template you can have a sidebar that provides links to all of the labels (so someone can click a category and see only its relevant posts). The problem is, Blogger only allows the use of the new templates (and hence, this ability) if you allow them to host your blog. If you host it yourself, you have to continue to use their Classic templates.

 

Even with Classic templates you can add labels to your posts. What you can't do is provide the sidebar so readers can limit the display of posts by category.

 

PHP may be the answer. I had a friend write a PHP script that works well, but I can't figure out how to implement it. You see, Blogger creates a directory off of the blog's main directory called "labels." In that directory are individual html pages that are conveniently named "<category>.html." so, for instance if your blogs are about apples, oranges, and grapes there will be an apple.html, orange.html, and grape.html in the ./labels directory. If you open any of those files, you'll get a list of only posts with that category.

 

My idea (which my friend accomodated) was to use PHP to create a script that displays all of those filenames, removes the file extension, and then hyperlinks to their files. He wrote the script (below), I saved it to a file called labels_list.php, and opened it. It worked perfectly! Here's the script:

 

<?php
    $startdir = "./labels/";
    $ignoredDirectory[] = "."; 
    $ignoredDirectory[] = "..";    //list any other files you want to ignore 
if (is_dir($startdir)) { 
        if ($dh = opendir($startdir)) { 
            while (($file = readdir($dh)) !== false) {
                if (!(array_search($file,$ignoredDirectory) > -1)) {
                       $directorylist[]= $file; 
     }}}}
    closedir($dh);
    natcasesort($directorylist);
    foreach ($directorylist as $filename) {
        $temp = explode(".",$filename);
        if ((count($temp)) > 1) {
            unset($temp[count($temp)-1]);}
        $displayname = implode($temp); 
        echo "<a href='". $startdir . $filename . "'>" . $displayname . "</a><br>\n";
    }
?>

 

As I said, when I save this as a PHP file in the blog's main directory, it works perfectly. What I can't figure out is how to integrate it into my blog as a sidebar.

 

Blogger allows me to save its main page as any filename I want to. I tried the following:

  • Simply putting the code in my Blogger template and saving the main file as index.htm as usual. This resulted in the blog displaying as normal but with PHP code in the sidebar
  • Putting the code in the template's sidebar as above but saving the main blog file as index.php. The resulted in a strange PHP error. I don't have the exact error message word-for-word at this moment, but it referred to a bad colon ":" in line 903. Line 903 is nowhere close to where the PHP code is. Line 903 and everything around it is text of a blog post

 

What should I be doing differently? Blogger's output is HTML. It uses its own "tags" throughout, which sort of amount to its own version of CSS and sections, and also which identify various elements of the blog's page (such as post, sidebar, author, comments, etc.). But it is HTML nonetheless, the way I see it.

 

Links would be helpful here. The blog I'm testing this on is at http://www.jbryant.org/quotes/christian, and index.htm is its main page. Of course, for that you'll see only the output that was generated from the template. If someone wants the actual template's code I can provide it as an attachment. Not sure if that's relevent.

 

At http://www.jbryant.org/quotes/christian/labels_list.php you can see the PHP script in action. I've only assigned category labels to a couple of my posts until I get this working. But when you go to that page you'll see a list of all the labels I've added, and if you click one of the labels you'll be taken to a display of posts that relate to it.

 

So, the final question summarized, is how can I integrate this PHP into my main page?

 

Any help you provide will be invaluable, not only to me but to zillions of other bloggers out there that serve their own Blogger blogs and want this capability. No one yet has come up with a way to display such a sidebar, and I think we're on the brink of a solution. Thank you in advance!

 

jb

Link to comment
https://forums.phpfreaks.com/topic/37906-using-a-php-script-in-an-html-document/
Share on other sites

trecool999:

 

Yes, that is what I want. Sorry it was long. I tried to clearly give relevent details that might complicate the correct answer (so no one would need to ask "What?").

 

Because I'm a newb, I didn't want to assume anything.

 

Can you help?

 

Thanks for your reply.

 

jb

You'd need to have access to .htaccess files (or be able to setup new handlers in your web hosting control panel).

 

For .htaccess:

AddHandler application/x-httpd-php .htm

 

That should work, but don't hold it as 100% perfect. I'm no master at .htaccess or Apache.

Archived

This topic is now archived and is closed to further replies.

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