簡體   English   中英

php空數組檢查

[英]Php empty array checking

我有一個下面的HTML表,其中的名稱包含數組。 如何檢查此數組的值是否為空?

echo "<input type='radio' name='ch[$roll][$sname][$class]' value='1' />&nbsp;";
echo "<input type='radio' name='ch[$id][$sname][$class]' value='0' />";

目前,我正在用以下代碼進行檢查,它無法正常工作,但我知道名稱是數組,必須將其與任何數組函數進行比較。 你們能給我個主意嗎?

if(isset($_POST['ch']))
{
   $ch = $_POST['ch'];
   if(empty($ch))
    echo "<div class='error>Select attendence field. </div>";   
}

問候。

更新:(完整代碼)

$action = htmlspecialchars($_SERVER['PHP_SELF'])."?class=$class_from";  
echo "<form method='post' action='$action' name='attendence'/>";

echo "<table width='100%' cellpadding='0' cellspacing='0' border='0'>";
echo "<tr>";
echo "<td class='tdhead' valign='top' width='200'><b>Student Name</b></td>";    
echo "<td class='tdhead' valign='top' width='250'><b>Roll No</b>
</td>";             
echo "<td class='tdhead' valign='top' width='250'><b>Class Name</b>
</td>";             
echo "<td class='tdhead' valign='top' width='200'><b>Present / Not present</b>
</td>";                 
echo "<td class='tdhead' valign='top' width='200'>
Present All <input type= 'checkbox' 
onclick='checkAll(this)'</td>";                 
echo "</tr>";

//start the counter variable
$counter = 1;
while($res2 = mysql_fetch_array($sql2))
{
$id = (int) $res2['id'];
$sname = inputvalid($res2['sname']);
$roll = inputvalid($res2['roll']);
$class = inputvalid($res2['class']);

echo "<tr>";
echo "<td class='tdhead2' valign='top'>$sname</td>";
echo "<td class='tdhead2' valign='top'>$roll</td>";
echo "<td class='tdhead2' valign='top'>$class</td>";
echo "<td class='tdhead2' valign='top'>";

//echo each radio with the counter for this row
echo "<input type='radio' name='ch[$roll][$sname][$class]' value='1' />&nbsp;";
echo "<input type='radio' name='ch[$id][$sname][$class]' value='0' />";

echo "</td>";
echo "<td class='tdhead2' valign='top'>&nbsp;</td>";
echo "</tr>";

//add one to the counter
$counter++;
}

echo "<tr>";                
echo "<td class='tdhead2'>&nbsp;</td>";
echo "<td class='tdhead2'>&nbsp;</td>";
echo "<td class='tdhead2'>&nbsp;</td>";             
echo "<td class='tdhead2'>&nbsp;</td>";
echo "<td class='tdhead2'><input type='submit' value='Record' name='Submit' 
class='submit' /></td>";                
echo "</tr>";               

echo "</table>";
echo "</form>";


if(isset($_POST['Submit']) && $_POST['Submit'] == "Record")
{
if(isset($_POST['ch']))
{

 $ch = array_filter($_POST['ch']);

if (empty($ch))
     {
     echo "<div class='error>Select attendence field. </div>";   
 }
  }

  if(count($ch) == 0)
  {
echo "<div class='error>Select attendence field. </div>";   
}   
else
{

foreach ($_POST['ch'] as $roll => $arr1)
{
    $roll;
    foreach ($arr1 as $name => $arr2)
    {
    $name;
    foreach ($arr2 as $class => $value)
    {
    $class;
    $value;

 $sql = mysql_query("INSERT INTO e_attendence VALUES('', '$name', '$roll', '$class', 
'$value', '$current_date')");
    }
}
  }
}

if($sql)                        
echo "<div class='success'>Succesfully recorded.   
</div>";                                    

}   

使用empty()代替isset()

檢查以下鏈接提供的比較表

http://php.net/manual/zh/types.comparisons.php

empty() method work for finding whether the array is empty or not.

但為您的解決方案使用以下:

if(isset($_POST['ch']) && $_POST['ch']=='1')
{
   $ch = $_POST['ch'];
   if(empty($ch))
   echo "<div class='error>Select attendence field. </div>";   
}

如果您想在未選擇任何廣播時顯示錯誤消息,請使用以下代碼:-

if(!isset($_POST['ch']) && $_POST['ch']=='')
{
     echo "You have not selected any radio button";
}

嘗試這個

if(isset($_POST['ch'])){

   $ch = array_filter($_POST['ch']);

   if (empty($ch)) {
     echo "<div class='error>Select attendence field. </div>";   
   }
}

嘗試這個

print_r($_POST['ch']);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM