Warning: Invalid argument supplied for foreach()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rashgang
    New Member
    • Oct 2008
    • 17

    Warning: Invalid argument supplied for foreach()

    no check box selected when i gave delete all link the error is coming


    Code:
    <?php
     include "includes/connection.php";
     include "includes/Functions_category.php";
     include "includes/functions_db.php";
     $Strtitle="Category Management (Category Listing)";
     $strAddedDate=date("Y-m-d h:m:s");
     
        if($_POST['frmcheckall']){    
           print doDeleteAll($_POST);
    	   
       }
           if(isset($_GET['id'])){
        //if($_POST['id']){
        //$strDelete=doDeleteAll['id'];
       // }
        if(!doDelete($_GET['id'])){$strMessage= "Invalid Inputs Detected!";}
       }
    include"include s/pagestart.php"; ?>

    Code:
    <link href="css/fonts.css" rel="stylesheet" type="text/css">
    <script type="text/javascript">
    function checkAll(field){
     field=document.frmcategory.frmcheckall;
     for (i = 0; i < field.length; i++){
     field[i].checked = true;
     highlightRow('frmcheckall'+i,'frmcheck'+i);
     }
     }
    function uncheckAll(field){
     field=document.frmcategory.frmcheckall;
     for (i = 0; i < field.length; i++){
     field[i].checked = false;
     highlightRow('frmcheckall'+i,'frmcheck'+i);
     }
     }
    /*function deleteall(field){
    
        field=document.frmcateogry.frmcheckall;
     flag=0;
     for(i=0;i< field.length;i++){
      
      if(field[i].checked)
      flag=1;
      }
     if(flag==1){
       alert("Hi");
       document.frmcateogry.frmcheckall.submit();
     }else
       alert("Please select one category to Delete");
     }*/
     function deleted(field){
     //field=document.frmcategory.frmcheckall;
      flag=0;
      for(i=0;i< field.length;i++){
       if(field[i].checked)
       flag=1;
      }
      
      if(flag==1){
       if(confirm("Are You Sure To Delete The Category List?")){
       field.submit();
       }
      }else
       alert("Please Select One Category To DELETE");
     }
     function highlightRow(id,id1) { //alert(id1);
     
      if (document.getElementById(id).checked) {
      
       document.getElementById(id1).bgColor = '#E2E6A8';
      }
      else {
      
       document.getElementById(id1).bgColor = '#FFFFFF';
      }
      }
     
    function deleteConfirm(delUrl) {
      if (confirm("Are You Sure To Delete This Category?")) {
        document.location = delUrl;
        }
      }
    </script>
    <form name="frmcategory" method="post" action="">
    
      <table width="90%" border="0" cellspacing="3" cellpadding="3" align="center">
      <tr><td><? print $strMessage ;?></td></tr>
        <tr>
          <td align="center">
            <?
      $strQuery="select * from tbl_categories where IsDeleted='No'";
      $strValue=SelectQry($strQuery);
       if(is_array($strValue)){
       ?>
            <span class="tablehead">Category List</span></td>
        </tr>
      </table>
      <div align="center">&nbsp;
        <table width="414" height="20" border="0" cellpadding="0" cellspacing="0" class="size">
          <tr>
            <td width="414" class="headerfont"><a href="javascript:onclick=checkAll();" class="headerfont">SELECT
              ALL | </a><a href="javascript:onclick=uncheckAll();" class="headerfont">UNSELECT
              ALL </a>| <a href="javascript:onclick=deleted(frmcategory);" class="headerfont">DELETE
              SELECTED </a></td>
          </tr>
        </table>
      </div>
      <table width="421" border="1" align="center" cellpadding="5" cellspacing="0" bordercolor="#999999" class="headerfont" style="border-collapse:collapse">
        <tr class="tablehead">
          <td width="44" height="29">&nbsp;</td>
          <td width="278" align="center">CATEGORY NAME </td>
          <td width="179" align="center">ACTION </td>
        </tr>
        <?
     //while($rows = mysql_fetch_object($stredit)){
     for($i=0;$i<count($strValue);$i++) {
      ?>
        <tr class="tablehead" id="frmcheck<?php print $i; ?>">
          <td height="25" align="center"><input name="frmcheckall" type="checkbox" id="frmcheckall<?php print $i; ?>" value="<? print $strValue[$i]['CategoryId']; ?>" onclick="highlightRow('frmcheckall<?php print $i; ?>','frmcheck<?php print $i; ?>');" />
          </td>
          <td class="tablecontent"><? print $strValue[$i]['CategoryName']; ?></td>
          <td align="center" class="tablecontent"><a href="edit.php?id=<? print $strValue[$i]['CategoryId']; ?>">Edit</a>
            | <a href="javascript:deleteConfirm('cateogrylist.php?id=<? print $strValue[$i]['CategoryId']; ?>')">Delete
            </a> </td>
        </tr>
        <?
     }
    //mysql_free_result($stredit);
    ?>
        <tr align="left" class="tablehead">
          <td height="25" colspan="3"><span class="tablecontent2"><a href="javascript:onclick=checkAll();" class="size">Select
            all | </a><a href="javascript:onclick=uncheckAll();" class="size">UnSelect
            All</a> | <a href="javascript:onclick=deleted(frmcategory);" class="size">Delete
            SELECTED </a></span>
            <div align="center"></div>
            <div align="center"></div></td>
        </tr>
        <? }else{?>
        <font face="Verdana, Arial, Helvetica, sans-serif" size="+1" color="#FF0000">
        <center>
          <? print "Sorry!No Records Found !!!";?>
        </center>
        </font>
        <? }?>
      </table>
     
      <p class="headerfont">&nbsp;</p>
    </form>
    <? include "includes/pageend.php"; ?>


    and this is the function category coding

    Code:
    <?
    function CheckCategory($objArray){ 
    	$QryCheck="SELECT * 
    	            FROM 
    				tbl_categories 
    				WHERE
    				CategoryName ='".$objArray["frmCategoryName"]."' 
    				AND 
    				CategoryId <> '" .$_GET['id']."' 
    				AND IsDeleted='No'";
    	$ResCheck = SelectQry($QryCheck);//Calling the Functions_db
    	if(is_array($ResCheck))
    		return 1;
    	else 
    		return 0;
    }
    
    function InsertCategory($objArray){
    	$strAddedDate = date("Y-m-d h:m:s");
    	$strAddedIP = $_SERVER['REMOTE_ADDR'];
    	$QryInsert 	=	"INSERT 
    						 INTO 
    							tbl_categories(
    							CategoryName,
    							CategoryDescription,
    							AddedDate,
    							AddedIP
    							) 
    						 VALUES 
    							('".$objArray["frmCategoryName"]."',
    							'".$objArray["frmCategoryDescription"]."',
    							'".$strAddedDate."',
    							'".$strAddedIP."')";
    
    	if(ExecuteQry($QryInsert))//calling the functions Execute Query
    		return 1;
    	else 
    		return 0;
    		
    }
    function getCategoryList()	{
    	$QryCategory = "SELECT *
    				    FROM 
    				  	  tbl_categories
    				    ORDER BY
    				     CategoryId ";
    	$ResultCategory = SelectQry($QyrCategory);
    	if(is_array($ResultCategory))	{
    		return $ResultCategory;
    	}
    	else {
    		return 0;
    	}	
    }
    
    function CheckUpdateCategory($objArray) {
    	$Query = "UPDATE 
    				  tbl_categories
    				  SET
    					   CategoryName = '".$objArray["frmCategoryName"]."',
    					   CategoryDescription = '".$objArray["frmCategoryDescription"]."',
    					   AddedDate = '".$strAddedDate."'
    				  WHERE
    					   tbl_categories. CategoryId= '".$_GET['id']."'
    				  ";
    	
    		if(ExecuteQry($Query))
    			return 1;
    		else
    			return 0;
    	}
    function doDeleteAll($objArray){
    
    
    	foreach($objArray['frmcheckall'] as $strkey=>$strValue) {
    			$strDeleteId = $strValue;
    			
    			 doDelete($strDeleteId);
    			  //print_r($strDeleteId);
    						/*$strQuery=  "UPDATE 
    			               tbl_categories  
    						  SET 
    						      IsDeleted = 'Yes' 
    						  WHERE
    						      Ident =".$strDeleteId." 
    						   LIMIT 1";*/
    						  
    						  //doDelete($objGetArray['id']);
    						 
    /*if(doDelete($objGetArray['id'])){
    			return 1;
    			//print_r(($objGetArray['id']));
    			}
    			else{
    			return 0;}*/
    		}
    	}
    
    /*function doDelete($objGetArray){
    	if(is_numeric($objGetArray['id'])){
    		$strQuery="SELECT * 
    		           FROM
    					      tbl_categories 
    				   WHERE 
    					  Ident=".$objGetArray['id'];
    		$strRows = SelectQry($strQuery);
    		if(is_array($strRows)){
    			$strQuery = "UPDATE
    			                 tbl_categories 
    						 SET 
    						      IsDeleted = 'Yes' 
    						 WHERE  
    						      Ident =".$objGetArray['id']." 
    						 LIMIT 1";
    			ExecuteQry($strQuery);
    			return 1;
    		}else{
    			return 0;
    		}
    	}else{
    			return 0;
    	}
    } */
    function doDelete($strDeleteId){
    //print_r($objGetArray);
    //if($objGetArray['id']){
    $strQuery = "UPDATE
    			             tbl_categories 
    						 SET 
    						      IsDeleted = 'Yes' 
    						 WHERE  
    						      CategoryId = '".$strDeleteId."'";
     // print_r($strQuery);
    		if(ExecuteQry($strQuery))
    			return 1;
    		else
    			return 0;
    		
    	}
    
    function CheckId($objArray) {
    $strAddedDate=date("Y-m-d h:m:s");
    $strIp=$_SERVER['REMOTE_ADDR'];
    		if(is_numeric($_GET[id])){
    				$Query = "SELECT * 
    						  FROM
    						       tbl_categories 
    						  WHERE  
    						        CategoryId=".$_GET['id'] ;
    				$strRecords = SelectQry($Query );
    				if(!is_array($strRecords)) {
    					return 0;
    				}else{
    					ExecuteQry($Query);
    					return $strRecords;
    				}
    			}
    }
    
    ?>
    Last edited by Markus; Jan 13 '09, 02:08 PM. Reason: Fixed code tags.
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    First of all, we don't need every file you use. The error should point to a specific file (and also line number) - something which you have neglected to include. Secondly, I don't see a foreach loop anywhere, but that's maybe because my brain hurts from trying to browse all that code you supplied. Thirdly, read the Posting Guidelines pay attention on the part on how to ask a question. You have provided barely any information that could be of use and, therefore, you will be lucky to receive any help.

    Please revise your post and then ask your question again.

    PS: The error is because you aren't passing an array to a foreach loop.

    Comment

    • Markus
      Recognized Expert Expert
      • Jun 2007
      • 6092

      #3
      Rashgang, is there a reason you are double posting your thread after being told previously not to do so? You are already on thin lines for your disregard to our forum guidelines.

      I'm giving you the benefit of the doubt here: this is your final warning before receiving a ban or some sort.

      Comment

      • rashgang
        New Member
        • Oct 2008
        • 17

        #4
        ok here after i will not double posting

        Comment

        • nathj
          Recognized Expert Contributor
          • May 2007
          • 937

          #5
          Hi,

          First of al I agree with Markus - a pointer to the line would have been nice. I have, using a ctrl+f tracked down the line.

          For those interested it's line 76 the third code listing

          I recommend you change the first parameter to $objArray, that should do you.

          Cheers
          nathj

          Comment

          • rashgang
            New Member
            • Oct 2008
            • 17

            #6
            not helpful plz where to change tell me exactly

            Comment

            • nathj
              Recognized Expert Contributor
              • May 2007
              • 937

              #7
              I'm not sure if something has happened when threads have been merged but the line and code sample I was referring to are no longer present.

              I'm also not sure what the problem is, this is the main issue with double posting.

              If you could post back with a small relevant code sample indicating the following:

              1. what the code should do
              2. what the code is not doing that it should do
              3. what the code is doing that it should not do
              4. any error messages you are getting with line numbers (based on the code snippet)

              With that information I'm sure we'll be able to help you out pretty quickly.

              Cheers
              nathj

              Comment

              • nathj
                Recognized Expert Contributor
                • May 2007
                • 937

                #8
                Originally posted by rashgang
                not helpful plz where to change tell me exactly
                line 76 of the third code sample.

                currently this read:
                Code:
                foreach($objArray['frmcheckall'] as $strkey=>$strValue) {
                Try making it:
                Code:
                foreach($objArray as $strkey=>$strValue) {
                Last edited by nathj; Nov 19 '08, 02:04 PM. Reason: adding code sample

                Comment

                • rashgang
                  New Member
                  • Oct 2008
                  • 17

                  #9
                  i have given the checkbox in dynamic and highlight the row and when i click check all the error is coming tells that warning : foreach invaid argument in line no 75
                  i cant check the checkboxes

                  Comment

                  • nathj
                    Recognized Expert Contributor
                    • May 2007
                    • 937

                    #10
                    Please supply line 75 of your code for us to look at. It's impossible to diagnose a syntax error like this without seeing the syntax you have used.

                    nathj

                    Comment

                    • rashgang
                      New Member
                      • Oct 2008
                      • 17

                      #11
                      Code:
                      function doDeleteAll(){
                          foreach($objArray['frmcheckall'] as $strkey=>$strValue) {
                                  $strDeleteId = $strValue;
                                     doDelete($strDeleteId);
                      Last edited by Markus; Jan 13 '09, 02:06 PM. Reason: Fixed code tags. Rashgang, please read your PMs

                      Comment

                      • Markus
                        Recognized Expert Expert
                        • Jun 2007
                        • 6092

                        #12
                        Originally posted by rashgang
                        Code:
                        function doDeleteAll(){
                            foreach($objArray['frmcheckall'] as $strkey=>$strValue) {
                                    $strDeleteId = $strValue;
                                       doDelete($strDeleteId);
                        Is $objArray['frmcheckall'] an array? It has to be for foreach to work.

                        Comment

                        • rashgang
                          New Member
                          • Oct 2008
                          • 17

                          #13
                          yes frmcheckall is an array. how to take screen shot and send the error page to u

                          Comment

                          • rashgang
                            New Member
                            • Oct 2008
                            • 17

                            #14
                            Problem : no check box selected

                            select all and unselect all working but when i click delete selected i got error
                            Code:
                            warning: Invalid argument supplied for foreach() in /home/webuser/html/training/arunkarthik/siteadmins/includes/Functions_category.php
                            on line 77 i am doing like yahoo mail first check all and delete the selected one

                            Comment

                            • code green
                              Recognized Expert Top Contributor
                              • Mar 2007
                              • 1726

                              #15
                              Why are you not paying attention to the help given???
                              Your problem has been answered but you are completeley ignoring the comments
                              As Markus asked
                              Is $objArray['frmcheckall'] an array? It has to be for foreach to work
                              As you completeley ignored him I will answer for you.
                              No it isn't, because nathj suggestion
                              line 76 of the third code sample.
                              currently this read:
                              Code:
                              foreach($objArray['frmcheckall'] as $strkey=>$strValue) {
                              Try making it:
                              Code:
                              foreach($objArray as $strkey=>$strValue) {
                              Points out the problem
                              As you have totally ignored them I will repeat

                              this line calls doDeleteAll() in which the WHOLE $_POST array is passed
                              Code:
                               
                              if($_POST['frmcheckall']){     
                                     print doDeleteAll($_POST);
                              Yet in the function you are trying to loop ONE element only
                              Code:
                               
                              foreach($objArray['frmcheckall'] as $strkey=>$strValue)
                              Carry out nathj suggestion then we will look at your checkboxes

                              Comment

                              Working...