Forum Moderators: coopster

Message Too Old, No Replies

php class avoiding redundancy

         

nanat

8:42 am on Jul 23, 2010 (gmt 0)

10+ Year Member



Im Beginner of php OOP and i hate hard code :(.. i have a sample redundant script called Form..


class Form
{
public $countGrp;
public $magGrp;

function countME ($dataFrom, $dateTo, $value)
{
global $database;

$database->myDatabaseCon();

$result = "select emp_id, huraLabor from where fromDate= '$dataFrom' and toDate= '$dateTo' and location = '$value)'";

if( odbc_num_rows( $query ) > 0 ){
return odbc_num_rows( $query );
}
}

function TotalMandays ($dataFrom, $dateTo, $value)
{
global $database;
$database->myDatabaseCon();
$result = "select emp_id, huraLabor from where fromDate= '$dataFrom' and toDate= '$dateTo' and location = '$value)'";
$query = odbc_exec($database->connection,$result);
$addMe = 0;
while ( $t = odbc_fetch_array($query))
{
$totalHours = $t['huraLabor'];
$addMe = $addMe + $totalHours;
}

return $addMe;

}
}



i tried to combine the the two script but it doesn't work


i try to echo the $this->getMe inside of countME() return true
but inside the showGet() it will return false whats wrong with my code?

class Form
{
public $countGrp;
public $magGrp;
public $getMe;

function countME ($dataFrom, $dateTo, $value)
{
global $database;

$database->myDatabaseCon();

$result = "select emp_id, huraLabor from where fromDate= '$dataFrom' and toDate= '$dateTo' and location = '$value)'";

if( odbc_num_rows( $query ) > 0 ){
return odbc_num_rows( $query );
}


while ( $t = odbc_fetch_array($query))
{
$totalHours = $t['huraLabor'];
$addMe = $addMe + $totalHours;
}

$this->getMe = $addMe;
}

function showGet()
{
echo $this->getMe
}

}



tnx nanat

lostdreamer

9:03 am on Jul 23, 2010 (gmt 0)

10+ Year Member



At a first glance there is nothing wrong, but $this->showGet() would only echo anything if you have first ran $this->countME() (since that function is filling the getMe variable)

Also, since $this->getMe is a public variable, you don't need a get function for it, you could just do:
$form->countME($dataFrom, $dateTo, $value);
echo $form->getMe;

Regards,
LostDreamer

Matthew1980

9:06 am on Jul 23, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there nanat,

while ( $t = odbc_fetch_array($query))
{
$addMe = $addMe + $t['huraLabor'];
}


Where is $addMe being set? because I can't see a reference to it so you are adding $totalHours to nothing in effect, so $this->getMe; is only equal to the value of $totalHours at this point.

I'm curious though as to why you are trying to access it from another function when all you need to do is something like this:-

$MyClass = new form();

echo $MyClass->getMe;//will echo the contents when it's set as it has no default value

I'm kinda new to OOp myself though so I could be a little off with that advice, but the logic is there.

there is no harm with calling it from a internal function, I just prefer to reference the var directly :)

Also:-

$result = "SELECT `emp_id`, `huraLabor` FROM TABLE NAME HERE WHERE `fromDate` = '".$dataFrom."' && `toDate` = '".$dateTo."' && `location` = '".$value."' ";

Needs attention, just replace the bolded text with your table name as this is what's missing, at least that's what it looks like to me :)

[EDIT]: Lostdreamer got it right first there!

Cheers,
MRb

nanat

6:47 am on Jul 26, 2010 (gmt 0)

10+ Year Member



@lostdreamer ic.. I get it thx ^^