SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2005
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    help needed... php along with javascripts

    Hi,

    I have a script written in PHP and JavaScipts that auto calculates the total and the grand total of orders derived from suppliers. It is working fine provided that the "supplier name" is just one word but when it's having two words already like Cad Group or with a hyphen "-" like e-Computers or with a "()" like Emitech (Sales), the auto calculation javascript is not working anymore.

    Can anyone help sort out the problem please? thank you in advance.

    Below are the code and the snapshot of the running script.

    The code: with the help of killerkooki

    PHP Code:
    <html> 
    <head> 
    </head> 
    <body> 
    <?php 
    include("includes/functions.inc.php"); 
    include(
    "config/db.conf.php"); 

    //Change following line as necessary to reflect the page the form is submitted to 
    $pageToPostTo '2.php'

    //To get a list of all possible suppliers. 
    //$supplierArray will be an associative array holding name and the position in $supplierTotalArray. 
    //$supplierTotal will be an associative array holding the totals for the items. 
    $supplierArray = array(); 
    $supplierTotalArray = array(); 
    $getSuppliersSQL "SELECT 
                            DISTINCT items.supplier_name 
                        FROM 
                            ticket, 
                            ticketitems, 
                            items 
                        WHERE 
                            ticket.t_id = ticketitems.t_id AND 
                            ticketitems.ItemName = items.ItemName 
                        ORDER BY 
                            items.supplier_name"

    $getSuppliersResult query($getSuppliersSQL); 

    //Initialise $supplierArray to 0 for all possible suppliers. 
    $supplierTotalPositionCount 0
    while (
    $resultRow mysql_fetch_row($getSuppliersResult)) { 
        
    $supplierName $resultRow[0]; 
        
    $supplierArray{$supplierName} = $supplierTotalPositionCount
        
    $supplierTotalArray[] = 0
        
    $supplierTotalPositionCount++; 
        } 

    //Rendering the table 
    $supplierCount count($supplierArray); 
    ?> 
    <form name='formPrices' method='post' action='<?php echo $pageToPostTo ?>'> 
    <table width="100%" border="0" cellspacing="0" cellpadding="0"> 
        <tr valign="top"> 
            <th>Item Order</th> 
            <th>Item Name</th> 
            <th>Quantity</th> 
            <th colspan="<? echo $supplierCount?>">Supplier / Unit Price</th> 
        </tr> 
        <tr valign="top"> 
            <td>&nbsp;</td> 
            <td>&nbsp;</td> 
            <td>&nbsp;</td> 
    <? 
    $suppliers 
    array_keys($supplierArray); 
    foreach (
    $suppliers As $currentName) { 
        echo 
    '<th>' $currentName '</th>'
        } 
    ?> 
        </tr> 
    <? 

    //To get a list of all possible orders. 
    //$ordersArray will be an associative array holding the order details. 
    //$ordersHTMLArray will be an associative array holding the order HTML. 
    $ordersListArray = array(); 
    $ordersArray =  array(); 
    $ordersHTMLArray = array(); 
    $getOrdersSQL "SELECT 
                        ticketitems.ItemOrder, 
                        ticketitems.ItemName, 
                        ticketitems.ItemQty 
                    FROM 
                        ticket, 
                        ticketitems 
                    WHERE 
                        ticket.t_id = ticketitems.t_id 
                    ORDER BY 
                        ticketitems.ItemOrder ASC"

    $getOrdersResult query($getOrdersSQL); 

    //Populate $ordersArray with Details. 

    while ($resultRow mysql_fetch_row($getOrdersResult)) { 
        
    $itemOrder $resultRow[0]; 
        
    $itemName $resultRow[1]; 
        
    $itemQuantity $resultRow[2]; 
        
    $ordersArray{$itemName} = array('order' => $itemOrder'name' => $itemName'quantity' => $itemQuantity); 
        
    $ordersListArray[] = $itemOrder
        } 
    array_unique($ordersListArray); 

    //Get Suppliers for the Items 
    $orders array_keys($ordersArray); 

    foreach (
    $orders As $currentItem) { 
        
    $itemRowSQL "SELECT 
                            items.description, 
                            items.brandname, 
                            items.price, 
                            items.supplier_name 
                        FROM 
                            items 
                        WHERE 
                            items.ItemName = '" 
    $currentItem "' 
                        ORDER BY 
                            items.supplier_name ASC"

        
    $itemRowResult query($itemRowSQL); 

        
    $lowestPrice = -1
        
    $lowestPriceSupplier ''
        while (
    $resultRow mysql_fetch_row($itemRowResult)) { 
            
    $itemPrice $resultRow[2]; 
            if (
    $lowestPrice == -1) { 
                
    $lowestPrice $itemPrice
                
    $lowestPriceSupplier $resultRow[3]; 
                } 
            else { 
                if (
    $itemPrice <= $lowestPrice) { 
                    
    $lowestPrice $itemPrice
                    
    $lowestPriceSupplier $resultRow[3]; 
                    } 
                } 
            } 

        
    $itemRowResult query($itemRowSQL); 

        
    $positionalArray = array(); 
        while (
    $resultRow mysql_fetch_row($itemRowResult)) { 
            
    $itemDescription $resultRow[0]; 
            
    $itemBrand $resultRow[1]; 
            
    $itemPrice $resultRow[2]; 
            
    $itemSupplier $resultRow[3]; 
            
    $supplierColumn $supplierArray[$itemSupplier]; 
            
    $tempArray $ordersArray[$currentItem]; 
            
    $itemOrder $tempArray['order']; 
            
    $itemName $tempArray['name']; 
            
    $itemQuantity $tempArray['quantity']; 
            
    $supplierTotalArray[$supplierColumn] = $supplierTotalArray[$supplierColumn] + ($itemPrice *$itemQuantity); 
            
    $formattedPrice sprintf('%.2f'$itemPrice); 
            if (
    $itemSupplier == $lowestPriceSupplier) { 
                
    $checkedString ' checked="checked"'
                } 
            else { 
                
    $checkedString ''
                } 
            
    $positionalArray[$supplierColumn] = '<td>' $formattedPrice ' <input type="hidden" name="h_price_' $itemOrder '_' $itemSupplier '" value="' $itemPrice '"><input type="radio" name="radio_' $itemOrder '" value="' $itemSupplier '"' $checkedString ' onClick="doRecalculate(this)">' '</td>'
            } 
        
    $rowSupplierHTML ''
        for (
    $counter 0$counter $supplierCount$counter++) { 
            if (
    array_key_exists($counter$positionalArray)) 
                
    $rowSupplierHTML .= $positionalArray[$counter]; 
            else 
                
    $rowSupplierHTML .= '<td>&nbsp;</td>'
            } 
        
    $ordersHTMLArray{$itemName} = '<tr><td>' $itemOrder '</td><td>' $itemName '</td><td>' $itemQuantity '</td><input type="hidden" name="h_qty_' $itemOrder '" value="' $itemQuantity '">' $rowSupplierHTML '</tr>'
        } 

    foreach (
    $ordersHTMLArray As $currentHTML) { 
        echo 
    $currentHTML
        } 

    ?> 

        <tr> 
            <td>&nbsp;</td> 
            <td>&nbsp;</td> 
            <td>Total</td> 
    <? 
    foreach ($suppliers As $currentValue) { 
        echo 
    '<td>' '<input type="text" name="t_total_' $currentValue '" value="" readonly="readonly">' '</td>'
        } 
    ?> 
        </tr> 
        <tr> 
            <td colspan='<?php echo $supplierCount 3?>' align="right">Grand Total: <input type="text" name="t_grand_total" value="" readonly="readonly"></td> 
        </tr> 
        <tr> 
            <td colspan='<?php echo $supplierCount 3?>' align="right"><input type="submit" value="Submit"></td> 
        </tr> 
    </table> 

    </form> 
    <script language="JavaScript"> 
    var supplierList = new Array(<?php echo '\'' str_replace(',''\',\''implode($suppliers',')) . '\''?>); 
    var orderList = new Array(<?php echo '\'' str_replace(',''\',\''implode($ordersListArray',')) . '\''?>); 

    function doRecalculate(obj) { 
        var supplierListLength = supplierList.length; 
        var orderListLength = orderList.length; 
        for (supplierListCounter = 0; supplierListCounter < supplierListLength; supplierListCounter++) { 
            stringToEvalate = "var total" + supplierList[supplierListCounter] + " = 0;"; 
            eval (stringToEvalate); 
            } 

        for (orderListCounter = 0; orderListCounter < orderListLength; orderListCounter++) { 
            radioName = "document.formPrices.radio_" + orderList[orderListCounter]; 
            eval("radioObjName = " + radioName + ";"); 
            if (radioObjName.length > 0) { 
                //Instructions if more than one radio button 
                radioOptionsCount = radioObjName.length; 
                for (radioCounter = 0; radioCounter < radioOptionsCount; radioCounter++) { 
                    if (radioObjName[radioCounter].checked) 
                        supplierName = radioObjName[radioCounter].value; 
                    } 
                } 
            else { 
                //Instructions if only one radio button 
                supplierName = radioObjName.value; 
                } 

            hiddenPriceName = "document.formPrices.h_price_" + orderList[orderListCounter] + "_" + supplierName; 
            eval("hiddenPriceObjName = " + hiddenPriceName + ";"); 
            unitPrice = hiddenPriceObjName.value; 

            hiddenQtyName = "document.formPrices.h_qty_" + orderList[orderListCounter]; 
            eval("hiddenQtyObjName = " + hiddenQtyName + ";"); 
            unitQty = hiddenQtyObjName.value; 

            rowTotal = unitPrice * unitQty; 

            eval("total" + supplierName + " += rowTotal;"); 
            } 

        grandTotal = 0; 
        for (supplierListCounter = 0; supplierListCounter < supplierListLength; supplierListCounter++) { 
            stringToEvaluate = "totalPrice = formatPrice(total" + supplierList[supplierListCounter]+ ");"; 
            eval (stringToEvaluate); 
            grandTotal = parseFloat(grandTotal) + parseFloat(totalPrice); 
            totalName = "document.formPrices.t_total_" + supplierList[supplierListCounter]; 
            stringToEvaluate = totalName + ".value = totalPrice;"; 
            eval (stringToEvaluate); 
            } 

        document.formPrices.t_grand_total.value = formatPrice(grandTotal); 
        } 

    function formatPrice(value) { 
        value = value + ""; 
        valueLength = value.length; 
        decimalPosition = value.indexOf("."); 
        if (decimalPosition < 0) { 
            formattedValue = value + ".00"; 
            } 
        else { 
            wholeLength = decimalPosition; 
            wholePart = value.substr(0, wholeLength); 
            decimalPart = value.substr(decimalPosition + 1, 2); 
            if (decimalPart.length == 1) { 
                decimalPart = decimalPart + "0"; 
                } 
            formattedValue = wholePart + '.' + decimalPart; 
            } 
        return formattedValue; 
        } 

    doRecalculate(); 
    </script> 

    </body> 
    </html>
    Snapshot: should be this way, but if the supplier names got 2 words or with hyphen etc., the auto calculation doesn't work.

    I sincerely thank you all for the help & shared knowledge. - cyp

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)

    Duplicate Thread

    Because this is a javascript question and this is the PHP forum, please see your other, earlier thread in the javascript forum
    http://www.sitepoint.com/forums/showthread.php?t=300224

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2005
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mittineague
    Because this is a javascript question and this is the PHP forum, please see your other, earlier thread in the javascript forum
    http://www.sitepoint.com/forums/showthread.php?t=300224

    Because certain characters have significance in Javascript, (and other scripting languages too) they need to be escaped before they can be used in the script.
    Using these
    PHP Code:
      for "space"
    - for -
    ( for (
    ) for ) 
    (and similarly for any other problem causing characters) will allow values to be used in the script and will display them correctly in the HTML
    -------------
    Thanks for your reply Mittineague, i have posted it to both forum since the script was written in both php & javascripts and hoping i could get an answer to both as well... i have tried the options you have suggested but it didn't work still... the auto calculation's still disabled unless i make all the "supplier names" with more than 2 words or with special characters as one word. If it is one word, then it does work... any other ideas guys? is there any work around with this? Thank you in advance.
    Last edited by cyp; Sep 17, 2005 at 11:46. Reason: correcting tags
    I sincerely thank you all for the help & shared knowledge. - cyp

  4. #4
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)

    PHP and javascript

    Quote Originally Posted by cyp
    ... i have tried the options you have suggested but it didn't work still... the auto calculation's still disabled unless i make all the "supplier names" with more than 2 words or with special characters as one word. If it is one word, then it does work... ...
    If you replace a space with &#38;#32; then the 2 words effectively become one word. For example, "Acme Codeworks" becomes "Acme&#38;#32;Codeworks"
    If you changed the characters in the PHP but the javascript still failed it is most likely because you did not escape the ampersand (kind of like "double" escaping), You need to escape the ampersand in the PHP so that when the browser renders the page's javascript it writes the escaped character, which creates the correct HTML display. Because you did not properly escape the characters in the above post I'm assuming you don't know how. For example the space would be
    In the PHP script
    change the space to &#38;#38;#32;
    this will produce the javascript
    &#38;#32;
    which will produce an HTML space - " "


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •