开发框架 版主:迅睿框架研发组
根据身份证号码通过JS统计年龄段人数
类型:迅睿CMS 更新时间:2024-08-21 17:24:40 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);
?>

这样输出报错,查阅论坛,找不到方法,请求帮助。

回帖
  • 迅睿官方创始人
    #1楼    迅睿官方创始人
    2024-08-20 22:53:26
    Chrome 0
    $data = []; 
    {module module=cmda}
    $data[] = {$t.sfzhm}; 
    {/module} 
    你这样写会遍历全表不划算,耗时比较严重了
    开发建议,你在入库的时候,做一个出生日期字段,可以从身份证中提取出来,将出生日期做条件查询,比你这种全表对比快的多
  • 学习狂人
    #2楼    学习狂人
    2024-08-20 23:06:12
    Chrome 0
    迅睿官方创始人 如果我的出生年月日的字段是csnyr那具体的语句怎么写呢?
  • 迅睿官方创始人
    #3楼    迅睿官方创始人
    2024-08-20 23:07:32
    iPhone手机 0
    这个要看你这个字段值,它具体存储是什么值,它每种类型的值它的查询方式不一样,其实这种很基础的查询,你可以问Ai呀,现在ai很发达的这种基础的查询语句它都可以给你生出来。
  • 学习狂人
    #4楼    学习狂人
    2024-08-20 23:23:49
    Chrome 0
    迅睿官方创始人 这个字段类型是:varchar(255)是的,平常都是用AI模型来寻求帮助的,这回这个AI答的都不准确


    微信截图_20240820232326

  • 迅睿官方创始人
    #5楼    迅睿官方创始人
    2024-08-20 23:35:24
    iPhone手机 0
    这个写法很对逻辑非常清晰,很标准,很规范,就是效率比较低
    满意答案
  • 学习狂人
    #6楼    学习狂人
    2024-08-21 17:24:22
    Chrome 0
                                            <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>
    最后用这个搞定了,谢谢老大的指导。
  • 学习狂人
    #7楼    学习狂人
    2024-08-21 17:24:40
    Chrome 0
    @迅睿官方创始人:老大的指导。