0

I've a following html table which name contain array. How can i check this array if it's value is empty ?

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

Currently i'm checking it with following code, it's not working but i know the name is array and it's must be compare with any array function. Can you guys give me a idea ?

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

Regards.

Update: (Full Code)

$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>";                                    

}   
Alex
  • 21
  • 1
  • 7

4 Answers4

0

use empty() instead of isset()

check the comparison table available at the below link

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

Nishant
  • 3,614
  • 1
  • 20
  • 26
0
empty() method work for finding whether the array is empty or not.

But for Your solution use below:-

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

if You want to show a error message when no radio is selected then use below code:-

if(!isset($_POST['ch']) && $_POST['ch']=='')
{
     echo "You have not selected any radio button";
}
Ripa Saha
  • 2,532
  • 6
  • 27
  • 51
  • @ripa it's a 2 radio button. User have to choose only one. But if user didn't select any radio button then I want to show a error message. – Alex Sep 12 '13 at 05:30
  • @DipeshParmar you right, I want to check that user actually check only one radio button. If user didn't check any radio button then i want to show a error message. – Alex Sep 12 '13 at 05:32
  • @Alex can you tell me what are the value for `$roll` and `$id`..? – Dipesh Parmar Sep 12 '13 at 05:33
  • check that first print_r($_POST['ch']);. This is best way to check and then you can do that as you wish. If you get array then use count function. – user2727841 Sep 12 '13 at 05:39
  • @Alex remove $ch = $_POST['ch']; if(empty($ch)), these 2 lines. and upvote my answer. – Ripa Saha Sep 12 '13 at 05:56
  • @ripa One more help about the check box. I want a option to select all the radio button by once. is it possible with javascript ? – Alex Sep 12 '13 at 06:14
  • post new question. I'll give answer. And upvote this answer first. – Ripa Saha Sep 12 '13 at 06:15
  • @ripa i'm doing it now. – Alex Sep 12 '13 at 06:17
  • @ripa check it...http://stackoverflow.com/questions/18756831/javascript-select-all-radio-button-by-once – Alex Sep 12 '13 at 06:24
  • do You know how to "upvote" an answer? just click on upper arrow. – Ripa Saha Sep 12 '13 at 06:28
0

Try this

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

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

   if (empty($ch)) {
     echo "<div class='error>Select attendence field. </div>";   
   }
}
Baby Groot
  • 4,637
  • 39
  • 52
  • 71
rohitcopyright
  • 362
  • 1
  • 2
  • 9
0

try this

print_r($_POST['ch']);
user2727841
  • 715
  • 6
  • 21