3

I have mysql search results from a keyword search being performed on my site. They're sorted by membership rank (0-3). However, I need to display the ranks differently than each other - like rank 3 gets more prominent formatting than the others.

I was thinking of splitting the rows up into individual arrays. So like array0 would contain all the rows that had the rank of 0, etc. Then loop through these arrays to display the results. I just have NO idea how to do this -- split the array up into smaller arrays.

(For reference, I found this question: splitting a large array into smaller arrays based on the key names but I wasn't really sure if that's what I needed... maybe some clarification on that q would help here?)

For example here is my array:

Array ( 
     [rank]         => 3 
     [organization] => Test Company 
     [imagecompany] => 1636.gif 
     [website]      => http://www.google.com 
     [phone]        => 344-433-3424 
     [fax]          => 
     [address_on_web] => physical   
     [address] => 2342 Test Ave 
     [city] => York 
     [stateprov] => WA 
     [postalcode] => 00000 
     [address_mailing] => 2342 Test Ave 
     [city_mailing] => Seattle 
     [state_mailing] => WA 
     [zip_mailing] => 00000 
     [description] => 'Test test Test test Test test Test test Test 

test Test test Test test Test test Test test Test test Test test Test test Test test 

Test test Test test'
     [customerid] => 1636 ) 
4
  • 1
    for pure array splitting you can use array_chunk(), but I guess you need more of a reorganization than a split. Do a loop and save the results into another array, maybe like Wesley van Opdorp suggested. Commented Jun 17, 2011 at 7:04
  • You don't have to split them up. They are ordered by membership rank, so you simply run one foreach, and if the current rank is different from the previous one, apply different formatting. Commented Jun 17, 2011 at 7:06
  • @bazmegakapa, that was how I had it set up, but some formatting is drastically different. Commented Jun 17, 2011 at 7:18
  • Simply break the loop, and start a new one where the previous one finished. Commented Jun 17, 2011 at 7:28

3 Answers 3

13

You can use the rank as a key to create an multidimensional array like this:

$aRanks = array();
foreach($aArray as $aEntry) {
    $aRanks[$aEntry['rank']][] = $aEntry;
}
echo '<pre>';
print_r($aRanks);
Sign up to request clarification or add additional context in comments.

Comments

3

I have mysql search results from a keyword search

Then sort it using the database/SQL - not PHP. It's faster and uses less code.

2 Comments

Not always. E.g. in my case I need to merge data from two tables. PHP seems to be easier to use
Assuming, for example the OP wants to order by RANK with the 3 values at the top, then in SQL, it means adding 'ORDER BY IF(rank=3, -1, rank)'. About 27 characters, 2 instructions. Or maybe its a simple descending numerical sort with '3' as the top value: 'ORDER BY rank DESC' (19 chars, one instruction with a modifier). I would be very impressed if you can implement this in PHP with less code and/or fewer instructions.
0
$query = mysql_query(); // your query here 
$result = mysql_fetch_array($query);
foreach($result as $row){
  switch($row['rank']){
    case 3: 
    // statement to apply formatting, add to each case
    // example:
    echo "<span style="color:red;">;
    break;
    case 2: break;
    case 1: break;
  }
}

Then output each row, echo closing </span> (or div or whatever) where you want the formatting to end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.