Using mysql output

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pyoungson
Forum Newbie
Posts: 18
Joined: Thu Aug 09, 2007 5:35 am

Using mysql output

Post by pyoungson »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I know you might think that this should be in the database forum but I think it is more of a php question.  I have managed to obtain the numbers I need from a mysql database but now I have no idea how to perform the calculation I want on them.

The html is very simple and lets a user select which solvents they want via a checklist.

[syntax="html"]

<html>
<head>
  <title>Solvent Calculations</title>
</head>
<body>
  <form name="myform" action="process1.php" method="POST">
    <input type="hidden" name="check_submit" value="1" />
   
    <br />
    Choose the solvents:
      <input type="checkbox" name="Solvents[]" value="1,1-dimethylhydrazine" checked="checked" /> 1,1-dimethylhydrazine
      <input type="checkbox" name="Solvents[]" value="isobutene" /> isobutene
      <input type="checkbox" name="Solvents[]" value="2-ethoxyethyl acetate" /> 2-ethoxyethyl acetate
      <input type="checkbox" name="Solvents[]" value="methyl tert-butyl ether" /> methyl tert-butyl ether
    <br /><br />

    <input type="submit" />
  </form>
</body>
</head>
</html>

The php then matches the inputs to a mysql database which lists the names of the solvents and three coefficients for each one (dispersive, polar and hbonding). The php (below) displays the selected solvents with their respective coefficients but what I really want is to find the maximum dispersive coefficient for the selected solvents.[/syntax]

Code: Select all

<?php

if (array_key_exists('check_submit', $_POST)) {
   
   if ( isset($_POST['Solvents']) ) { 
     $_POST['Solvents'] = implode('\', \'', $_POST['Solvents']); 
   }

   echo "Solvents you chose: {$_POST['Solvents']}<br />";
} else {
    echo "You can't see this page without submitting the form.";
}

$con = mysql_connect("localhost","root");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("solvents", $con);

$result = mysql_query("SELECT * FROM solvents WHERE Name 

IN('{$_POST['Solvents']}')");

while($row = mysql_fetch_array($result))
  {
  echo $row['Name'] . " " . $row['Dispersive'] . " " . $row['Polar'] . " " . 

$row['Hbonding'];
  echo "<br />";
  }

?>
I tried using the max() function on $result but all this gives me is the max for each row not for each column. What I want is the maximum dispersive component, maximum polar component and maximum hbonding component for the selected solvents.

I would be very grateful for an answer to this, thank you for your time.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
pyoungson
Forum Newbie
Posts: 18
Joined: Thu Aug 09, 2007 5:35 am

Post by pyoungson »

Sorry I thought I had done that correctly, I will read up on it.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Post by califdon »

I'm unclear about what you want to display. You have several checkboxes on a form, the user can check one or more of them, then you look up values in a database table, and then what? Do you want to display only the one with the highest dispersive value? Or do you want to display that one first? Or do you want to display all of them, perhaps rendering the highest dispersive value in boldface? I'm confused.
Rovas
Forum Contributor
Posts: 272
Joined: Mon Aug 21, 2006 7:09 am
Location: Romania

Post by Rovas »

In the MySQL table are the values you want for that type of solvent that the user wants. From want I understand you already used the sql operator Max.
Did you do it like here.
If you want to this using PHP is a little more complicated.
Post Reply