Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Mar 2009
    Posts
    16
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Nested foreach loop and array

    Hi,
    Is me again. I have a question that when I do the nested foreach loop in the coding, I get an error such as 'Invalid argument supplied for foreach', I knew this can be happened if I try to do a foreach on an variable that isn't an array. But how to solve it? I not really sure about it,can someone kindly give some suggestion? Thanks.....

    <?php
    //Connect to the MySQL server
    $db_host = "localhost";
    $db_username = "root";
    $db_password = "fsktm";
    $server = mysql_connect($db_host,$db_username,$db_password)or die("Could not connect!/n");

    //Select a database that has created
    $db_name = "cinema";
    mysql_select_db($db_name)or die("Could not select the database $db_name!\n");

    //Retrieve or select the data
    $category = array('CM1','HR1','AM1','AV1','AC1','SF1','DM1','RM1');

    foreach($category as $genres)
    {
    $retrieve = mysql_query("SELECT * FROM movie WHERE category_id = $genres");

    foreach($retrieve as $result)
    {
    echo"<table border = 1 width = 100% height = 100% style = 'background-color:#F0F8FF;'>
    <tr>
    <th>Movie_ID</th>
    <th>Movie_Name</th>
    <th>Duration</th>
    <th>Description</th>
    <th>Category_ID</th>
    </tr>";

    //Display the data from the database in a table using aray
    while($row = mysql_fetch_array($result))
    {
    echo "<tr>";
    echo"</td><td>";
    echo $row['movie_id'];
    echo "</td><td>";
    echo $row['movie_name'];
    echo "</td><td>";
    echo $row['duration'];
    echo "</td><td>";
    echo $row['description'];
    echo "</td><td>";
    echo $row['category_id'];
    echo "</tr>";
    }

    echo "</table>";
    }
    }

    mysql_close($server);
    ?>

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,852
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Please wrap PHP codes using the [PHP][/PHP] tag. You may edit your above post. Check the manual for examples and related function to know about php-mysql DB operations. See http://php.net/mysql_fetch_array
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • Users who have thanked abduraooft for this post:

    pelehelp (03-23-2009)

  • #3
    New Coder
    Join Date
    Mar 2009
    Posts
    16
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Very sorry ya cos I dono that my code is quite messy and I dono that it has the way to wrap PHP codes using the tag. Thanks for telling me.

    <?php
    //Connect to the MySQL server
    $db_host = "localhost";
    $db_username = "root";
    $db_password = "fsktm";
    $server = mysql_connect($db_host,$db_username,$db_password)or die("Could not connect!/n");

    //Select a database that has created
    $db_name = "cinema";
    mysql_select_db($db_name)or die("Could not select the database $db_name!\n");

    //Retrieve or select the data
    $category = array('CM1','HR1','AM1','AV1','AC1','SF1','DM1','RM1');

    foreach($category as $genres)
    {
    $retrieve = mysql_query("SELECT * FROM movie WHERE category_id = $genres");

    foreach($retrieve as $result)
    {
    PHP Code:
    echo"<table border = 1 width = 100% height = 100% style  = 'background-color:#F0F8FF;'>
                           <tr>
                           <th>Movie_ID</th>
                           <th>Movie_Name</th>
                           <th>Duration</th>
                           <th>Description</th>
                           <th>Category_ID</th>
                           </tr>"

    //Display the data from the database in a table using aray
    while($row = mysql_fetch_array($result))
    {
    PHP Code:
    echo "<tr>"
    PHP Code:
    echo"</td><td>"
    PHP Code:
    echo $row['movie_id']; 
    PHP Code:
    echo "</td><td>"
    PHP Code:
    echo $row['movie_name']; 
    PHP Code:
    echo "</td><td>"
    PHP Code:
    echo $row['duration']; 
    PHP Code:
    echo "</td><td>"
    PHP Code:
    echo $row['description']; 
    PHP Code:
    echo "</td><td>"
    PHP Code:
    echo $row['category_id']; 
    PHP Code:
    echo "</tr>"
    }

    PHP Code:
    echo "</table>"
    }
    }

    mysql_close($server);
    ?>

  • #4
    New Coder
    Join Date
    Mar 2009
    Posts
    16
    Thanks
    9
    Thanked 0 Times in 0 Posts
    So now can you kindly give some guidelines on how to solve the problem with the error display as such 'Invalid argument supplied for foreach()'?

  • #5
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    use [ php] and [ /php] like this, please:
    Quote Originally Posted by pelehelp View Post
    Hi,
    Is me again. I have a question that when I do the nested foreach loop in the coding, I get an error such as 'Invalid argument supplied for foreach', I knew this can be happened if I try to do a foreach on an variable that isn't an array. But how to solve it? I not really sure about it,can someone kindly give some suggestion? Thanks.....

    PHP Code:
    <?php
        
    //Connect to the MySQL server
        
    $db_host "localhost";
        
    $db_username "root";
        
    $db_password "fsktm";
        
    $server mysql_connect($db_host,$db_username,$db_password)or die("Could not connect!/n");
            
        
    //Select a database that has created
        
    $db_name "cinema";
        
    mysql_select_db($db_name)or die("Could not select the database $db_name!\n");
            
        
    //Retrieve or select the data
        
    $category = array('CM1','HR1','AM1','AV1','AC1','SF1','DM1','RM1'); 
        
        foreach(
    $category as $genres)
        {
            
    $retrieve mysql_query("SELECT * FROM movie WHERE category_id = $genres");
        
            foreach(
    $retrieve as $result)
            {
                echo
    "<table border = 1 width = 100% height = 100% style = 'background-color:#F0F8FF;'>
                <tr>
                <th>Movie_ID</th>
                <th>Movie_Name</th>
                <th>Duration</th>
                <th>Description</th>
                <th>Category_ID</th>
                </tr>"
    ;
            
                
    //Display the data from the database in a table using aray
                
    while($row mysql_fetch_array($result))
                {
                    echo 
    "<tr>";
                    echo
    "</td><td>";
                    echo 
    $row['movie_id'];
                    echo 
    "</td><td>";
                    echo 
    $row['movie_name'];
                    echo 
    "</td><td>";  
                    echo 
    $row['duration'];
                    echo 
    "</td><td>";  
                    echo 
    $row['description'];
                    echo 
    "</td><td>";
                    echo 
    $row['category_id'];
                    echo 
    "</tr>";
                }
          
                echo 
    "</table>";
             }
        }

        
    mysql_close($server);
    ?>
    you have problem with this foreach:
    PHP Code:
            $retrieve mysql_query("SELECT * FROM movie WHERE category_id = $genres");
        
            foreach(
    $retrieve as $result)
            {
    ?> 
    $retrive is not a array, you don't need to use foreach here but you must check if is valid, so replace it with a if:
    PHP Code:
            $retrieve mysql_query("SELECT * FROM movie WHERE category_id = $genres");
            if(
    $retrieve){
             echo
    "<table border = 1 width = 100% height = 100% style = 'background-color:#F0F8FF;'>
                <tr>
                <th>Movie_ID</th>
                <th>Movie_Name</th>
                <th>Duration</th>
                <th>Description</th>
                <th>Category_ID</th>
                </tr>"
    ;
                 
    //Display the data from the database in a table using aray
                
    while($row mysql_fetch_array($retrieve)) 
    best regards

  • Users who have thanked oesxyl for this post:

    pelehelp (03-23-2009)

  • #6
    New Coder
    Join Date
    Mar 2009
    Posts
    16
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Thanks for ur guide...It seems logically but now I cant even print out the output, so I wondering where is the error? Can you kindly suggest a way to solve it? I think that sth wrong with "$genres" in line $retrieve = mysql_query("SELECT * FROM movie WHERE category_id = $genres"); which it may not support string type.

  • #7
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by pelehelp View Post
    Thanks for ur guide...It seems logically but now I cant even print out the output, so I wondering where is the error? Can you kindly suggest a way to solve it? I think that sth wrong with "$genres" in line $retrieve = mysql_query("SELECT * FROM movie WHERE category_id = $genres"); which it may not support string type.
    if the table header is not visible the problem is in query, check the value of $genres:
    PHP Code:
     $query "SELECT * FROM movie WHERE category_id = $genres";
     
    $retrieve mysql_query($query);
            if(
    $retrieve){
    ....
      }else{
         print 
    '<pre>'.mysql_error().'</pre>';
         print 
    '<pre>'.$query.'</pre>';
      } 
    best regards

  • Users who have thanked oesxyl for this post:

    pelehelp (03-23-2009)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •