Jump to content

Sorting the output of a CSV file


madmega

Recommended Posts

Hi, my scripts loads up a CSV file, and divide it in parts.

Is it possible to sort these parts alphabetical ?

As $parts[0] is the first to display, i want that row to be sorted, i have tried the sort command but it says that i need an array.

Since the array is the whole $parts[0] i don't know how to use that command.

Is there someone who knows how to do this ?

 

<html>
<head>
</head>
<body bgcolor=#E2E965 link=#E2E965 vlink=#E2E965 alink=#E2E965 text=#D5DF23>

<style type="text/css">
body
{
scrollbar-arrow-color: #E2E965;
scrollbar-face-color: #D7182A;
scrollbar-highlight-color: #000000;
scrollbar-3dlight-color: #D6DF23;
scrollbar-shadow-color: #00000;
scrollbar-darkshadow-color: #00000;
scrollbar-track-color: #D6DF23;
}

input:link {text-decoration: none; color: #E2E965;}
input:visited {text-decoration: none; color: #E2E965;}
input:active {text-decoration: none; color: #E2E965;}

.spacer_black {
margin: 0px;
padding: 0px;
border: 5px;
height: 2px;
width: 100%;
line-height: 0px;
font-size: 0px;
background-color: #000000;}
</style>
</html>
<?PHP
$offset = isset($_GET['offset'])?$_GET['offset']:0;
$LinesToDisplay = 20;
$row = $offset + $LinesToDisplay;
$row2 = $offset - $LinesToDisplay;
$file_handle = fopen("web.txt", "rb");
error_reporting( E_ALL ); // DEBUGGING 

while ((($parts = fgetcsv($file_handle,4096,"|")) !== FALSE)  && ($LinesToDisplay > 0) && (!feof($file_handle)))
{
    //new code
    //skip first $offset lines    


$num = count($parts[5]);

if ($parts[5] == 9999)
{

if ($offset-- > 0)   
{continue;}


$parts[0] = ucwords(strtolower($parts[0]));
$parts[1] = ucwords(strtolower($parts[1]));

echo "<p style='margin:9px;'><table BACKGROUND='background.jpg' border=0><td width='243' height='105'>";

echo "<font size=-1 face='helvetica' color=#812990><b>$parts[0]</b></font>";
echo "<i>";

?>
<html>
<div onMouseOver="this.style.color = 'black';" onMouseOut="this.style.color = '#D5DF23';"></html><?php
echo "<font size=2>-$parts[1]</font>";
echo "</div></i>";

?>
<TABLE BORDER='0' cellpadding='0' CELLSPACING='0'>
<TR>
<TD WIDTH='70' HEIGHT='20' BACKGROUND='pricebackground.jpg' VALIGN='bottom'>
<center>
<font size=2 color=white face='helvetica'>
<b></html>
<?php echo "€  $parts[2]";

?>
</b>
</td>
</tr>
</table>
<a href="#" title="Koop <?php echo $parts[0]; ?>" onClick="window.open('form.php?p=<?php echo urlencode($parts[0]); ?>','popuppage','width=400,height=400,top=250,left=250,resizable=0,statusbar=0,titlebar=yes,toolbar=no,scrollbars=no,location=no,directories=no');">
<div><img src='ster.jpg' border=0 width='50' align='right'></a>

<?php echo "<b><font size=3 color=#D7182A>Op Voorraad In:<br></font>";
      echo ("<font color=black> Amsterdam </font>");
      if ( $parts[3] >= 1 )  echo ("<IMG SRC =green.gif>")."";
      if ( $parts[3] <= 0 )  echo ("<IMG SRC =red.gif>")."";
      echo ("<font color=black> Utrecht </font>");
      if ( $parts[4] >= 1 )  echo ("<IMG SRC =green.gif>")."</td></table></b><p style='margin:9px;'>";
      if ( $parts[4] <= 0 )  echo ("<IMG SRC =red.gif>")."</td></table></b><p style='margin:9px;'>";


      $LinesToDisplay--;
     }}
fclose($file_handle);
?>

<html>
<head>
<body>
<script language="Javascript">

<!--
if (document.images)
{
image1 = new Image
image2 = new Image
image1.src = 'vorige1.jpg'
image2.src = 'vorige2.jpg'
image3 = new Image
image4 = new Image
image3.src = 'volgende1.jpg'
image4.src = 'volgende2.jpg'

}
-->

</script>
</head>
</body>

<center><a href="occasiona.php?offset=<?php echo $row2;?>" onMouseOver="document.mouseover.src=image2.src" onMouseOut="document.mouseover.src=image1.src"><img src="vorige1.jpg" border=0 name="mouseover"></a>


<a href="occasiona.php?offset=<?php echo $row;?>"
onMouseOver="document.mouseover2.src=image4.src" onMouseOut="document.mouseover2.src=image3.src"><img src="volgende1.jpg" border=0 name="mouseover2"></a>
</center>
</html>

Link to comment
https://forums.phpfreaks.com/topic/192136-sorting-the-output-of-a-csv-file/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.