我正试图找到foreach循环到底的时间,以便我可以关闭HTML标记并修改这个问题,使其更加简洁。我现在将数据集作为数组传递给函数,希望它可以使它更容易。我需要确定其中一个字段何时达到我无法解决的最大值,因为每次经过foreach时,它“认为”它是最大值直到下一次传递,依此类推。
数据集数组看起来像这样:
Array
(
[0] => 3391
[ID] => 3391
[1] => 2
[SubID] => 2
[2] => 2
[ListType] => 2
[3] => 0
[IndentText] => 0
[4] => 3
[BaseGroup] => 3
[4] => 1
[Quantity] => 1
)
如何在循环时获取最高的SubID值?我正在考虑像这样的函数,但无法确定如何在每行运行时获得最高值。
function getMax($index, $array) {
$array = array($array[$index]);
return max($array);
}
我希望将其作为 $ maxID = getMax(“SubID”,$ array) 运行,但还没弄明白如何让它实际上让它给出任何有意义的结果但是一旦它起作用,那么:
if ($maxID == $row['SubID']) :
// CLOSING HTML HERE
endif;
...会告诉代码它位于最后一行,标签可以关闭。有什么想法吗?
这是整个功能,但问题与它没有直接关系,所以我不确定它是否会有所帮助。
function Notes($rowEntry) {
// Create variables
$ListType = (isset($rowEntry['ListType'])) ? $rowEntry['ListType'] : "";
/* $ID = (isset($rowEntry['ID'])) ? $rowEntry['ID'] : ""; */
$SubID = (isset($rowEntry['SubID'])) ? $rowEntry['SubID'] : "";
$GroupNo = (isset($rowEntry['GroupNo'])) ? $rowEntry['GroupNo'] : "";
$Title = (isset($rowEntry['Title'])) ? $rowEntry['Title'] : "";
$SubPartNo = (isset($rowEntry['SubPartNo'])) ? $rowEntry['SubPartNo'] : "";
$SubDescription = (isset($rowEntry['SubDescription'])) ? LinkAbbvr($rowEntry['SubDescription']) : " ";
$SubPage = (isset($rowEntry['SubPage'])) ? $rowEntry['SubPage'] : "";
// SubQuantity field is integer so, to match original hardcopy, 0 used in place of X
// Condition below changes it back to X for display
$SubQuantity = (isset($rowEntry['SubQuantity'])) ? $rowEntry['SubQuantity'] : "";
$SubQuantity = ($SubQuantity == 0) ? "X" : $SubQuantity;
// Create Edit link to SubQuantity value
$EditSubQuantity = (isset($_SESSION['AccessLevel']) && $_SESSION['AccessLevel'] == 3)
? "<a href=\"/administration/adminforms.php?FormID=33&ID=$SubID\">$SubQuantity</a>"
: $SubQuantity;
// For note of a specific group by itself, ie Group 3.5890 page 98
// Uses style .groupcaption
if ($ListType == 1) :
if ($GroupNo && $Title) :
$last_groupnote = "";
if ($Title && $GroupNo != $last_groupnote) :
$last_groupnote = $GroupNo;
$NotesText = "<div class=\"groupcaption\">\n";
$NotesText .= "<div class=\"SubTitle\">$Title</div>\n";
$NotesText .= "</div>\n\n";
echo $NotesText;
endif;
endif;
// For indented sub-listings, ie Group 3.639 page 108
// This lists Quantity, PartNo and Description
// Uses style #css-subtable
elseif ($ListType == 2) :
global $TitleCaption;
$NotesText = "";
// Show Title column only once
if ($Title != $TitleCaption) :
$TitleCaption = $Title;
// Close part listing row table
$NotesText .= "</div>\n\n";
// Open subtable and title
$NotesText .= "<div id=\"css-subtable\">\n";
$NotesText .= "<div class=\"RowCaption\">$Title</div>\n\n";
endif;
if ($SubDescription && !is_null($SubDescription)) :
$NotesText .= "<div class=\"row\">\n";
$NotesText .= "<div class=\"col\">$EditSubQuantity</div>\n";
$NotesText .= "<div class=\"col\">$SubPartNo</div>\n";
$NotesText .= "<div class=\"col\">$SubDescription</div>\n";
$NotesText .= "</div>\n\n";
endif;
$subMax = getMax("SubID", $rowEntry);
// Check to see if last row
if ($SubID == $subMax) :
//if ($SubID == 15) :
// Close the subtable
$NotesText .= "</div>\n\n";
// Restart row table
if ($ListType < 5) :
$NotesText .= "<div id=\"css-table\">\n";
elseif ($ListType > 4) :
$NotesText .= "<div id=\"css-sublist$ListType\">\n";
endif;
endif;
echo $NotesText;
// For indented sub-listings description-only list, ie Group 1.322 page 16
// This lists Description only
// Uses style #css-subsingletable
elseif ($ListType == 3) :
global $rowCount;
$NotesText = "";
$rowCount++;
// Clost main style, open new one
if ($rowCount == 1) :
// Close part listing row table
$NotesText .= "</div>\n\n";
// Open subtable and title
$NotesText .= "<div id=\"css-subsingletable\">\n";
endif;
if ($SubDescription && !is_null($SubDescription)) :
$rowCount++;
$NotesText .= "<div class=\"row\">\n";
$NotesText .= "<div class=\"col\">$SubDescription</div>\n";
$NotesText .= "</div>\n\n";
endif;
if ($rowCount == 3) :
$NotesText .= "</div>\n\n";
// Restart row table
if ($ListType < 5) :
$NotesText .= "<div id=\"css-table\">\n";
elseif ($ListType > 4) :
$NotesText .= "<div id=\"css-sublist$ListType\">\n";
endif;
endif;
echo $NotesText;
// For single notes, ie Group 1.607 page 29
// Uses style #css-partnotes
elseif ($ListType == 4) :
$TitleCaption = "";
if ($Title) :
if ($Title != $TitleCaption) :
$TitleCaption = $Title;
// Close part listing row table
$NotesText = "</div>\n\n";
$NotesText .= "<div id=\"css-partnotes\">\n";
$NotesText .= "<div class=\"row\">\n";
$NotesText .= "<div class=\"col\">\n";
$NotesText .= "<div class=\"RowCaption\">$Title</div>\n";
$NotesText .= "</div>\n";
$NotesText .= "</div>\n";
$NotesText .= "</div>\n\n";
// Restart row table
if ($ListType < 5) :
$NotesText .= "<div id=\"css-table\">\n";
elseif ($ListType > 4) :
$NotesText .= "<div id=\"css-sublist$ListType\">\n";
endif;
echo $NotesText;
endif;
endif;
endif;
}
上面的函数是从:
调用的// This function used to simplify partsbook.php and partsbookdetails.php
// IN CONJUNCTION WITH ABOVE, FUNCTION PARTIALLY REPLACES OBSOLETE PartID()
function PartsList($sqlView) {
$rowView = DBConnect($sqlView, "Multiple", "pchome_packardsimperials");
if ($rowView) :
$last_groupno = "";
$last_id = "";
$rowNumber = 0;
foreach ($rowView as $row) :
$rowNumber++;
$ID = (isset($row['ID'])) ? $row['ID'] : "";
$ListType = (isset($row['ListType'])) ? $row['ListType'] : "";
$BaseGroup = (isset($row['BaseGroup'])) ? $row['BaseGroup'] : 0;
$GroupNo = (isset($row['GroupNo'])) ? $row['GroupNo'] : "";
$GroupName = (isset($row['GroupName'])) ? LinkAbbvr($row['GroupName']) : "";
$Name = (isset($row['Name'])) ? LinkAbbvr($row['Name']) : "";
$Title = (isset($row['Title'])) ? $row['Title'] : "";
$PartNo = (isset($row['PartNo'])) ? $row['PartNo'] : "";
$Models = (isset($row['Models'])) ? $row['Models'] : "";
$Description = (isset($row['Description'])) ? LinkAbbvr($row['Description']) : " ";
$PageNo = (isset($row['PageNo'])) ? $row['PageNo'] : "";
$SubPage = (isset($row['SubPage'])) ? $row['SubPage'] : "";
$RevDate = (isset($row['RevDate'])) ? $row['RevDate'] : "";
$Edition = (isset($row['Edition'])) ? $row['Edition'] : "";
// Choose variable for use in title of GroupNo() function
if ($ListType < 5) :
$ListName = $Name;
elseif ($ListType > 4) :
$ListName = $Title;
endif;
// Check to see if GroupNo is different than previous one
if ($GroupNo != $last_groupno || $rowNumber == 1) :
$last_groupno = $GroupNo;
$NewGroup = TRUE;
else :
$NewGroup = FALSE;
endif;
// Show GroupNo() captions only once per group
if ($NewGroup === TRUE) :
GroupNo($GroupNo, $NewGroup, $ListName, $rowNumber, $ListType);
endif;
if ($ListType < 5) :
// To prevent duplicates, check to see if ID is different than previous one before displaying
if ($ID != $last_id) :
$last_id = $ID;
PartRow($row);
endif;
Notes($row);
elseif ($ListType > 4) :
SpecialRow($row);
endif;
endforeach;
endif;
}
答案 0 :(得分:0)
要查找最高SubID
,您可以使用以下内容:
$max = max(array_map(function ($row) {
return $row['SubID'];
}, $data));
编辑或更好:
$max = array_reduce($data, function ($max, $row) {
return $row['SubID'] > $max ? $row['SubID'] : $max;
}, 0);
您需要在主foreach
循环之外使用此属性,然后使用结果值进行比较。
如果您发布更多代码,我们可能会建议更有效的方法。
答案 1 :(得分:0)
感谢大家的帮助! 不可能不是我经常使用的词,但在这种情况下,我认为它适用于我想要做的事情。一旦我接受了这个术语,我发现了一个似乎可以解决问题的解决方法,但是通过我之前从未想过的子查询进行查询。下面是查询的一部分,它只是给出了parts_notes表可能具有的最高值,然后函数中的条件与它正在接收的行的子ID进行比较,并在匹配时关闭标记。到目前为止它似乎运作良好。
SELECT DISTINCT
pl.ID,
pn.ID AS SubID,
IF(ListType,(SELECT MAX(ID)
FROM parts_notes
WHERE PageNo=108
AND ListType IS NOT NULL
AND SubPage IS NULL ),NULL) AS SubMax,
ListType,
IndentText, . . .