leonthelion
12-15-2008, 04:03 AM
Hi,
I have to use a flat file database for a current project and am in need of a simple sort method for sorting by the last item/column in the array.
I'm quite a newb with arrays and I may be handling this the wrong way, but would prefer a quick and easy fix for prototyping :)
my db structure:
field1|field2|field3 ...
field1|field2|field3 ...
field1|field2|field3 ...
each line is read into an array from text file. when displaying output on html page, am exploding using pipe symbol.
before displaying, I would like to sort the "rows" in the array to the last field's numeric value.
here is my php for reference:
<?php
$fp = fopen('test.db','r');
if (!$fp) {echo 'ERROR: Unable to open file.</table></body></html>'; exit;}
$ac = 0;
while (!feof($fp)) {
$lines[$ac] = fgets($fp,3062); //use 2048 if very long lines
$ac++;
$fp++;
}
fclose($fp);
//sort by something
echo '<table width="8000" border="1">
<tr>
<th>id</th>
<th>region</th>
<th>title</th>
<th>visible</th>
<th>age</th>
<th>materials</th>
<th>cost</th>
<th>areas</th>
<th>space</th>
<th>level</th>
<th>period</th>
<th>producer</th>
<th>img1</th>
<th>caption1</th>
<th>img2</th>
<th>caption2</th>
<th>img3</th>
<th>caption3</th>
<th>img4</th>
<th>caption4</th>
<th>img5</th>
<th>caption5</th>
<th>body1</th>
<th>body2</th>
<th>sort_order</th>
</tr>
';
for($x=0;$x<count($lines);$x++){
$a= explode("|",$lines[$x]);
echo '<tr>
<td>'.$a[0].'</td>
<td>'.$a[1].'</td>
<td>'.$a[2].'</td>
<td>'.$a[3].'</td>
<td>'.$a[4].'</td>
<td>'.$a[5].'</td>
<td>'.$a[6].'</td>
<td>'.$a[7].'</td>
<td>'.$a[8].'</td>
<td>'.$a[9].'</td>
<td>'.$a[10].'</td>
<td>'.$a[11].'</td>
<td>'.$a[12].'</td>
<td>'.$a[13].'</td>
<td>'.$a[14].'</td>
<td>'.$a[15].'</td>
<td>'.$a[16].'</td>
<td>'.$a[17].'</td>
<td>'.$a[18].'</td>
<td>'.$a[19].'</td>
<td>'.$a[20].'</td>
<td>'.$a[21].'</td>
<td>'.$a[22].'</td>
<td>'.$a[23].'</td>
<td>'.$a[24].'</td>
</tr>';
}
echo '</table';
?>
I have to use a flat file database for a current project and am in need of a simple sort method for sorting by the last item/column in the array.
I'm quite a newb with arrays and I may be handling this the wrong way, but would prefer a quick and easy fix for prototyping :)
my db structure:
field1|field2|field3 ...
field1|field2|field3 ...
field1|field2|field3 ...
each line is read into an array from text file. when displaying output on html page, am exploding using pipe symbol.
before displaying, I would like to sort the "rows" in the array to the last field's numeric value.
here is my php for reference:
<?php
$fp = fopen('test.db','r');
if (!$fp) {echo 'ERROR: Unable to open file.</table></body></html>'; exit;}
$ac = 0;
while (!feof($fp)) {
$lines[$ac] = fgets($fp,3062); //use 2048 if very long lines
$ac++;
$fp++;
}
fclose($fp);
//sort by something
echo '<table width="8000" border="1">
<tr>
<th>id</th>
<th>region</th>
<th>title</th>
<th>visible</th>
<th>age</th>
<th>materials</th>
<th>cost</th>
<th>areas</th>
<th>space</th>
<th>level</th>
<th>period</th>
<th>producer</th>
<th>img1</th>
<th>caption1</th>
<th>img2</th>
<th>caption2</th>
<th>img3</th>
<th>caption3</th>
<th>img4</th>
<th>caption4</th>
<th>img5</th>
<th>caption5</th>
<th>body1</th>
<th>body2</th>
<th>sort_order</th>
</tr>
';
for($x=0;$x<count($lines);$x++){
$a= explode("|",$lines[$x]);
echo '<tr>
<td>'.$a[0].'</td>
<td>'.$a[1].'</td>
<td>'.$a[2].'</td>
<td>'.$a[3].'</td>
<td>'.$a[4].'</td>
<td>'.$a[5].'</td>
<td>'.$a[6].'</td>
<td>'.$a[7].'</td>
<td>'.$a[8].'</td>
<td>'.$a[9].'</td>
<td>'.$a[10].'</td>
<td>'.$a[11].'</td>
<td>'.$a[12].'</td>
<td>'.$a[13].'</td>
<td>'.$a[14].'</td>
<td>'.$a[15].'</td>
<td>'.$a[16].'</td>
<td>'.$a[17].'</td>
<td>'.$a[18].'</td>
<td>'.$a[19].'</td>
<td>'.$a[20].'</td>
<td>'.$a[21].'</td>
<td>'.$a[22].'</td>
<td>'.$a[23].'</td>
<td>'.$a[24].'</td>
</tr>';
}
echo '</table';
?>