Page 1 of 3 123 LastLast
Results 1 to 10 of 25

Thread: PHP - Best price?

  1. #1
    Join Date
    Aug 2005
    Posts
    174
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default PHP - Best price?

    Hi, I have many products which are the same in my database, but want to show only the *lowest* priced... at the moment, only the 1st result in the database is shown..

    $sql = "SELECT * FROM $table WHERE make='$getmake' GROUP BY handset";

    Cheers..

  2. #2
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by nikomou
    Hi, I have many products which are the same in my database, but want to show only the *lowest* priced... at the moment, only the 1st result in the database is shown..

    $sql = "SELECT * FROM $table WHERE make='$getmake' GROUP BY handset";
    When the GROUP BY clause is used, only expressions within that clause can appear in SELECT expression list unless an aggregate function is used. That is, only handset can be used by itself; including * should raise an error.

    Luckly, there is quite an appropriate aggregate function:

      SELECT handset, MIN(price) WHERE make='...' GROUP BY handset

    The result set should contain one instance of each handset value with the appropriate make, and the lowest price within each group.

    Mike

  3. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    ORDER BY is also helpful.
    If you took the first result of the list ordered by price, then that would be the lowest price. (ASC [ascending] is the default. For descending, use DESC)
    Ex. of desc:
    ....ORDER BY `price` DESC
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  4. #4
    Join Date
    Aug 2005
    Posts
    174
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    came up with an error.. there any other ways to do this?

  5. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Error means fix, not give up.
    Post the code that gave the error.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  6. #6
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by djr33
    Post the code that gave the error.
    Also, please provide more information about the database structure, including what data you're actually looking for in each result.

    Mike

  7. #7
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Yeah, that too.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  8. #8
    Join Date
    Aug 2005
    Posts
    174
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    code used:
    PHP Code:
    $sql "SELECT handset, MIN(price) WHERE make='$getmake' GROUP BY handset"
    error msg:
    HTML Code:
    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/xxxx/public_html/test.php on line 18
    database example:

    ID | Handset | Make | Network | Tariff | Total Cost| Line Rental | Mins | Texts | URL

    There are on average 20 records with the same handset name.

    I want to show every handset determaned by its make, but only show the ones with the min "Total Cost"

    e.g. of code
    PHP Code:
    <?php
    $getmake 
    $_GET['make'];
    $address "localhost";
    $username "xxxx";
    $password "xxxx";
    $db "xxxx";
    $table "xxxx";
    $conn mysql_connect($address$username$password);
    $rs mysql_select_db($db$conn);
    $sql "SELECT handset, MIN(price) WHERE make='$getmake' GROUP BY handset";
    $rs mysql_query($sql$conn);
    $j 0;
    echo(
    "<table width='100%' border='0' cellpadding='0' cellspacing='0' id='$make'><tr valign='top'>"); 
    while(
    $row mysql_fetch_array($rs)) { 

    $handset $row[handset];
    $make $row[make];
    $tariff $row[tariff];
    $tariffid $row[tariffid];
    $id $row[handsetid];
    $price $row[price];
    $url $row[url];
    $gift $row[gift];
    $texts $row[texts];
    $xnet $row[xnet];
    $offpeak $row[offpeak];
    $merchant $row[merchant];
    $contractlength $row[contractlength];
    $montlycost $row[monthlycost];
    $cashback $row[cashback];
    $freerental $row[freerental];
    $halfrental $row[halfrental];
    $total $row[total];

    if (
    $xnet == "0"$xnetmin = ("");
    if (
    $xnet != "0"$xnetmin = ("$xnet Anytime Minutes");

    if (
    $offpeak == "0"$offpeakmin = ("");
    if (
    $offpeak != "0"$offpeakmin = ("$offpeak Off Peak Minutes");

    if (
    $texts == "0"$textsmin = ("Text Bundles Available");
    if (
    $texts != "0"$textsmin = ("$texts <b>FREE</b> Texts");

    echo(
    "<th class='menusmall' align='center' width='16%'><p align='center'><font face='Tahoma' style='font-size: 9pt'><b><br />");
      echo(
    "<table border='0' width='200' id='$handset' cellspacing='5' cellpadding='0'><tr><td colspan='2' width='0'><p align='left'><font style='FONT-SIZE: 9pt' face='Tahoma'><b><span style='TEXT-DECORATION: none'><font color='#2d75d7'><a href='http://www.xxxx.com/handsets.php?hs=$id.'><font color='#2D75D7'><span style='text-decoration: none'>$handset</span></font></a></font></span></b></font></td><td width='9%'><font face='Tahoma' size='1'><a class='tditem' href='http://www.xxxx.com/handsets.php?hs=$id'><img src='http://www.xxxx.com/images/network$network.gif' border='0' align='right'></a></font></td></tr><tr><td width='10%' valign='top'><font style='FONT-SIZE: 9pt' face='Tahoma'><b><span style='TEXT-DECORATION: none'><font color='#2d75d7'><a href='http://www.xxxx.com/handsets.php?hs=$id'><img alt='$handset' src='http://www.xxxx.com/images/handset_$id.jpg' border='0'></a></font></span></b></font></td><td width='83%' colspan='2'><font color='#2D75D7' face='Tahoma'><span style='font-size: 9pt'>$gift</span></font><p><font color='#2D75D7' face='Tahoma'><span style='font-size: 9pt'>$tariff<br><b>$xnetmin$offpeakmin</b><br>$textsmin</span></font></td></tr></table>");
      echo(
    "<br /><br /></th>"); 
      
    $j++; 
      if((
    $j 4) == 0) echo("</tr>\n<tr>"); 

    echo(
    "</tr></table>"); 
    mysql_close();
    ?>

  9. #9
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Hmm.
    It all looks fine to me.
    Are you sure that the $getmake variable is set? Did you have (eg) ?make=xxxxxx on the end of the url?
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  10. #10
    Join Date
    Aug 2005
    Posts
    174
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    yeah, used ?make=nokia

    also tried a few others, but still get the error

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
  •