MySQL Sort by every other row?
Okay so I’ve run into a road block here and I need some help..
Thanks!
I have a table with a name and sex field.
I need sort this table by sex but it needs to be every other row.
In other words..
M
F
M
F
etc..
I’ve been trying to think of a PHP solution for it but what I came up with didn’t work quite right..
Does anyone have any suggestions on how I may accomplish this in PHP or SQL?
Observing members:
0
Composing members:
0
9 Answers
Couldn’t you just grab the males in a query and do the same for the females. Then loop through them in PHP.
If that made any sense.
I’ve done a query and through the males in an array and the females in their own..
Say we have 3 males and 1 Female I need to spit it out as
M
F
M
BLANK
M
Any suggestions on how to do that in PHP?
I hope that made sense…
Couldn’t you use isset and echo something like <br /> if it returns false.
Here’s the perl answer. Been a while since I did PHP. I assume you can convert…
my @males = get_males_from_db();
my @females = get_females_from_db();
my $num_males = @males;
my $num_females = @females;
for my $i (0..max($num_males, $num_females) – 1) {
print $males[$i] || ‘BLANK’;
print $females[$i] || ‘BLANK’;
}
@mritty I actually just came up with a solution which is essentially the same thing. Heres the code I came up with:
http://pastebin.com/f223d79f3
I’m sure there’s a simpler way to check the array keys but I’m too tired to think right now :)
Thanks for all your help!
Looks like you answered your own question… Here is my pathetic pseudo code;
This is psuedo code for one way to do what you want to do;
mquery = males results
fquery = females results
if fquery number of results is greater than mquery number of results {
. . . run function large_result(fquery)
} elseif mquery is greater than fquery {
. . . run function large_result(mquery)
} elseif mquery = fquery results {
. . . run function simpleloop
}
function large_result {
. . . for i <= large number results {
. . . . . . add2line .= larger[i]
. . . . . . if large increment i <= max of the smaller {
. . . . . . . . . for k <= smaller number results {
. . . . . . . . . . . . add2line .= smaller[k]
. . . . . . . . . }
. . . . . . } else {
. . . . . . . . . add2line .= blank
. . . . . . }
. . . }
}
funciton simpleloop {
. . . for i <= result (doesn’t matter which one) {
. . . . . . add2line .= mquery[i]
. . . . . . add2line .= fquery[i]
. . . }
}
echo add2line
pretty sad but it should produce what you’re looking for…
you can also look into a PHP Modulus function, i had one in a project a while back to sort a table with alternating colors, and im sure you can set it up to sort categories. i would need to search my project files for it, but if i find it ill post it.
@wenn Normally I would use the modulus but this was a special case in which it would not work.
I think you’re design is flawed for what you want to do. What you want to do is create two separate queries/cursors, one for all M and one for all F. Then in your processing loop, fetch one from the M cursor and one from the F cursor.
You’ll need to plan for what will happen if you come to the end of the F or M cursor before the end of the other cursor.
Answer this question
This question is in the General Section. Responses must be helpful and on-topic.