如何在echo中使用if语句

时间:2014-11-04 09:54:13

标签: php mysql if-statement

我必须检查来自fetch数组的结果,我使用else ifif来获取备注。但我不知道如何在echo中使用它,当我运行此代码时,所有的备注语句都以与代码中所写的相同的形式出现。

在这里我必须检查来自fetch数组的结果,所以对于这个我正在使用if if或者如果得到备注..但我不知道如何在echo中使用它。当我运行此代码时,所有备注语句都以与代码

中所写的相同的形式出现

代码:

     <?php
    $answer = '';
      include('config.php');
      if(isset($_GET["results"]))
     $answer = $_GET["results"];
      $id = $_GET["idk"];
      switch ($answer) 
      {  
        case 'Nursery':
        $qqqs = mysql_query("select * from result where u_id='$id' AND sc='Nursery' ");
        $rows=mysql_fetch_assoc($qqqs);
        $a=$rows['u_id'];
        $b=$rows['name'];
        $c=$rows['fname'];
        $d=$rows['reg'];
        $e=$rows['sc'];
        $f=$rows['ss'];
        $g=$rows['se'];
        $h=$rows['e1'];
        $i=$rows['u1'];
        $j=$rows['m1'];
        $k=$rows['s1'];
        $l=$rows['ss1'];
        $m=$rows['i1'];
        $n=$rows['e2'];
        $o=$rows['u2'];
        $p=$rows['e3'];
        $pp=$rows['u3'];
        $pic=$rows['picture'];
        $total=$h+$i+$j+$k+$l+$m+$n+$o+$p;
        $totals=230;

        $res = ( $totals / $total) * 100;

        // 0 digit after the decimal point
        $res = round($res); // 67

        // 1 digit after the decimal point
        $res = round($res, 1); // 66.7

        // 2 digits after the decimal point
        $res = round($res, 2); // 66.67


        echo "<html>
        <head>

        </head>
        <body>
        <table align='center' border='4' bgcolor='white' width='500' class='table table-bordered'>
        <tr>
        <td bgcolor='orange' colspan='4'><h2 align='center'>Leads Grammar School</h2><p align='center'>Babar Road, Kirri Jamandan, Multan</p></td>
        </tr>
        <tr>
        <td bgcolor='yellow' colspan='4'><h2 align='center'>Monthly Test Feb-March</h2></td>
        </tr>
        <tr>
         <td colspan='2'><img src='images/$pic' width='100px' height='100px' align='right'></td>
         </tr>
        <tr>
        <td align='right'>Student's Name</td>

        <td colspan='3'>$b</td>
        </tr>
        <tr>
        <td align='right'>Father's Name</td>
        <td colspan='3'> $c</td>

        </tr>
        <tr>
        <td align='right'>Registration</td>
        <td colspan='3'>$d</td>
        </tr>
        <tr>
        <td align='right'>Student Class</td>
        <td colspan='3'>$e</td>
        </tr>
        <tr>
        <td align='right'>Exams</td>
        <td colspan='3'>$f</td>
        </tr>
        <tr bgcolor='gray'>
        <th>Subject Name</th>
        <th>Marks</th>
        <th>Obtained Marks</th>
        <th>Grades</th>
        </tr>
        <tr>
        <tr><td>English(Writing)</td>
        <td>50</td>
        <td> $h</td>

        </tr>
        <tr>
        <tr ><td>Urdu(Writing)</td>
        <td>50</td>
        <td>$i</td>

        </tr>
        <tr>
        <tr ><td>Math(Writing)</td>
        <td>50</td>
        <td> $j</td>

        </tr>
        <tr><th align='center' colspan='3' bgcolor='orange'>General Knowledge</th></tr>
        <tr>
        <tr ><td>Science</td>
        <td>10</td>

        <td> $k</td>

        </tr>
        <tr>
        <tr ><td>S.Studies</td>
        <td>10</td>
        <td>$l</td>
        </tr>

        <tr>
        <tr ><td>Islamiat</td>
        <td>10</td>
        <td>$m</td>
        </tr>

        <tr><th align='center' colspan='3' bgcolor='orange'>Poems</th></tr>
        <tr>
        <tr ><td>English </td>
        <td>15</td>
        <td> $n</td>
        </tr>

        <tr>
        <tr ><td>Urdu</td>
        <td>15</td>
        <td>$o</td>
        </tr>

        <tr><th align='center' colspan='3' bgcolor='orange'>Book Reading</th></tr>
        <tr>
        <tr><td>English </td>
        <td>10</td>
        <td>$p</td>
        </tr>

        <tr>
        <tr >
        <td>Urdu</td>
        <td>10</td>
        <td>$pp</td>
        </tr>
         <tr>
         <td><h1>Total Marks</h1></td>
         <td>230</td>
         <td>$total</td>
         <td>$res</td>
         </tr>
         <tr><td><h2>Remarks</h2></td>
         <td colspan='3'>
         if(91=<$res<=100)
         {
            echo 'Exceptional';
         } 
        else  if(81=<$res<=90)
         {
            echo 'Excellent';
         } 
          else  if(71=<$res<=80)
         {
            echo 'Very Good';
         } 
          else  if(61=<$res<=70)
         {
            echo 'Good';
         } 
          else  if(51=<$res<=60)
         {
            echo 'Fair';
         } 
          else  if(40=<$res<=50)
         {
            echo 'Pass';
         } 
          else  if(01=<$res<=39)
         {
            echo ' Needs Improvement';
         } 
     else  if($res==0)
         {
            echo ' Needs Improvement';
         } 
    </td>
    </tr>
        ";

        break;
}
?>

7 个答案:

答案 0 :(得分:1)

试试这个:

<?php
$answer = '';
  include('config.php');
  if(isset($_GET["results"]))
 $answer = $_GET["results"];
  $id = $_GET["idk"];
  switch ($answer) 
  {  
    case 'Nursery':
    $qqqs = mysql_query("select * from result where u_id='$id' AND sc='Nursery' ");
    $rows=mysql_fetch_assoc($qqqs);
    $a=$rows['u_id'];
    $b=$rows['name'];
    $c=$rows['fname'];
    $d=$rows['reg'];
    $e=$rows['sc'];
    $f=$rows['ss'];
    $g=$rows['se'];
    $h=$rows['e1'];
    $i=$rows['u1'];
    $j=$rows['m1'];
    $k=$rows['s1'];
    $l=$rows['ss1'];
    $m=$rows['i1'];
    $n=$rows['e2'];
    $o=$rows['u2'];
    $p=$rows['e3'];
    $pp=$rows['u3'];
    $pic=$rows['picture'];
    $total=$h+$i+$j+$k+$l+$m+$n+$o+$p;
    $totals=230;

    $res = ( $totals / $total) * 100;

    // 0 digit after the decimal point
    $res = round($res); // 67

    // 1 digit after the decimal point
    $res = round($res, 1); // 66.7

    // 2 digits after the decimal point
    $res = round($res, 2); // 66.67


    $out = "<html>
    <head>

    </head>
    <body>
    <table align='center' border='4' bgcolor='white' width='500' class='table table-bordered'>
    <tr>
    <td bgcolor='orange' colspan='4'><h2 align='center'>Leads Grammar School</h2><p align='center'>Babar Road, Kirri Jamandan, Multan</p></td>
    </tr>
    <tr>
    <td bgcolor='yellow' colspan='4'><h2 align='center'>Monthly Test Feb-March</h2></td>
    </tr>
    <tr>
     <td colspan='2'><img src='images/$pic' width='100px' height='100px' align='right'></td>
     </tr>
    <tr>
    <td align='right'>Student's Name</td>

    <td colspan='3'>$b</td>
    </tr>
    <tr>
    <td align='right'>Father's Name</td>
    <td colspan='3'> $c</td>

    </tr>
    <tr>
    <td align='right'>Registration</td>
    <td colspan='3'>$d</td>
    </tr>
    <tr>
    <td align='right'>Student Class</td>
    <td colspan='3'>$e</td>
    </tr>
    <tr>
    <td align='right'>Exams</td>
    <td colspan='3'>$f</td>
    </tr>
    <tr bgcolor='gray'>
    <th>Subject Name</th>
    <th>Marks</th>
    <th>Obtained Marks</th>
    <th>Grades</th>
    </tr>
    <tr>
    <tr><td>English(Writing)</td>
    <td>50</td>
    <td> $h</td>

    </tr>
    <tr>
    <tr ><td>Urdu(Writing)</td>
    <td>50</td>
    <td>$i</td>

    </tr>
    <tr>
    <tr ><td>Math(Writing)</td>
    <td>50</td>
    <td> $j</td>

    </tr>
    <tr><th align='center' colspan='3' bgcolor='orange'>General Knowledge</th></tr>
    <tr>
    <tr ><td>Science</td>
    <td>10</td>

    <td> $k</td>

    </tr>
    <tr>
    <tr ><td>S.Studies</td>
    <td>10</td>
    <td>$l</td>
    </tr>

    <tr>
    <tr ><td>Islamiat</td>
    <td>10</td>
    <td>$m</td>
    </tr>

    <tr><th align='center' colspan='3' bgcolor='orange'>Poems</th></tr>
    <tr>
    <tr ><td>English </td>
    <td>15</td>
    <td> $n</td>
    </tr>

    <tr>
    <tr ><td>Urdu</td>
    <td>15</td>
    <td>$o</td>
    </tr>

    <tr><th align='center' colspan='3' bgcolor='orange'>Book Reading</th></tr>
    <tr>
    <tr><td>English </td>
    <td>10</td>
    <td>$p</td>
    </tr>

    <tr>
    <tr >
    <td>Urdu</td>
    <td>10</td>
    <td>$pp</td>
    </tr>
     <tr>
     <td><h1>Total Marks</h1></td>
     <td>230</td>
     <td>$total</td>
     <td>$res</td>
     </tr>
     <tr><td><h2>Remarks</h2></td>
     <td colspan='3'>";

     if(91=<$res<=100)
     {
        $out .= 'Exceptional';
     } 
    else  if(81=<$res<=90)
     {
        $out .= 'Excellent';
     } 
      else  if(71=<$res<=80)
     {
        $out .= 'Very Good';
     } 
      else  if(61=<$res<=70)
     {
        $out .= 'Good';
     } 
      else  if(51=<$res<=60)
     {
        $out .= 'Fair';
     } 
      else  if(40=<$res<=50)
     {
        $out .= 'Pass';
     } 
      else  if(01=<$res<=39)
     {
        $out .= ' Needs Improvement';
     } 
 else  if($res==0)
     {
        $out .= ' Needs Improvement';
     } 
$out .="</td></tr>";
echo $out;
    break;
}
?>

答案 1 :(得分:0)

PHP不支持此语法:

if(01=<$res<=39)

相反,您必须这样做:

if(1 <= $res && $res <= 39)

另外,不要用0开始你的数字,否则它们会被解释为八进制数字(基数为8),这会产生一些奇怪的后果。

仔细检查后编辑

哦......另外你确实不能在echo中包含条件语句。您必须先在代码中构建整个字符串,然后然后回显它。

答案 2 :(得分:0)

只需结束你的第一个echo,然后使用你的if语句来回显剩余的表。

<?php
$answer = '';
  include('config.php');
  if(isset($_GET["results"]))
 $answer = $_GET["results"];
  $id = $_GET["idk"];
  switch ($answer) 
  {  
    case 'Nursery':
    $qqqs = mysql_query("select * from result where u_id='$id' AND sc='Nursery' ");
    $rows=mysql_fetch_assoc($qqqs);
    $a=$rows['u_id'];
    $b=$rows['name'];
    $c=$rows['fname'];
    $d=$rows['reg'];
    $e=$rows['sc'];
    $f=$rows['ss'];
    $g=$rows['se'];
    $h=$rows['e1'];
    $i=$rows['u1'];
    $j=$rows['m1'];
    $k=$rows['s1'];
    $l=$rows['ss1'];
    $m=$rows['i1'];
    $n=$rows['e2'];
    $o=$rows['u2'];
    $p=$rows['e3'];
    $pp=$rows['u3'];
    $pic=$rows['picture'];
    $total=$h+$i+$j+$k+$l+$m+$n+$o+$p;
    $totals=230;

    $res = ( $totals / $total) * 100;

    // 0 digit after the decimal point
    $res = round($res); // 67

    // 1 digit after the decimal point
    $res = round($res, 1); // 66.7

    // 2 digits after the decimal point
    $res = round($res, 2); // 66.67


    echo "<html>
    <head>

    </head>
    <body>
    <table align='center' border='4' bgcolor='white' width='500' class='table table-bordered'>
    <tr>
    <td bgcolor='orange' colspan='4'><h2 align='center'>Leads Grammar School</h2><p align='center'>Babar Road, Kirri Jamandan, Multan</p></td>
    </tr>
    <tr>
    <td bgcolor='yellow' colspan='4'><h2 align='center'>Monthly Test Feb-March</h2></td>
    </tr>
    <tr>
     <td colspan='2'><img src='images/$pic' width='100px' height='100px' align='right'></td>
     </tr>
    <tr>
    <td align='right'>Student's Name</td>

    <td colspan='3'>$b</td>
    </tr>
    <tr>
    <td align='right'>Father's Name</td>
    <td colspan='3'> $c</td>

    </tr>
    <tr>
    <td align='right'>Registration</td>
    <td colspan='3'>$d</td>
    </tr>
    <tr>
    <td align='right'>Student Class</td>
    <td colspan='3'>$e</td>
    </tr>
    <tr>
    <td align='right'>Exams</td>
    <td colspan='3'>$f</td>
    </tr>
    <tr bgcolor='gray'>
    <th>Subject Name</th>
    <th>Marks</th>
    <th>Obtained Marks</th>
    <th>Grades</th>
    </tr>
    <tr>
    <tr><td>English(Writing)</td>
    <td>50</td>
    <td> $h</td>

    </tr>
    <tr>
    <tr ><td>Urdu(Writing)</td>
    <td>50</td>
    <td>$i</td>

    </tr>
    <tr>
    <tr ><td>Math(Writing)</td>
    <td>50</td>
    <td> $j</td>

    </tr>
    <tr><th align='center' colspan='3' bgcolor='orange'>General Knowledge</th></tr>
    <tr>
    <tr ><td>Science</td>
    <td>10</td>

    <td> $k</td>

    </tr>
    <tr>
    <tr ><td>S.Studies</td>
    <td>10</td>
    <td>$l</td>
    </tr>

    <tr>
    <tr ><td>Islamiat</td>
    <td>10</td>
    <td>$m</td>
    </tr>

    <tr><th align='center' colspan='3' bgcolor='orange'>Poems</th></tr>
    <tr>
    <tr ><td>English </td>
    <td>15</td>
    <td> $n</td>
    </tr>

    <tr>
    <tr ><td>Urdu</td>
    <td>15</td>
    <td>$o</td>
    </tr>

    <tr><th align='center' colspan='3' bgcolor='orange'>Book Reading</th></tr>
    <tr>
    <tr><td>English </td>
    <td>10</td>
    <td>$p</td>
    </tr>

    <tr>
    <tr >
    <td>Urdu</td>
    <td>10</td>
    <td>$pp</td>
    </tr>
     <tr>
     <td><h1>Total Marks</h1></td>
     <td>230</td>
     <td>$total</td>
     <td>$res</td>
     </tr>
     <tr><td><h2>Remarks</h2></td>
     <td colspan='3'>";
     if(91=<$res<=100)
     {
        echo 'Exceptional';
     } 
    elseif(81=<$res<=90)
     {
        echo 'Excellent';
     } 
      else  if(71=<$res<=80)
     {
        echo 'Very Good';
     } 
      elseif(61=<$res<=70)
     {
        echo 'Good';
     } 
      elseif(51=<$res<=60)
     {
        echo 'Fair';
     } 
      elseif(40=<$res<=50)
     {
        echo 'Pass';
     } 
      else  if(01=<$res<=39)
     {
        echo ' Needs Improvement';
     } 
 elseif($res==0)
     {
        echo ' Needs Improvement';
     } 
echo "</td>";
echo "</tr>";

    break;
}
?>

答案 3 :(得分:0)

您可以在echo:

中使用三元运算符
echo "start of string....".(bool_condition1 ? "condition is true" : "condition is false").".... more string";

答案 4 :(得分:0)

是的,您可以通过连接:

在echo中使用if / else条件
$x = 10;
echo "X is " . ($x==10? "equal" : "not equal") . " to 10";

我还建议您创建一个返回评估分数的字符串的函数。 尝试将尽可能少的逻辑混合到演示文稿中。

答案 5 :(得分:0)

if内不能有echo声明。你可以做的是将所有if移到回声之外,并将结果存储在变量中。例如:

if(91 <= $res && $res <= 100) // Note how the condition *should* be written.
{
  $remark = 'Exceptional';
} 
else  if(... 

然后在表输出中使用该变量:

 <tr><td><h2>Remarks</h2></td>
 <td colspan='3'>$remark

答案 6 :(得分:0)

您还可以将三元运算符与自定义函数结合使用:

echo "fixed part".(inbetween($res,91,100)?'Exceptional':(inbetween($res,81,90)?'Excellent':'SOMETHING ELSE'));

function inbetween($int,$min,$max)
{
   return ($int>=$min && $int<=$max);
}

这个三元运算符的语法可以在以下示例中看到:

echo "This is the result: " . ( $testiftrue ? 'TRUE' : 'FALSE' );

这将测试条件$ testiftrue,然后显示'TRUE'或'FALSE'