0

I have a table on a php page where "Offical_gene_Symbol" column I need to sort "Assending". here is the php block for that particular column.

if($test['test_type_id'] == 1 || $test['test_type_id'] == 5) {
    $gene_ids = FetchGenesByPanelTest($test['id']);
    $genes = array();
    foreach($gene_ids  as $gi){
        $genes[] = FetchGene($gi);      
    }
    <table>
                <tr>
                    <th>Offical Gene Symbol</th>
                    <th style="width:200px;">OMIM Id</th>
                </tr>
                <?php foreach($genes as $gene): ?>
                <tr>
                    <td><a href="/gene-detail.php?id=<?= $gene['id']; ?>&iframe=true&width=790&height=600" rel="prettyPhoto" title="<?= htmlentities($gene['offical_gene_symbol']); ?>"><?= $gene['offical_gene_symbol']; ?></a></td>
                    <td><a href="http://omim.org/entry/<?= $gene['omim_id']; ?>" target="_blank"><?= $gene['omim_id']; ?></a></td>
                </tr>
                <?php endforeach; ?>
                </table>

Could you please help how can I do that in this code? Thanks in Advance. Updated code.

Kortoa
  • 1
  • 4

4 Answers4

3

Since $genes seems to be a multidimensional array and i suppose you want it sorted by the title ('offical_gene_symbol') i suggest you to use the usort function that will provide you a way to create a custom comparation algorithm. As for the comparation, php offers you different comparing method for two strings:

  • strcasecmp Binary safe case-insensitive string comparison,Returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal.

  • strcmp Binary safe string comparison (case sensitive). works as the above function.

In php 5.3+ you can perform the sorting this way:

usort($genes, function($a, $b) {
    return strcasecmp($a['offical_gene_symbol'],$b['offical_gene_symbol']);
});

foreach($genes as $gene) {
...
}

In php < 5.3 you have to use a callable function :

function cmp($a, $b)
{
    return strcasecmp($a['offical_gene_symbol'],$b['offical_gene_symbol']);
}
    usort($genes, 'cmp');

    foreach($genes as $gene) {
    ...
    }
kawashita86
  • 1,555
  • 3
  • 18
  • 25
0

A straightforward use of PHP sort() function. Isn't it?

Ashraf Samhouri
  • 408
  • 3
  • 8
0

If this is a PHP array please check this functions, make sure to go check the other sort functions at php docs

$array = array(
    array('Offical_gene_Symbol' => 'v'),
    array('Offical_gene_Symbol' => 't'),
    array('Offical_gene_Symbol' => 'a'),
    array('Offical_gene_Symbol' => 'c'),
    array('Offical_gene_Symbol' => 'd')
    );

sort

sort($array);
echo debug($array);

Output

array(5) (
    0 => array(1) (
        "Offical_gene_Symbol" => string(1) "a"
    )
    1 => array(1) (
        "Offical_gene_Symbol" => string(1) "c"
    )
    2 => array(1) (
        "Offical_gene_Symbol" => string(1) "d"
    )
    3 => array(1) (
        "Offical_gene_Symbol" => string(1) "t"
    )
    4 => array(1) (
        "Offical_gene_Symbol" => string(1) "v"
    )
)

rsort

rsort($array);
print_r($array);

Output

array(5) (
    0 => array(1) (
        "Offical_gene_Symbol" => string(1) "v"
    )
    1 => array(1) (
        "Offical_gene_Symbol" => string(1) "t"
    )
    2 => array(1) (
        "Offical_gene_Symbol" => string(1) "d"
    )
    3 => array(1) (
        "Offical_gene_Symbol" => string(1) "c"
    )
    4 => array(1) (
        "Offical_gene_Symbol" => string(1) "a"
    )
)
Jorge Faianca
  • 791
  • 5
  • 11
-1

You can sort the array before you echo it to the page.

Something like:

</tr>
<?php
sort($genes);
foreach($genes as $gene):
?>
<tr>

Which will give you the values in ascending order by the value.

Edit: Changed asort to sort.

Alex Villa
  • 362
  • 2
  • 10
  • This works for few but not all. Like gene with A,B,C,N,D,A. Initially gene list was like N,D,Z,A,C,G,. Thanks for the tips. Could you please give me something else. Thanks. – Kortoa Jan 27 '14 at 18:04
  • You can try it with just sort perhaps. Can you provide more information on the array $genes? Is it a has/associative array or just an array? Do you want to sort by the values or the keys? – Alex Villa Jan 27 '14 at 18:08
  • sort and asort both has same result. Thanks. – Kortoa Jan 27 '14 at 18:10
  • actually array $genes come from if($test['test_type_id'] == 1 || $test['test_type_id'] == 5) { $gene_ids = FetchGenesByPanelTest($test['id']); $genes = array(); foreach($gene_ids as $gi){ $genes[] = FetchGene($gi); } – Kortoa Jan 27 '14 at 18:17
  • @Kortoa: Check the two other Answers mentioning usort. There is one by me and one that is even better by kawashita86. If those do not work for you, you might want to add more detail to your question/goal. – Andresch Serj Jan 28 '14 at 08:10