Jump to content

Recommended Posts

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







Link to comment
https://forums.phpfreaks.com/topic/5641-need-help-passing-arrays-out-of-a-class/
Share on other sites

Change

[code]while($row = mysql_fetch_array($result,MYSQL_NUM)) {

              $this->values = $row;

}[/code]

to

[code]while($row = mysql_fetch_array($result,MYSQL_NUM)) {

               $this->values[] = $row;

}[/code]
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.