Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-30-2012, 12:18 PM   PM User | #1
kingot
Regular Coder

 
Join Date: Feb 2011
Posts: 114
Thanks: 12
Thanked 0 Times in 0 Posts
kingot is an unknown quantity at this point
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
kingot is offline   Reply With Quote
Old 01-30-2012, 12:29 PM   PM User | #2
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,056
Thanks: 8
Thanked 1,032 Times in 1,023 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
Why does it have to be a function?
There's no reason to have a function?
mlseim is offline   Reply With Quote
Old 01-30-2012, 12:36 PM   PM User | #3
kingot
Regular Coder

 
Join Date: Feb 2011
Posts: 114
Thanks: 12
Thanked 0 Times in 0 Posts
kingot is an unknown quantity at this point
Hi ,
Thanks for your time helping me.

I have solve it

Thanks once again

Clement Osei
kingot is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:30 AM.


Advertisement
Log in to turn off these ads.