Jump to content

PHP Classes


seran128

Recommended Posts

OK what I want to do is create a class that I can include on my other pages, call function from that class and return those resluts to the calling page. Any references to online materials on php classes would be great!

My code should be something like;


class_db.php

  class db(){

//Top level db connection

var $db_name="db_name";
var $db_host="localhost";
var $db_user="db_user";
var $db_password="db_password";


$connection=mysql_connect($db_host,$db_user,$db_password) or die("I Couldn't connect");
$db=mysql_select_db($db_name,$connection) or die("I Couldn't select your database");

function getlist($inTable){

$sql="select * from $inTable order by ID";
$result=mysql_query($sql,$connection) or die(mysql_error());

//I need to return my $result here but don't know how

}

function updatelist($inTable,$description,$id){

            $sql="update $inTable set description='$description' where regionId='$id'";
$result=mysql_query($sql,$connection) or die(mysql_error());


//I need to return my $result here but don't know how

}

function deletelist($inTable,$id){

            $sql="delete from $inTable where regionId='$id'";
$result=mysql_query($sql,$connection) or die(mysql_error());


//I need to return my $result here but don't know how

}


//also need to close any dbconnection but don't know how
//end the class
}


page.php

include("class_db.php");
.......
<?

getlist(my_table)
$i = 0;

//Need to return the output from the function so the following line can use it.

while($row=mysql_fetch_array($result)) {
$i++;
echo "<tr class=\"d".($i & 1)."\">";
?>

....other HTML code Is Here........

<? } ?>


Link to comment
Share on other sites

Access the variables and functions within the class using "->".

Example:
[code]
//myClass.inc
class myClass{
  var $testVar;
  function testFunc(){
    return $this->testVar;
  }
}

//test.php

include("myClass.inc");
$mc = new myClass;
$mc->testVar = "Hello there!";

echo $mc->testFunc();  //this will output "Hello there" to the screen
[/code]

Some further reading:
[url=http://us3.php.net/class]http://us3.php.net/class[/url]
[url=http://codewalkers.com/tutorials/54/1.html]http://codewalkers.com/tutorials/54/1.html[/url]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.