Avatar billede tvj Nybegynder
23. december 2000 - 18:37 Der er 5 kommentarer

Sort.....

Hej

jeg har denne kode:
    if ($categ == \"$cat\" || $cat == \"\"):
   
        displayProd($categ,$type,$id,$name,$options,$price,$normpr,$img,$stockop,$advdate,$details,$desc);

    endif;

    $i++;

Hvordan får jeg den til at sortere outputet alfabetisk efter $name
Avatar billede mortenfn Nybegynder
23. december 2000 - 18:53 #1
Komandoen displayProd kender jeg ikke hvad gør den
Avatar billede tvj Nybegynder
23. december 2000 - 19:18 #2
Her er hele filen:

<? 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=\"100%\" 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;

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>
Avatar billede chrris Nybegynder
23. december 2000 - 21:21 #3
Må vi også se lib.php?
Det skulle ikke undre mig, om funktionen defineres her. Uden den, er det jo lidt vanskeligt at hjælpe dig.
Avatar billede tvj Nybegynder
23. december 2000 - 21:50 #4
her er lib.php:
Men ellers kan du hente hele scriptet her..:
http://www.webscriptworld.com/scripts/wsm.phtml

PS: Glædelig Jul

<?

function style () {

    include(\"vars.php\");
   
    echo \"<STYLE TYPE=\\\"text/css\\\">
    <!--
   
    BODY  {
        font-family : $font;
        font-size : $font_size pt;
        color : Black;
        font-weight : normal;
    }
   
    TD  {
        font-family : $font;
        font-size : $font_size pt;
        color : Black;
        font-weight : normal;
    }
   
    INPUT  {
        font-family : $font;
        font-size : $font_size pt;
        color : Black;
        font-weight : normal;
    }
   
      -->
    </STYLE>\";

}

function dispHeader ($categ,$search,$cat) {

    include(\"vars.php\");
   
    echo \"
    <SCRIPT LANGUAGE=\\\"JavaScript\\\">
    <!--
    function jumpto(){
        location = document.links.link_select.options[document.links.link_select.selectedIndex].value
        }
    -->
    </SCRIPT>
   
    <center>
    <table width=\\\"460\\\">
    <tr>
        <td>
   
    <p><table width=\\\"100%\\\" border=\\\"1\\\" cellspacing=\\\"0\\\" cellpadding=\\\"2\\\" bgcolor=\\\"$tdbg\\\" bordercolor=\\\"black\\\" bordercolorlight=\\\"black\\\" bordercolordark=\\\"black\\\">
    <tr>
        <td><font color=\\\"$font1\\\"><b>Store Options</b></font></td>
    </tr>
    </table>
   
    <table width=\\\"470\\\" border=\\\"0\\\" cellspacing=\\\"0\\\" cellpadding=\\\"0\\\">
    <form method=\\\"POST\\\" name=\\\"links\\\">
    <tr>
        <td width=\\\"160\\\" valign=\\\"TOP\\\">
   
    <br><select name=\\\"link_select\\\">
    <P><option value=\\\"index.php\\\">View all products\";
   
    $cartFile = File(\"dbase.txt\");
    $length = sizeof($cartFile);
    $i = 1;
    while ($i < $length):
        $tok = strtok($cartFile[$i],\"|\");
        $categ = $tok;
            if (!$lookupAr[$categ]):
                $categ2 = \"$categ\";
                $categ = ereg_replace(\"&\",\"%26\",$categ);
                echo \"<option value=\\\"index.php?cat=$categ\\\">$categ2 \\n\";
                $lookupAr[$categ] = 1;
            endif;
        $i++;
    endwhile;
   
    echo \"</select>
    <input type=\\\"button\\\" value=\\\"go\\\" onClick=\\\"jumpto()\\\">
   
    </td>
    </form>
    <form method=\\\"post\\\" action=\\\"index.php\\\">
    <td align=\\\"right\\\" valign=\\\"bottom\\\">
    <input type=\\\"hidden\\\" name=\\\"option\\\" value=\\\"search\\\">
    <input type=\\\"text\\\" name=\\\"search\\\" size=\\\"17\\\" value=\\\"$search\\\">
    <input type=\\\"submit\\\" value=\\\"Search\\\">
    </td>
    </tr>
    </form>
    </table>
   
    <p align=\\\"right\\\"><a href=\\\"mailto:$store_email\\\">Email Us</a>\"; if ($faqs != \"\"): echo \" | <a href=\\\"$faqs\\\">FAQs</a>\"; endif; echo \" | <a href=\\\"view.php\\\">View Shopping</a> | <a href=\\\"checkout.php\\\">Check Out</a>\";

}

function dispFooter () {

    include(\"vars.php\");
   
    echo \"<center>All prices quoted in $currency. Prices are \";
   
    if ($tax == \"no\"): echo \"exclusive\"; else: echo \"inclusive\"; endif;
   
    echo \" of $tax_name.
    </center>
    </td>
    </tr>
    </table>\";

}

function displayProd ($categ,$type,$id,$name,$options,$price,$normpr,$img,$stockop,$advdate,$details,$desc) {

    include(\"vars.php\");
   
    echo \"<P><table>
    <tr>\";
   
        if ($img != \"\"):
   
            echo \"<td valign=\\\"top\\\"><img src=\\\"images/$img\\\" border=\\\"0\\\"></td><td width=\\\"30\\\"></td>\";
           
            if ($details != \"\"):
           
                echo \"<p><font size=\\\"1\\\"><center><a href=\\\"$details\\\">Click for more details</a>\";
               
            endif;
   
        endif;
   
    echo \"<td valign=\\\"top\\\">
    <font color=\\\"$font1\\\"><b>$name</b></font>\";
   
    if ($stockop == \"advance\"):
   
        echo \"<br><b>Advance Order Only</b> - Available $advdate\";
   
    elseif ($stockop == \"outof\"):
   
        echo \"<br><b>Out of Stock</b>\";
   
    endif;
   
    echo \"<p>$desc\";
   
    if ($stockop == \"instock\" || $stockop == \"advance\"):
   
        if ($type == \"single\"):
   
            if ($normpr != \"\"):
           
                echo \"<p>RRP: <strike>$cur$normpr</strike> | <font color=\\\"$font2\\\"><b>Our Price: $cur$price</b></font>\";
           
            else:
           
                echo \"<p><font color=\\\"$font2\\\"><b>Price: $cur$price</b></font>\";
           
            endif;
   
            echo \" | <input name=\\\"\".$id.\"info\\\" type=\\\"hidden\\\" value=\\\"$name|$price\\\"> \";
   
        else:
   
            $tokAr = explode(\"~\",$options);
            $numTok = sizeof($tokAr);
            for ($i=0;$i<$numTok;$i++) {
                if (trim($tokAr[$i])!=\"\") {
                    $tokformat = ereg_replace(\" - \",\"|\",$tokAr[$i]);
                    $tok = ereg_replace(\" - \",\" - $cur\",$tokAr[$i]);
               
                    $options2 .= \"<option value=\\\"$name - $tokformat\\\">$tok\";
                }
            }
   
            $options = $options2;
   
            echo \"<p><select name=\\\"\".$id.\"info\\\">$options</select> \";
   
        endif;
   
        echo \"<input type=\\\"value\\\" name=\\\"\".$id.\"quant\\\" value=\\\"1\\\" size=\\\"3\\\"> <input type=\\\"button\\\" name=\\\"\".$id.\"add\\\" value=\\\"Add\\\" onclick=\\\"addItem(document.itemsform.\".$id.\"info,document.itemsform.\".$id.\"quant.value)\\\">\";
   
    endif;
   
        echo \"</td>
    </tr>
    </table>
   
    <P><hr width=\\\"100%\\\" size=\\\"1\\\" noshade color=\\\"black\\\">\";

}


function dispRegForm ($user,$email,$passwd,$firstname,$surname,$billing_adr,$delivery_adr,$evening_ph,$day_ph,$fax,$paytype,$ccno,$ccexp,$noc,$option) {

    include(\"vars.php\");
   
    $billing_adr = ereg_replace(\"<br>\",\"\\n\",$billing_adr);
    $delivery_adr = ereg_replace(\"<br>\",\"\\n\",$delivery_adr);
   
    echo \"<p><table border=\\\"0\\\" cellspacing=\\\"0\\\" cellpadding=\\\"2\\\">
    <tr>
        <td colspan=\\\"2\\\"><font color=\\\"$font2\\\"><b>Contact Details<br><br></b></td>
    </tr>\";
   
    if ($option != \"update\"):
   
    echo \"<tr>
        <td width=\\\"140\\\">Username:</td>
        <td width=\\\"200\\\"><input type=\\\"text\\\" name=\\\"user\\\" value=\\\"$user\\\" size=\\\"30\\\"></td>
    </tr>\";
   
    else:
   
        echo \"<input type=\\\"hidden\\\" name=\\\"user\\\" value=\\\"$user\\\">
        <input type=\\\"hidden\\\" name=\\\"passwd\\\" value=\\\"$passwd\\\">
        <input type=\\\"hidden\\\" name=\\\"option\\\" value=\\\"update2\\\">\";
   
    endif;
   
    echo \"<tr>
        <td>Email:</td>
        <td><input type=\\\"text\\\" name=\\\"email\\\" value=\\\"$email\\\" size=\\\"30\\\"></td>
    </tr>\";
   
    if ($option != \"update\"):
   
    echo \"<tr>
        <td>Password:</td>
        <td><input type=\\\"password\\\" name=\\\"passwd\\\" value=\\\"$passwd\\\" size=\\\"30\\\"></td>
    </tr>\";
   
    endif;
   
    echo \"<tr>
        <td>First Name :</td>
        <td><input type=\\\"text\\\" name=\\\"firstname\\\" value=\\\"$firstname\\\" size=\\\"30\\\"></td>
    </tr>
    <tr>
        <td>Surname :</td>
        <td><input type=\\\"text\\\" name=\\\"surname\\\" value=\\\"$surname\\\" size=\\\"30\\\"></td>
    </tr>
    <tr>
        <td valign=\\\"top\\\">Billing Address:</td>
        <td><textarea name=\\\"billing_adr\\\" cols=\\\"26\\\" rows=\\\"4\\\" wrap=\\\"physical\\\">$billing_adr</textarea></td>
    </tr>
    <tr>
        <td valign=\\\"top\\\">Delivery Address:</td>
        <td><textarea name=\\\"delivery_adr\\\" cols=\\\"26\\\" rows=\\\"4\\\" wrap=\\\"physical\\\">$delivery_adr</textarea></td>
    </tr>
    <tr>
        <td>Home Phone:</td>
        <td><input type=\\\"text\\\" name=\\\"evening_ph\\\" value=\\\"$evening_ph\\\" size=\\\"30\\\"></td>
    </tr>
    <tr>
        <td>Work Phone:</td>
        <td><input type=\\\"text\\\" name=\\\"day_ph\\\" value=\\\"$day_ph\\\" size=\\\"30\\\"></td>
    </tr>
    <tr>
        <td>Fax:</td>
        <td><input type=\\\"text\\\" name=\\\"fax\\\" value=\\\"$fax\\\" size=\\\"30\\\"></td>
    </tr>
    <tr>
        <td colspan=\\\"2\\\"><br><br><font color=\\\"$font2\\\"><b>Preferred Payment Method<br><br></b></td>
    </tr>
    <tr>
        <td width=\\\"140\\\">Payment Type:</td>
        <td width=\\\"200\\\"><select name=\\\"paytype\\\">\";
   
        $cartFile = File(\"paytype.txt\");
        $length = sizeof($cartFile);
        $i = 0;
        while ($i < $length):
        $pt = ($cartFile[$i]);
       
            echo \"<option\"; if ($paytype != \"\"): if (eregi($paytype,$pt)): echo \" selected\"; endif; endif; echo \">$pt\";
       
        $i++;
        endwhile;
       
    echo \"</select>
        </td>
    </tr>
    <tr>
        <td>Credit Card No.:</td>
        <td><input type=\\\"text\\\" name=\\\"ccno\\\" value=\\\"$ccno\\\" size=\\\"30\\\"></td>
    </tr>
    <tr>
        <td>Expiry Date:</td>
        <td><input type=\\\"text\\\" name=\\\"ccexp\\\" value=\\\"$ccexp\\\" size=\\\"30\\\"></td>
    </tr>
    <tr>
        <td>Name on Card:</td>
        <td><input type=\\\"text\\\" name=\\\"noc\\\" value=\\\"$noc\\\" size=\\\"30\\\"></td>
    </tr>
    </table>\";

}

?>
Avatar billede erikjacobsen Ekspert
23. december 2000 - 21:53 #5
Det er nu ikke så svært. Løb dit execArr igennem, og træk dit \"name\" ud.
Put hele linien ind i et andet array $arr, med \"name\" som nøgle.
Sorter $arr med ksort.
Udkskriv som du plejer.
Og skift iøvrigt til en rigtig database engang, og brug SQL ;-)

Nedenstående er et kørende eksempel, som du sikkert kan kopiere fra, men
selvfølgelig ikke helt det du har:

<?
$execAr[]=\"3333|ccc|eee|...\";
$execAr[]=\"4444|bbb|eee|...\";
$execAr[]=\"5555|zzz|eee|...\";
$execAr[]=\"6666|fff|eee|...\";
$execAr[]=\"7777|xxx|eee|...\";
$execAr[]=\"8888|aaa|eee|...\";
$execAr[]=\"9999|rrr|eee|...\";

  while ( list($key,$val) = each( $execAr ) ) {
    $s=split(\"\\|\",$val);
    $arr[$s[1]]=$val;
  }

  ksort($arr);

  while ( list($key,$val) = each( $arr ) ) {

      $categ = strtok($val,\"|\");
      $name = strtok(\"|\");

      echo \"$categ: $name<br>\\n\";

    }
   
?>
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Vi tilbyder markedets bedste kurser inden for webudvikling

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester