Tutorial Categories
» PHP
» PHP User System
» Web Development
» Javascript
» Cascading Style Sheets
» HTML
» Adobe Photoshop
» Adobe Dreamweaver
» Adobe Fireworks
» Microsoft Windows
» Microsoft Office
» Apple
» Linux
Partners
» How To Build A Solar Panel » Affordable Web Hosting » How to make a website » Tech Product Reviews » Web Hosting Reviews » Buy Website Traffic » Linux Training » Mac Pro Memory » Dating » EducationWarning: file() [function.file]: URL file-access is disabled in the server configuration in /home/techt/public_html/index.php on line 203
Warning: file(http://www.tucows.no/external.asp) [function.file]: failed to open stream: no suitable wrapper could be found in /home/techt/public_html/index.php on line 203
Warning: Invalid argument supplied for foreach() in /home/techt/public_html/index.php on line 205
We'll start off with the object-oriented first, it's simple, we only need 4 functions. We'll call out class tutorials.
So that's our class, the first function will be a construct function, this function executes as soon as we start the class, but this only happens if you use PHP 5, so for those still on php 4, we make another function the same name as the class, which also executes as we start the class.
What's this? So when it starts, for php 5, it will direct us to do the tutorials function which connects to our mysql and database.
Now we do a function to get the tutorial being requested. We call this, get_tut.
We need the $id of the tutorial that's being requested. Now we select it from the table, and check to see if a tutorial is found using mysql_num_rows, if not then we show an error, if so then return the results.
Our next function is a fairly long one, it will take care of all the pagination for us, with it being fairly advanced by checking all the numbers including the previous and next links. So first off, let's make the function.
Num is the number of total pages, page is page we're on now, and id is the id of the tutorial.
If we have more than one page, then we can show the pagination.
If we're NOT on the first page.
Then we can show the previous link, to do this we make a variable called prev, and use the page number - 1, so say $page = 2, our prev link would be 1. Then the link ?id=1&page=1.
This next bit of code is just a simple set variable as 1.
We check to see if the variable is less than or equal to the number of pages.
We then print the link of the page number and add one to the variable.
Now we check to see if it's not the last page.
If not, then we can show the next link.
We can also close the function there, because we're finished with it now. So one more function left to do, easy, it's called __deconstruct and starts automatically after the file has finished downloading.
That's the end of the file, so we close mysql. Now it's onto the PHP side, showing the pagination.
<?php
class tutorials{
}
?>So that's our class, the first function will be a construct function, this function executes as soon as we start the class, but this only happens if you use PHP 5, so for those still on php 4, we make another function the same name as the class, which also executes as we start the class.
<?php function __construct(){ //for php 5
$this->tutorials();
}
function tutorials(){ //for php 4
mysql_connect("localhost","USER","PASS");
mysql_select_db("DATABASE") or die(mysql_error());
}What's this? So when it starts, for php 5, it will direct us to do the tutorials function which connects to our mysql and database.
Now we do a function to get the tutorial being requested. We call this, get_tut.
<?php function get_tut($id){
}We need the $id of the tutorial that's being requested. Now we select it from the table, and check to see if a tutorial is found using mysql_num_rows, if not then we show an error, if so then return the results.
<?php $sql = mysql_query('SELECT * FROM `tuts` WHERE `id` = '.$id) or die(mysql_error());
if( mysql_num_rows( $sql ) == 0 ){
die("No tutorial can be selected from that ID");
}
return $sql;Our next function is a fairly long one, it will take care of all the pagination for us, with it being fairly advanced by checking all the numbers including the previous and next links. So first off, let's make the function.
<?php function pagination($num, $page, $id){
}Num is the number of total pages, page is page we're on now, and id is the id of the tutorial.
<?php if( $num > 1 ){If we have more than one page, then we can show the pagination.
<?php //If it isn't the first page
if( $page != 1 ){If we're NOT on the first page.
<?php //Show the previous link $prev = $page - 1; print '<a href="?id='. $id .'&page='. $prev .'">Previous</a> '; }
Then we can show the previous link, to do this we make a variable called prev, and use the page number - 1, so say $page = 2, our prev link would be 1. Then the link ?id=1&page=1.
This next bit of code is just a simple set variable as 1.
<?php //Set a variable as 1 $i = 1;
We check to see if the variable is less than or equal to the number of pages.
<?php //While the variable is less then or equals the number of pages.
while( $i <= $num ){We then print the link of the page number and add one to the variable.
<?php //Print the number with a link. print '<a href="?id='. $id .'&page='. $i .'">'. $i .'</a> '; //Add one to the variable $i++; }
Now we check to see if it's not the last page.
<?php //If it's not the last page
if( $page < $num ){If not, then we can show the next link.
<?php //Show the next link $next = $page + 1; print ' <a href="?id='. $id .'&page='. $next .'">Next</a>'; }}
We can also close the function there, because we're finished with it now. So one more function left to do, easy, it's called __deconstruct and starts automatically after the file has finished downloading.
<?php function __deconstruct(){
mysql_close();
}
}
?>That's the end of the file, so we close mysql. Now it's onto the PHP side, showing the pagination.