Jump to content

Sorting a script


civic

Recommended Posts

I have a problem. My script outputs what I need but I want it to sort by DESC active recruits. Does anyone know how or where to put that code in? Thank you!

 

	function recruiters() {		

	global $skin, $members, $db;

	$users = $members->fetch_rows();
	$data = '';

	foreach($users as $user) {

		$total_recruits = $db->result($db->query('SELECT COUNT(*) FROM `members` WHERE `recruiter` = "' . $user['id'] . '"'));
		$active_recruits = $db->result($db->query('SELECT COUNT(*) FROM `members` WHERE `recruiter` = "' . $user['id'] . '" AND `status` <> (`status` | 64 )'));

		$ratio = $active_recruits / $total_recruits;

		if($total_recruits > 0 and $ratio > 0) {

			$total_recruits = number_format($total_recruits, 0, '.', ',') . '%';
			$active_recruits = number_format($active_recruits, 0, '.', ',');
			$ratio = number_format($ratio * 100, 2, '.', ',');

			$data .= '<tr><td class="normal">' . $members->link($user['id']) . '</td><td class="normal">' . $total_recruits . '</td><td class="normal">' . $active_recruits . '</td><td class="normal">' . $ratio . '</td></tr>';

		}
  
	}

	return $skin->form_start('Top Recruiters') .
	'<table style="width:400px"><tr><td class="normal"><b>Member</b></td><td class="normal"><b>Total Recruits</b></td><td class="normal">Active Recruits</td><td class="normal">Percentage Active</td></tr>'
	. $data		
	. '</table>' . $skin->form_end();

}

}

Link to comment
Share on other sites

Try it in both queries

 

<?php
function recruiters() {		

	global $skin, $members, $db;

	$users = $members->fetch_rows();
	$data = '';

	foreach($users as $user) {

		$total_recruits = $db->result($db->query('SELECT COUNT(*) FROM `members` WHERE `recruiter` = "' . $user['id'] . '" ORDER BY `recruiter` DESC'));
		$active_recruits = $db->result($db->query('SELECT COUNT(*) FROM `members` WHERE `recruiter` = "' . $user['id'] . '" AND `status` <> (`status` | 64 ) ORDER BY `recruiter` DESC') );

		$ratio = $active_recruits / $total_recruits;

		if($total_recruits > 0 and $ratio > 0) {

			$total_recruits = number_format($total_recruits, 0, '.', ',') . '%';
			$active_recruits = number_format($active_recruits, 0, '.', ',');
			$ratio = number_format($ratio * 100, 2, '.', ',');

			$data .= '<tr><td class="normal">' . $members->link($user['id']) . '</td><td class="normal">' . $total_recruits . '</td><td class="normal">' . $active_recruits . '</td><td class="normal">' . $ratio . '</td></tr>';

		}
  
	}

	return $skin->form_start('Top Recruiters') .
	'<table style="width:400px"><tr><td class="normal"><b>Member</b></td><td class="normal"><b>Total Recruits</b></td><td class="normal">Active Recruits</td><td class="normal">Percentage Active</td></tr>'
	. $data		
	. '</table>' . $skin->form_end();

}

}
?>

Link to comment
Share on other sites

Yes there are changes, P2grace added DESC at the end of the queries.  Try what he suggested, it should work.

 

I doubt it.

Each of those queries returns a single row with a single value.

[pre]

+---------------+          +---------------+

| COUNT(*)  ASC |          | COUNT(*) DESC |

+---------------+          +---------------+

|      50      |          |      50      |

+---------------+          +---------------+[/pre]

 

You would need a single query with a join with the member table for the ORDER BY to make any difference

 

eg (assumes your $members query, not shown, pulled rows from same member table)

 

SELECT a.name,
COUNT(b.recruiter) as total_recruits,
SUM (IF(b.`status` <> (b.`status` | 64 ), 1, 0)) as active_recruits
FROM members a
LEFT JOIN members b ON a.id = b.recruiter
ORDER BY active_recruits DESC

Link to comment
Share on other sites

I have a problem. My script outputs what I need but I want it to sort by DESC active recruits. Does anyone know how or where to put that code in? Thank you!

 

 

This is his first post, he says it outputs what he needs, he just wants it in DESC order.  Which is what P2grace did.

Link to comment
Share on other sites

P2grace added "ORDER BY DESC" to a queries returning a single value. Where's the point in that? You can see the difference it makes with the two result sets below

 

Each of those queries returns a single row with a single value.

 

[pre]

+---------------+          +---------------+

| COUNT(*)  ASC |          | COUNT(*) DESC |

+---------------+          +---------------+

|      50      |          |      50      |

+---------------+          +---------------+[/pre]

Link to comment
Share on other sites

  • 2 weeks later...

Forgot the GROUP BY  bit

SELECT a.name,
COUNT(b.recruiter) as total_recruits,
SUM (IF(b.`status` <> (b.`status` | 64 ), 1, 0)) as active_recruits
FROM members a
LEFT JOIN members b ON a.id = b.recruiter
GROUP BY a.name
ORDER BY active_recruits DESC

 

which will give a row for each member with their total_recruits and total_active recruits, sorted by by descending number of active recruits, so it should replace your foreach loop and the two queries inside it.

Link to comment
Share on other sites

K well when I put that in I put it in as:

 

function recruiters() {		

	global $skin, $members, $db;



	$users = $members->fetch_rows();
	$data = '';
$db->query('SELECT a.name,
COUNT(b.recruiter) as total_recruits,
SUM (IF(b.`status` <> (b.`status` | 64 ), 1, 0)) as active_recruits
FROM members a
LEFT JOIN members b ON a.id = b.recruiter
GROUP BY a.name
ORDER BY active_recruits DESC');

		$ratio = $active_recruits / $total_recruits;

		if($total_recruits > 0 and $ratio > 0) {

			$total_recruits = number_format($total_recruits, 0, '.', ',') . '%';
			$active_recruits = number_format($active_recruits, 0, '.', ',');
			$ratio = number_format($ratio * 100, 2, '.', ',');

			$data .= '<tr><td class="normal">' . $members->link($user['id']) . '</td><td class="normal">' . $total_recruits . '</td><td class="normal">' . $active_recruits . '</td><td class="normal">' . $ratio . '</td></tr>';


  
	}

	return $skin->form_start('Top Recruiters') .
	'<table style="width:400px"><tr><td class="normal"><b>Member</b></td><td class="normal"><b>Total Recruits</b></td><td class="normal">Active Recruits</td><td class="normal">Percentage Active</td></tr>'
	. $data		
	. '</table>' . $skin->form_end();

}

}

 

But now it won't pull anything, it is just blank with the table, is there anything I have to replace for you a.name and b.recruiter etc...? where did you get that from?

Link to comment
Share on other sites

What table aliases? In the script it is pulling info from

$total_recruits = $db->result($db->query('SELECT COUNT(*) FROM `members` WHERE `recruiter` = "' . $user['id'] . '" ORDER BY `recruiter` DESC'));
		$active_recruits = $db->result($db->query('SELECT COUNT(*) FROM `members` WHERE `recruiter` = "' . $user['id'] . '" AND `status` <> (`status` | 64 ) ORDER BY `recruiter` DESC') );

 

Also in the code it looks for the $active_recruits and $total_recruits which is not in your query...

Link to comment
Share on other sites

I apologise if the concept is too advanced, but that is a query that cannot be done without table aliases since there is one physical members table that needs to be treated as two logical tables.

 

Also in the code it looks for the $active_recruits and $total_recruits which is not in your query...
They are if you bother to look
Link to comment
Share on other sites

I posted here because I'm not that great at PHP. So YES it is complicated. It seems as if you threw a query up there and said hey! you need array's too. Well, what are arrays? What do I put in as a and b? Not everyone is PHP experts. Thanks!!!

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.