Solved

How to sort files in table cells ?

Posted on 2006-11-17
8
235 Views
Last Modified: 2008-02-01
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
Comment
Question by:blueshaolin
  • 2
  • 2
  • 2
  • +1
8 Comments
 
LVL 16

Accepted Solution

by:
HackneyCab earned 250 total points
ID: 17966190
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
 
LVL 10

Assisted Solution

by:stormist
stormist earned 50 total points
ID: 17966453
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
 

Author Comment

by:blueshaolin
ID: 17966493
Thanks for your advices! I'll rewrite it.
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 27

Assisted Solution

by:yodercm
yodercm earned 200 total points
ID: 17967364
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
 
LVL 16

Assisted Solution

by:HackneyCab
HackneyCab earned 250 total points
ID: 17968184
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
 
LVL 27

Assisted Solution

by:yodercm
yodercm earned 200 total points
ID: 17968400
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
 

Author Comment

by:blueshaolin
ID: 18236361
Still rewriting code.... after holidays! hehe

I'm gonna split points as they suggested!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question