tags:

views:

23

answers:

2

I have three columns in my category table: ID, name and parent_id. If the category is parent there is 0 under the parent_id, if it's not parent category id is recorded instead. I would like to get all subcategory ids with one parent category id as an array.

How is that possible using php?

Can anyone please help?

Thanks in advance

A: 

Query should be SELECT id From category_table where parent_id = $Category_ID

When fetching the the results use mysql_fetch_array to put them in an array http://php.net/manual/en/function.mysql-fetch-array.php

If you use MySQLi you can use http://php.net/manual/en/mysqli-result.fetch-array.php

You can also use the PDO database connector instead it is a matter of preference.

Either way this is a basic select which simply populates the results into an array (vs an object or class perhaps). This is a very basic PHP procedure I'd suggest picking up some books on PHP if you are having issues with this or reading some beginner tutorials online.

CogitoErgoSum
+1  A: 

This is the simplest solution I can think of right now, for more you need to give us more code, that you have written so far, and information on where you were stuck.

$result = mysql_query("SELECT id FROM categories WHERE parend_id='some_id'");

$categories = array();

while($row = mysql_fetch_array($result)) {
  $categories[] = $row["id"];
}
jigfox