Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Littleodie914

macrumors 68000
Original poster
Jun 9, 2004
1,813
8
Rochester, NY
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

macrumors 68030
Nov 7, 2003
2,872
0
Littleodie914 said:
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

macrumors 68000
Original poster
Jun 9, 2004
1,813
8
Rochester, NY
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. :eek:
 

Littleodie914

macrumors 68000
Original poster
Jun 9, 2004
1,813
8
Rochester, NY
superbovine said:
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
Ah, 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

macrumors 6502a
Nov 21, 2005
904
1
Raleigh, NC
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.
 

superbovine

macrumors 68030
Nov 7, 2003
2,872
0
savar said:
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

macrumors 68040
Apr 17, 2004
3,544
306
Nowheresville
Here's how I did it:
Code:
<!-- main.php - the file that does the stuff //-->
<?
$file = "./test.php";             
$data = file_get_contents($file);
echo $data;
?>

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

macrumors 68000
Original poster
Jun 9, 2004
1,813
8
Rochester, NY
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

macrumors 68000
Original poster
Jun 9, 2004
1,813
8
Rochester, NY
Update!

I was looking around a bit more on the web, and I think I might want to use the serialize() 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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.