Jump to content


Photo

Need help passing arrays out of a class


  • Please log in to reply
1 reply to this topic

#1 stormflurry

stormflurry
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 23 March 2006 - 11:31 PM

Hi All,

I'm trying to become a smarter programmer I guess by changing all of the files that I previously used as scripts to php classes. I'm having trouble moving arrays out of the class once I've selected the appropriate data. Here is the code below.

This is my class. What the class is supposed to do for me is connect me up to the data base set the date range that I want to look at and then select the appropriate information from the table. It currently does all of these things.

class select {

function DBSelect ($db_name) {

define('MYSQL_DB_USER','root');
define('MYSQL_DB_PASSWORD','----');
define('MYSQL_DB_HOST','localhost');
define('MYSQL_DB_NAME',$db_name);

$dbc = mysql_connect (MYSQL_DB_HOST,MYSQL_DB_USER,MYSQL_DB_PASSWORD);
mysql_select_db (MYSQL_DB_NAME);

}

function SetDate () {

if ($_GET['FirstSelectDay'] && $_GET['FirstSelectMonth'] && $_GET['FirstSelectYear']) {

$this->$NewDate = $_GET['FirstSelectYear'];
$this->$NewDate .= "-";
$this->$NewDate .= $_GET['FirstSelectMonth'];
$this->$NewDate .= "-";
$this->$NewDate .= $_GET['FirstSelectDay'];
return $this->newDate;

} else {

$this->$NewDate = date("Y-m-d");
return $this->newDate;

}

}

function GetResults ($procedure) {

$this->DBSelect('tndcs');
$this->setDate();
$date = $this->$newDate;

$this->$procedure = 'procedures/';
$this->$procedure .= $procedure;
$this->$procedure .= '.inc';

include_once($this->$procedure);

$result = @mysql_query($query);

if ($result) {

while($row = mysql_fetch_array($result,MYSQL_NUM)) {

$this->values = $row;

}

mysql_free_result($result);
return $this->values;


} else {

$error = mysql_error();
$error .= "\n\nUser id: {$_SESSION['user_id']}";
$message = "<p>$error<br>There has been an application error the server administrator has been contacted.</p>";
echo $message;
mail ("stormflurry@gmail.com","Error in Action $action",$error,"From: {$_SESSION['user_email']}");

}

}

}

My issue is that I can only get a single row back to the main script so if I have multiple rows of data I'm stuck. I've been pounding on this issue for many days and the only thing that I can come up with is placing the code that formats the selected data inside a subclass of select (). Basically make an extension that holds both the query that I want to run and the formatting that I need to apply to the results.

if I run the query in the main script getting multiple rows is no problem I do my standard

while ($row = mysql_fetch_array($result,MYSQL_NUM)) {}

I guess what i need to know how to do is this.

while ($values = $sql->getResults2('select_order_total')) {}

but this doesn't work.

Help please do I write unique extendor classes for each query which I feel is a mess or is there some better way.

Thanks,

Matt









#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 24 March 2006 - 12:10 AM

Change

while($row = mysql_fetch_array($result,MYSQL_NUM)) {

              $this->values = $row;

}

to

while($row = mysql_fetch_array($result,MYSQL_NUM)) {

               $this->values[] = $row;

}

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users