PHP数据库查询而不是使用0和1使用true或false / paid或unpaid

时间:2015-05-26 04:41:31

标签: php mysql database

新查询

$query = "SELECT COUNT(*) FROM payments $sCriteria";
$result         = mysql_query($query) or die("Error encountered on retrieving logs.");
$query_data     = mysql_fetch_row($result);
$numrows        = $query_data[0];
$rows_per_page  = 30;
$lastpage       = ceil($numrows/$rows_per_page);
$pageno         = (int)$pageno;

if ($pageno > $lastpage) {
    $pageno = $lastpage;
}
if ($pageno < 1) {
    $pageno = 1;
}

$limit          = "LIMIT ".($pageno - 1) * $rows_per_page .",".$rows_per_page;
$rs             = mysql_query("SELECT * FROM payments $sCriteria ORDER BY pay_date DESC $limit") or die("Error encountered on retrieving logs.".mysql_error());
$current_rows   = mysql_num_rows($rs);

if ($current_rows) {
    while ($rows = mysql_fetch_array($rs)) {
        echo "<tr>
                <td>".date("m/d/Y n:H:s",strtotime($rows["pay_date"]))."</td>
                <td>".$rows["po_no"]."</td>
                <td>".$rows["or_no"]."</td>
                <td>".$rows["sold_to"]."</td>
                <td>Php ".number_format($rows["amt_to_pay"],2,".",",")."</td>
                <td>Php ".number_format($rows["amt_paid"],2,".",",")."</td>
                <td>".$rows["pay_status"]."</td>
                **<td>".$rows["verified"]."</td>**
                <td><a href='pay_preview.php?id=$iPayID' class='action preview' title='Print Preview'><img src='images/preview.png' class='action_img' /></a></td>

旧查询

$query          = "SELECT COUNT(*) FROM payments";
$result         = mysql_query($query) or die("Error encountered on retrieving payment details.");
$query_data     = mysql_fetch_row($result);
$numrows        = $query_data[0];
$rows_per_page  = 10;
$lastpage       = ceil($numrows/$rows_per_page);
$pageno         = (int)$pageno;

if ($pageno > $lastpage) {
    $pageno = $lastpage;
}
if ($pageno < 1) {
    $pageno = 1;
}
$limit          = "LIMIT ".($pageno - 1) * $rows_per_page .",".$rows_per_page;
$rs             = mysql_query("SELECT * FROM payments ORDER BY pay_date DESC, po_id $limit") or die("Error encountered on retrieving payment details.");
$current_rows   = mysql_num_rows($rs);

if ($current_rows) {
    while ($rows = mysql_fetch_array($rs)) {
        $iPayID     = $rows["pay_id"];
        $iPoID      = $rows["po_id"];
        $dSubTotal  = get_po_subtotal($iPoID);
        $sPoNo      = get_value("po","po_no","WHERE po_id=$iPoID");
        $iDis       = get_value("po","discount","WHERE po_id=$iPoID");
        $dNet       = $dSubTotal - ($dSubTotal * ($iDis/100));
        $sStat      = $rows["pay_status"];

        switch($sStat) {
            case "Paid":    $sColor = "class='blue'";break;
            default:        $sColor = "";break;
        }

        if ($rows["verified"]) {
            $sVerified = "Yes";
        }
        else {
            $sVerified = "No <a href='payment_verification.php?id=$iPayID' class='action verify' title='Verify' id='$iPayID'>(verify)</a>";
        }
        echo "<tr>

                <td>".date('Y-m-d',strtotime($rows["pay_date"]))."</td>
                <td>".$rows["or_no"]."</td>
                <td>$sPoNo</td>
                <td>".$rows["sold_to"]."</td>
                <td class='right'>Php ".number_format($rows["amt_to_pay"],2,".",",")."</td>
                <td class='right'>Php ".number_format($rows["amt_paid"],2,".",",")."</td>
                <td $sColor>".$rows["pay_status"]."</td>
                <td>$sVerified</td>
                <td><a href='pay_preview.php?id=$iPayID' class='action preview' title='Print Preview'><img src='images/preview.png' class='action_img' /></a></td>
              </tr>\n";
    }
}

我的问题是,在我的新查询中,我无法将已验证的输出更改为付费或未付款,它保持为0和1 **我也复制并将其与我的新旧查询进行比较,它就是这样它不起作用,有人可以帮助我吗?非常感谢你。

2 个答案:

答案 0 :(得分:0)

我建议像在mysql中使用条件一样的例子: 我的SQL语句指定了列,不使用SELECT *

SELECT IF(Field > 0,'TRUE','FALSE') AS verified FROM Table

您可以添加字段以将其更改为paidunpaid

SELECT IF(Field > 0,'Paid','Unpaid') AS verified FROM Table

作为一个例子。您可以添加任何条件和结果输出

ALSO

在您的代码中,我无法确定$sVerified在哪里被赋值?

答案 1 :(得分:0)

我不确定您的要求是什么..我不知道您是否需要将值打印到页面,或者是否需要将其更改为其他查询..

也许开关盒可以工作..允许您将格式更改为您需要的格式。基本上转换旧的查询结果,以便您的新查询可以处理它..

$value = 0;  // or false.. this is your value from first query
$newval = null;  // just placeholder for newval in switch

switch($value) {
    case 0:
        $newval = 'Unpaid';
        break;
    case 1:
        $newval = 'Paid';
        break;
    default:
        $newval = 'Unknown';
}

echo $newval;