By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,830 Members | 2,276 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,830 IT Pros & Developers. It's quick & easy.

Sorting Data into a table using to if conditions

P: 28
Man, I need help. :D

I have a database and the table has 4 fields.

ident, chroma, hue, value

I have the script to display ALL the data into the table. But what I am looking to do is to set a condition for displaying only data i want.

For example:

Only list the items in the table that fit this condition:
chroma=7
value=9
Here is my current code that just displays the data in a table:

[PHP]<?
include("XXXXXXXX.php");
mysql_connect($dbhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM rembrandt";
$result=mysql_query($query);

$num=mysql_numrows($result);



mysql_close();

echo "<b><center>Database Output</center></b><br><br>";

?>
<table border="1" cellspacing="6" cellpadding="6">
<tr>
<th><font face="Arial, Helvetica, sans-serif">Pastel Identifier</font></th>
<th><font face="Arial, Helvetica, sans-serif">Hue</font></th>
<th><font face="Arial, Helvetica, sans-serif">Chroma</font></th>
<th><font face="Arial, Helvetica, sans-serif">Value</font></th>
</tr>

<?
$i=0;
while ($i < $num) {
$ident=mysql_result($result,$i,"ident");
$hue=mysql_result($result,$i,"hue");
$chroma=mysql_result($result,$i,"chroma");
$value=mysql_result($result,$i,"value");
?>

<tr>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$ident"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$hue"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$chroma"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$value"; ?></font></td>
</tr>

<?

$i++;

}

echo "</table>";

?>[/PHP]

I have tried putting this IF statement in but with no success as I really don't know where to put it.
if ( $chroma=7 && $value=9 ) {
Can someone help me here.

Thank you!!!!!
Feb 22 '08 #1
Share this Question
Share on Google+
12 Replies


ronverdonk
Expert 2.5K+
P: 4,258
Why not put the IF in the SELECT statement like
Expand|Select|Wrap|Line Numbers
  1. $query="SELECT * FROM rembrandt WHERE chroma=7 AND value=9";
  2.  
Ronald
Feb 22 '08 #2

P: 28
I did that and it does work, but I have a bigger plan in mind as well.

Eventually, I want to have a main page with three drop downs:
Brand, Colour, Value
Where "Brand" in the TABLE in the data base. In my script it is calling 'rembrandt'.

Colour=Chroma
This will be a bit of an issue for me as the data in the database is integer based and the colour will be defined based on the range of chroma. So RED would equal a chroma of between 0 and 10. Orange between 11 and 22. I need to figure out how to code that as well.
Value=value

After the user selects what they want and hits the SUBMIT button they will be taken to a page that will display the chosen results in a table.

I thought I would start off small at first.

But if there are suggestions for implementing that code as well, I would very happy! :D
Feb 22 '08 #3

ronverdonk
Expert 2.5K+
P: 4,258
Ok, let's assume that the user-entered values are in $chroma_value and in $value_value. Then your table makeup will be something like[php]
if ($chroma == $chroma_value AND $value == $value_value) {
?>
<tr>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$ident"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$hue"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$chroma"; ?>/font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$value"; ?></font></td>
</tr>
<?php
}
[/php]
Is this what you have been looking for?

Ronald
Feb 22 '08 #4

P: 28
Ok, let's assume that the user-entered values are in $chroma_value and in $value_value. Then your table makeup will be something like[php]
if ($chroma == $chroma_value AND $value == $value_value) {
?>
<tr>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$ident"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$hue"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$chroma"; ?>/font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$value"; ?></font></td>
</tr>
<?php
}
[/php]
Is this what you have been looking for?

Ronald
Worked great! Thanks!!

Would you be willing to help me on the drop downs and how that data gets put into that script? See my next post...
Feb 22 '08 #5

P: 28
In my form HTML I have:
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <body>
  3.  
  4. <form action="value-3.php" method="get">
  5. Brand:
  6. <select name="brand">
  7. <option value="Rembrant">Rembrant</option>
  8. <option value="Unison">Unison</option>
  9. </select>
  10.  
  11. Colour:
  12. <select name="colour">
  13. <option value="Red">Red</option>
  14. <option value="Orange">Orange</option>
  15. <option value="Yellow">Yellow</option>
  16. <option value="Green">Green</option>
  17. <option value="Blue">Blue</option>
  18. <option value="Violet">Violet</option>
  19. <option value="Neutral">Neutral</option>
  20. </select>
  21.  
  22. Chroma:
  23. <select name="chroma">
  24. <option value="1">1</option>
  25. <option value="2">2</option>
  26. <option value="3">3</option>
  27. <option value="4">4</option>
  28. <option value="5">5</option>
  29. <option value="6">6</option>
  30. <option value="7">7</option>
  31. </select>
  32.  
  33. Value:
  34. <select name="value">
  35. <option value="1">1</option>
  36. <option value="2">2</option>
  37. <option value="3">3</option>
  38. <option value="4">4</option>
  39. <option value="5">5</option>
  40. <option value="6">6</option>
  41. <option value="7">7</option>
  42. <option value="8">8</option>
  43. <option value="9">9</option>
  44. </select>
  45. <input type="submit" />
  46. </form>
  47.  
  48. </body>
  49. </html>
  50.  
And I altered my PHP to:
[PHP]
<?
include("dbinfo.inc.php");
mysql_connect($dbhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM rembrandt";
$result=mysql_query($query);

$num=mysql_numrows($result);



mysql_close();

echo "<b><center>Database Output</center></b><br><br>";

?>
<table border="1" cellspacing="6" cellpadding="6">
<tr>
<th><font face="Arial, Helvetica, sans-serif">Pastel Identifier</font></th>
<th><font face="Arial, Helvetica, sans-serif">Hue</font></th>
<th><font face="Arial, Helvetica, sans-serif">Chroma</font></th>
<th><font face="Arial, Helvetica, sans-serif">Value</font></th>
</tr>

<?
$i=0;
while ($i < $num) {
$ident=mysql_result($result,$i,"ident");
$hue=mysql_result($result,$i,"hue");
$chroma=mysql_result($result,$i,"chroma");
$value=mysql_result($result,$i,"value");
if ($chroma==$_REQUEST["chroma"] AND $value==$_REQUEST["value"]) {
?>


<tr>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$ident"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$hue"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$chroma"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$value"; ?></font></td>
</tr>

<?
}

$i++;

}

echo "</table>";

?>
[/PHP]

Works great for that.

However I want to be able to search via COLOUR and VALUE more than anything. The VALUE search is easy but with COLOUR it is different.

I want the user to select either
RED, ORANGE, YELLOW, GREEN, BLUE, VIOLET

but letters are useless as the data for these colours are actually numerical in the database.

RED=1 to 10
Orange= 11 to 21
etc.... up to 100

How do i translate that into the PHP file?
Feb 22 '08 #6

ronverdonk
Expert 2.5K+
P: 4,258
Stick the numerical values in the SELECT list, like this[php]<select name="colour">
<option value="1,10">Red</option>
<option value="11,20">Orange</option>
<option value="21,30">Yellow</option>
<option value="31,40">Green</option>
<option value="41,50">Blue</option>
<option value="51,60">Violet</option>
<option value="61,70">Neutral</option>
</select>[/php]
Then read them both in via [php]$color_array=explode(',', $_POST['colour'];[/php]
Now you have 2 (min and max) in $color_array[0] and $color_array[1].
Checking it is something like (as you did with chroma and value):
[php] if ($hue >= $color_array[0] AND $hue <= $color_array[1]) {
.... etc
}[/php]
Ronald
Feb 22 '08 #7

P: 28
Got it thanks!!!

Now my next questions...

I want the POST data to tell which TABLE to look in. i thought it would be easy but I get an error when I do this...

[PHP]
<?
include("dbinfo.inc.php");
mysql_connect($dbhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM $_REQUEST["brand"]";
$result=mysql_query($query);

$num=mysql_numrows($result);

etc
etc
[/PHP]

it gives me this error:


Parse error: parse error, unexpected '\"', expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/content/m/r/k/mrking/html/pastel-sorting/value-3.php on line 5

Same thing when I try this:

[PHP]$query="SELECT * FROM ($_REQUEST["brand"])";[/PHP]

How do I tell the php area to sue the data from teh pull down?

Brand being:
Expand|Select|Wrap|Line Numbers
  1. Brand:
  2. <select name="brand">
  3. <option value="rembrant">Rembrant</option>
  4. <option value="unison">Unison</option>
  5. </select>
  6.  
Feb 22 '08 #8

ronverdonk
Expert 2.5K+
P: 4,258
You are having a double quoted string within a double quoted string. You either escase the double quotes or put the string within single quotes, i.e.[php]$query="SELECT * FROM {$_POST['db']}";[/php]Ronald
Feb 22 '08 #9

P: 28
You are having a double quoted string within a double quoted string. You either escase the double quotes or put the string within single quotes, i.e.[php]$query="SELECT * FROM {$_POST['db']}";[/php]Ronald
That doesn't seem to work for me. I now get this error...


Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource in /home/content/m/r/k/mrking/html/pastel-sorting/value-3.php on line 8
Database Output

I am thinking it can't execute numrows as it cannot find the correct table.
Feb 22 '08 #10

ronverdonk
Expert 2.5K+
P: 4,258
You database table is, of course, 'brand' so you must replace 'db' with 'brand' like
[php]$query="SELECT * FROM {$_POST['brand']}";
[/php]Ronald
Feb 22 '08 #11

P: 28
You database table is, of course, 'brand' so you must replace 'db' with 'brand' like
[php]$query="SELECT * FROM {$_POST['brand']}";
[/php]Ronald
hehe ;)

Yes that is what I put into it.

just found an typo in my other file. oops.

THANK YOU!!!
Feb 23 '08 #12

ronverdonk
Expert 2.5K+
P: 4,258
You are welcome.

Ronald
Feb 23 '08 #13

Post your reply

Sign in to post your reply or Sign up for a free account.