Solved

How to sort files in table cells ?

Posted on 2006-11-17
8
232 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

708 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now