Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I am a novice at PHP (at best) but 2

Status
Not open for further replies.

novice60

Technical User
Apr 27, 2012
4
US
I am novice at PHP and I am trying to use an array to pull data from different tables within a database. Then to report that data to a form that i have created.

I have created separate functions that pulls the data from the database. Which they work because I can pull data when I set the array up to pull from only one table.

This is the array that I have to created. I know this is wrong... probably not even close. I can get it to work when pull the data one table but I am struggling to pull from multiple tables.

This is my connection and array to the DB.

<?php
/*Accounts*/
$currentMember->connection = $conn;
$accounts = $currentMember->retrieve_all_accounts();

/*Loop through account - Grabs data*/
while($account = mysqli_fetch_assoc($accounts)){
$transactionlog->connection = $conn;

/*Retrieve Account data*/
$transactionlog = new Bankaccount($account['BankAccountID'] );

//Different table within that database
$transactionlog = mysqli_fetch_assoc($transactionlog->retrieve_transactions());
$transactionlog->connection = $conn;

//Different table within the database
//$transactionlog = mysqli_fetch_assoc($transactionlog->retrieve_transactiontype());

echo '<tr>' . "\n";
echo "\t" . '<td>' . $account['BankAccountID'] . '</td>' . "\n";
echo "\t" . '<td>' . date($transactionlog['TransactionDate'] ) . '</td>' . "\n";
echo "\t" . '<td>' . $account($transactionlog['TransactionType']) . '</td>' . "\n";
echo "\t" . '<td>$' . $account($transactionlog['TransactionAmount'], 2) . '</td>' . "\n";
echo "\t" . '<td>$' . number_format($balance['CurrentBalance'], 2) . '</td>' . "\n";
echo '<tr>' . "\n";
}

/*Close DB*/
mysqli_close($db->connection);
?>

 
Looks like you have abstracted the actual query building.

You'd need to show us what your classes and their functions are doing.

Such as object currentMember and its method retrieve_all_accounts()

As a starting point it all comes down to how you build your queries.
So perhaps instead of abstracting single table queries, you can create a method that takes as parameters table names in an array even and builds queries based on that.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
I have included class and my functions. I do okay with SQL but PHP is a different world for me. I am a Student... I am not trying to get anyone to do my work for me. However, I do want to learn this.

I have been working on this and now I am getting data from multiples tables but I am only getting data from one account. and it is does display all the data. I would like for it to display all the data.

<?php
/*Bankaccount Class*/
class Bankaccount {

/*attributes for the first and last name of the class*/
private $accountid;
private $memberid;
private $accounttypeid;
private $accounttypename;
public $connection;

/*Constructs the function*/
function __construct($accountid){
$this->accountid = $accountid;
} //end constructor

/*Destroys the function*/
function __destruct(){
} //ends destructor

/*Get funtion*/
public function __get($name) {
return $this->$name;
} // Ends get funtction

/*Use the set function*/
public function __set($name, $value) {
$this->$name=$value;
} //End set function


/*This is what retrieves the values from memmory*/
public function retrieve_current_balance() {
$balance_query = "SELECT CurrentBalance FROM BankAccount WHERE BankAccountID = " . $this->accountid . " LIMIT 0,1";

$result = mysqli_query($this->connection, $balance_query);

return $result;
}
//query for transactions
public function retrieve_transactions() {
$transaction_query = "SELECT CurrentBalance, TransactionType, TransactionAmount, TransactionDate FROM BankAccount, TransactionType, TransactionLog = " . $this->accountid . " LIMIT 0,300";

$result = mysqli_query($this->connection, $transaction_query);

return $result;
}

/*Function validates user account for user ID*/
public function validate_useraccount(){

$account_query = "SELECT UserID FROM BankAccount WHERE UserID = " . $this->accountid;
$results = mysqli_query($this->connection, $account_query);
$db_array = mysqli_fetch_array($results);
$return_value = 1;

if(count($db_array()) < 1)
{
$return_value = 0;
}
return $return_value;
}

/*Deposit*/
public function deposit($UserID=0, $BankAccountID=0, $DepositAmount=0){
$getbalance = mysqli_fetch_assoc($this->retrieve_current_balance());
$currentbalance = $getbalance['CurrentBalance'];
$today = date('Y-m-d', mktime());

/*Deposit Funds*/
$newbalance = $currentbalance + $DepositAmount;
$deposit = mysqli_query($this->connection, "UPDATE BankAccount SET CurrentBalance = $newbalance WHERE BankAccountID = $BankAccountID");

if($deposit){ //transaction completed

/*Update Transaction log*/
$log_query = "INSERT INTO TransactionLog(TransactionTypeID, BankAccountID, UserID, TransactionAmount, TransactionDate) VALUES (1, $BankAccountID, $UserID, $DepositAmount, '$today')";

mysqli_query($this->connection, $log_query);
}

/*New Balance*/
return $newbalance;

}
} //End of class

?>

 
the bank account class appears to work if you get some data from it.

the while loop in your first post appears correctly constructed save that these two lines are incorrect
Code:
echo "\t" . '<td>' .  $account($transactionlog['TransactionType']) . '</td>' . "\n";
echo "\t" . '<td>$' . $account($transactionlog['TransactionAmount'], 2) . '</td>' . "\n";

replace them with the following

Code:
echo "\t" . '<td>' .  $transactionlog['TransactionType']) . '</td>' . "\n";
echo "\t" . '<td>' . number_format($transactionlog['TransactionAmount'], 2) . '</td>' . "\n";

the next line relates to a $balance object. i do not know where you are creating that but i'd guess that $balance is supposed to be $transactionlog.

if this does not fix the problem then you will need to post the retrieve_all_accounts method and associated class (and check/doublecheck that there are in fact more than one account to that user id)

 
Thanks.. You pointed me in the right direction.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top