从数据库返回字符串中查找某个字符
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;
}
}
?>
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%'