PDA

View Full Version : Need help hand-coding my PHP blog!




Littleodie914
Jun 21, 2006, 08:56 PM
Hey guys, I've been scouring the forums as well as the web looking for some help with my website. I've been using blog programs such as Wordpress, etc. in the past but am getting tired of not having control over how everything works.

What I'd like to do is have an index.php page that contains all the basic stuff, like the navigation at the top, yadda yadda, but then in the main DIV calls another PHP page. Allow me to explain...

Lets say I have a new post. I'll put it in a file called post38.php, with the title in <posttitle></posttitle> tags and the main stuff in like <postbody></postbody> tags. What I would like to do is update (manually) the index.php file to somehow call the post38.php file so that it echos, returns, whatever, all of its contents to be displayed as HTML in index.php.

So...

<html>
<? get(post38.php)
?>
</html>

Would somehow result in...

<html>
<posttitle>My Dog Jack</posttitle>
<postbody>He's great, but a bit too furry for my liking.</postbody>
</html>

Is there any way to do this? I understand PHP to the point where in the links section on the side I'll be able to use some directory functions and if/else loops to display links to like the latest 5 posts, but I can't figure out how to have one PHP file open the contents of another PHP file. Any ideas would be really appreciated! (Hope it all makes sense too.) :rolleyes:



superbovine
Jun 21, 2006, 09:12 PM
Hey guys, I've been scouring the forums as well as the web looking for some help with my website. I've been using blog programs such as Wordpress, etc. in the past but am getting tired of not having control over how everything works.

What I'd like to do is have an index.php page that contains all the basic stuff, like the navigation at the top, yadda yadda, but then in the main DIV calls another PHP page. Allow me to explain...

Lets say I have a new post. I'll put it in a file called post38.php, with the title in <posttitle></posttitle> tags and the main stuff in like <postbody></postbody> tags. What I would like to do is update (manually) the index.php file to somehow call the post38.php file so that it echos, returns, whatever, all of its contents to be displayed as HTML in index.php.

So...

<html>
<? get(post38.php)
?>
</html>

Would somehow result in...

<html>
<posttitle>My Dog Jack</posttitle>
<postbody>He's great, but a bit too furry for my liking.</postbody>
</html>

Is there any way to do this? I understand PHP to the point where in the links section on the side I'll be able to use some directory functions and if/else loops to display links to like the latest 5 posts, but I can't figure out how to have one PHP file open the contents of another PHP file. Any ideas would be really appreciated! (Hope it all makes sense too.) :rolleyes:

it really depends on whats in that php you want to call. i would recommend against this method because if your blog gets long it will be a pain.

look into include.
http://us2.php.net/manual/en/function.include.php

Littleodie914
Jun 21, 2006, 09:13 PM
Ahh... Now that I think about it... Mods, would this thread be better suited in the Web Programming section? I guess I wasn't thinking hard enough when I picked the category. :o

Littleodie914
Jun 21, 2006, 09:17 PM
it really depends on whats in that php you want to call. i would recommend against this method because if your blog gets long it will be a pain.

look into include.
http://us2.php.net/manual/en/function.include.phpAh, I think that'll work just fine! Instead of calling and inserting the HTML, I can just use the variables now.

It shouldn't be too bad in terms of maintaining it, I'm just going to change the main page every time I post from include 'post38.php' to include 'post39.php' and go from there. As for like the archives page and what-not, I think I'll just set up a for loop that scans the directory for files starting with post and lists them. Thanks again for the help! :D

panoz7
Jun 21, 2006, 09:35 PM
Includes should def. do what you're looking for. If I were you though I'd consider implementing a simple mySQL database. The beauty of php for me was the elimination of all those individual files. Rather then create a new file for each post you could simply create a new record in a database.

Maybe Iím not understanding exactly what youíre doing, Iím sure you have your own reasons for setting it up this way, but just though Iíd suggest it anyway. When I was first learning PHP I didnít know some of the tricks and ended up wasting hundreds of lines of code and countless hours to do something that would now take me only a few minutes.

If you want to give the database thing a try, or are just curious, but donít know how to do it send me a PM and I'd be happy to help.

savar
Jun 21, 2006, 11:01 PM
it really depends on whats in that php you want to call. i would recommend against this method because if your blog gets long it will be a pain.

look into include.
http://us2.php.net/manual/en/function.include.php

I suggest require_once instead...but as others said, a mySQL backend would be more flexible.

superbovine
Jun 22, 2006, 05:21 PM
I suggest require_once instead...but as others said, a mySQL backend would be more flexible.

probably a better choice. the only time i think to use that is when i am making my db connection stuff.

slooksterPSV
Jun 23, 2006, 12:50 AM
Here's how I did it:

<!-- main.php - the file that does the stuff //-->
<?
$file = "./test.php";
$data = file_get_contents($file);
echo $data;
?>



<!-- test.php - the file that contains the contents //-->
<posttitle>THISPOST</postitle>
<postdata>Post Data</postdata>


Now what did I miss in his question cause I think he doesn't want to do it this way?

Littleodie914
Jun 24, 2006, 11:21 PM
Alright, thanks for everything so far guys, you've been a huge help. I've got the blog aspect figured out, and actually learned how to use MySQL for the databases.

Now what I'm trying to work on is how to implement a database table that doesn't already know how many values will be added to it. For example, lets say that I've got a table that holds $title, $date, $text, and the ID for each of my posts. What if people want to add comments? How should I store those? There's no way to store an array as a value in a table, is there?

For each set of comments attached to a post, would I have to create a new table? So then for the post with ID = 7, would I have a table called Comments7 where each row is a comment? Is that the most efficient way to do this? Thanks again for all of the help, I really appreciate it! :)

Littleodie914
Jun 24, 2006, 11:35 PM
Update!

I was looking around a bit more on the web, and I think I might want to use the serialize() (http://us3.php.net/manual/en/function.serialize.php) function to change the array from (you guessed it) an array to a "string containing a byte-stream representation of the value". Apparently, I can create and update the array in PHP, and then serialize it so it can be stored in a single cell. Once the user adds a comment, the "value" is unserialized and converted back into an array. It can then be updated/added to and re-serialized again.

Woo hoo! (I hope) :D