1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Need help hand-coding my PHP blog!

Discussion in 'Mac Programming' started by Littleodie914, Jun 21, 2006.

  1. macrumors 68000


    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.


    <? get(post38.php)

    Would somehow result in...

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

    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:
  2. macrumors 68030


    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.
  3. macrumors 68000


    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:
  4. macrumors 68000


    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
  5. macrumors 6502a


    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.
  6. macrumors 68000


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


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


    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 //-->
    <postdata>Post Data</postdata>
    Now what did I miss in his question cause I think he doesn't want to do it this way?
  9. macrumors 68000


    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! :)
  10. macrumors 68000



    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

Share This Page