The Un-Official Proxomitron Forum

Full Version: Basic PHP
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hey everyone! I'm starting to try and learn a little PHP. It's a lot more complicated than HTML, but I think I can get the hang of it. Right now I'm trying to figure out how to get like 3 or more webpages into 1. So it would be like http://www.mysite.com/index.php?page=about or page=downloads and so on. I've Googled for this, but haven't got anything because I don't really know what to call it. Can any of you guys give me a simple explaination on how to do this?
Shea;

<!# PHP - bang head on desktop;>

That didn't do it for you? Big Teeth OK, let's see what else I can come up with.....

What you are doing is something that I've been working towards for awhile now. In essence, what you want to do is make one or more templates, then use PHP conditional logic statements to choose and populate those templates. The results are what gets sent to the viewer. What all this means is that your website is no longer static, the pages are created dynamically. Of course, this also means that your templates and your logic have to be impeccable, but many PHP environment packages out there come with samples already set up for you to use, or you can dissect and rearrange them to suit your needs.

For a good example of this, simply download any of a number of BBS/Forum packages, just as Kye-U did for this very place we are now inhibiting! Big Teeth All the code is there, you can pick it apart and see what they did, and imitate it yourself. Be aware, your d/l'ed package may or may not include a PHP environment, so you may need to d/l that too.

As for searching with Google, you might try using terms like: php, template, and "conditional logic". Templates are sometimes called patterns, so stick that in there too. The way Google likes it is: template OR pattern. Note the capitalized OR, that's necessary. (And a pain in the tush if you're searching for something in Oregon state! Banging Head ) Lots of results there, I'm sure you'll find what you need, but do try the Invision package, it's loaded with goodies. Big Teeth

And for what it's worth, I'm still using PERL to generate my site's pages. Not as elegant, but it's what I already know, so......


Oddysey
Thanx Oddysey, I understood most of that and I'll continue searching Google. I also downloaded a copy of IPB 1.3 Final and am looking it over for hints. It's kind of hard to tell what to look for though because I don't know what the function is.

And it would save me a lot of time if anyone else knows the functions or functions to do that already.
I have figured it out! I searched Google some more for php url page variables and that came up with a lot. This is the code I got:

Code:
&#60;?php
if &#40;&#036;id==&#34;home&#34;&#41; &#123;
include &#40;&#34;home.php&#34;&#41;;
&#125;

elseif &#40;&#036;id==&#34;links&#34;&#41; &#123;
include &#40;&#34;links.php&#34;&#41;;
&#125;

elseif &#40;&#036;id==&#34;about&#34;&#41; &#123;
include &#40;&#34;about.php&#34;&#41;;
&#125;

else &#123;
echo &#34;That is not a valid document!&#34;;
&#125;
?&#62;
Shea;

You got it before I could get back here after my din-din - good for you! Cheers

That's about how it all works. If the pages are all quite similar in appearance, then you can template them easily. Even if they are all different, a set of templates can still save you some headaches in the future. But for now, just calling on static pages will still give you a quicker and more cohesive control system.

Keep it up!


Oddysey

p.s. In PHP, you use the question mark to tell the server what to do. In PERL, you use the !# pair of characters. Same job, different languages. But each side takes a friendly poke at the other guys once in awhile. :o :P
OK Oddysey, I got that method to work perfectly and helped Kye-U do it on the site. As you can see it works fine on the site. But I have one more question. How do you make it like this:

http://www.yoursite.com/index.php?page=tutorials&id=

I've searched Google for the same thing (php url page variables) but it only tells me how to make it index.php?page=whatever and not how to add a second variable. Do you know how to do this?
Shea;

You might give this site a shot - I like the way this author does things.

Beginning PHP Tutorial - Part 1

Lemme know what you think.


Oddysey
I read that whole tutorial Oddysey and it didn't tell me about how to do it. The closest thing it talked about was how to make 'Next' and 'Previous' buttons on pages, which I copied down ofr future reference. I'm still trying to figure it out by trial and erro with no luck. I've also been searching google still, but it still only comes up with one variable not two.
Shea;

How is it that you require two or more variables for an URL? By that I mean, do you make a user select multiple items to generate a desired page? Or do you take him through a menu of several choices, building up an array of his answers to insert into the URL? Perhaps you are using a form to collect those choices, I don't know. I guess that my question really is, are you building a complex page out of several simple templates? If so, that's alright, but it'd be nice for me to know, that's all.

One way to accomplish your goal is to create an array, and fill it with multiple selections made by the viewer. Another is to simply use brute force, and expand on your previous multiple if/elseif logic sequence. That always works, even if it's inelegant, and modifications are easy. Probably the most "unorthodox" way would be to construct your page as a form, and use the GET method to place the desired variables into the URL. But that's not using straightforward PHP... or is it?

Sure it is. You're using a form to construct what we can call an "interim" URL. Don't forget that the form's ACTION keyword denotes just where the URL will be sent to. And you've guessed by now that it can go to a PHP script, just like any other resource (another website, a server-side script, or a client-side script). In your PHP script, you can use the 'full_url' function to pick apart the URL that was passed in, and decode it to derive a final URL where you will then send your viewer.

Not the most elegant method, but it would certainly work, and considering that it uses only two or three variables, it would be mighty quick. No noticible time lag at all, except maybe on machines connected at 2400 baud! Smile!

Beyond that, I might suggest that you go back to Google, and add the word "multiple" to your previous search. I just spent nearly an hour there, on various sites - very interesting, indeed. <_<


Oddysey
To answer your questiona bout what I'm doing............

I have it setup like so:

index.php?page=tutorials -- This makes it include the tutorials page
index.php?page=tutorials&id=1 -- This makes it include the first tutorial.
index.php?page=tutorials&id=2 -- This makes it include the second tutorial.

I am able to do the first one and make it include the tutorials page, but its the second one I need help with. I want to be like:

IF page=tutorials AND id=1
include('tut1.php');
ELSEIF page=tutorials AND id=2
include('tut2.php');
ELSE
include('404.php');

Does that make it more clear to you of what I'm trying to do?
Shea;

Your example explains it all quite clearly - thanks.

Lemme double check myself here. If the variable named page contains the value "tutorials", and the variable id contains a digit between 1 and whatever, then the appropriate script named tut<span style='color:red'>x</span>.php should be executed. Have I got it correct so far?

If this is the case, then I can best help you by referring you to one of the websites I found earlier today, thanks to Google. (See my previous post for my search terms.) Go here, and scroll down to the first or second answer (both by someone named BlueDragon). Personally, I like the second one better - I'm just a function oriented kind of guy, I guess. Smile!

Now, that said, you could also institute a completely different numbering schema. Notice that any php-type bulletin board (like this one) uses unique ID numbers - there are no repeats at all. In that case, it doesn't matter what the page variable is equal to, the id variable is guaranteed to carry a completely unique value, therefore it will call the correct script, and hence generate the correct page. This might be something to consider, if nothing else seems to work for you.

Yes, it doesn't solve the actual problem, it only sidesteps it with a different methodology, but sometimes you gotta curb that desire to solve whatever problem you're facing, and get the job done for the customer (even if you are your own customer). You can always pick up the problem later, when time isn't so pressing to get the job done.

Hope this helps - good luck!


Oddysey
Thanx for the link Oddysey! I read that thread and searched google some more and I think i know how to do it now! I'm going to test the code below and see if it works. Tell me if you think there's an easier way.

if (&#036;_GET['page']=="" {
include('main.php');
}
elseif (&#036;_GET['id']=="") {
include('&#036;_GET['page']/main.php');
}
else {
include('&#036;_GET['page']/&#036;_GET['id']');
}

PS - I'm using &#036;_GET['variable'] instead of just &#036;variable because my host has register_globals turned off.
So far it's not working. Sad But I havent given up!! Big Teeth I've been trying some more methods and none have come close.....YET.
Shea;

In general, the &#036;_GET method is preferred for just the reason you mention - you never know how your host will configure (or re-configure) his/her PHP service. [angry]

Your routine looked good to me, until I saw your second reply stating that it didn't work. Hmmm..... OK, try this.

Your code calls for <span style='color:red'>include('&#036;_GET['page']/&#036;_GET['id']');</span>, right? Well, unless you have the period and the PHP extension in the ID variable, then your call won't be pointing to a valid script name, will it? See if this works: <span style='color:red'>include('&#036;_GET['page']/&#036;_GET['id']</span><span style='color:purple'>.php</span><span style='color:red'>');</span>.


Oddysey
Shea;

You know, I just realized that Invision itself does what you're looking for, more or less. The address bar at the top of this "Reply" page shows four variables on the address line. Might I suggest that you go into the Invision code a bit more deeply, and see what they are doing?

The obvious starting point would be where they are posting "Reply" buttons on the actual forum threads themselves. From there, one would think you should be able to follow the programming flow to see what they are doing. Of course, when I hit the "Preview Post" button, the address bar simply erased all of the variables, and now all I see (in preview mode) is the stock /index.php? line. Go figure.

If you do elect to scan the Invision code, let us know what you find. Wink


Oddysey
Pages: 1 2
Reference URL's