开发者

Retrieve subcategory ids as an array

开发者 https://www.devze.com 2023-01-02 08:55 出处:网络
I have开发者_C百科 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 li

I have开发者_C百科 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


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"];
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消