douxin1163 2012-04-18 17:33
浏览 74
已采纳

检查查询返回的输出是否包含字符串/数字

I am working on Moodle but have a php-mysql related doubt which is turning out to be complicated, thanks to the lack of my knowledge.

I have a query which returns the output fine. Using IF-ELSe condition I need to check if there is a combination of one of the fields containing a string and one of the field containing a number.

The fields I m checking are of the following type in the database table.

 element - varchar(255)
 value - longtext

This is my query which returns the o/p as mentioned below:

Query

    $scormstatus = $DB->get_records_sql("SELECT sc.*, s.name AS activityname FROM mdl_scorm_scoes_track AS sc JOIN mdl_scorm AS s ON s.id = sc.scormid WHERE sc.scormid = '" .$activityid. "' AND sc.userid = '" .$userid. "' AND sc.attempt = '".$scormattempt->attempt."'");
     foreach($scormstatus as $status)
      {
         echo "<br/>".$status->element."**".$status->value."<br/>";
         if(is_numeric($status->value))
         {
           if($status->element = 'cmi.core.score.raw' && $status->value != '0')
           {
              echo "<br/>Score";
           }    
           else
           {
                if($status->element = 'x.start.time' && $status->value != '0')
                {
                  echo "<br/>Started";
                }
           }
        }
    }

Output:

         cmi.core.lesson_location**2
         Score

         cmi.core.lesson_status**incomplete
         cmi.core.score.max**50
         Score

         cmi.core.score.min**0
         cmi.core.score.raw**0
         cmi.core.total_time**00:00:22.00

         x.start.time**1334767290
         Score

Now as you would have seen that even though I'm checking for a condition which is a combination of 'cmi.core.score.raw' and value greater than 0 it still prints 'Score' for all records and never goes in the else part.

Ideally the output I'm looking forward to see is something like this

IDEAL O/P:

     cmi.core.lesson_location**2
     cmi.core.lesson_status**incomplete
     cmi.core.score.max**50
     cmi.core.score.min**0
     cmi.core.score.raw**0
     cmi.core.total_time**00:00:22.00
     x.start.time**1334767290
     Started

My guess is that it isn't recognizing the value field which is a number in my IF--ELSE case and hence always ends up going in the wrong IF part.

Any guesses as to what is happening. Thanks in advance

  • 写回答

1条回答 默认 最新

  • drba1172 2012-04-18 17:42
    关注

    This code:

    if($status->element = 'cmi.core.score.raw'
    

    needs to be:

     if($status->element == 'cmi.core.score.raw'
    

    A single = is for assignment, while two == is for comparison.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建