简体   繁体   English

PHP MySQL的回声 如何从提交表单继承颜色?

[英]php mysql echo | How to inherit colors from a submit form?

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. 简而言之,我有一个表单,用户可以从下拉列表中选择一个类别,输入一些内容,然后单击Submit进入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>

etc 等等

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,它将存储的已提交数据(类别和内容)拉到同一页上按日期排序的表中。

<?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);
?> 

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? 我的问题是,当echo将信息放置在表格中时,有没有办法让我以与用户表格相同的颜色显示“类别”? (IE, on the table, Option_1 would show up as #00CC66, Option 2 as 0066CC, etc...) (例如,在桌子上,IE_1会显示为#00CC66,选项2会显示为0066CC,等等。)

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
    }
}

Then in your while loop: 然后在您的while循环中:

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>";
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM