Results 1 to 5 of 5

Thread: Max Function resolved

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Newcastle
    Posts
    260

    Resolved Max Function resolved

    How can I get the max value from a record set im pulling back, Im creating a table by looping thro the record set.

    PHP Code:
    <?php


    $result 
    mysql_query("SELECT order_code, description, price, postage, quantity FROM tbl_cart WHERE transid='" $_SESSION['cartnumber'] . "'") or die(mysql_error()); 

    $num_rows mysql_num_rows($result);
        
            if (!(
    $num_rows))        
            {
                
    //format all this pretty pretty
                
    die('Your shopping cart is empty.'mysql_error());
            
            }

    ?>
    and im populating a table with this

    PHP Code:
      <table width="100%" border="1">
        <tr><th>Item Code</th> <th>Description</th> <th>Quantity</th> <th>Price</th> <th>Total</th><th>Remove</th></tr>
    <?php 
            
    while($row mysql_fetch_array$result )) {
            
    $amount $row['quantity'];
            
    $price ltrim($row['price'],£);
            
    $tamount = ($amount $price);
            
    $gt $gt $tamount;
    echo    
    '<td width="10%">'.$row['order_code']. '</td>';
    echo    
    '<td width="50%">'.$row['description']. '</td>';
    echo    
    '<td width="10%">'$row['quantity']. '</td>';
    echo    
    '<td width="10%">'.$row['price']. '</td>';
    echo    
    '<td width="10%">' '£' $tamount '</td>';
    echo    
    '<td width="10%"><a href="deleteentryfromcart.php?oc='.$row['order_code'].'&transid=' $_SESSION['cartnumber'] .'" target="_self">Remove</a></td>';
    echo    
    '</tr>';

    }
    ?>
      </table>

    I need to find the highest value in the postage field, I have found the max() function but im not sure how to incorate it into the loop.

    thanks Chris
    Last edited by Chrisio; Mar 23rd, 2005 at 04:35 AM. Reason: Resolved

  2. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Max Function

    The max() function is a grouping function. What do you mean? Do you want the highest postage amount to be first or only the records with the highest postage amount?
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Newcastle
    Posts
    260

    Re: Max Function

    i want to only use the highest postage cost so for instance i have

    1.50
    1.20
    1.60
    1.99

    then i will get 1.99 as my value.

    as it happens i have got around this by loading each value into a array and then using rsort($rarray) on it that way I can just pull the 0 element and know that that is the highest.

    if there is a better way please show me...i want to learn


    cheers
    chrisio

  4. #4
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Max Function

    Its actually quite a small query:
    Code:
    SELECT Max(postage) FROM tablename;
    Will give a result set containing one row and one column, with the maximum value in.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Newcastle
    Posts
    260

    Re: Max Function

    Quote Originally Posted by visualAd
    Its actually quite a small query:
    Code:
    SELECT Max(postage) FROM tablename;
    Will give a result set containing one row and one column, with the maximum value in.

    Thanks I knew there would be an easier way.

Posting Permissions

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



Click Here to Expand Forum to Full Width