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 3 of 3
  1. #1
    Regular Coder
    Join Date
    Feb 2011
    Posts
    114
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Need help on looping through function code

    Hi, i am creating a simple blog, which i have a function like this
    PHP Code:
    <?php
    function get_category($id=null){
        
    //displaying categories is better to store it in an array
        
    $categories=array();
        
    $query=mysql_query("SELECT `id`,`name` FROM `categories`");
        while(
    $rows=mysql_fetch_assoc($query)){
           
    $categories[]=$rows;
        }
        return 
    $rows;

    ?>
    which will display my blog topic category post at category_list.php

    Now i loop through using a foreach loop ,here is the code
    PHP Code:
    <?php

       
    foreach(get_category() as $category){
        echo 
    $category['name'].'<br/>';
       }
     
    ?>
    Now it given me this error
    ! ) Warning: Invalid argument supplied for foreach() in C:\wamp\www\blog\category_list.php on line 12
    Call Stack

    Can some help me on how to loop through that function on the top.

    Thanks
    Clement OSei

  • #2
    Master Coder mlseim's Avatar
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,369
    Thanks
    8
    Thanked 1,075 Times in 1,066 Posts
    Why does it have to be a function?
    There's no reason to have a function?

  • #3
    Regular Coder
    Join Date
    Feb 2011
    Posts
    114
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Hi ,
    Thanks for your time helping me.

    I have solve it

    Thanks once again

    Clement Osei


  •  

    Posting Permissions

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