开发者

echo font green if field1 > field2?

开发者 https://www.devze.com 2023-03-10 10:35 出处:网络
I\'ve done some research and coding to be able to make this functi开发者_运维问答on: (if field1 > field 2 echo font color=green else if field 2 > field 1 echo fontcolor=green else echo font b

I've done some research and coding to be able to make this functi开发者_运维问答on:

 (if field1 > field 2 echo font color=green else if field 2 > field 1 echo fontcolor=green else echo font black

(the double green has its purpose)

   $query = "SELECT *  FROM table WHERE CATEGORY='15' ";
$result = mysql_query($query); 

 If ($result [field1] > $result [field2] )

 echo "    <style font="green"/>   This category is green </style> "

else if ($result [field1] > $result [field2] )


echo "    <style font="green"/>   This category is green </style> "

else

echo "    <style font="black"/>   This category is black </style> "

Is this how its done? Are there any errors?


This is what your code should look like:

$sql = "SELECT *  FROM table WHERE CATEGORY='15' ";
$query = mysql_query($sql);
$result = mysql_fetch_assoc($query);

if($result['field1'] > $result['field2']){
    echo '<span style="color:green;">This category is green </span>';
}
else{
    echo '<span style="color:black;">This category is black</span>';
}

I'd recommend that you read up some more on PHP before you ask questions like this.


How about....

if ($result['field1'] != $result['field2'])
  echo '<span style="color:green;">This category is green</span>';
else
  echo '<span style="color:black;">This category is black</span>';

Since saying if 1>2 or 2>1 is the same as not equal

-- shorter:

$color = ($result['field1'] != $result['field2'] ? 'green' : 'black';
printf('<span style="color:%s;">This category is %s</span>',$color,$color);


Use

<span style="color:green;">This category is green</span>

and

<span style="color:black;">This category is black</span>


Well there are some errors:

$query = "SELECT *  FROM table WHERE CATEGORY='15' ";
$result = mysql_query($query); 
// 1. you need the row, not the result
$row = mysql_fetch_assoc($result);

if ($row['field1'] > $row['field2'])
{
    // 2. style tag is not to be used here, try span, and close the lines with ;
    echo "    <span style="color: green;">This category is green </span>";
}
// 3. here you should test for "lest than"
elseif ($row['field1'] < $row['field2'])
{
    echo "    <span style="color: green;">This category is green </span>";
}
else
{
    echo "    <span style="color: black;">This category is black </span>";
}


No, this is not even close to how you would do that. After you execute a mysql query in PHP, you will always have to fetch the results. This is how your code could look (I am assuming that your query will return a single result, not multiple):

$query = "SELECT *  FROM table WHERE CATEGORY='15' ";
$res = mysql_query($query) or die(mysql_errno();

$arr = mysql_fetch_assoc($res);

If ($result['field1'] > $result ['field2'])
{
   echo '<span style="color: green;"/>This category is green</span>';
}
else if ($result['field1'] > $result['field2'])
{
    echo '<span style="color: green;"/>This category is green</span>';
}
else
{
    echo '<span style="color: black;"/>This category is black </span>";
}

So, in summary, the query wasn't fetched, the conditional statements were okay, but, since you seem new to this, will likely cause issues if you don't properly close them and the HTML was incorrect (looked like CSS).

If you need any more help, let me know, I can help you further.


When your mysql call returns something from the database it's not immediately able to be manipulated. This is because the query returns a 'resource', not an array. You first have to turn the resource into an array, using mysql_fetch_array($result) before you can go through that array to get to the information you want. You can then use the column names inside the brackets to get to the data you want in any particular row.

Your code should then look something like this:

$sql = "SELECT *  FROM table WHERE CATEGORY='15'";
$query = mysql_query($sql);//$query is now the resource
$result = mysql_fetch_assoc($query); //$result is now an array built from the resource

Now, you have a number of errors in logic. The first and second if statements, as others have noted, are the same. Now that you're actually able to check on the data, you need to properly write the if statement. Something like this:

if($result['field1']>$result['field2']){
echo "<span style='color:green;'>   This category is green </span>";
}else{
echo "<span style='color:black;'> This category is black </span>";
}

Notice that when you echo anything, after you get done with the echo, you have to close it with a semicolon ;. Also, if you're wanting to fix the stylings for these particular words, it may be best to just use a <span> tag and put them inside of it. Also, when you're declaring styles inside a tag, you must do it in the form of style='characteristic:value;'

Lastly, when you're inside of an echo statement with ", you have to use single quotes ', otherwise it closes the echo.

Hope my novel helps! :P

0

精彩评论

暂无评论...
验证码 换一张
取 消