Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-25-2007, 09:04 PM   PM User | #1
JohnDubya
Super Moderator


 
JohnDubya's Avatar
 
Join Date: Nov 2006
Location: Missouri
Posts: 634
Thanks: 12
Thanked 18 Times in 18 Posts
JohnDubya is on a distinguished road
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:

PHP Code:
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:

PHP Code:
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!!

Last edited by JohnDubya; 01-25-2007 at 09:08 PM..
JohnDubya is offline   Reply With Quote
Old 01-25-2007, 09:31 PM   PM User | #2
Brandoe85
teh Moderatorinator


 
Join Date: Sep 2004
Location: USA
Posts: 2,472
Thanks: 4
Thanked 40 Times in 40 Posts
Brandoe85 will become famous soon enough
Does:
$_SESSION['donations_info'] = array();

store a personID as well?
__________________
-Brando
Why using tables for eating is stupid!
Brandoe85 is offline   Reply With Quote
Old 01-25-2007, 09:35 PM   PM User | #3
JohnDubya
Super Moderator


 
JohnDubya's Avatar
 
Join Date: Nov 2006
Location: Missouri
Posts: 634
Thanks: 12
Thanked 18 Times in 18 Posts
JohnDubya is on a distinguished road
Quote:
Originally Posted by Brandoe85 View Post
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:

PHP Code:
while ($y2 mysql_fetch_array($x2)) {
            
$_SESSION['donations_info']['date'][] = $y2['date'];
            
$_SESSION['donations_info']['amount'][] = $y2['amount'];
            
$_SESSION['donations_info']['fund'][] = $y2['fund'];
        } 
JohnDubya is offline   Reply With Quote
Old 01-25-2007, 09:50 PM   PM User | #4
Brandoe85
teh Moderatorinator


 
Join Date: Sep 2004
Location: USA
Posts: 2,472
Thanks: 4
Thanked 40 Times in 40 Posts
Brandoe85 will become famous soon enough
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;
__________________
-Brando
Why using tables for eating is stupid!
Brandoe85 is offline   Reply With Quote
Old 01-25-2007, 10:00 PM   PM User | #5
JohnDubya
Super Moderator


 
JohnDubya's Avatar
 
Join Date: Nov 2006
Location: Missouri
Posts: 634
Thanks: 12
Thanked 18 Times in 18 Posts
JohnDubya is on a distinguished road
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:

PHP Code:
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!
JohnDubya is offline   Reply With Quote
Old 01-25-2007, 10:29 PM   PM User | #6
Brandoe85
teh Moderatorinator


 
Join Date: Sep 2004
Location: USA
Posts: 2,472
Thanks: 4
Thanked 40 Times in 40 Posts
Brandoe85 will become famous soon enough
Hm, maybe set up your array like this:
PHP Code:
$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>";
    }

good luck;
__________________
-Brando
Why using tables for eating is stupid!
Brandoe85 is offline   Reply With Quote
Old 01-25-2007, 10:50 PM   PM User | #7
JohnDubya
Super Moderator


 
JohnDubya's Avatar
 
Join Date: Nov 2006
Location: Missouri
Posts: 634
Thanks: 12
Thanked 18 Times in 18 Posts
JohnDubya is on a distinguished road
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

PHP Code:
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:

PHP Code:
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!

Last edited by JohnDubya; 01-25-2007 at 11:02 PM..
JohnDubya is offline   Reply With Quote
Old 01-25-2007, 10:57 PM   PM User | #8
Brandoe85
teh Moderatorinator


 
Join Date: Sep 2004
Location: USA
Posts: 2,472
Thanks: 4
Thanked 40 Times in 40 Posts
Brandoe85 will become famous soon enough
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.
PHP Code:
    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?
__________________
-Brando
Why using tables for eating is stupid!
Brandoe85 is offline   Reply With Quote
Old 01-25-2007, 11:23 PM   PM User | #9
JohnDubya
Super Moderator


 
JohnDubya's Avatar
 
Join Date: Nov 2006
Location: Missouri
Posts: 634
Thanks: 12
Thanked 18 Times in 18 Posts
JohnDubya is on a distinguished road
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:

PHP Code:
    $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:

PHP Code:
    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!!!!

Last edited by JohnDubya; 01-25-2007 at 11:30 PM..
JohnDubya is offline   Reply With Quote
Old 01-25-2007, 11:36 PM   PM User | #10
Brandoe85
teh Moderatorinator


 
Join Date: Sep 2004
Location: USA
Posts: 2,472
Thanks: 4
Thanked 40 Times in 40 Posts
Brandoe85 will become famous soon enough
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:
PHP Code:
$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++;
        }
        
        
    } 
__________________
-Brando
Why using tables for eating is stupid!
Brandoe85 is offline   Reply With Quote
Old 01-26-2007, 12:33 AM   PM User | #11
JohnDubya
Super Moderator


 
JohnDubya's Avatar
 
Join Date: Nov 2006
Location: Missouri
Posts: 634
Thanks: 12
Thanked 18 Times in 18 Posts
JohnDubya is on a distinguished road
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?
JohnDubya is offline   Reply With Quote
Old 01-26-2007, 02:52 PM   PM User | #12
Brandoe85
teh Moderatorinator


 
Join Date: Sep 2004
Location: USA
Posts: 2,472
Thanks: 4
Thanked 40 Times in 40 Posts
Brandoe85 will become famous soon enough
On your second page, what is the output of:
PHP Code:
echo '<pre>';
print_r($_SESSION['donation_info']);
print_r($_SESSION['person_info']);
echo 
'</pre>'
__________________
-Brando
Why using tables for eating is stupid!
Brandoe85 is offline   Reply With Quote
Old 01-26-2007, 03:16 PM   PM User | #13
JohnDubya
Super Moderator


 
JohnDubya's Avatar
 
Join Date: Nov 2006
Location: Missouri
Posts: 634
Thanks: 12
Thanked 18 Times in 18 Posts
JohnDubya is on a distinguished road
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?

Last edited by JohnDubya; 01-26-2007 at 03:20 PM..
JohnDubya is offline   Reply With Quote
Old 01-26-2007, 03:26 PM   PM User | #14
Brandoe85
teh Moderatorinator


 
Join Date: Sep 2004
Location: USA
Posts: 2,472
Thanks: 4
Thanked 40 Times in 40 Posts
Brandoe85 will become famous soon enough
I think I overwrote your PersonID with $id, remove that on the first page:
PHP Code:
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:
PHP Code:
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;

__________________
-Brando
Why using tables for eating is stupid!
Brandoe85 is offline   Reply With Quote
Old 01-26-2007, 03:37 PM   PM User | #15
JohnDubya
Super Moderator


 
JohnDubya's Avatar
 
Join Date: Nov 2006
Location: Missouri
Posts: 634
Thanks: 12
Thanked 18 Times in 18 Posts
JohnDubya is on a distinguished road
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
JohnDubya is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:09 AM.


Advertisement
Log in to turn off these ads.