php mysql echo |如何继承提交表单中的颜色? [英] php mysql echo | How to inherit colors from a submit form?

查看:162
本文介绍了php mysql echo |如何继承提交表单中的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些真正的麻烦找到关于这个话题的信息,我会非常感谢任何帮助。总之,我有一个表单,用户从下拉列表中选择一个类别,输入一些内容,并点击提交到SQL。下拉菜单中的每个类别都用颜色编码:

I'm having some real trouble finding information on this topic and I would be very appreciative for any help. In short I have a form where users select a category from a drop down list, enter some contents, and hit submit which goes to SQL. Each category in the dropdown is color coded:

<option STYLE="color: #00CC66;" value="Option_1">Option_1</option>
<option STYLE="color: #0066CC;" value="Option_2">Option_2</option>
<option STYLE="color: #996633;" value="Option_3">Option_3</option>

,将存储的提交数据(类别和内容)拉到按照日期排序的同一页上的表中。

Then I have a php that pulls up the stored submitted data (categories and contents) into a table on that same page sorted by date.

<?php
$con=mysqli_connect("localhost","myuser","mypassword","mydb");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$result = mysqli_query($con,"SELECT * FROM mytable order by date DESC");

echo "<table border='1'>
<tr>
<th>Category</th>
<th>Contents</th>
<th>Date/Time</th>
</tr>";

while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['category'] . "</td>";
  echo "<td>" . $row['contents'] . "</td>";
  echo "<td>" . $row['date'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysqli_close($con);
?> 

我的问题是,当echo将信息放在表中时,类别以与用户表单相同的颜色显示? (IE,在表上,Option_1将显示为#00CC66,选项2为0066CC等)

My question is, when echo places the information in the table, is there a way I can get 'category' showing up in the same colors as the user form? (IE, on the table, Option_1 would show up as #00CC66, Option 2 as 0066CC, etc...)

基本上我想要获取的实际类别文本表以显示与下拉表单中的相同。我不介意,如果我需要手动设置每一个类别是有限的,我只是没有线索从哪里开始这一个。预先感谢任何帮助!

Basically I want the actual category text on the fetched table to display the same as it is in the drop down form. I don't mind if I need to manually set each one as the categories are limited, I just have no clue where to begin on this one. Appreciate any help in advance!

推荐答案

是的,但是您需要将选择框的值更改为颜色,手动做这样:

Yes, but you would need to either change the value of the select box to the colour or manually do it like this:

function getColor($strOption)
{
   switch ($strOption)
   {
       case "Option_1":
       return "#00CC66";

       case "Option_2":
       return "#0066CC";
       #etc
    }
}

while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td><font color='".getColor($row['category'])."'> " . $row['category'] . "</font></td>";
  echo "<td>" . $row['contents'] . "</td>";
  echo "<td>" . $row['date'] . "</td>";
  echo "</tr>";
  }

这篇关于php mysql echo |如何继承提交表单中的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆