PDA

View Full Version : PHP Question



david622
09-03-2005, 01:37 AM
I'll start this off by saying that I'm starting a new webcomic. Reading one of the threads in the Books/Comics subdivision in this forum I learned about offensivecomic.com

I enjoy the comic, but what I'm interested in is the PHP behind the site. I looked at the source code but I don't think I'm picking up everything so I'm asking for your help.

In the archives page of offensivecomic.com, each comic link is to offensivecomic.com/?comic=XX

This seems to make it at each click of the link the index page will load, same as always, but with the specified comic chosen in the body of the page. This is nice because you don't have to make 100 different HTML pages that contain the same content except for the actual comic.

How do I assign these PHP comic numbers to each issue like that done in this site so I can make my own site that works in a similar way?

I appreciate your help,
david

p.s. excuse this explanation, if you need me to clarify what I mean please say so.

edit: also, how would I be able to make a "previous" button, so the comic number goes down by 1 from the current comic selected?

SeK612
09-03-2005, 06:20 AM
I'd also be interested in finding out how the exampleurl.com/?page=XX thing works. I've seen similar on lots of sites but haven't really found out how it funtions and the benifets of such a setup.

Looking at the source code of a PHP page may not reveal a lot. As I understand it PHP that is executed often doesn't appear on the page after it's loaded and so won't show up in the source.

For ease of editing and keeping certain things the same site wide (such as a navigation menu) you can used includes. These are code in a seperate file which you can place in your root directory and call to any page on your site. There may be a better way to do this though.

tesco
09-03-2005, 03:00 PM
When you have a php extension on a page, and php on your server, then you can put
?variable=value
Or for more than one variable
?variable1=value1&variable2=value2
(they are seperated by '&')

Now, in the php file all of those variables will be, for example, $_GET['variable1'] and $_GET['variable2'], and the value of them will be what is after the '=' sign.

To work with these, your articles will have to be in some sort of php. The easiest way you can do this all in one page is to setup like this:



<?php
if ($_GET['comic'] == 1)
{
?>
INSERT THE HTML FOR THIS COMIC HERE
<?php
}
else if ($_GET['comic'] == 2)
{
?>
INSERT THE HTML FOR THIS COMIC HERE
<?php
}
else if ($_GET['comic'] == 3)
{
?>
INSERT THE HTML FOR THIS COMIC HERE
<?php
}
else
{
?>
HTML FOR IF THERE IS NO ARTICLE WITH NAME/ID THEY SAID
<?php
}
?>

etc.
Note: The area between the <?php and ?> is the php code, anything outside of those tas will be treated as plain html code.

An even better way is to store the articles in a database(mysql) but that takes some understanding of PHP to get going so it would be best if you stick with this unles you want to start reading some tutorials on php.

I could also PM you some example sites that i've made that use mysql.

david622
09-03-2005, 03:03 PM
alright, thanks! so, if in the archives page for the comic, i just pasted this PHP there, it should do what I'm talking about? And also, how would I be able to do a "previous" button.

i should probably add that i have no previous php knowledge ^_^,

tesco
09-03-2005, 03:07 PM
alright, thanks! so, if in the archives page for the comic, i just pasted this PHP there, it should do what I'm talking about? And also, how would I be able to do a "previous" button.

i should probably add that i have no previous php knowledge ^_^,
The previous button will be
<a href="index.php?comic=<?php echo $_GET['article']-1; ?>">Previous Article</a>
Assuming you will be using numbers

david622
09-03-2005, 03:09 PM
alright thanks

i'll let you know how this works out when i try it

sparsely
09-03-2005, 07:42 PM
in such a case, you would be much safer using a switch.


switch($_GET['comic'])
{
case 1:
// show #1
break;
case 2:
// etc
break;
default:
// something;
}

david622
09-03-2005, 10:08 PM
Before I actually start making the page, I'm a bit confused about a couple things.

Firstly, rossco, I'm wondering, would this code you posted before go in an archives page? And what would go on the "current issue" index page to display the current comic?

Maybe if I could see an example source code it woule be much clearer. I appreciate your help, but I'm new to the PHP world. Rossco's code seems to be defining a number to each comic, but I'm not sure where this code would go.


Second, sparsely's code confuses me b/c I'm not exactly sure what it's for. does it serve the same function as rossco's?

please, explain each detail to me!

i really appreciate the help,
david

tesco
09-04-2005, 01:40 AM
Sparsely's is the same idea as mine, just another way of doing it.
I've never done switches before tho so i'm not sure if it's better.

Mine would go in an index.php file or whatever name you want. All the comics are included in that one file, so you don't really need any other files other than css, images, etc.

sparsely
09-04-2005, 04:08 AM
the problem with using uri parms in if statements is that someone could specify something other than what you want.

with a switch there are a set of valid values, and that's all, and you can make a default action. and a switch would also be a bit faster than a series of ifelse's

tesco
09-04-2005, 02:15 PM
with a switch there are a set of valid values, and that's all, and you can make a default action.
Same wiht If's :huh:

But if it's faster then go for it.

Rick Phlegm
09-04-2005, 03:59 PM
Using a switch statement instead of nested if-else statements is just better programming. It results in better looking code and should be more efficient, but not to the degree that it would actually be noticable for such a small program.