简体   繁体   English

php custum sql返回值

[英]php custum sql return value

I have a data table in which I return value from sql query. 我有一个数据表,其中我从sql查询返回值。 Now there is one problem. 现在有一个问题。 I want to return not actual sql value but custum parameter which matches with sql value db. 我想返回的不是实际的sql值,而是返回与sql值db匹配的custum参数。 For example, I have a state value which is in sql database described as D - done, N - new, P - in progress. 例如,我有一个状态值,该值在sql数据库中描述为D-完成,N-新,P-进行中。 So in datable table I want to see these values instead of these three letters. 因此,在datable表中,我想查看这些值而不是这三个字母。 My php code fragment: 我的PHP代码片段:

        $data = "";
        $array = db_get_task();
        if ($array != null) 
        {
            for ($i = 0; $i < count($array); $i++)
            {
                $data .= '<tr>
                    <td>' . $array[$i]['task'] . '</td>
                    <td>' . $array[$i]['id'] . '</td>
                    <td>' . $array[$i]['cust'] . '</td>
                    <td>' . $array[$i]['City'] . '</td>
                    <td>' . $array[$i]['STATE'] . '</td>   //this line returns D, N or P values
                    <td>' . $array[$i]['date'] . '</td>
                    </tr>';
            }
        }

By the way. 顺便说说。 In database these letters use varchar type 在数据库中,这些字母使用varchar类型

try this 尝试这个

 $data = "";

 $values = array('D' => 'done', 'N' => 'new', 'P' => 'in progress');

 $array = db_get_task();

    if ($array != null) 
    {
        for ($i = 0; $i < count($array); $i++)
        {
            $data .= '<tr>
                <td>' . $array[$i]['task'] . '</td>
                <td>' . $array[$i]['id'] . '</td>
                <td>' . $array[$i]['cust'] . '</td>
                <td>' . $array[$i]['City'] . '</td>
                <td>' . $values[$array[$i]['STATE']] . '</td>   //this line returns done, new or in progress values
                <td>' . $array[$i]['date'] . '</td>
                </tr>';
        }
    }

Try: 尝试:

$states = array('D' => 'done', 'N' => 'new', 'P' => 'in progress');

$data = "";
$array = db_get_task();
if ($array != null) 
{
    for ($i = 0; $i < count($array); $i++)
    {
        $data .= '<tr>
                <td>' . $array[$i]['task'] . '</td>
                <td>' . $array[$i]['id'] . '</td>
                <td>' . $array[$i]['cust'] . '</td>
                <td>' . $array[$i]['City'] . '</td>
                <td>' . if(array_key_exists($array[$i]['STATE'], $states) { $states[$array[$i]['STATE']]; }. '</td>
                <td>' . $array[$i]['date'] . '</td>
                </tr>';
    }
}


Explanation: 说明:

Create a custom associative array for all the states . 为所有states创建一个custom associative array The if the value (viz. D, N, P) exists as the key in the array , get the value . 所述,如果value (即d,N,P)中存在的key的在array ,得到的value

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

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