DJMaze was kind enough to give me this code to assist in building a dynamic recipe library (I added the line numbers for clarity):
Code:
1 <html>
2 <body>
3 <table>
4 <tr>
5 <!-- start side navigation
6 <td>
7 <a href="index.php?recipe=1">link 1</a><br/>
8 <a href="index.php?recipe=2">link 2</a><br/>
9 <a href="index.php?recipe=3">link 3</a><br/>
10 </td>
11 <td>
12 <?php
13 // Start the dynamic content/recipes
14
15 if (isset($_GET['recipe']) && preg_match('#[a-z0-9_]#i',$_GET['recipe'])){
16 // cool the visitor wants to see my recipe
17 $recipe = 'recipes/'.$_GET['recipe'].'.html';
18 if (is_file($recipe)) {
19 // the requested recipe exists
20 include($recipe);
21 } else {
22 // the recipe does not exist
23 include('no_recipe.html');
24 }
25 } else {
26 include('index.html');
27 }
28 ?>
29 </td>
30 </tr>
31 </table>
32 </body>
33 </html>
If I follow this correctly, when I load index.php initally, there's no parameter being passed, so the isset($_GET('recipe') should be false, and I should drop down to the include on line 26, which I do - and the page loads fine. Now, if I click on one of the menu items, I should get a match someplace . . .
7 <a href="index.php?irishcoffee">Irish Coffee</a><br/>
8 <a href="index.php?lambmintsauce">Lamb with Mint Sauce</a><br/>
9 <a href="index.php?rhubarbcrunch">Rhubarb Crunch</a><br/>
However, I still see that isset($_GET('recipe') is false and I show my initial page again. (I went through and put a bunch of print statements in the code to follow what's going on).
I am assuming (and we all know what happens when you assume!), that when I load the index.php?irishcoffee, $_GET('recipe') will return the string value "irishcoffee". The preg_match function is validating the field as to content (no strange characters) and I should drop into line 17 and filename validation. Since I have a file recipes/irishcoffee.html, I should hit line 20 and see the text in the file.
Am I interpreting how this works incorrectly? I checked the php documention online for the $_GET, preg_match and isset functions, and don't see why it isn't working.
Thanks in advance - I've only been working with PHP for a day.
Frank