How to find percentage of result with php+mysql ?

Discussion in 'MySQL' started by Amilo, Oct 17, 2006.

  1. #1
    I am trying to display a percentage of a result on my webpage.

    I have managed to get the result numers of: 50.0 and 90.0

    But do not know the code to add to the query to take off 20% so I can also display the numbers:40.0 and 72.0 ?
     
    Amilo, Oct 17, 2006 IP
  2. Phynder

    Phynder Well-Known Member

    Messages:
    2,603
    Likes Received:
    145
    Best Answers:
    0
    Trophy Points:
    178
    #2
    $p = 50;

    $discounted_price = $p - ($p*.20);

    $p is the pre discount price
     
    Phynder, Oct 17, 2006 IP
  3. Amilo

    Amilo Peon

    Messages:
    624
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Many thanks Phynder;)
     
    Amilo, Oct 17, 2006 IP
  4. Amilo

    Amilo Peon

    Messages:
    624
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I have managed to get this working on all pages except the results page of a search form,anyone any ideas ?

    This is my results page:

     $search = "%" . $_POST["search"] . "%";
    
      $result = "SELECT **,***,** FROM members WHERE ** LIKE '$search'";
      $result = mysql_query ($result);
    
    
        {
        while ($row = mysql_fetch_array ($result)) {
    
          echo $row[0] . "<b><br>";
     echo $row[1] ."\$<b><br>" ;
    echo $row[2] ."\$<b><br>" ;
    echo $row[3] ."<b><br><hr>" ;
    
        }
      }
    
    ?>
    PHP:
    I managed to get it working on all other pages like this:

    
    $query="SELECT BAAR,foo FROM `mydatabase`ORDER BY foo DESC LIMIT 5";
    
    $result=mysql_query($query);
    
    $num=mysql_numrows($result);
    
    
    mysql_close();
    
    echo "<b><center></center></b><br><br>";
    
    $i=0;
    while ($i < $num) {
    
    
    $domain=mysql_result($result,$i,"BAAR");
    $cost=mysql_result($result,$i,"foo");
    echo "<b>$domain<br>HOW MUCH($)$cost<hr><br>";
    $p = "$cost";
    $per = $p - ($p*.20);
    $L = "$cost";
    $low = $L - ($L*.80);
    $i++;
    }
    
    ?>
    
    PHP:
    As you can see above the 20% and 80% is not stored on the database I just need to do the math from the $cost result.
    The problem I have is I am getting confused trying to play with the echo $row results in example one above.

    Do I need to query the echo $row[1] result ?
    ($row[1]) is $cost
     
    Amilo, Oct 18, 2006 IP
  5. harsh789

    harsh789 Member

    Messages:
    29
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #5
    You can put math in sql itself and display results directly using echo $row[0]

    example
     
    harsh789, Oct 18, 2006 IP