Php MySQL Select语句的表格数据修改(输出时)

Php MySQL Select语句的表格数据修改(输出时),php,mysql,datatable,Php,Mysql,Datatable,你好!这是我的第一个问题 我已经在网络上搜索过了,但我相信它太具体了,我找不到答案。我猜我的代码可能需要逻辑重建 问题背景: 我正在建立一个简单的网站,其中包含用户可编辑的库存数据(一个使用过的房车列表),用于朋友的业务。这基本上是我第一个使用php的大项目,好吧,有史以来,而且我的最后期限很紧(我们现在正赶上发布季节!),所以我的代码非常糟糕。除此之外,我还把过程编程和面向对象编程混为一谈 问题说明: MySQL表“rv_list”中的一些数据,即“status”和“condition”字段(

你好!这是我的第一个问题

我已经在网络上搜索过了,但我相信它太具体了,我找不到答案。我猜我的代码可能需要逻辑重建

问题背景: 我正在建立一个简单的网站,其中包含用户可编辑的库存数据(一个使用过的房车列表),用于朋友的业务。这基本上是我第一个使用php的大项目,好吧,有史以来,而且我的最后期限很紧(我们现在正赶上发布季节!),所以我的代码非常糟糕。除此之外,我还把过程编程和面向对象编程混为一谈

问题说明: MySQL表“rv_list”中的一些数据,即“status”和“condition”字段(请参阅随附的屏幕截图以获得更清晰的信息),目前存储为1位整数

我认为(使用php正则表达式或其他东西)以后将这些数字转换为字符串会非常容易,因为它们在输入表单上。事实证明,使用我最终决定输出数据的方法,在select语句完成后,我找不到转换它们的方法

代码:
$status\u map=array(1=>“可用”,2=>“保留”,3=>“已售出/停用”);
$condition_map=array(1=>'New',2=>'Used-Excellent',3=>'Used-Good',4=>'Used-Fair',5=>'Used-Poor');
foreach($colname=>$value的行)
{
如果($colname=='status')
$value=$status_map[$value];
如果($colname=='condition')
$value=$condition_map[$value];
回显“.$value.”;
}
完美!这正是我需要的!非常感谢。看起来我需要研究数组和foreach()函数。
<?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;
}
$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>';
}