模型cmda里有個身份證字段sfzhm
我在前臺想通用身份證號碼來統(tǒng)計有多少八十歲以上的老人,前臺標(biāo)簽
<?php
$data = [];
{module module=cmda}
$data[] = {$t.sfzhm};
{/module}
function countOverEighty($data) {
$count = 0;
foreach ($data as $idCardNumber) {
$birthYear = substr($idCardNumber, 6, 4);
$currentYear = date('Y');
$age = $currentYear - $birthYear;
if ($age >= 80) {
$count++;
}
}
return $count;
}
echo countOverEighty($data);
?>這樣輸出報錯,查閱論壇,找不到方法,請求幫助。
官方提醒:使用module內(nèi)容循環(huán)標(biāo)簽的生成工具,填寫參數(shù)就可以生成相關(guān)的代碼,每個參數(shù)后面都有用法解釋
$data = []; {module module=cmda} $data[] = {$t.sfzhm}; {/module} 你這樣寫會遍歷全表不劃算,耗時比較嚴(yán)重了開發(fā)建議,你在入庫的時候,做一個出生日期字段,可以從身份證中提取出來,將出生日期做條件查詢,比你這種全表對比快的多
開源是一種精神,但不是義務(wù),幫忙是情分,不幫也不要抱怨,建議大家多研究代碼、多閱讀代碼、多翻閱社區(qū)歷史問題!
回復(fù)@迅睿官方創(chuàng)始人
如果我的出生年月日的字段是csnyr
那具體的語句怎么寫呢?
開源是一種精神,但不是義務(wù),幫忙是情分,不幫也不要抱怨,建議大家多研究代碼、多閱讀代碼、多翻閱社區(qū)歷史問題!
回復(fù)@迅睿官方創(chuàng)始人
這個字段類型是:varchar(255)
是的,平常都是用AI模型來尋求幫助的,這回這個AI答的都不準(zhǔn)確
開源是一種精神,但不是義務(wù),幫忙是情分,不幫也不要抱怨,建議大家多研究代碼、多閱讀代碼、多翻閱社區(qū)歷史問題!
<script src="{HOME_THEME_PATH}static/js/echarts541.min.js"></script> <div id="main" style="width: 600px;height:400px;"></div> <script> // 身份證號碼數(shù)組 const idCards = [ {module module=cmda return=sfz} '{$sfz.sfzhm}', {/module} ]; // 計算年齡 function calculateAge(idCard) { const birthDate = idCard.substring(6, 14); // 提取出生日期 const birthYear = parseInt(birthDate.substring(0, 4), 10); const birthMonth = parseInt(birthDate.substring(4, 6), 10); const birthDay = parseInt(birthDate.substring(6, 8), 10); const currentDate = new Date(); const currentYear = currentDate.getFullYear(); const currentMonth = currentDate.getMonth() + 1; // 注意月份是從0開始的 const currentDay = currentDate.getDate(); let age = currentYear - birthYear; if (currentMonth < birthMonth || (currentMonth === birthMonth && currentDay < birthDay)) { age--; } return age; } // 獲取年齡數(shù)組 const ages = idCards.map(idCard => calculateAge(idCard)); // 統(tǒng)計不同年齡段的人數(shù) function countAgeGroups(ages) { const ageGroups = { '29歲以下': 0, '30-39歲': 0, '40-49歲': 0, '50-79歲': 0, '80歲以上': 0 }; ages.forEach(age => { if (age <= 29) ageGroups['29歲以下']++; else if (age >= 30 && age <= 39) ageGroups['30-39歲']++; else if (age >= 40 && age <= 49) ageGroups['40-49歲']++; else if (age >= 50 && age <= 79) ageGroups['50-79歲']++; else if (age >= 80) ageGroups['80歲以上']++; }); return ageGroups; } const ageGroupCounts = countAgeGroups(ages); // 總?cè)丝跀?shù) const totalPopulation = 1341; // 計算百分比 const ageGroupPercentages = Object.entries(ageGroupCounts).map(([group, count]) => ({ group, count, percentage: (count / totalPopulation * 100).toFixed(2) })); // 初始化 ECharts 實例 const chartDom = document.getElementById('main'); const myChart = echarts.init(chartDom); // 配置圖表 const option = { tooltip: { trigger: 'axis', axisPointer: { type: 'shadow' }, formatter: params => { const { group, count, percentage } = params[0].data; return `${group}: ${count}人 (${percentage}%)`; } }, grid: { left: '3%', right: '4%', bottom: '3%', containLabel: true }, xAxis: { type: 'category', data: ageGroupPercentages.map(item => item.group), axisTick: { alignWithLabel: true } }, yAxis: { type: 'value' }, series: [{ name: '人數(shù)', type: 'bar', barWidth: '60%', data: ageGroupPercentages.map(item => ({ value: item.count, name: item.group, percentage: item.percentage })) }] }; // 使用配置項和數(shù)據(jù)顯示圖表 myChart.setOption(option); </script>最后用這個搞定了,謝謝老大的指導(dǎo)。