Jump to content

Combining 2 arrays and sorting them and outputting


beanymanuk

Recommended Posts

Hi everyone

 

I have 2 queries which are pulling out results into arrays

I think they are right

I then insert a new column into each array one with [typeofdonation] as "cash" and one as "card" this is to be used later when outputting the results

 

I then need to combine the 2 arrays into 1 array and then sort the combined array by date showing the latest ones first

I then need to output the top 3 results

The way the results are outputted is dependant on what [typeofdonation] is as they are outputting and styled differently

 

Here is my code below I have got upto the combining the 2 arrays but am having difficulty doing this

Once I have done this I then need to sort the results but I think my code I have tested on each array works ok and so will work once I have the combined array.

Final part I am not sure on how to find and use what is in [typeofdonation] to determine the styling and way each of the 3 results is outputted

 

Thankyou in advance for everyone who can hopefully help me in the right direction

Peter

 

<b>Card Donations</b><br><br>
	<?php 
	$getcarddonations =  $wpdb->get_results("SELECT wp_supporters_donations.SFMemberNumber, wp_supporters_donations.KickbackAmount, wp_supporters_donations.MerchantName, wp_supporters_donations.SpendDate from supporter_2_cause RIGHT JOIN wp_supporters_donations ON supporter_2_cause.supp_id=wp_supporters_donations.SFMemberNumber WHERE supporter_2_cause.caus_id = '54' ORDER BY wp_supporters_donations.SpendDate");
	//UNION SELECT id, amout, cause, date FROM cashdepo WHERE cause='Lisas charity' AND status='2' ORDER BY id DESC
	foreach($getcarddonations as $getcarddonations){
		$i = 0;
		while($i < 100) {
		$data[$i]['member'] = $getcarddonations->SFMemberNumber;
		$data[$i]['ammount'] = $getcarddonations->KickbackAmount;
		$data[$i]['merchant'] = $getcarddonations->MerchantName;
		$data[$i]['date'] = $getcarddonations->SpendDate;
		$data[$i]['typeofdonation'] = 'card';
		$i++;
		}
	}	

	function compareItems($a, $b)
	{
		if ( $a->date < $b->date ) return -1;
		if ( $a->date > $b->date ) return 1;
		return 0; // equality
	}

	uasort($data, "compareItems");
	print_r( $data );
	?>
	<br><br><br><b>Cash Donations</b><br><br>
	<?php
	$getcashdonationsdb = $wpdb->get_results("SELECT * FROM cashdepo WHERE cause='Lisas charity' AND status='2' ORDER BY id DESC");
	foreach($getcashdonationsdb as $getcashdonationsdb){
		$i = 0;
		while($i < 11) {
		$dataa[$i]['member'] = $getcashdonationsdb->id;
		$dataa[$i]['ammount'] = $getcashdonationsdb->amout;
		$dataa[$i]['merchant'] = $getcashdonationsdb->cause;
		$dataa[$i]['date'] = $getcashdonationsdb->date;
		$dataa[$i]['typeofdonation'] = 'cash';
		$i++;
		}
	}

	uasort($dataa, "compareItems");
	print_r( $dataa );

	?>
	<br><br><br>
	<b>Combined</b><br><br>
	<?php 
	$datar1 = ($data);
	$datar2 = ($dataa);
	$finalarray = array_combine($datar1, $datar2);

	//sort
	uasort($finalarray, "date");

	//Print the output
	print_r($finalarray);
	?>

Link to comment
Share on other sites

You don't need those loops if $wpdb->get_results() returns an array.  Just use aliases in your query and your array will look the way you want it.  You can also add the card or cash like so:

 

SELECT 'card' AS typeofdonation, wp_supporters_donations.SFMemberNumber AS member . . .

 

Then just array_merge() the two and run a sort().  Actually, you might be able to use a UNION to combine both queries.

Link to comment
Share on other sites

array_merge for combining the two. What are you trying to do for the output?

 

I am trying to sort my results by date to show the latest 3

I have now combined all the results but I am not getting the latest 3 results outputted so I think I am sorting my array incorrectly.

Any ideas how I sort by ['date'] so the latest 3 show?

 

This is code I am using to sort

 

uasort($data, "compareItems");

function compareItems($a, $b)
	{
		if ( $a->date < $b->date ) return -1;
		if ( $a->date > $b->date ) return 1;
		return 0; // equality
	}

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.