status = 0 (評価されていない画像)およびstatus != 1 (評価された画像)という条件で、多次元配列内で変更日の降順 (最新のものを意味する) で並べ替えようとしています。
上位2つが以下のロジックに従うように配列をソートすることは可能ですか:
If (there are multiple versions of an image)
If (there is a version that has been rated)
If (there is a new unrated version of the image)
Display (the most recent version that has a rating) and
(the most recent unrated version) as top 2 of array
Else
Display (the most recent version that has a rating) as top 1
Else
Display (the most recent unrated version) as top 1
Else
Display (the only version) as top 1
これが私の更新されたPHPコードです(2013年9月6日更新):
updateImageDisplayStatus($caseID,$patientID);
function updateImageDisplayStatus($caseid,$patientid) {
$connection = getMySqlConnection();
$sql = "SELECT image_name FROM images WHERE patientid = '{$patientid}' AND caseid = '{$caseid}' ";
$imageGroup = array();
$result = mysql_query($sql,$connection);
while($row = mysql_fetch_assoc($result)) {
$fileName = $row['image_name'];
$tmpName = $row['image_name'];
list($leftN,$rightN) = explode($caseid."_",$tmpName);
//$spltName = explode('_',$rightN);
$shortFileName = $rightN;
list($noExtensionFileName,$extension) = explode('.',$shortFileName);
//$extension = '.nii.gz';
list($groupName,$appendDate) = explode('_',$noExtensionFileName);
// echo $groupName = just the original name.
$sql_2 = "SELECT image1.image_name as i1_image_name
,image1.comments as i1_comments
,image1.status as i1_status
,image1.mod_date as i1_mod_date
,image2.image_name as i2_image_name
,image2.comments as i2_comments
,image2.status as i2_status
,image2.mod_date as i2_mod_date
FROM (SELECT CASE WHEN status = 0 THEN 1 ELSE 0 END as rated
,image_name
,comments
,status
,mod_date
FROM images
WHERE image_name like '%{$groupName}%'
ORDER BY 1 ASC, mod_date DESC
LIMIT 1
) image1
LEFT OUTER
JOIN images image2
ON image1.status > 0
AND image2.status = 0
AND image2.mod_date > image1.mod_date
AND image2.image_name like '%{$groupName}%'
AND patientid = '{$patientid}' AND caseid = '{$caseid}'
ORDER BY image2.mod_date DESC
LIMIT 1";
$result_2 = mysql_query($sql_2,$connection);
if($row = mysql_fetch_assoc($result_2)) {
$imageGroup[$groupName] = array();
$imageGroup[$groupName][] = array(
'image_name' => $row['i1_image_name'],
'comments' => $row['i1_comments'],
'status' => $row['i1_status'],
'mod_date' => $row['i1_mod_date']
);
if ( !empty( $row['i2_image_name'] ) ) {
$imageGroup[$groupName][] = array(
'image_name' => $row['i2_image_name'],
'comments' => $row['i2_comments'],
'status' => $row['i2_status'],
'mod_date' => $row['i2_mod_date']
);
}
}
echo "Group $groupName:<br />";
echo "Size of Group:".sizeof($imageGroup[$groupName]).'<br />';
echo $imageGroup[$groupName][0]['image_name'].'<br />';
echo $imageGroup[$groupName][1]['image_name'].'<br />';
}
}
アップデート:
SQLに仕事を任せる提案をしてくれた@gwcに感謝します。検索およびソートされる 1 ~ 2 枚の画像は、指定された $caseid および $patientid 内にある必要があることを付け加えるのを忘れていました。その結果、最初の sql ステートメントは次のようになります。$sql = "SELECT image_name FROM images WHERE patientid = '{$patientid}' AND caseid = '{$caseid}' ";