我正在制作一个“color-me-table”,其中我有一个单元格网格,用户选择一个单元格,然后可以决定将该单元格中的颜色更改为什么。我把桌子放下了,但是我并不真正理解会话。我本来可以问一个关于会话的一般性问题,但我认为这个例子可能更有意义。这是我的表,没有任何实际功能:
<?php
session_start() ;
if(!isset($_SESSION['color_data'])){
$_SESSION['color_data']=array(
array("#999999", "#999999", "#999999", "#999999", "#999999", "#999999", "#999999", "#999999", "#999999", "#999999"),
array("#999999", "#999999", "#999999", "#999999", "#999999", "#999999", "#999999", "#999999", "#999999", "#999999"),
array("#999999", "#999999", "#999999", "#999999", "#999999", "#999999", "#999999", "#999999", "#999999", "#999999"),
array("#999999", "#999999", "#999999", "#999999", "#999999", "#999999", "#999999", "#999999", "#999999", "#999999"),
array("#999999", "#999999", "#999999", "#999999", "#999999", "#999999", "#999999", "#999999", "#999999", "#999999"),
array("#999999", "#999999", "#999999", "#999999", "#999999", "#999999", "#999999", "#999999", "#999999", "#999999"),
array("#999999", "#999999", "#999999", "#999999", "#999999", "#999999", "#999999", "#999999", "#999999", "#999999"),
array("#999999", "#999999", "#999999", "#999999", "#999999", "#999999", "#999999", "#999999", "#999999", "#999999"),
array("#999999", "#999999", "#999999", "#999999", "#999999", "#999999", "#999999", "#999999", "#999999", "#999999"),
array("#999999", "#999999", "#999999", "#999999", "#999999", "#999999", "#999999", "#999999", "#999999", "#999999")
);
}
switch($_POST['col']) {
case "0" :
case "1" :
case "2" :
case "3" :
case "4" :
case "5" :
case "6" :
case "7" :
case "8" :
case "9" :
$col = $_POST['col'] ;
}
switch($_POST['row']) {
case "0" :
case "1" :
case "2" :
case "3" :
case "4" :
case "5" :
case "6" :
case "7" :
case "8" :
case "9" :
$row = $_POST['row'] ;
}
switch($_POST['r']) {
case "00" :
case "33" :
case "66" :
case "99" :
case "CC" :
case "FF" :
$r = $_POST['r'] ;
}
switch($_POST['g']) {
case "00" :
case "33" :
case "66" :
case "99" :
case "CC" :
case "FF" :
$g = $_POST['g'] ;
}
switch($_POST['b']) {
case "00" :
case "33" :
case "66" :
case "99" :
case "CC" :
case "FF" :
$b = $_POST['b'] ;
}
$color_data= $_SESSION['color_data'] ;
if ($r && $g && $b) {
$color_data[$_POST["row"]][$_POST["col"]]= "#$r$g$b";
}
if (!is_null($r) && !is_null($g) && !is_null($b) && !is_null($row) && !is_null($column) ) {
$color_value = "#$r$g$b" ;
$color_data[] = array($row, $column, $color_value) ;
}
$_SESSION['color_data'] = $color_data ;
?><html>
<head>
<title>Color Me Table</title>
</head>
<body><?php echo $color_value ; ?>
<form action="assignment4.php" method="POST">
<table>
<tr>
<td width="20"></td>
<td><input type="radio" name="col" value="0">0</td>
<td><input type="radio" name="col" value="1">1</td>
<td><input type="radio" name="col" value="2">2</td>
<td><input type="radio" name="col" value="3">3</td>
<td><input type="radio" name="col" value="4">4</td>
<td><input type="radio" name="col" value="5">5</td>
<td><input type="radio" name="col" value="6">6</td>
<td><input type="radio" name="col" value="7">7</td>
<td><input type="radio" name="col" value="8">8</td>
<td><input type="radio" name="col" value="9">9</td>
</tr>
<tr>
<td><input type="radio" name="row" value="0">0</td>
<td width="20" bgcolor="#999999">00</td>
<td width="20" bgcolor="#999999">01</td>
<td width="20" bgcolor="#999999">02</td>
<td width="20" bgcolor="#999999">03</td>
<td width="20" bgcolor="#999999">04</td>
<td width="20" bgcolor="#999999">05</td>
<td width="20" bgcolor="#999999">06</td>
<td width="20" bgcolor="#999999">07</td>
<td width="20" bgcolor="#999999">08</td>
<td width="20" bgcolor="#999999">09</td>
</tr>
<tr>
<td><input type="radio" name="row" value="1">1</td>
<td width="20" bgcolor="#999999">10</td>
<td width="20" bgcolor="#999999">11</td>
<td width="20" bgcolor="#999999">12</td>
<td width="20" bgcolor="#999999">13</td>
<td width="20" bgcolor="#999999">14</td>
<td width="20" bgcolor="#999999">15</td>
<td width="20" bgcolor="#999999">16</td>
<td width="20" bgcolor="#999999">17</td>
<td width="20" bgcolor="#999999">18</td>
<td width="20" bgcolor="#999999">19</td>
</tr>
<tr>
<td><input type="radio" name="row" value="2">2</td>
<td width="20" bgcolor="#999999">20</td>
<td width="20" bgcolor="#999999">21</td>
<td width="20" bgcolor="#999999">22</td>
<td width="20" bgcolor="#999999">23</td>
<td width="20" bgcolor="#999999">24</td>
<td width="20" bgcolor="#999999">25</td>
<td width="20" bgcolor="#999999">26</td>
<td width="20" bgcolor="#999999">27</td>
<td width="20" bgcolor="#999999">28</td>
<td width="20" bgcolor="#999999">29</td>
</tr>
<tr>
<td><input type="radio" name="row" value="3">3</td>
<td width="20" bgcolor="#999999">30</td>
<td width="20" bgcolor="#999999">31</td>
<td width="20" bgcolor="#999999">32</td>
<td width="20" bgcolor="#999999">33</td>
<td width="20" bgcolor="#999999">34</td>
<td width="20" bgcolor="#999999">35</td>
<td width="20" bgcolor="#999999">36</td>
<td width="20" bgcolor="#999999">37</td>
<td width="20" bgcolor="#999999">38</td>
<td width="20" bgcolor="#999999">39</td>
</tr>
<tr>
<td><input type="radio" name="row" value="4">4</td>
<td width="20" bgcolor="#999999">40</td>
<td width="20" bgcolor="#999999">41</td>
<td width="20" bgcolor="#999999">42</td>
<td width="20" bgcolor="#999999">43</td>
<td width="20" bgcolor="#999999">44</td>
<td width="20" bgcolor="#999999">45</td>
<td width="20" bgcolor="#999999">46</td>
<td width="20" bgcolor="#999999">47</td>
<td width="20" bgcolor="#999999">48</td>
<td width="20" bgcolor="#999999">49</td>
</tr>
<tr>
<td><input type="radio" name="row" value="5">5</td>
<td width="20" bgcolor="#999999">50</td>
<td width="20" bgcolor="#999999">51</td>
<td width="20" bgcolor="#999999">52</td>
<td width="20" bgcolor="#999999">53</td>
<td width="20" bgcolor="#999999">54</td>
<td width="20" bgcolor="#999999">55</td>
<td width="20" bgcolor="#999999">56</td>
<td width="20" bgcolor="#999999">57</td>
<td width="20" bgcolor="#999999">58</td>
<td width="20" bgcolor="#999999">59</td>
</tr>
<tr>
<td><input type="radio" name="row" value="6">6</td>
<td width="20" bgcolor="#999999">60</td>
<td width="20" bgcolor="#999999">61</td>
<td width="20" bgcolor="#999999">62</td>
<td width="20" bgcolor="#999999">63</td>
<td width="20" bgcolor="#999999">64</td>
<td width="20" bgcolor="#999999">65</td>
<td width="20" bgcolor="#999999">66</td>
<td width="20" bgcolor="#999999">67</td>
<td width="20" bgcolor="#999999">68</td>
<td width="20" bgcolor="#999999">69</td>
</tr>
<tr>
<td><input type="radio" name="row" value="7">7</td>
<td width="20" bgcolor="#999999">70</td>
<td width="20" bgcolor="#999999">71</td>
<td width="20" bgcolor="#999999">72</td>
<td width="20" bgcolor="#999999">73</td>
<td width="20" bgcolor="#999999">74</td>
<td width="20" bgcolor="#999999">75</td>
<td width="20" bgcolor="#999999">76</td>
<td width="20" bgcolor="#999999">77</td>
<td width="20" bgcolor="#999999">78</td>
<td width="20" bgcolor="#999999">79</td>
</tr>
<tr>
<td><input type="radio" name="row" value="8">8</td>
<td width="20" bgcolor="#999999">80</td>
<td width="20" bgcolor="#999999">81</td>
<td width="20" bgcolor="#999999">82</td>
<td width="20" bgcolor="#999999">83</td>
<td width="20" bgcolor="#999999">84</td>
<td width="20" bgcolor="#999999">85</td>
<td width="20" bgcolor="#999999">86</td>
<td width="20" bgcolor="#999999">87</td>
<td width="20" bgcolor="#999999">88</td>
<td width="20" bgcolor="#999999">89</td>
</tr>
<tr>
<td><input type="radio" name="row" value="9">9</td>
<td width="20" bgcolor="#999999">90</td>
<td width="20" bgcolor="#999999">91</td>
<td width="20" bgcolor="#999999">92</td>
<td width="20" bgcolor="#999999">93</td>
<td width="20" bgcolor="#999999">94</td>
<td width="20" bgcolor="#999999">95</td>
<td width="20" bgcolor="#999999">96</td>
<td width="20" bgcolor="#999999">97</td>
<td width="20" bgcolor="#999999">98</td>
<td width="20" bgcolor="#999999">99</td>
</tr>
</table>
<table border="1" >
<tr>
<th>Red</th>
<th>Green</th>
<th>Blue</th>
</tr>
<tr>
<td>
<select name="r" >
<option value="FF" >100%</option>
<option value="CC" >80%</option>
<option value="99" >60%</option>
<option value="66" >40%</option>
<option value="33" >20%</option>
<option value="00" >0%</option>
</select>
</td>
<td>
<select name="g" >
<option value="FF" >100%</option>
<option value="CC" >80%</option>
<option value="99" >60%</option>
<option value="66" >40%</option>
<option value="33" >20%</option>
<option value="00" >0%</option>
</select>
</td>
<td>
<select name="b" >
<option value="FF" >100%</option>
<option value="CC" >80%</option>
<option value="99" >60%</option>
<option value="66" >40%</option>
<option value="33" >20%</option>
<option value="00" >0%</option>
</select>
</td>
</tr>
</table>
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
答案 0 :(得分:0)
修改强>
我误解了问题的原始意图,但在再次审阅之后,这是一个可接受的会话用例,因为假设数据持久性只是暂时的。如果您在服务器上检查实际会话文件(假设基于文件的会话存储),则AoA数据结构以序列化形式存储,因此这将允许用户导航网站的其他区域并仍保留原始状态color-me-table选择,直到会话超时。
<强> ORIGINAL 强>
变量名称存在一些问题,即$ col,$ column。
$ _POST数组正在检查名为“col”的HTML输入表单变量。但是,代码中有两个部分引用名为“$ column”的变量。由于$ column不存在,if语句块中检查is_nulls的代码永远不会被执行。我将$ column重命名为$ col,并在表单提交后在页面顶部收到HTML十六进制颜色代码。