Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

General Discussion

Storing html in a database

i'm looking at creating a recipe website and instead of creating a page for every recipe i just want a few pages and the content for the recipes to be loaded into the page dynamically. i'm thinking the only real way is by storing the content in a DB and retrieving it using PHP. Does anyone know any other way that i could do this as i'm only really at a beginners level with PHP.

2 Answers

If you are worried about working with databases it's not that bad. You could download a Bitnami WAMP or MAMP stack to practice locally.

If you are OK with JavaScript then you could try an AJAX/JSON alternative to a database.

If you are confident with JavaScript I'd probably try an SPA (single page application) with something like Angular. Treehouse has lessons on Angular.

If you want to stick with PHP but just dont want to use a database then I guess you could save the data to a file. If you serialize it that would make it easy to read and convert back into something useful for PHP. I never tried running a site like that so I don't know how it be in terms of performance but this is how it works:

<?php

// recipe data
$recipes = [
    ["name" => "PBJ", "ingredients" => ["2 tbsp Peanut Butter", "2 tbsp Jelly", "2 slices of bread"]],
    ["name" => "Cheese Toast", "ingredients" => ["1 slice of cheese", "1 tsp margerine", "2 slices of bread"]]
];

// serialize data
$recipeSerial = serialize($recipes);

/* 
output for testing
OUTPUTS:
a:2:{i:0;a:2:{s:4:"name";s:3:"PBJ";s:11:"ingredients";a:3:{i:0;s:20:"2 tbsp Peanut Butter";i:1;s:12:"2 tbsp Jelly";i:2;s:17:"2 slices of bread";}}i:1;a:2:{s:4:"name";s:12:"Cheese Toast";s:11:"ingredients";a:3:{i:0;s:17:"1 slice of cheese";i:1;s:15:"1 tsp margerine";i:2;s:17:"2 slices of bread";}}}
*/
echo $recipeSerial;

// save recipes string to the file "recipes.txt"
file_put_contents("recipes.txt", $recipeSerial, LOCK_EX);

// read recipe back into an array from string in file
$recipeData = unserialize(file_get_contents("recipes.txt"));

/* 
output for testing
OUTPUTS:
Array(
    [0] => Array
        (
            [name] => PBJ
            [ingredients] => Array
                (
                    [0] => 2 tbsp Peanut Butter
                    [1] => 2 tbsp Jelly
                    [2] => 2 slices of bread
                )

        )

    [1] => Array
        (
            [name] => Cheese Toast
            [ingredients] => Array
                (
                    [0] => 1 slice of cheese
                    [1] => 1 tsp margerine
                    [2] => 2 slices of bread
                )

        )

)
*/
print_r( $recipeData );

?>

Thanks for the reply. I might have a go using AJAX/JSON as i'm more comfortable with JavaScript than PHP. Thanks