I'm a newbie and I can't figure out how to sort this array. My array is a text file (dbase.txt) setup like this:
category|name|type|id|options|price|..|..|..|..|..|..
I have tokenized each element of the array and have it so it lists by category ($categ). But now I want to sort the $name (which happens to be numbers) numerically so it displays in numeric order.
Here is the whole file, (the code I need help on is 2/3rds down):
<? include("vars.php" ?>
<? include("lib.php" ?>
<html>
<head>
<title><? echo "$title"; ?></title>
</head>
<? style(); ?>
<body>
<? if (file_exists("header.php"): include("header.php" endif; ?>
<script language="javascript">
<!--
function addItem(nameInfo, qtyItem) {
if (nameInfo.selectedIndex) {
nameInfoVal = nameInfo[nameInfo.selectedIndex].value;
}
else {
nameInfoVal = nameInfo.value;
}
splitPos = nameInfoVal.indexOf("|"
nameItem = nameInfoVal.substring(0,splitPos);
priceItem = nameInfoVal.substring(splitPos+1,nameInfoVal.length);
if (qtyItem <= 0) {
alert('Error!\n\nYou must enter a quantity');
return false;
}
if (confirm('Add '+qtyItem+' x '+nameItem+' @ $'+priceItem+' ea. to Shopping')) {
index = document.cookie.indexOf("ShopCart"
countstart = (document.cookie.indexOf("=", index) + 1);
countend = document.cookie.indexOf(";", index);
if (countend == -1) {
countend = document.cookie.length;
}
document.cookie="ShopCart="+document.cookie.substring(countstart, countend)+"["+nameItem+","+priceItem+"#"+qtyItem+"]";
<?
if ($confirm_type == "view":
echo "document.location = \"view.php\";";
endif;
?>
}
return true;
}
//-->
</script>
<? dispHeader($categ,$search,$cat); ?>
<p><table width="<? echo "$twidth"; ?>" border="1" cellspacing="0" cellpadding="2" bgcolor="<? echo "$tdbg"; ?>" bordercolor="Black" bordercolorlight="Black" bordercolordark="Black">
<tr>
<td><font color="<? echo "$font1"; ?>"><b>
<? if ($cat == "": echo "All Products"; else: echo "$cat"; endif; ?>
</b></font></td>
</tr>
</table>
<form name="itemsform">
<?
if ($option == "search":
exec("grep -i $search dbase.txt",$execAr);
if ($execAr[0] != "":
while ( list($key,$val) = each( $execAr ) ) {
$tok = strtok($val,"|"
$categ = $tok;
$tok = strtok("|"
$name = $tok;
$tok = strtok("|"
$type = $tok;
$tok = strtok("|"
$id = $tok;
$tok = strtok("|"
$options = $tok;
$tok = strtok("|"
$price = $tok;
$tok = strtok("|"
$normpr = $tok;
$tok = strtok("|"
$img = $tok;
$tok = strtok("|"
$stockop = $tok;
$tok = strtok("|"
$advdate = $tok;
$tok = strtok("|"
$details = $tok;
$tok = strtok("|"
$desc = $tok;
displayProd($categ,$type,$id,$name,$options,$price,$normpr,$img,$stockop,$advdate,$details,$desc);
}
else:
echo "There were no matches for your search query.
<P><br><br><br><br><hr width=\"100%\" size=\"1\" noshade color=\"black\">";
endif;
// below is where I'm having trouble, how do I sort by
// $categ before displaying?
else:
$cartFile = File("dbase.txt"
$length = sizeof($cartFile);
$i = 1;
while ($i < $length):
$tok = strtok($cartFile[$i],"|"
$categ = $tok;
$tok = strtok("|"
$name = $tok;
$tok = strtok("|"
$type = $tok;
$tok = strtok("|"
$id = $tok;
$tok = strtok("|"
$options = $tok;
$tok = strtok("|"
$price = $tok;
$tok = strtok("|"
$normpr = $tok;
$tok = strtok("|"
$img = $tok;
$tok = strtok("|"
$stockop = $tok;
$tok = strtok("|"
$advdate = $tok;
$tok = strtok("|"
$details = $tok;
$tok = strtok("|"
$desc = $tok;
if ($categ == "$cat" || $cat == "":
displayProd($categ,$type,$id,$name,$options,$price,$normpr,$img,$stockop,$advdate,$details,$desc);
endif;
$i++;
endwhile;
endif;
?>
</form>
<? dispFooter(); ?>
<? if (file_exists("footer.php"): include("footer.php" endif; ?>
</body>
</html>
category|name|type|id|options|price|..|..|..|..|..|..
I have tokenized each element of the array and have it so it lists by category ($categ). But now I want to sort the $name (which happens to be numbers) numerically so it displays in numeric order.
Here is the whole file, (the code I need help on is 2/3rds down):
<? include("vars.php" ?>
<? include("lib.php" ?>
<html>
<head>
<title><? echo "$title"; ?></title>
</head>
<? style(); ?>
<body>
<? if (file_exists("header.php"): include("header.php" endif; ?>
<script language="javascript">
<!--
function addItem(nameInfo, qtyItem) {
if (nameInfo.selectedIndex) {
nameInfoVal = nameInfo[nameInfo.selectedIndex].value;
}
else {
nameInfoVal = nameInfo.value;
}
splitPos = nameInfoVal.indexOf("|"
nameItem = nameInfoVal.substring(0,splitPos);
priceItem = nameInfoVal.substring(splitPos+1,nameInfoVal.length);
if (qtyItem <= 0) {
alert('Error!\n\nYou must enter a quantity');
return false;
}
if (confirm('Add '+qtyItem+' x '+nameItem+' @ $'+priceItem+' ea. to Shopping')) {
index = document.cookie.indexOf("ShopCart"
countstart = (document.cookie.indexOf("=", index) + 1);
countend = document.cookie.indexOf(";", index);
if (countend == -1) {
countend = document.cookie.length;
}
document.cookie="ShopCart="+document.cookie.substring(countstart, countend)+"["+nameItem+","+priceItem+"#"+qtyItem+"]";
<?
if ($confirm_type == "view":
echo "document.location = \"view.php\";";
endif;
?>
}
return true;
}
//-->
</script>
<? dispHeader($categ,$search,$cat); ?>
<p><table width="<? echo "$twidth"; ?>" border="1" cellspacing="0" cellpadding="2" bgcolor="<? echo "$tdbg"; ?>" bordercolor="Black" bordercolorlight="Black" bordercolordark="Black">
<tr>
<td><font color="<? echo "$font1"; ?>"><b>
<? if ($cat == "": echo "All Products"; else: echo "$cat"; endif; ?>
</b></font></td>
</tr>
</table>
<form name="itemsform">
<?
if ($option == "search":
exec("grep -i $search dbase.txt",$execAr);
if ($execAr[0] != "":
while ( list($key,$val) = each( $execAr ) ) {
$tok = strtok($val,"|"
$categ = $tok;
$tok = strtok("|"
$name = $tok;
$tok = strtok("|"
$type = $tok;
$tok = strtok("|"
$id = $tok;
$tok = strtok("|"
$options = $tok;
$tok = strtok("|"
$price = $tok;
$tok = strtok("|"
$normpr = $tok;
$tok = strtok("|"
$img = $tok;
$tok = strtok("|"
$stockop = $tok;
$tok = strtok("|"
$advdate = $tok;
$tok = strtok("|"
$details = $tok;
$tok = strtok("|"
$desc = $tok;
displayProd($categ,$type,$id,$name,$options,$price,$normpr,$img,$stockop,$advdate,$details,$desc);
}
else:
echo "There were no matches for your search query.
<P><br><br><br><br><hr width=\"100%\" size=\"1\" noshade color=\"black\">";
endif;
// below is where I'm having trouble, how do I sort by
// $categ before displaying?
else:
$cartFile = File("dbase.txt"
$length = sizeof($cartFile);
$i = 1;
while ($i < $length):
$tok = strtok($cartFile[$i],"|"
$categ = $tok;
$tok = strtok("|"
$name = $tok;
$tok = strtok("|"
$type = $tok;
$tok = strtok("|"
$id = $tok;
$tok = strtok("|"
$options = $tok;
$tok = strtok("|"
$price = $tok;
$tok = strtok("|"
$normpr = $tok;
$tok = strtok("|"
$img = $tok;
$tok = strtok("|"
$stockop = $tok;
$tok = strtok("|"
$advdate = $tok;
$tok = strtok("|"
$details = $tok;
$tok = strtok("|"
$desc = $tok;
if ($categ == "$cat" || $cat == "":
displayProd($categ,$type,$id,$name,$options,$price,$normpr,$img,$stockop,$advdate,$details,$desc);
endif;
$i++;
endwhile;
endif;
?>
</form>
<? dispFooter(); ?>
<? if (file_exists("footer.php"): include("footer.php" endif; ?>
</body>
</html>