Jump to content

[SOLVED] Help with arrays from queries


tomdchi

Recommended Posts

I have two queries where the output is to be put into one array in a certain order.  I am having trouble getting the order right and it including extra data.  There is probably something wrong in the way that I am putting my query together or putting the arrays together.  Can someone help me out with this?  I need the querys below to output the array as it is listed at the bottom.  If I need to be doing this in another way please let me know.

 

 

Thanks!

Tom

 

 

 

Queries:

                    $sql2 = "SELECT 
                    rapid_credithist.transid,
                    rapid_credithist.fundingdate,
                    rapid_credithist.amount
                    FROM rapid_credithist
                    GROUP BY rapid_credithist.transid";
                $result2 = mysqli_query($dbLayer, $sql2) or die(mysqli_error());
                while ($rs2 = mysqli_fetch_array($result2)) {
                        $transid = $rs2['transid'];
           
                    	$sql = "SELECT
			tblclients.id,
		        tblclients.firstname,
			tblclients.lastname,
			tblaccounts.userid,
			tblaccounts.amountin,
			tblaccounts.fundingdate,
			tblaccounts.fundingid				
			FROM tblclients, tblaccounts
			WHERE tblclients.id = tblaccounts.userid
			AND tblaccounts.fundingid = '$transid'";
                        $result = mysqli_query($dbLayer, $sql) or die(mysqli_error());
                        while ($rs = mysqli_fetch_array($result)) {
                        $fundid = $rs['fundingid'];
                        $payments[] = $rs;
                        }

                        $date = $rs2['fundingdate'];
                        $amount = $rs2['amount'];
                        $deposit['date'] = $date; 
                            $deposit['transid'] = $transid; 
                            $deposit['amount'] = $amount; 
                            $deposit['payments'] = $payments; 
                            $deposits[] = $deposit;	
                        }   

 

The above produces the array:

 

Array (
[0] => Array ( [date] => 2009-07-01 [transid] => 6230860059999-13246785 [amount] => 760.77
    [payments] => Array ( [0] => Array ( [0] => 1 [id] => 1 [1] => Tom [firstname] => Tom [2] => Vaughan [lastname] => Vaughan [3] => 1 [userid] => 1 [4] => 107.00 [amountin] => 107.00 [5] => 2009-06-22 [fundingdate] => 2009-06-22 [6] => 6230860059999-13246785 [fundingid] => 6230860059999-13246785 )
                          [1] => Array ( [0] => 4 [id] => 4 [1] => Mike [firstname] => Mike [2] => Sanders [lastname] => Sanders [3] => 4 [userid] => 4 [4] => 225.77 [amountin] => 225.77 [5] => 2009-06-22 [fundingdate] => 2009-06-22 [6] => 6230860059999-13246785 [fundingid] => 6230860059999-13246785 )
                          [2] => Array ( [0] => 1 [id] => 1 [1] => Tom [firstname] => Tom [2] => Vaughan [lastname] => Vaughan [3] => 1 [userid] => 1 [4] => 428.00 [amountin] => 428.00 [5] => 2009-06-22 [fundingdate] => 2009-06-22 [6] => 6230860059999-13246785 [fundingid] => 6230860059999-13246785 ) ) )
[1] => Array ( [date] => 2009-06-26 [transid] => 770875636-13246785 [amount] => 257.00
    [payments] => Array ( [0] => Array ( [0] => 1 [id] => 1 [1] => Tom [firstname] => Tom [2] => Vaughan [lastname] => Vaughan [3] => 1 [userid] => 1 [4] => 107.00 [amountin] => 107.00 [5] => 2009-06-22 [fundingdate] => 2009-06-22 [6] => 6230860059999-13246785 [fundingid] => 6230860059999-13246785 )
                          [1] => Array ( [0] => 4 [id] => 4 [1] => Mike [firstname] => Mike [2] => Sanders [lastname] => Sanders [3] => 4 [userid] => 4 [4] => 225.77 [amountin] => 225.77 [5] => 2009-06-22 [fundingdate] => 2009-06-22 [6] => 6230860059999-13246785 [fundingid] => 6230860059999-13246785 )
                          [2] => Array ( [0] => 1 [id] => 1 [1] => Tom [firstname] => Tom [2] => Vaughan [lastname] => Vaughan [3] => 1 [userid] => 1 [4] => 428.00 [amountin] => 428.00 [5] => 2009-06-22 [fundingdate] => 2009-06-22 [6] => 6230860059999-13246785 [fundingid] => 6230860059999-13246785 )
                          [3] => Array ( [0] => 15 [id] => 15 [1] => Mick [firstname] => Mick [2] => Jagger [lastname] => Jagger [3] => 15 [userid] => 15 [4] => 150.00 [amountin] => 150.00 [5] => 2009-06-26 [fundingdate] => 2009-06-26 [6] => 770875636-13246785 [fundingid] => 770875636-13246785 )
                          [4] => Array ( [0] => 4 [id] => 4 [1] => Mike [firstname] => Mike [2] => Sanders [lastname] => Sanders [3] => 4 [userid] => 4 [4] => 107.00 [amountin] => 107.00 [5] => 2009-06-26 [fundingdate] => 2009-06-26 [6] => 770875636-13246785 [fundingid] => 770875636-13246785 ) ) )
[2] => Array ( [date] => 2009-06-30 [transid] => 852741-13246785 [amount] => 105.85
    [payments] => Array ( [0] => Array ( [0] => 1 [id] => 1 [1] => Tom [firstname] => Tom [2] => Vaughan [lastname] => Vaughan [3] => 1 [userid] => 1 [4] => 107.00 [amountin] => 107.00 [5] => 2009-06-22 [fundingdate] => 2009-06-22 [6] => 6230860059999-13246785 [fundingid] => 6230860059999-13246785 )
                          [1] => Array ( [0] => 4 [id] => 4 [1] => Mike [firstname] => Mike [2] => Sanders [lastname] => Sanders [3] => 4 [userid] => 4 [4] => 225.77 [amountin] => 225.77 [5] => 2009-06-22 [fundingdate] => 2009-06-22 [6] => 6230860059999-13246785 [fundingid] => 6230860059999-13246785 )
                          [2] => Array ( [0] => 1 [id] => 1 [1] => Tom [firstname] => Tom [2] => Vaughan [lastname] => Vaughan [3] => 1 [userid] => 1 [4] => 428.00 [amountin] => 428.00 [5] => 2009-06-22 [fundingdate] => 2009-06-22 [6] => 6230860059999-13246785 [fundingid] => 6230860059999-13246785 )
                          [3] => Array ( [0] => 15 [id] => 15 [1] => Mick [firstname] => Mick [2] => Jagger [lastname] => Jagger [3] => 15 [userid] => 15 [4] => 150.00 [amountin] => 150.00 [5] => 2009-06-26 [fundingdate] => 2009-06-26 [6] => 770875636-13246785 [fundingid] => 770875636-13246785 )
                          [4] => Array ( [0] => 4 [id] => 4 [1] => Mike [firstname] => Mike [2] => Sanders [lastname] => Sanders [3] => 4 [userid] => 4 [4] => 107.00 [amountin] => 107.00 [5] => 2009-06-26 [fundingdate] => 2009-06-26 [6] => 770875636-13246785 [fundingid] => 770875636-13246785 )
                          [5] => Array ( [0] => 17 [id] => 17 [1] => Edward [firstname] => Edward [2] => Van Halen [lastname] => Van Halen [3] => 17 [userid] => 17 [4] => 105.85 [amountin] => 105.85 [5] => 2009-06-30 [fundingdate] => 2009-06-30 [6] => 852741-13246785 [fundingid] => 852741-13246785 ) ) ) )


 

 

 

For it to be displaying as I need it to it should look like:

 


Array (
[0] => Array ( [date] => 2009-07-01 [transid] => 6230860059999-13246785 [amount] => 760.77
    [payments] => Array ( [0] => Array ( [0] => 1 [id] => 1 [1] => Tom [firstname] => Tom [2] => Vaughan [lastname] => Vaughan [3] => 1 [userid] => 1 [4] => 107.00 [amountin] => 107.00 [5] => 2009-06-22 [fundingdate] => 2009-06-22 [6] => 6230860059999-13246785 [fundingid] => 6230860059999-13246785 )
                          [1] => Array ( [0] => 4 [id] => 4 [1] => Mike [firstname] => Mike [2] => Sanders [lastname] => Sanders [3] => 4 [userid] => 4 [4] => 225.77 [amountin] => 225.77 [5] => 2009-06-22 [fundingdate] => 2009-06-22 [6] => 6230860059999-13246785 [fundingid] => 6230860059999-13246785 )
                          [2] => Array ( [0] => 1 [id] => 1 [1] => Tom [firstname] => Tom [2] => Vaughan [lastname] => Vaughan [3] => 1 [userid] => 1 [4] => 428.00 [amountin] => 428.00 [5] => 2009-06-22 [fundingdate] => 2009-06-22 [6] => 6230860059999-13246785 [fundingid] => 6230860059999-13246785 ) ) )
[1] => Array ( [date] => 2009-06-26 [transid] => 770875636-13246785 [amount] => 257.00
    [payments] => Array ( [0] => Array ( [0] => 15 [id] => 15 [1] => Mick [firstname] => Mick [2] => Jagger [lastname] => Jagger [3] => 15 [userid] => 15 [4] => 150.00 [amountin] => 150.00 [5] => 2009-06-26 [fundingdate] => 2009-06-26 [6] => 770875636-13246785 [fundingid] => 770875636-13246785 )
                          [1] => Array ( [0] => 4 [id] => 4 [1] => Mike [firstname] => Mike [2] => Sanders [lastname] => Sanders [3] => 4 [userid] => 4 [4] => 107.00 [amountin] => 107.00 [5] => 2009-06-26 [fundingdate] => 2009-06-26 [6] => 770875636-13246785 [fundingid] => 770875636-13246785 ) ) )
[2] => Array ( [date] => 2009-06-30 [transid] => 852741-13246785 [amount] => 105.85
    [payments] => Array ( [0] => Array ( [0] => 17 [id] => 17 [1] => Edward [firstname] => Edward [2] => Van Halen [lastname] => Van Halen [3] => 17 [userid] => 17 [4] => 105.85 [amountin] => 105.85 [5] => 2009-06-30 [fundingdate] => 2009-06-30 [6] => 852741-13246785 [fundingid] => 852741-13246785 ) ) ) )


Link to comment
Share on other sites

I'll be honest here, that is a lot of data to read through to see what is different. It'd be better to make a smaller batch of example data to show us.

 

One thing I'd change right off the bat: mysqli_fetch_array to mysqli_fetch_assoc. There is no need to create an array with both the numeric keys and the associate keys. That'll get rid of half of your entries right there.

Link to comment
Share on other sites

Thanks for the tip.  I know that this a lot to read over and i hope its not to much.  I always see posts where the op says "I can't get X to work. why not?" and not provide any info for the people on here to help.

 

I changed it over to using assoc and took out the variables that are not important for the array to be in the order it needs to be in.  the output of the query is:

 

Array (
[0] => Array ( [transid] => 6230860059999-13246785
    [payments] => Array ( [0] => Array ( [id] => 1 [userid] => 1 [fundingid] => 6230860059999-13246785 )
                          [1] => Array ( [id] => 4 [userid] => 4 [fundingid] => 6230860059999-13246785 )
                          [2] => Array ( [id] => 1 [userid] => 1 [fundingid] => 6230860059999-13246785 ) ) )
[1] => Array ( [transid] => 770875636-13246785
    [payments] => Array ( [0] => Array ( [id] => 1 [userid] => 1 [fundingid] => 6230860059999-13246785 )
                          [1] => Array ( [id] => 4 [userid] => 4 [fundingid] => 6230860059999-13246785 )
                          [2] => Array ( [id] => 1 [userid] => 1 [fundingid] => 6230860059999-13246785 )
                          [3] => Array ( [id] => 15 [userid] => 15 [fundingid] => 770875636-13246785 )
                          [4] => Array ( [id] => 4 [userid] => 4 [fundingid] => 770875636-13246785 ) ) )
[2] => Array ( [transid] => 852741-13246785
    [payments] => Array ( [0] => Array ( [id] => 1 [userid] => 1 [fundingid] => 6230860059999-13246785 )
                          [1] => Array ( [id] => 4 [userid] => 4 [fundingid] => 6230860059999-13246785 )
                          [2] => Array ( [id] => 1 [userid] => 1 [fundingid] => 6230860059999-13246785 )
                          [3] => Array ( [id] => 15 [userid] => 15 [fundingid] => 770875636-13246785 )
                          [4] => Array ( [id] => 4 [userid] => 4 [fundingid] => 770875636-13246785 )
                          [5] => Array ( [id] => 17 [userid] => 17 [fundingid] => 852741-13246785 ) ) ) )

 

transid should match with fundingid.  It does so correctly in the first part but not after.

 

It should look like:

 

 


Array (
[0] => Array ( [transid] => 6230860059999-13246785
    [payments] => Array ( [0] => Array ( [id] => 1 [userid] => 1 [fundingid] => 6230860059999-13246785 )
                          [1] => Array ( [id] => 4 [userid] => 4 [fundingid] => 6230860059999-13246785 )
                          [2] => Array ( [id] => 1 [userid] => 1 [fundingid] => 6230860059999-13246785 ) ) )
[1] => Array ( [transid] => 770875636-13246785
    [payments] => Array ( [0] => Array ( [id] => 15 [userid] => 15 [fundingid] => 770875636-13246785 )
                          [1] => Array ( [id] => 4 [userid] => 4 [fundingid] => 770875636-13246785 ) ) )
[2] => Array ( [transid] => 852741-13246785
    [payments] => Array ( [0] => Array ( [id] => 17 [userid] => 17 [fundingid] => 852741-13246785 ) ) ) )

 

 

Link to comment
Share on other sites

change

	                        $result = mysqli_query($dbLayer, $sql) or die(mysqli_error());
                        while ($rs = mysqli_fetch_array($result)) {
                        $fundid = $rs['fundingid'];
                        $payments[] = $rs;
                        }

to

	                        $result = mysqli_query($dbLayer, $sql) or die(mysqli_error());
                        $payments = array(); //add this line
                                while ($rs = mysqli_fetch_array($result)) {
                        $fundid = $rs['fundingid'];
                        $payments[] = $rs;
                        }

Link to comment
Share on other sites

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.