...

View Full Version : Help associating multiple array values with one array



JohnDubya
01-25-2007, 10:04 PM
I'm having issues with messing around with array's and foreach's. Here's my situation:

I'm getting a $PersonID (something like 42426 or whatever), and with that $PersonID, I am doing a query to find that one person's contact information. That info is put into an array


$_SESSION['person_info'] = array();

So then, that session is filled with the name, phone number, etc. as $_SESSION['person_info']['name'] , $_SESSION['person_info']['phone_number'] , and the rest.

But now, I'm needing to grab all the donations that person has made in the last year. I've written the code to grab all the donations and put them into another array:


$_SESSION['donations_info'] = array();

On the next page, the code puts this within a function to print out the person's info:


echo ($_SESSION['person_info']['name'][$counter].
$_SESSION['person_info']['phone_number'][$counter]);

The $counter is specified by something inside of a script called FPDF, which transfers PHP code into pdf format. I think the counter is calling the number of the entry into the array...not sure how that works yet. So now, I'm trying to figure out how to display the multiple (or single) donations that each separate person made. I chose two people who had each given two donations, and I've got it to print out all the donations that the persons have made by using a foreach:


foreach ($_SESSION['donations_info']['date'] as $date) {
$c = 10;
$this->SetXY(5,$b);
$this->SetFont('Arial','',9);
$i = 0;
$this->Cell(80,8, $date);
$this->Ln();
$b = $b + $c;
$p++;
}

...but I can't figure out how to specify which donations are for which person. Do I need to set up another key for each value in an array or what? My brain is about to explode over this problem. Anything that can help me understand this situation is greatly, muchly appreciated!!

Brandoe85
01-25-2007, 10:31 PM
Does:
$_SESSION['donations_info'] = array();

store a personID as well?

JohnDubya
01-25-2007, 10:35 PM
Does:
$_SESSION['donations_info'] = array();

store a personID as well?

It doesn't right now, but it could. Here's what I'm putting into the ['donations_info'] array right now:


while ($y2 = mysql_fetch_array($x2)) {
$_SESSION['donations_info']['date'][] = $y2['date'];
$_SESSION['donations_info']['amount'][] = $y2['amount'];
$_SESSION['donations_info']['fund'][] = $y2['fund'];
}

Brandoe85
01-25-2007, 10:50 PM
Ok, well quick solution I think of is, if you store it in that array as well, you can do a check to see if the persons are the same (psuedo code):
if(person_info_personID == donation_info_personID)
{
// match
}

good luck;

JohnDubya
01-25-2007, 11:00 PM
Gotcha, gotcha...we're getting there.

Ok, so how do I call up all of the ['donations_info'] entries with a foreach()? I can't do a:


foreach ($_SESSION['donations_info']['PersonID'] as $PersonID,$_SESSION['donations_info']['date'] as $date) {
echo "$PersonID - $date";
}

So how do I bring up all those values to be able to print them and do the if statement?

Thanks so much for helping me understand this process!

Brandoe85
01-25-2007, 11:29 PM
Hm, maybe set up your array like this:


$donationCount = 0;
while ($y2 = mysql_fetch_array($x2))
{
$_SESSION['donations_info'][$donationCount]['date'] = $y2['date'];
$_SESSION['donations_info'][$donationCount]['amount'] = $y2['amount'];
$_SESSION['donations_info'][$donationCount]['fund'] = $y2['fund'];
$_SESSION['donations_info'][$donationCount]['personID'] = $y2['personID']; // person id from query, not sure of your field name
$donationCount++;
}
And then loop like:


// loop through and find match
foreach($_SESSION['donation_info'] as $value => $args)
{
if($_SESSION['person_info']['personID'] == $args['personID'])
{
echo $args['personID'] . ' - ' . $args['date'] . "<BR>";
}
}


good luck;

JohnDubya
01-25-2007, 11:50 PM
Ok, I tried that re-written array, and it's a blank echo.

Let me give this as well, to hopefully shed more light. This is how the ['person_info'] is being put into the array. I know the [] at the end does something, but I'm not completely sure what. Can you tell I haven't done much with arrays yet? lol


while($y = @mysql_fetch_array($x)){
$_SESSION['person_info']['id'][] = $y['PersonID'];
$_SESSION['person_info']['first'][] = $y['FirstName'];
$_SESSION['person_info']['last'][] = $y['LastName'];
$_SESSION['person_info']['street_one'][] = $y['Street'];
$_SESSION['person_info']['street_two'][] = $y['Street2'];
$_SESSION['person_info']['city'][] = $y['City'];
$_SESSION['person_info']['state'][] = $y['State_Abbr'];
$_SESSION['person_info']['zipcode'][] = $y['Zipcode'];
}

I just found this at the bottom of the file:


for($i=0;$i<count($_SESSION['person_info']['id']);$i++){
$total = "$".number_format($_SESSION['person_info']['total_amount'][$i],2);
$pdf->MakeReceipt($total,$i);
}

So that $counter variable must be from the $i mentioned here. So with this code here, does that help you understand my situation? Would it help if I gave you all the code to look at? My mind is getting to the point where I feel like I'm looking at jargon when I glance through the code. lol Thanks for your continued help!

Brandoe85
01-25-2007, 11:57 PM
I think were ok, just mixed up a few field names. I used:
$_SESSION['person_info']['personID'], but is it just:
$_SESSION['person_info']['id'] ?

and actually, probably need the $counter on the end (or whatever variable you have):
$_SESSION['person_info']['personID'][$counter]

The [$counter] part would have to be added in the if statement as well.


if($_SESSION['person_info']['personID'][$counter] == $args['personID'])
{
echo $args['personID'] . ' - ' . $args['date'] . "<BR>";
}

And when you set it in the while loop, did you change it to the right field coming from your table?
$_SESSION['donations_info'][$donationCount]['personID'] = $y2['personID'];

Is the $y2['personID'] the correct field?

JohnDubya
01-26-2007, 12:23 AM
I'm just gonna show all the code...I don't think I'm giving you some info you need to help me. I'm not very good at explaining everything...because most of the time, I barely understand it myself! lol But thanks for stickin with me. My due date is tomorrow, so I'm trying to crank this out!

Ok, so on the first page, it gets the year that the user wants to see the donations of the persons he chose. Also, it gets the PersonID's of however many people the user has selected. Let's say he has chosen two people, so the script pulls those two PersonID's from the array (as $id) and works with them in the queries:


$year = $_POST['year'];

$_SESSION['person_info'] = array();
$_SESSION['donations_info'] = array();

foreach($_SESSION['year_end_receipt_ids'] as $id){


//////////////////////
// Get Address info //
//////////////////////

$x = @mysql_query("SELECT * FROM Person AS p, Address AS a WHERE p.PersonID = '$id'
AND a.person_id = '$id' AND address_type = 'permanent'");


/////////////////////////////////
// Get itemized donations info //
/////////////////////////////////

$x2 = mysql_query("SELECT cd.amount AS amount, cd.fund_id, cdf.id,
DATE_FORMAT(cd.gift_date,'%M %D, %Y') AS date,
cdf.name AS fund,
cd.donor_id
FROM chapter_donations AS cd,
chapter_donor_funds AS cdf
WHERE cd.donor_id = '$id' AND cd.chapter_id = '$SelectedChapterID'
AND cdf.chapter_id = '$SelectedChapterID' AND cd.fund_id = cdf.id
AND cd.gift_date >= '$year-01-01' AND cd.gift_date <= '$year-12-31'
ORDER BY cd.gift_date");


if(!$x || !$x2){
die(mysql_error());
}

while($y = @mysql_fetch_array($x)){
$_SESSION['person_info']['id'][] = $y['PersonID'];
$_SESSION['person_info']['first'][] = $y['FirstName'];
$_SESSION['person_info']['last'][] = $y['LastName'];
$_SESSION['person_info']['street_one'][] = $y['Street'];
$_SESSION['person_info']['street_two'][] = $y['Street2'];
$_SESSION['person_info']['city'][] = $y['City'];
$_SESSION['person_info']['state'][] = $y['State_Abbr'];
$_SESSION['person_info']['zipcode'][] = $y['Zipcode'];
}

$donationCount = 0;

while ($y2 = mysql_fetch_array($x2)) {
$_SESSION['donations_info'][$donationCount]['PersonID'][] = $y2['donor_id'];
$_SESSION['donations_info'][$donationCount]['date'][] = $y2['date'];
$_SESSION['donations_info'][$donationCount]['amount'][] = $y2['amount'];
$_SESSION['donations_info'][$donationCount]['fund'][] = $y2['fund'];
}

$donationCount++;
}

Then, on the second page (the FPDF page), it takes those session entries to insert the information into the document. It uses each separate ['person_info'] on one page each. In other words, for the first $id's information, it prints that on one PDF page, prints some other stuff it grabbed, then starts another page and uses the next $id's information.

So what I'm doing is trying to insert each $id's donations (could be one or multiple) into a section below the person's information. Here is what the code in the FPDF file looks like:


function DonationDetails($total,$counter) {

$this->SetXY(5,50);
$this->SetFont('Arial','',12);
$this->Cell(100,5,$_SESSION['person_info']['first'][$counter].
' '.$_SESSION['person_info']['last'][$counter]);
$this->Ln();

$this->SetXY(5,55);
$this->Cell(100,5,$_SESSION['person_info']['street_one'][$counter]);
$this->Ln();

$this->SetXY(5,60);
if($_SESSION['person_info']['street_two'][$counter] != ""){
$this->Cell(100,5,$_SESSION['person_info']['street_two'][$counter]);
$this->Ln();
$this->SetXY(5,65);
$this->Cell(100,5,$_SESSION['person_info']['city'][$counter].
', '.$_SESSION['person_info']['state'][$counter].
' '.$_SESSION['person_info']['zipcode'][$counter]);
$this->Ln();
} else {
$this->SetXY(5,60);
$this->Cell(100,5,$_SESSION['person_info']['city'][$counter].
', '.$_SESSION['person_info']['state'][$counter].
' '.$_SESSION['person_info']['zipcode'][$counter]);
$this->Ln();
}

$b = 125;

foreach($_SESSION['donation_info'] as $value => $args) {
if($_SESSION['person_info']['id'][$counter] != $args['PersonID']) {
$args['date'] = NULL;
}
$c = 5;
$this->SetXY(5,$b);
$this->SetFont('Arial','',9);
$i = 0;
$this->Cell(80,8, $args['date'].);
$this->Ln();
$b = $b + $c;
}

function MakeReceipt($total,$counter){
$this->AddPage();
$this->Top();
$this->DonationDetails($total,$counter);
$this->BodyContent($counter);
}

for($i=0;$i<count($_SESSION['person_info']['id']);$i++){
$total = "$".number_format($_SESSION['person_info']['total_amount'][$i],2);
$pdf->MakeReceipt($total,$i);
}

That's the main parts of the code that I'm trying to figure out how they all work together. If you need anything else from me, let me know. Ahhhhhhhhh, I'm going insane!!!! :eek:

Brandoe85
01-26-2007, 12:36 AM
Ok, I see some things but to get it going...decalare $donationCounter outside your foreacah and put the $donationCounter++ back into the while loop, take off the [] as we already formated the array, only the first page should need to be changed, try this for it:


$year = $_POST['year'];
$donationCount = 0;
$_SESSION['person_info'] = array();
$_SESSION['donations_info'] = array();

foreach($_SESSION['year_end_receipt_ids'] as $id){


//////////////////////
// Get Address info //
//////////////////////

$x = @mysql_query("SELECT * FROM Person AS p, Address AS a WHERE p.PersonID = '$id'
AND a.person_id = '$id' AND address_type = 'permanent'");


/////////////////////////////////
// Get itemized donations info //
/////////////////////////////////

$x2 = mysql_query("SELECT cd.amount AS amount, cd.fund_id, cdf.id,
DATE_FORMAT(cd.gift_date,'%M %D, %Y') AS date,
cdf.name AS fund,
cd.donor_id
FROM chapter_donations AS cd,
chapter_donor_funds AS cdf
WHERE cd.donor_id = '$id' AND cd.chapter_id = '$SelectedChapterID'
AND cdf.chapter_id = '$SelectedChapterID' AND cd.fund_id = cdf.id
AND cd.gift_date >= '$year-01-01' AND cd.gift_date <= '$year-12-31'
ORDER BY cd.gift_date");


if(!$x || !$x2){
die(mysql_error());
}

while($y = @mysql_fetch_array($x)){
$_SESSION['person_info']['id'][] = $y['PersonID'];
$_SESSION['person_info']['first'][] = $y['FirstName'];
$_SESSION['person_info']['last'][] = $y['LastName'];
$_SESSION['person_info']['street_one'][] = $y['Street'];
$_SESSION['person_info']['street_two'][] = $y['Street2'];
$_SESSION['person_info']['city'][] = $y['City'];
$_SESSION['person_info']['state'][] = $y['State_Abbr'];
$_SESSION['person_info']['zipcode'][] = $y['Zipcode'];
}



while ($y2 = mysql_fetch_array($x2)) {
$_SESSION['donations_info'][$donationCount]['PersonID'] = $y2['donor_id'];
$_SESSION['donations_info'][$donationCount]['date'] = $y2['date'];
$_SESSION['donations_info'][$donationCount]['amount'] = $y2['amount'];
$_SESSION['donations_info'][$donationCount]['fund'] = $y2['fund'];
$_SESSION['donations_info'][$donationCount]['PersonID'] = $id;
$donationCount++;
}


}

JohnDubya
01-26-2007, 01:33 AM
Ok, I did that. It didn't give any errors, but it isn't printing anything out when I use $args['date'] or whatever. Should it be yet?

Brandoe85
01-26-2007, 03:52 PM
On your second page, what is the output of:


echo '<pre>';
print_r($_SESSION['donation_info']);
print_r($_SESSION['person_info']);
echo '</pre>';

JohnDubya
01-26-2007, 04:16 PM
Well, because it's this FPDF thing, I guess it's object-oriented or something, so I can't run just normal PHP code, but I did get this to print out (with other errors):

Array ( [0] => Array ( [PersonID] => 45621 [date] => February 23rd, 2006 [amount] => 1000.00 [fund] => General Operating )
[1] => Array ( [PersonID] => 45621 [date] => December 22nd, 2006 [amount] => 1000.00 [fund] => General Operating )
[2] => Array ( [PersonID] => 45659 [date] => April 1st, 2006 [amount] => 300.00 [fund] => General Operating )
[3] => Array ( [PersonID] => 45659 [date] => December 11th, 2006 [amount] => 5000.00 [fund] => General Operating ) )

Array ( [id] => Array ( [0] => 45621 [1] => 45659 ) [first] => Array ( [0] => Beth & David [1] => Shirley & Jim ) [last] => Array ( [0] => Coffman [1] => Francis ) [street_one] => Array ( [0] => P.O. Box 47000 [1] => 4284 McGirts Boulevard ) [street_two] => Array ( [0] => [1] => ) [city] => Array ( [0] => Jacksonville [1] => Jacksonville ) [state] => Array ( [0] => FL [1] => FL ) [zipcode] => Array ( [0] => 32247 [1] => 32210 ) [total_amount] => Array ( [0] => 2000 [1] => 5300 ) ) Array ( [0] => Array ( [PersonID] => 45621 [date] => February 23rd, 2006 [amount] => 1000.00 [fund] => General Operating ) [1] => Array ( [PersonID] => 45621 [date] => December 22nd, 2006 [amount] => 1000.00 [fund] => General Operating ) [2] => Array ( [PersonID] => 45659 [date] => April 1st, 2006 [amount] => 300.00 [fund] => General Operating ) [3] => Array ( [PersonID] => 45659 [date] => December 11th, 2006 [amount] => 5000.00 [fund] => General Operating ) ) Array ( [id] => Array ( [0] => 45621 [1] => 45659 ) [first] => Array ( [0] => Beth & David [1] => Shirley & Jim ) [last] => Array ( [0] => Coffman [1] => Francis ) [street_one] => Array ( [0] => P.O. Box 47000 [1] => 4284 McGirts Boulevard ) [street_two] => Array ( [0] => [1] => ) [city] => Array ( [0] => Jacksonville [1] => Jacksonville ) [state] => Array ( [0] => FL [1] => FL ) [zipcode] => Array ( [0] => 32247 [1] => 32210 ) [total_amount] => Array ( [0] => 2000 [1] => 5300 ) )

So the first one is the one I did. Looks completely sensible. The second one is the person_info array, and it just about made my head explode when I looked at it. lol What the crap is up with that?

Brandoe85
01-26-2007, 04:26 PM
I think I overwrote your PersonID with $id, remove that on the first page:


while ($y2 = mysql_fetch_array($x2)) {
$_SESSION['donations_info'][$donationCount]['PersonID'] = $y2['donor_id'];
$_SESSION['donations_info'][$donationCount]['date'] = $y2['date'];
$_SESSION['donations_info'][$donationCount]['amount'] = $y2['amount'];
$_SESSION['donations_info'][$donationCount]['fund'] = $y2['fund'];
$donationCount++;
}


On the second page, in your foreach..print out both values just so we can see:


foreach($_SESSION['donation_info'] as $value => $args) {

echo 'Person info = ' . $_SESSION['person_info']['id'][$counter] . ' donation info = ' . $args['PersonID'] . ', ' . $args['date'] . "<BR>";
if($_SESSION['person_info']['id'][$counter] != $args['PersonID']) {
$args['date'] = NULL;
}
$c = 5;
$this->SetXY(5,$b);
$this->SetFont('Arial','',9);
$i = 0;
$this->Cell(80,8, $args['date'].);
$this->Ln();
$b = $b + $c;
}

JohnDubya
01-26-2007, 04:37 PM
With that code, this is what I get:

Person info = 45621 donation info = K, K
Person info = 45621 donation info = 2, 2
Person info = 45659 donation info = K, K
Person info = 45659 donation info = 2, 2

Brandoe85
01-26-2007, 04:47 PM
The person info you posted looks ok, just has an extra array because of the $counter. But the donation info you posted is really off, K and 2?

JohnDubya
01-26-2007, 05:02 PM
FYI, I'm not opposed to changing how the SESSION arrays are written. If there's any easier way to get all the values into the PDF, I'm completely ok with that. It looks like the guy who wrote this script threw that array together kinda halphazardly. Or maybe that's how it has to be, I don't know. I just wish I could figure this out!

JohnDubya
01-26-2007, 11:42 PM
Just wanted to give a huge thank you to Brando for his time and patience in helping! He was able to help get my script up and running in time for my deadline! Thanks so much, Brando!!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum