News:

Tekforums.net - The improved home of Tekforums! :D

Main Menu

Php Help

Started by neXus, February 10, 2007, 14:21:03 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

neXus

Ok I am slowly learning php properly in terms of large applications from java and learning .net as well.

I have written a database connection class:

class connectionFactory {

function makeConnection() {

require_once "settings.inc.php";

/* Conncect to mysql */
$connection = mysql_connect($dbhost, $dbuser, $dbpass);
$this->connection = $connection;
if (!mysql_connect($dbhost, $dbuser, $dbpass)) {
return "error";
}

/* Conncect to the database */
mysql_select_db("$dbname");
if (!mysql_select_db("$dbname")) {
return "error";
}

/* small test line to confirm that a conncection has been successfull*/
//return "connection accomplished";

}

/* make the mysql statement */
function SqlQuery($query) {

$query = mysql_query($query);

If (!$query) {
return "error";
}
else { return $query; }

}

//*** Function: fetchArray, Purpose: Get array of query results ***
function fetchArray($result) {

return mysql_fetch_array($result);

}

/* Close the database connection after use */
function dbClose() {
mysql_close();
}

/* Tag Devinitions */
//  Associative Array.
//  -----------------------------------

//  -----------------------------------


}


?>


This is fine as far as I can see
Then I have a data access object - this just gets the data related to a certain element of what your doing.




require_once "connectionFactory.inc.php";

/* Display All 10 Man Raids withen the last 10 days */
function get10manRaids() {
$connection = &New connectionFactory();
$connection->makeConnection();
$result = $connection->SqlQuery(SELECT * FROM 10man_raid);

while ($row = $connection->fetchArray($result)) {
$raid = $row["raid_day"] .$row["raid_time"];
return $raid;
//echo $raid;
}
$connection->dbClose();
}

get10manRaids();

/*  */

/*  */



?>


get10manRaids(); is just displaying for test purposes.
As you can see it uses the connection class and works fine
The echo $raid; is there for testing and displays the result fine

if i comment that and the function call and and uncomment the return it returns no errors so seems to work fine.

Here it is with the echo displaying some fields from a table
http://www.5thdigital.com/development/booking/library/raidschedualDOA.inc.php

Now, I create a file which the display, using the DOA file to fetch the data and use it for display.

So you gone database---->get data---->display data

So this display file is....




// Display results
include "library/raidschedualDOA.inc.php";
$display = get10manRaids();
echo $display;
?>


http://www.5thdigital.com/development/booking/raidschedual.php

Issue I have is that the return

         $raid = $row["raid_day"] .$row["raid_time"];
         return $raid;


It is not displaying a result when I use it above, if I comment out the return and use echo it will work fine

Any ideas how you not echo a result in the DOA function and just get the result only then use that data to display.

The next step if anyone can help is just being able to get bits of data and use them to display in rows in a table.
Do you need to have a function for each field in php or can you do a return and use the result in the html rows etc?

neXus

I can Use returns fine when I create simple object classes in php knowing what I do from Java..

This return works fine


/*
Comment: A person: name, age, gender, address
*/

class person {

/* Persons First Name */
var $fistName;
/* Persons Last Name */
var $lastName;
/* Persons current Address */
var $address;
/* Home Telepone Number */
var $telephone;
/* Date of Birth - 99/99/1999 */
var $dob;
/* A persons gender - M or F */
var $gender;

/* Creates a person */
function person( $fistName, $lastName, $address, $telephone, $dob, $gender ) {
$this->firstname = $fistName;
$this->lastname = $lastName;
$this->address = $address;
$this->telephone = $telephone;
$this->dob = $dob;
$this->gender = $gender;
}

function getFistName() {
return $this->firstname;
}

function getLastName() {
return $this->lastname;
}

function getAddress() {
return $this->address;
}

function getTelephone() {
return $this->telephone;
}

function getDob() {
return $this->dob;
}

function getGender() {
return $this->gender;
}

function setFistName($fistName) {
$this->firstname = $fistName;
}

function setLastName($lastName) {
$this->lastname = $lastName;
}

function setAddress($address) {
return $this->address;
}

function setTelephone($telephone) {
return $this->telephone;
}

function setDob($dob) {
return $this->dob;
}

function toString() {
return ( "name: " . $this->firstname . " " . $this->lastname . " " . " address: " . $this->address . " tel: " .  $this->telephone . " DOB: " . $this->dob . " gender: " . $this->gender);
}

}


?>



Should I change the DOA to something similar?

White Giant

No idea how to help you, but are you writing a raid manager?

neXus

Doing a proper one from one I have already created which works great but crap code. (well steveF did it initially and i just worked on it a bit more)
So doing one that is a proper system in structure.

White Giant

Cool, Id love to see the finished product.

cornet

Good to see you are still looking at PHP.

I think the problem is that you misunderstand the use for "return".

return means "Return this value back and stop all further processing"

So if there is 1 or more rows returned by the query you will end up with the following issues:

* You will only return the first row (which could have blank data in it)
* The line $connection->dbClose(); will never be reached.

However dont re-invent the wheel. Go look at http://pear.php.net/, specifically http://pear.php.net/package/MDB2 which is a database abstraction layer.

This will make things much easier and quicker, especially when dealing with data extracted from the database and catching errors (which you are not really doing at the moment)

Another thing to get out of the habbit of doing is writing statements like:
"select * from someTable"

Always specify the fields, even if you want to return all of them. The reason for this is maintainability. If 6months down the line you need to add a column to your database table then you will potentially break all your existing SQL.


All that said, keep at it :)

Cornet

M3ta7h3ad

Or.... look at ruby if you really like the MVC architecture :D

Bloody amazing programming language is that.

I bought the Beginning Ruby on Rails book from Wrox publishing from amazon for under Ã,£20. Just working my way through it :)

Web 2.0 and AJAX here I come :D

neXus

Thanks cornet but this is early stuff as I am learning the more detailed stuff and something I would like to do from scratch myself and have as my thing from start to finish.

By database class has more to it now all working great, I just need to be able to know how to return a value only not echo it and then call the function to get the data then do something with it.