PDA

View Full Version : [php]mulitple pages


cokacola
10-10-08, 07:58 PM
hey, how many of you want to make pages.php?page=1
well...why shouldn't I make a tutorial for it :P

pages.php

<?php
switch($_GET['page']) {
//page 1
case 1:
echo "page1";
break;
//page 2, loads a different file
case 2:
include("page2.php");
break;
//show this if nothing else is shown
default:
echo "<a href='pages.php?page=1'>Page 1</a> <a href='pages.php?page=2'>Page 2</a>";
}
as you can see, I'm not the best at explaining stuff, but it seems simple enough if you are moderate at php :P

Hitterman
10-10-08, 08:07 PM
I always wanted to learn making webpages in PHP:).
Thanks for it. If you can add more tutorials in this thread i'll nominate this for TOTM.

cokacola
10-10-08, 08:12 PM
this thread, or this forum?
because, php is my thing :P
i assume u ment thread lol
just trying to think of one..
by the way...
what happons if you win totm?

Hitterman
10-10-08, 08:22 PM
If you'll post more PHP tutorials i'll nominate you for Tutorial of the Month. And who knows, you may get a high post at tutorialistic.

cokacola
10-10-08, 08:46 PM
I have a fan of this thread now don't I hitty :P
thought I might make a few mysql tutorials
---
1.connect to mysql server
here we go..

<?php
mysql_connect("mysql_server", "mysql_username", "mysql_password") or die("<p>mysql_error: ".mysql_error()."</p>");
//replace mysql_server with your server, mysql_username with your mysql username, and mysql_password with your mysql password...if it fails, it will say error: then state the mysql error.
//select a database
mysql_select_db("mysql_database");
//just replace mysql_database with your database name.
?>
wow, you just connected to a database :P
2.select data from a database and display it

<?php
//connect to server
mysql_connect("mysql_server", "mysql_username", "mysql_password") or die ("<p>Mysql_Errro: ".mysql_error()."</p>");
//select database
mysql_select_db("mysql_database") or die("<p>Failed to select database, error: ".mysql_error()."</p>");
//set sql query, replacing `table` with your `table`(must have the ``)
$sql="SELECT * FROM `table`";
//run sql query
$s=mysql_query($sql) or die("<p>Error selecting data from db: ".mysql_error()."</p>");
//put data into an array
$row=mysql_fetch_assoc($s);
//display data, data 1 and data 2 must be in your table, and must have something inserted(showing you how to insert later...)
echo "<p>Data1: $row[data1]</p><p>data2: $row[data2]";
//thats about it for that :P
?>

wow, you can connect and display data from a database now :P
3.insert data into a database

<?php
//connect to DB
mysql_connect("server", "user", "pass") or die("<p>".mysql_error()."</p>");
//select a DB
mysql_select_db("database") or die("<p>".mysql_error()."</p>");
//set sql query
$sql="INSERT INTO `table` (`data1`, `data2`) VALUES (`text1`, `text2`)";
//run query
mysql_query($sql) or die("<p>".mysql_error()."</p>");
echo "Insert text1, and text2 into data1, and data2 successfully.";
?>

and, you can insert data aswell now :P
plus my neck herts aswell, so I hope someone enjoys this...
4.display certin data

<?php
//connect to server
mysql_connect("server", "user", "pass") or die("<p>".mysql_error()."</p>");
//select db
mysql_select_db("database") or die("<p>".mysql_error()."</p>");
//select sql
$sql="SELECT * FROM `table` WHERE `type`='data'";
//run query
$s=mysql_query($sql) or die("<p>".mysql_error()."</p>");
//display every thing that has a field called `type` that is set to 'data'
while($row=mysql_fetch_assoc($s)) {
echo "<p>$row[data1]</p><p>$row[data2]</p>";
}
?>

wow, my shoulders are starting to hert now, so you better like it :D
1 more..
5.checking if data exists

<?php
//connect
mysql_connect("server", "user", "pass") or die("<p>".mysql_error()."</p>");
//db selection
mysql_select_db("database") or die("<p>".mysql_error()."</p>");
//sql query data
$sql="SELECT * FROM `table` WHERE `data1`='text1'";
//sql query
$s=mysql_query($sql) or die("<p>".mysql_error()."</p>");
//see if any exist
$count=mysql_num_rows($s);
if($count !=0) {
echo "<p>$row[data1]</p>";
} else {
echo "<p>Sorry, the data doesnt exist.</p>";
}
?>

ok, done, now, i wasn't sitting strait...cuz I am an idiot(lol) so I have sore shoulders and a sore neck..and when i started I had a headache, and I still do...please, oh, PLEAE enjoy it!
p.s I sat here, and wrote this all, I did not copy and paste, and took no brakes. you just wait till I write a php user system tutorial...you will like :P(includes profile editing, comments, signitures for comments, admins being able to edit and delete users.....big big big...lol, may be a while before thats ready though)
enjoy anyways, for now, cya later!

Motorokr
11-10-08, 03:22 PM
nics and simple explaination

cokacola
11-10-08, 04:30 PM
well, thankyou there!
I was kinda hoping it was :P

RoMusik
10-11-08, 07:52 PM
Thnks for this helpfully tutorial. I was searching for some time for this tutorial. Thnks again

Hitterman
14-11-08, 06:39 PM
"Thnks" to you too:p. There are many available on Internet but Coke has really explained well.

SrijanK
16-11-08, 09:59 PM
really nice tutorials for starters like me.

cokacola
01-12-08, 06:19 PM
your welcome everyone!
enjoy!

piejustice
19-12-08, 08:58 AM
k this is a pretty basic tutorial :) there are still better and more comprehensive ways to do this, but this is good for a beginner :P