drd0833 2011-11-09 10:19 采纳率: 100%
浏览 14
已采纳

从数据库返回字符串中查找某个字符

I have the following class. Which returns certain fields. I am interested in Reference. I want it to split up the data so that after the query is returned reference contains an 'A' then return that, if it contains a B return that. seperately so i am able to put it in table show the amount of a's returned, b's returned etc. The data is listed by room.

<?php


class CHWIPProgress {

    var $conn;

    // Constructor, connect to the database
    public function __construct() {
        require_once "/var/www/reporting/settings.php";
        define("DAY", 86400);
        if(!$this->conn = mysql_connect(DB_HOST, DB_USERNAME, DB_PASSWORD)) die(mysql_error());
        if(!mysql_select_db(DB_DATABASE_NAME, $this->conn)) die(mysql_error());
    }

    public function ListWIPOnLocation($location) {
    $sql = "SELECT DISTINCT
            `ProgressPoint`.`PPDescription` AS Description ,`Bundle`.`WorksOrder` AS WorksOrder, `Bundle`.`BundleNumber` AS Reference,`TWOrder`.`DueDate` AS DueDate , `Stock`.`Description` as Stock , `Stock`.`ProductGroup` as Group 
        FROM TWOrder,Bundle,ProgressPoint, Stock
                    WHERE `Bundle`.`CurrentProgressPoint`=`ProgressPoint`.`PPNumber`
        AND `TWOrder`.`Colour`=`Bundle`.`Colour`
        AND `TWOrder`.`Size`=`Bundle`.`Size`
                    AND `TWOrder`.`WorksOrderNumber`=`Bundle`.`WorksOrder`
                    AND `TWOrder`.`Product`=`Stock`.`ProductCode`
                    AND `ProgressPoint`.`PPDescription` = '" . $location . "'
        ORDER BY `TWOrder`.`DueDate` ASC";
        mysql_select_db(DB_DATABASE_NAME, $this->conn);
        $result = mysql_query($sql, $this->conn);
                    $num_rows = mysql_num_rows($result);
                    echo "Number of rows : $num_rows"; 
        while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
            $return[] = $row;
        }       

        return $return;

    }


}
?>
  • 写回答

1条回答 默认 最新

  • 普通网友 2011-11-09 10:48
    关注

    The ListWIPOnLocation function appears to return an array where each item corresponds to a row from the query. You can iterate this array and isolate the items using PHP string functions and perhaps add them other arrays. There is more than one way to arrange the data, here is one example:

    $returned = array();
    $returned["A"] = array();
    $returned["B"] = array();
    foreach($return as $row) {
        if (strpos($row["Reference"], "A") !== FALSE) { // note the !== operator
            $returned["A"][] = $row;
        }
        if (strpos($row["Reference"], "B") !== FALSE) { // note the !== operator
            $returned["B"][] = $row;
        }
    }
    var_dump($returned);
    

    Alternately, You can use the MySQL LIKE operator in the WHERE clause to retrieve the rows where the reference column contains a specific character:

    WHERE ... AND Reference = LIKE '%A%'
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站