0

我遇到了一行代码的问题。我的老师甚至没有看到这个问题,而我已经为此奋斗了将近一个半星期。任何帮助将不胜感激。

编码:

{
  if (count($_POST['CINS']) > 0)
  {
    echo "<h2>Your CINS picks are:</h2>\n";
    echo "<ul>\n"; 
    foreach ($_POST['CINS'] as $element)
    {
      echo "\t<li>$element</li>\n";
    } // end of FOREACH statement
    echo "</ul>\n";
  } // end of IF count CINS

  if (count($_POST['CINT']) > 0 ) 
  {
    echo "<h2>Your CINT picks are:</h2>\n";
    echo "<ul>\n"; 
    foreach ($_POST['CINT'] as $element2)
    {
      echo "\t<li>$element2</li>\n";
    } // End of FOREACH CINT
    echo "</ul>\n";
  } // End of IF for CINT
  else
  {
    echo "CINT = " . count($_POST['CINT']) . " CINS = " . count($_POST['CINS']) . "<br />\n";
    echo __LINE__;
    if ((count($_POST['CINT'] == 0)) and (count($_POST['CINS'] == 0)))  // This is where the problem lies. It's showing up the echo statements even when CINS has a count of 1. but if CINT has a count of 1, the echo statements do not show up. 
    {
      echo "<h2>No classes</h2>\n";
      echo "<p>You need to pick a class from BOTH CINT and CINS to be a well rounded student.</p>\n";
      echo "CINT = " . count($_POST['CINT']) . " CINS = " . count($_POST['CINS']) . "<br />\n";
    }
  } // END ELSE COUNT CINS
}
?>
4

4 回答 4

3

错位的括号

if ((count($_POST['CINT'] == 0)) and (count($_POST['CINS'] == 0))) -> wrong

if ((count($_POST['CINT']) == 0) and (count($_POST['CINS']) == 0))
于 2013-03-02T04:59:07.097 回答
1

您不需要所有这些括号:

if(
    count($_POST['CINT']) == 0 AND
    count($_POST['CINS']) == 0
    )

看看一些缩进如何走很长的路:

{
    if (count($_POST['CINS']) > 0) {
        echo "<h2>Your CINS picks are:</h2>\n";
        echo "<ul>\n"; 
        foreach ($_POST['CINS'] as $element) {
            echo "\t<li>$element</li>\n";
        }

        echo "</ul>\n";
    }

    if (count($_POST['CINT']) > 0 ) {
        echo "<h2>Your CINT picks are:</h2>\n";
        echo "<ul>\n"; 
        foreach ($_POST['CINT'] as $element2) {
            echo "\t<li>$element2</li>\n";
        }

        echo "</ul>\n";
    }
else {
    echo "CINT = " . count($_POST['CINT']) . " CINS = " . count($_POST['CINS']) . "<br />\n";
    echo __LINE__;

    if(
        count($_POST['CINT']) == 0 AND
        count($_POST['CINS']) == 0
        ) {
        echo "<h2>No classes</h2>\n";
        echo "<p>You need to pick a class from BOTH CINT and CINS to be a well rounded student.</p>\n";
        echo "CINT = " . count($_POST['CINT']) . " CINS = " . count($_POST['CINS']) . "<br />\n";
    }
}
于 2013-03-02T05:08:42.647 回答
0

也许括号需要注意......

您的

if ((count($_POST['CINT'] == 0)) and (count($_POST['CINS'] == 0)))

可能会更好

if ((count($_POST['CINT']) == 0) && (count($_POST['CINS']) == 0)) 

试一试 - 用结果回复。

于 2013-03-02T05:10:25.353 回答
0

我想,你想要这个:

if (count($_POST['CINS']) > 0)
{
    …
} // End of IF for CINS

if (count($_POST['CINT']) > 0 ) 
{
    …
} // End of IF for CINT

if (count($_POST['CINS']) == 0 || count($_POST['CINT']) == 0 ) 
{
    …
}
于 2013-03-02T05:03:52.733 回答