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 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探