• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 259
  • Last Modified:

How to sort files in table cells ?

Hi, I'm working with PHP 4.3 , I have this code:

<table width="580" cellpadding="1px" cellspacing="0px">
<tr align="center"><td style="width:10%">Numero</td><td style="width:30%">Concepto</td><td style="width:10%">Fecha Apertura</td><td style="width:10%">Cierre</td><td style="width:10%">Monto</td><td style="width:30%">Archivos</td></tr>
<?php
//get names for links
    $a = array();
    $b = array();
    $d = opendir('invcot');
    while (false !== ($f = readdir($d))) {
      if (is_dir($f)) continue;
      if (preg_match('|(\d+)_([^\.]+)\.zip|', $f, $m)) {
        $id = intval($m[1], 10);
        if (!isset($a[$id]))
          $a[$id] = array();
        $a[$id][count($a[$id])] = $f;

        if (!isset($b[$id]))
          $b[$id] = array();
        $b[$id][count($b[$id])] = str_replace('_', ' ', $m[2]);
      }
    }
    closedir($d);
//end names for links
//start building table
$filectr=0;
$fpath="invcot/";
foreach (glob("invcot/*.txt") as $txtfile) { //do this for every txt file
?>
<tr>
<?
$arrcninv=preg_split('/[\/\.]/',$txtfile);
$cninv=$arrcninv[1];  //get inv number
$i=0;
$hndfile = @fopen($txtfile, "r");
while (!feof($hndfile)) { //do this for every line
       $bufer = fgets($hndfile, 4096);
         $tmpdata = explode (": ", $bufer);
         $arrfinfo[i]=$tmpdata[1];//storing data from .txt in arrays
         $i++;
         if ($i==2) {
         ?><td width="30%" align="left"><? }
         elseif ($i==1) {
         ?><td width="10%" align="center"><? }         
         else { ?> <td width="12%" align="center"> <? }
         echo $arrfinfo[i] ?></td><? //  show every line in columns
   } //end getting info from txt file
      fclose($hndfile);
?> <td width="24%" align="left"> <?      
      $linectr++;
      $textctr=0;
      foreach ($a[$linectr] as $test1) {
      ?><a href="invcot/<? echo $test1; ?>"><? echo $b[$linectr][$textctr];?></a><br /> <?
      /*$linkstring = '<a href="'.$test1.'">'.$b[$linectr][$textctr].'</a><br />';*/
//      echo $linkstring;
      $textctr++;
      }
  ?> </td>
</tr>            
<?
}//for every text file
?>
</table>


--------------------------------------
and you can see the preview: http://www.hflleras.gov.co/HF/invcot.php

The problem is that links in last column are not in ascending order..... I have no idea how to sort them in the bidimensional arrays.

Thanks for your help!
0
blueshaolin
Asked:
blueshaolin
  • 2
  • 2
  • 2
  • +1
5 Solutions
 
HackneyCabCommented:
Wow. I've just spent five minutes poring over that code, and I'm still fairly overwhelmed.

I'm assuming that $a contains the archivos URLs, and $b contains the text for the archivos hyperlinks, and I'm assuming that you want the links sorted so that the number at the start of the text is listed in ascending order. With your data organised like that, it could be a lot of work writing code to sort them both into the same order.

Instead, could you structure that data in the following way:

$archivos[$row_number][$link_number] = array("url" => "/001_5_Eval_Juridica.zip", "text" => "Eval Juridica");

That way, you can sort the Archivos links for a particular row, let's say row number 5, using the ksort function:

ksort($archivos[5]);

Which should sort that row by the keys of that row's array. The keys in this case are the $link_number values, which should be numeric and not strings.

Once sorted in that manner, you ought to be able to ouput the Archivos in the correct order using the following method:

foreach ($archivos[$row_number] as $link_number => $hyperlink_parts) {
    echo '<a href="'.$hyperlink_parts['url'].'">'.$link_number.' '.$hyperlink_parts['text'].'</a><br />';
}

If you use that method exactly, you may have to add some code to pad the $link_numbers if the highest $link_number is greater than 10. But if you're prepared to organise your data in a different way, it will make sorting much simpler.
0
 
stormistCommented:
Hackney has a good point, the code seems like it's being deliberately cryptic. It is always best to name variables what they actually are. =) I believe a rewriting of your original code is in order for clarity.
0
 
blueshaolinAuthor Commented:
Thanks for your advices! I'll rewrite it.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Cornelia YoderArtistCommented:
In general, what you want to do is this:  when you get the array with the links for the last column in it, first sort it by whatever criteria you want to order it, using the appropriate php array sort function.  Then it will be easy to display in the table in the order you want.

There are various php array sort functions you can choose from, such as asort and the others you can link to from this manual page.

http://us2.php.net/manual/en/function.asort.php
0
 
HackneyCabCommented:
yodercm, the probelm for blueshaolin is that his data is split across two separate arrays, but he wants them both to be sorted in the same way.

Writing code that synchronises the sorting of two separate arrays is a pain in the ass, which is why I've recommended restructuring his data so that the URL and link text is contained within one single array. Then sorting becomes trivial using PHP's built-in functions.
0
 
Cornelia YoderArtistCommented:
Exactly, HackneyCab.  

When you recommended he rewrite the code, I just wanted to show him the various sort functions available, so he can structure the data in the best way.  I usually find this is a better approach than just giving someone a few lines of actual code and then having them try to build around it.  

Once he checks out the array sorting possibilities, he'll have a much easier time properly designing an array structure for his data that will work for all the needs of his program.
0
 
blueshaolinAuthor Commented:
Still rewriting code.... after holidays! hehe

I'm gonna split points as they suggested!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now