ppypp伦理天堂,91手机在线视频,免费在线观看黄色毛片,夜夜穞天天穞狠狠穞AV美女按摩

聯系官方銷售客服

1835022288

028-61286886

投訴 已解決 根據身份證號碼通過JS統計年齡段人數 6 0
迅睿CMS版本:4.6.3 #CodeIgniter

模型cmda里有個身份證字段sfzhm

我在前臺想通用身份證號碼來統計有多少八十歲以上的老人,前臺標簽

<?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內容循環標簽的生成工具,填寫參數就可以生成相關的代碼,每個參數后面都有用法解釋

解決方案
  • $data = []; 
    {module module=cmda}
    $data[] = {$t.sfzhm}; 
    {/module} 
    你這樣寫會遍歷全表不劃算,耗時比較嚴重了

    開發建議,你在入庫的時候,做一個出生日期字段,可以從身份證中提取出來,將出生日期做條件查詢,比你這種全表對比快的多

  • 回復@迅睿官方創始人

    如果我的出生年月日的字段是csnyr

    那具體的語句怎么寫呢?

  • 回復@迅睿官方創始人

    這個字段類型是:varchar(255)

    是的,平常都是用AI模型來尋求幫助的,這回這個AI答的都不準確


    微信截圖_20240820232326

  •                                         <script src="{HOME_THEME_PATH}static/js/echarts541.min.js"></script>
                                            <div id="main" style="width: 600px;height:400px;"></div>
     <script>
        // 身份證號碼數組
        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;
        }
    
        // 獲取年齡數組
        const ages = idCards.map(idCard => calculateAge(idCard));
    
        // 統計不同年齡段的人數
        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);
    
        // 總人口數
        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: '人數',
            type: 'bar',
            barWidth: '60%',
            data: ageGroupPercentages.map(item => ({
              value: item.count,
              name: item.group,
              percentage: item.percentage
            }))
          }]
        };
    
        // 使用配置項和數據顯示圖表
        myChart.setOption(option);
      </script>
    最后用這個搞定了,謝謝老大的指導。
  • @迅睿官方創始人:老大的指導。