Jump to content

Best way to extract information from an array...


ckoeber

Recommended Posts

Hello.

 

I have an array that I created based on information from a table I created in a database. Here is the var_dump I get for it:

 

array(11) { 
[0]=> array(1) 
		{ [0]=> NULL 
		} 
[1]=> array(1) 
		{ [1]=> NULL 
		} 
[2]=> array(1) 
		{ [2]=> NULL 
		} 
[3]=> array(1) 
		{ [3]=> string(0) "" 
		} 
[4]=> array(1) 
		{ [4]=> NULL 
		} 
[5]=> array(1) 
		{ [5]=> NULL 
		} 
[6]=> array(1) 
		{ [6]=> string(0) "" 
		} 
[7]=> array(1)
		{ [7]=> string(0) "" 
		}
[8]=> array(1) 
		{ [8]=> string(10) "2009-03-31"
		}
[9]=> array(1) 
		{ [9]=> string(3) "add" 
		} 
[10]=> array(1) 
		{ [10]=> string(6) "100000" 
		} 
}

 

This code represents one row in the database. Here is the code for the query I used to get this row:

 

SELECT portfolio_transactions.uid, portfolio_transactions.symbol, portfolio_transactions.shares, portfolio_transactions.price AS originalprice, (
portfolio_transactions.shares * portfolio_transactions.shares
) + portfolio_transactions.commission AS cost, portfolio_transactions.transaction_date AS purchasedate, portfolio_transactions.type, portfolio_transactions.commission, portfolio_transactions.amount
FROM portfolio_transactions WHERE uid = !uid;

 

My questions are these:

 

1. How can I get each element as represented by the above var_dump as a row in an HTML table. This is what I have so far in code:

 

foreach ($performancedata as $singleline) {
		  	$output .= '<tr>';
		  	$output .= '<th scope="row">'.$singleline[9].'</th>';
		    $output .= '<td>'.$singleline[0].'</td>';
		    $output .= '<td>'.$singleline[2].'</td>';
		    $output .= '<td>'.$singleline[3].'</td>';
		    $output .= '<td>'.$singleline[1].'</td>';
		    $output .= '<td>'.$singleline[4].'</td>';
		    $output .= '<td>'.$singleline[10].'</td>';
		    $output .= '<td>'.$singleline[8].'</td>';
		    $output .= '</tr>';
		  }

 

Where $performancedata represents a single element that has the array I first described above. The problem is that the following happens with my output of $singleline:

 

$singleline: array(4) { [0]=> NULL [3]=> NULL [6]=> int(0) [7]=> int(0) } $singleline: array(1) { [1]=> NULL } $singleline: array(1) { [2]=> NULL } $singleline: array(1) { [3]=> string(0) "" } $singleline: array(1) { [4]=> NULL } $singleline: array(1) { [5]=> NULL } $singleline: array(1) { [6]=> string(0) "" } $singleline: array(1) { [7]=> string(0) "" } $singleline: array(1) { [8]=> string(10) "2009-03-31" } $singleline: array(1) { [9]=> string(3) "add" } $singleline: array(1) { [10]=> string(6) "100000" } 

 

Notice that a $singleline element is created for each part of the original array.

 

2. Also, how do I get the 100000 converted into a decimal value to perform numeric calculations (the value represents currency)?

 

Any guidance?

 

Thank you for your time.

 

Regards,

Christopher Koeber

Link to comment
Share on other sites

try changing

foreach ($performancedata as $singleline) {
              $output .= '<tr>';
              $output .= '<th scope="row">'.$singleline[9].'</th>';
             $output .= '<td>'.$singleline[0].'</td>';
             $output .= '<td>'.$singleline[2].'</td>';
             $output .= '<td>'.$singleline[3].'</td>';
             $output .= '<td>'.$singleline[1].'</td>';
             $output .= '<td>'.$singleline[4].'</td>';
             $output .= '<td>'.$singleline[10].'</td>';
             $output .= '<td>'.$singleline[8].'</td>';
             $output .= '</tr>';
           }

 

to

foreach ($performancedata as $id1=>$singleline){
              $output .= '<tr>';
              $output .= '<th scope="row">'.$singleline[9].'</th>';
             $output .= '<td>'.$singleline[0].'</td>';
             $output .= '<td>'.$singleline[2].'</td>';
             $output .= '<td>'.$singleline[3].'</td>';
             $output .= '<td>'.$singleline[1].'</td>';
             $output .= '<td>'.$singleline[4].'</td>';
             $output .= '<td>'.$singleline[10].'</td>';
             $output .= '<td>'.$singleline[8].'</td>';
             $output .= '</tr>';
           }

 

i would also take a look at the code where you build the array... it is wrong structure

why do you have 10 arrays with single element

its a waste.

but i guess you have your reasons.

 

let me know your progress

Link to comment
Share on other sites

OK, to answer how I built the array:

 

$statistics_db_query = t('SELECT portfolio_transactions.uid, portfolio_transactions.symbol, portfolio_transactions.shares, portfolio_transactions.price AS originalprice, (
portfolio_transactions.shares * portfolio_transactions.shares
) + portfolio_transactions.commission AS cost, portfolio_transactions.transaction_date AS purchasedate, portfolio_transactions.type, portfolio_transactions.commission, portfolio_transactions.amount
FROM portfolio_transactions WHERE uid = !uid;', array('!uid' => $user->uid,));
$statistics_db_results = db_query($statistics_db_query);
$symbols = array();
$performancedata = array();
$headers = array();
$headers = _portfolio_needed_headers('block');
$quotes = array();
$quotes = _portfolio_fetch_quotes($symbols, $headers);
while ($statistics = db_fetch_object($statistics_db_results)) {
	$symbols[] = $statistics->symbol;
	$performancedata[][0] = $statistics->symbol;
	$performancedata[][1] = $statistics->shares;
	$performancedata[][2] = $statistics->originalprice;
	$performancedata[][3] = ''; //Current Price
	$performancedata[][4] = $statistics->commission;
	$performancedata[][5] = $statistics->cost;
	$performancedata[][6] = ''; //Current Value
	$performancedata[][7] = ''; //Lost or Gain
	$performancedata[][8] = $statistics->purchasedate;
	$performancedata[][9] = $statistics->type;
	$performancedata[][10] = $statistics->amount;
}
$quotes = array();
$quotes = _portfolio_fetch_quotes($symbols, $headers);
$i = 0;
foreach ($quotes as $quote) {
  	$performancedata[$i][3] = $quote[3];
  	$performancedata[$i][6] = ($quote[3]*$performancedata[$i][1]) + $performancedata[$i][4];
  	$performancedata[$i][7] = $performancedata[$i][6] - $performancedata[$i][5];
  	}

 

The db_query() function is a function from Drupal that runs the query against the database for me (and returns a handle) and the db_fetch_object() function gets the results (provided you supply the handle).

 

Let me know if I am doing something wrong here.

 

Thanks for your time.

 

Regards,

Christopher Koeber

Link to comment
Share on other sites

First lets try this and see what happens

 

Replace

   $i = 0;
   foreach ($quotes as $quote) {
        $performancedata[$i][3] = $quote[3];
        $performancedata[$i][6] = ($quote[3]*$performancedata[$i][1]) + $performancedata[$i][4];
        $performancedata[$i][7] = $performancedata[$i][6] - $performancedata[$i][5];
     }

 

with

   foreach ($quotes as $quote) 
   {
        $performancedata[$i][3] = $quote[3];
        $performancedata[$i][6] = ($quote[3]*$performancedata[$i][1]) + $performancedata[$i][4];
        $performancedata[$i][7] = $performancedata[$i][6] - $performancedata[$i][5];
        /// you must increase the value of $i...  or the foreach loop will keep overwriting the previous values because $i remains with the value 0
        $i++;
     }

 

after that i would like to see the var_dump();

Link to comment
Share on other sites

Hello,

 

I have made the changes as suggested above and I still get the same issue. Here is my var_dump for my array:

 

$performancedata: 
array(11) { 
[0]=> array(1) 

	{ [0]=> NULL 
	} 

[1]=> array(1) 

	{ [1]=> NULL 
	} 

[2]=> array(1) 

	{ [2]=> NULL
	} 

[3]=> array(1) 

	{ [3]=> string(0) "" 
	} 

[4]=> array(1) 

	{ [4]=> NULL 
	} 

[5]=> array(1) 

	{ [5]=> NULL 
	}

[6]=> array(1) 

	{ [6]=> string(0) "" 
	} 

[7]=> array(1) 

	{ [7]=> string(0) "" 
	} 

[8]=> array(1) 

	{ [8]=> string(10) "2009-03-31" 
	} 

[9]=> array(1) 

	{ [9]=> string(3) "add" 
	} 

[10]=> array(1) 

	{ [10]=> string(6) "100000" 
	} 
} 

$singleline: array(4) { [0]=> NULL [3]=> NULL [6]=> int(0) [7]=> int(0) } 
$singleline: array(4) { [1]=> NULL [3]=> NULL [6]=> int(0) [7]=> int(0) } 
$singleline: array(1) { [2]=> NULL } 
$singleline: array(1) { [3]=> string(0) "" } 
$singleline: array(1) { [4]=> NULL } 
$singleline: array(1) { [5]=> NULL } 
$singleline: array(1) { [6]=> string(0) "" } 
$singleline: array(1) { [7]=> string(0) "" } 
$singleline: array(1) { [8]=> string(10) "2009-03-31" } 
$singleline: array(1) { [9]=> string(3) "add" } 
$singleline: array(1) { [10]=> string(6) "100000" }

 

In addition, I have attached my entire code file so that people can see what I wrote. I hope that this helps everyone who would like to know how to write good arrays.

 

Let me know if I should provide something else or I need to do anything.

 

Thank you so much for the time and effort.

 

Regards,

Christopher Koeber

 

[attachment deleted by admin]

Link to comment
Share on other sites

Let's try this

   $i = 0;
   foreach ($quotes as $quote) 
   {
        $performancedata_results[$i] = $quote[3];
        $performancedata_results[$i] = ($quote[3]*$performancedata[$i][1]) + $performancedata[$i][4];
        $performancedata_results[$i] = $performancedata[$i][6] - $performancedata[$i][5];
        /// you must increase the value of $i...  or the foreach loop will keep overwriting the previous values because $i remains with the value 0
        $i++;
     }

 

Then i want you to var_dump $performancedata_results;

also PM me with IM so i can get better understanding faster

Link to comment
Share on other sites

  • 2 weeks later...
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.