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.

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

报告相同问题?

悬赏问题

  • ¥20 关于#目标检测#的问题:(qq收集表到时间才能填写,填写的份数有上限)
  • ¥50 ZYNQ7020双核FLAHS烧写的问题
  • ¥20 ue 5 中想要实现第一人称人物左右行走摆动的效果,摄像头只向右摆动一次(关键词-结点)
  • ¥15 钢岔管添加弹性约束模拟围岩作用
  • ¥15 AD9164瞬时带宽1.8G,怎么计算出来?
  • ¥15 鼠标右键,撤销删除 复制 移动,要怎样删除
  • ¥15 使用MATLAB进行余弦相似度计算加速
  • ¥15 服务器安装php5.6版本
  • ¥15 我想用51单片机和数码管做一个从0开始的计数表 我写了一串代码 但是放到单片机里面数码管只闪烁一下然后熄灭
  • ¥20 系统工程中,状态空间模型中状态方程的应用。请猛男来完整讲一下下面所有问题