-3

这是我的代码..

    <?php
$SupportingMaterialTaskSql = "select task_name,section_name,ref_student_id from scores where ref_student_id = '".$studentid."' and section_name = 'sm1' GROUP BY task_name ORDER BY cast( SUBSTRING( task_name, 5, 2 ) AS unsigned ) ASC";

            $SupportingMaterialTaskResults = $obj->selectQuery($SupportingMaterialTaskSql);

            if($SupportingMaterialTaskResults > 0)
            {
                foreach($SupportingMaterialTaskResults as $Key => $SupportingMaterialTaskRow)
                {
                    $Task = $SupportingMaterialTaskRow['task_name'];

                    $str = "Task";
                    $TaskName = str_replace($str, "Task ", $Task);
?>
                <tr>
                  <td width="30%"  class="f13_blue cellbg"><?php echo $TaskName; ?></td>
                </tr> 
                <?php 
                } 

            } 
            ?>

这是此代码上方的输出..

Task2 Task3 Task5 Task6 Task7 Task8 Task9 Task10

所以我只想得到这样的结果。如果数据库具有像 Task1 这样的值,则打印 Task1,否则打印 Task1“N/A”。

那么我该如何解决呢?

4

1 回答 1

1

像下面这样的东西可以工作:

<?php

function check_not_null($var){
  if(trim($var) == ""){
    $var = "N/A";
  }
  return($var);
}


// some query info here...


while($fetch = mysqli_fetch_array($query)) {
  $task1 = check_not_null($fetch['task1']);
  $task2 = check_not_null($fetch['task2']);
  $task3 = check_not_null($fetch['task3']);
  // now echo or use the vars.. each loop they will be rechecked..
}

?>
于 2012-10-02T09:39:00.033 回答