Help associating multiple array values with one array
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
PHP Code:
$_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:
PHP Code:
$_SESSION['donations_info'] = array();
On the next page, the code puts this within a function to print out the person's info:
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:
...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!!
Last edited by JohnDubya; 01-25-2007 at 09:08 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
}
$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:
PHP Code:
// 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>"; } }
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
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!
Last edited by JohnDubya; 01-25-2007 at 11:02 PM..
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'];
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:
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");
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:
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!!!!
Last edited by JohnDubya; 01-25-2007 at 11:30 PM..
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:
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");
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):
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?
Last edited by JohnDubya; 01-26-2007 at 03:20 PM..
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