问候!这是我的第一个问题。
我在网上搜索过以及搜索到了SO,但我认为找到答案对我来说太具体了。我猜我的代码可能需要逻辑重建。
我正在为朋友的业务构建一个包含用户可编辑的库存数据(已用RV的列表)的简单站点。这基本上是我的第一个有PHP的大项目,嗯,永远,我的时间紧迫(我们现在正处于季节!),所以我的代码是绝对的污秽。除此之外,我正在混合程序和面向对象的编程,就像一个nutjob。
MySQL表“rv_list”中的一些数据 - 即'status'和'condition'字段(参见随附的屏幕截图以获得更清晰的想法),目前存储为1位整数即可。
我认为(稍后使用php regex或其他东西)很容易将这些数字转换为字符串,因为它们在输入表单上。事实证明,通过最终确定输出数据的方法,我在select语句完成后找不到转换它们的方法。
<?php
//unfinished query - "condition" is the column in the db table that I'd like to change, "status" will also need to be changed in a similar way for the outward-facing public-viewed list
$query = 'select * from rv_list where status="1" or status="2" order by modified desc';
$rvfieldquery = 'show columns from rv_list';
$result = $conn->query($query);
//check on the status of our query
if ($result)
{
//begin output of html table, followed by a loop for rows, and within, a nested loop for the datacells.
//based on function 3: http://www.barattalo.it/2010/01/29/10-php-usefull-functions-for-mysqli-improved-stuff/
echo '<table>';
echo '<th>Edit Status</th>';
while ($field = $result->fetch_field())
{
echo '<th class="columntitle">'.$field->name.'</th>';
}
echo '<th>DELETE</th>';
$shadecounter = 0;
while ($row = $result->fetch_assoc())
{
//shade every other row for usability
$shadecounter++;
if (is_float($shadecounter/2)) $shade = "lightgray";
else $shade = "white";
echo '<tr style="background:'.$shade.';">';
//Get rv_id key, attach to name of edit and delete buttons
echo '<td><input type="submit" name="edit '.$row['rv_id'].'" value="Edit"></input></td>';
//Insert row data
foreach ($row as $td)
{
//PROBLEM AREA HERE! - can I do an if statement or something here to check what column $td comes from? Confused.
echo '<td class="data">'.$td.'</td>';
}
//for delete record button, off-screen on right side of each record
echo '<td><input style="background:red;" type="submit" name="del '.$row['rv_id'].'" value="DELETE"></input></td>';
echo '</tr>';
}
echo '</table>';
}
else
{
exit;
}
http://i.stack.imgur.com/N8P8r.jpg
状态整数列表的键(相反,我希望它显示的内容):
条件整数列表的关键字:
如果我忘记了描述这个问题的任何重要信息,请告诉我。
最后但同样重要的是,非常感谢非常您花在阅读此问题上的时间!
答案 0 :(得分:0)
$status_map = array(1 => 'Available', 2 => 'On Hold', 3 => 'Sold / Deactivated');
$condition_map = array(1 => 'New', 2 => 'Used - Excellent', 3 => 'Used - Good', 4 => 'Used - Fair', 5 => 'Used - Poor');
foreach ($row as $colname => $value)
{
if($colname == 'status')
$value = $status_map[$value];
if($colname == 'condition')
$value = $condition_map[$value];
echo '<td class="data">'.$value.'</td>';
}