PHP拼接Sql,MySql拆分数据(substring_index())作为判断条件
源码:
$partsArr = array();
if($_POST['childType'] != 'eve' && empty($_POST['yearTwo'])) {
$index = 0;
for($i=0;$i<count($_SESSION['partsArr']);$i++) {
if($_POST['childType'] == $_SESSION['partsArr'][$i][0]) {
$partsArr[$index] = $_SESSION['partsArr'][$i];
$index++;
}
}
} else {
$time = $_POST['yearTwo'];
if($_POST['monthTwo'] != "eve") {
$time .= "-".$_POST['monthTwo'];
}
$partsType = $_POST['childType'];
if($_POST['partsTypeTwo'] == 1) {
$metalArray = array(
0 => "metalOne",
1 => "metalTwo",
2 => "metalThree",
3 => "metalFour",
4 => "metalFive"
);
for($i=0; $i<count($metalArray); $i++) {
$selSql = "SELECT $metalArray[$i],numbers FROM orders WHERE substring_index($metalArray[$i],',', 1) = $partsType AND state = 10 AND delivery LIKE '%$time%'";
$listpj[$i] = getRes($selSql);
}
$partsArr = ajaxPartsCoreB($listpj,$time,$_POST['partsTypeTwo'],$_POST['childType'],$metalArray);
} else if($_POST['partsTypeTwo'] == 2) {
$plasticArray = array(
0 => "plasticOne",
1 => "plasticTwo",
2 => "plasticThree",
3 => "plasticFour",
4 => "plasticFive"
);
for($i=0;$i<count($plasticArray);$i++) {
$selSql = "SELECT $plasticArray[$i],numbers FROM orders WHERE substring_index($plasticArray[$i],',', 1) = $partsType AND state = 10 AND delivery LIKE '%$time%'";
$listpj[$i] = getRes($selSql);
}
$partsArr = ajaxPartsCoreB($listpj,$time,$_POST['partsTypeTwo'],$_POST['childType'],$plasticArray);
} else if($_POST['partsTypeTwo'] == 3) {
$specialArray = array(
0 => "specialOne",
1 => "specialTwo",
2 => "specialThree",
3 => "specialFour",
4 => "specialFive"
);
for($i=0;$i<count($specialArray);$i++) {
$selSql = "SELECT $specialArray[$i],numbers FROM orders WHERE substring_index($specialArray[$i],',', 1) = $partsType AND state = 10 AND delivery LIKE '%$time%'";
$listpj[$i] = getRes($selSql);
}
$partsArr = ajaxPartsCoreB($listpj,$time,$_POST['partsTypeTwo'],$_POST['childType'],$specialArray);
}
}
print_r(json_encode($partsArr));
function ajaxPartsCoreB($listpj,$time,$partType,$childType,$mpsArray) {
$partsArr = array();
$selSql = "SELECT partsname,size FROM parts WHERE id = ".$childType;
$selRse = getRes($selSql);
$parstName = "";
if($partType == 1) {
$parstName = "金属配件";
} else if($partType == 2) {
$parstName = "塑料配件";
} else if($partType == 3) {
$parstName = "其他配件";
}
$partsArr = array(
0 => array(
0 => $childType,
1 => 0,
2 => $time,
3 => $selRse[0]['partsname'].$selRse[0]['size'],
4 => $parstName,
5 => $partType,
)
);
for($i=0;$i<count($listpj);$i++) {
$childs = $mpsArray[$i];
for($j=0;$j<count($listpj[$i]);$j++) {
if(!empty($listpj[$i][$j][$childs])) {
$array = explode(",",$listpj[$i][$j][$childs]);
} else {
$array[0] = null;
}
if(!empty($array[0])) {
$partsArr[0][1] += $array[1]*$listpj[$i][$j]['numbers'];
}
}
}
return $partsArr;
}