Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 03-08-2006, 06:57 PM   #1 (permalink)
ChefFrank
Code Monkey
 
ChefFrank's Avatar
 
Join Date: Mar 2006
Location: Woodbury, CT
Posts: 38
ChefFrank is on a distinguished road
Send a message via Yahoo to ChefFrank
Question Why doesn't this work?

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
ChefFrank is offline   Reply With Quote
Old 03-08-2006, 08:23 PM   #2 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,489
sde is on a distinguished road
you are correct.

in your information above, you refer to $_GET['recipe'] as $_GET('recipe'). (note, red is bad )

you're not using that syntax in your code right? $_GET is an array of the URL variables, and in PHP, brackets after a variable name indicate your are retrieving one element of that array. parenthasis indicate you are calling a function.
sde is offline   Reply With Quote
Old 03-08-2006, 09:05 PM   #3 (permalink)
ChefFrank
Code Monkey
 
ChefFrank's Avatar
 
Join Date: Mar 2006
Location: Woodbury, CT
Posts: 38
ChefFrank is on a distinguished road
Send a message via Yahoo to ChefFrank
SDE -

On my screen, it's $_GET['recipe'] - square brackets - in the code - it's parenthesis in my description of the problem - my bad! Now I know!

I understand what you're saying, though - another concept though my thick skull. Array handling is denoted the same way as in C or in ALGOL - with square brackets.

What I am inferring from what you're saying, is that I could call $_GET with multiple variables, such as $_GET['var1', 'var2'] - and I can further infer that PHP does dynamic variable allocation? Not that this fixes my problem, but it's nice to know.

I double checked the code - and even cut and paste the example DJMaze provided into a new file, and had the same problem. Could I have a PHP version problem? I was running V4, and just in case, just updated to V5 - still the same result. So, not a version issue.

Any other idea will be appreciated.

Frank
ChefFrank is offline   Reply With Quote
Old 03-08-2006, 10:44 PM   #4 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,489
sde is on a distinguished road
index.php?a=foo&b=bar

PHP Code:
echo $_GET['a']; // prints: foo

echo $_GET['b']; // prints: bar 
at the top of your page, put: echo $_GET['recipe']; to see if you can print what is in the url.

if you really want to know more about arrays in php, you may want to look at this article i wrote a little while back.
sde is offline   Reply With Quote
Old 03-09-2006, 04:42 AM   #5 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 676
DJMaze is on a distinguished road
Quote:
Originally Posted by ChefFrank
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/>
not "index.php?irishcoffee" but "index.php?page=irishcoffee"

$_GET uses a key and a value (index.php?key=value).
PHP Code:
$_GET['key'] = 'value'
there's also a way to only use "index.php?irishcoffee" but just keep it simple for now.
DJMaze is offline   Reply With Quote
Old 03-09-2006, 06:40 AM   #6 (permalink)
ChefFrank
Code Monkey
 
ChefFrank's Avatar
 
Join Date: Mar 2006
Location: Woodbury, CT
Posts: 38
ChefFrank is on a distinguished road
Send a message via Yahoo to ChefFrank
DJMaze -

Thanks for that tip - So, now the index table looks like:

<a href="rindex.php?recipe=irishcoffee">Irish Coffee</a><br/>
<a href="rindex.php?recipe=lambmintsauce">Lamb with Mint Sauce</a><br/>
<a href="rindex.php?recipe=rhubarbcrunch">Rhubarb Crunch</a><br/>

And it works like a champ! I just have to do some formatting stuff and it will be good to go.

I see that I could also build a .php script that takes the same input .html file and format it differently through HTML if someone wanted to print just the recipe. Kewl!!!

Thanks a lot. Any recommendations on PHP tutorials?

Frank
ChefFrank is offline   Reply With Quote
Old 03-09-2006, 07:27 AM   #7 (permalink)
ChefFrank
Code Monkey
 
ChefFrank's Avatar
 
Join Date: Mar 2006
Location: Woodbury, CT
Posts: 38
ChefFrank is on a distinguished road
Send a message via Yahoo to ChefFrank
OK - the sucker's running - when I get some time, I'll pretty it up a bit.

If anyone wants to play with it, it's

http://www.cheffrankonline.com/rindex.php

SDE - now's the time to get that lamb recipe

Frank
ChefFrank is offline   Reply With Quote
Old 03-09-2006, 07:51 AM   #8 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 676
DJMaze is on a distinguished road
Cool you got it working, the only thing i see in the source is many DOCTYPE and <html>

Normaly a document only allows one of them so get rid of that in your recipes (.html files) since rindex.php handles that.

Another thing i see is � (question marks) this caused by unknown characters like backticks.
These chars are language dependant so you should set the correct Content-Type for processing.
By default Apache uses 'utf8' but you are using something else like iso-8859-1.

Either edit your html files to be in utf8/unicode or set the Content-Type.

A simple unicode supported editor is notepad2 on windows or vim on linux.
http://flos-freeware.ch/notepad2.html

modify Content-Type:
rindex.php
PHP Code:
<?php
// headers must be send BEFORE any output (echo, print, etc.)
// see http://www.w3.org/International/O-HTTP-charset
header('Content-Type: text/html; charset=utf-8');
?>
<html>
<body>
<table>
DJMaze is offline   Reply With Quote
Old 03-09-2006, 07:57 AM   #9 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,489
sde is on a distinguished road
Quote:
Originally Posted by ChefFrank
OK - the sucker's running - when I get some time, I'll pretty it up a bit.

If anyone wants to play with it, it's

http://www.cheffrankonline.com/rindex.php

SDE - now's the time to get that lamb recipe

Frank
nice, you picked that up pretty quick.

i tried out my new iron skillet last night and i over-cooked some steak after i moved it to the oven. i think i got the feel for it now though. i did 20 minutes in the oven after i seered it, .. i'll try 12 next time.

i've never cooked lamb before, but i think i'm up to it. maybe i'll give it a whril this weekend.
sde is offline   Reply With Quote
Old 03-09-2006, 05:56 PM   #10 (permalink)
ChefFrank
Code Monkey
 
ChefFrank's Avatar
 
Join Date: Mar 2006
Location: Woodbury, CT
Posts: 38
ChefFrank is on a distinguished road
Send a message via Yahoo to ChefFrank
Thanks for that tip - the HMTL editor I use 'helpfully' adds things like that if I don't put it in when I close the file. I'll turn that 'helpful' option off and that should clear things up. As I said, I'm experimenting and I have a lot of cleanup to do before I roll it out on the site.

Frank
ChefFrank is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't get loop to work rockybalboa Java 5 03-20-2005 03:54 PM
IsNumeric: Will this work? keeling Standard C, C++ 6 03-19-2005 10:13 AM
This should work so why doesn't it ntws01 Standard C, C++ 18 08-27-2003 01:24 PM
San Diego Tech. work. Admin Lounge 10 02-03-2003 06:19 PM
Getting X to work in Debian w00t Linux / BSD / OS X 7 08-25-2002 01:44 PM


All times are GMT -8. The time now is 09:16 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting