How to write the ct call in discuz?-php Tutorial

Source: Internet
Author: User
In discuz, how to write the ct call? the following code is changed from DB to Ct, but the result is incorrect.
 init();$data = $query = C::t('#portal_category')->fetch_all($catid);while($value=$this->fetch_array($data)){$data[]=$value;}print_r($data);?>


The following are the error messages.


Reply to discussion (solution)

C: t ('# portal_category'). do not add the # sign. it will think it is the table model under the plug-in directory, and an error will be reported, prompting you that you do not have the table model.
For example, C: t ('# qqconnect # common_member_qqconnect')-> insert ($ insert_arr );
Discuz will automatically go to the directory \ source \ plugin \ qqconnect \ table to find the table model file table_common_member_qqconnect.php.

Fetch_all ($ catid), $ catid is an array, and it is also a number
You do not need your while, so you can try it as follows:

//$catid=1;//$catid=array(1,2,3);$res = C::t('portal_category')->fetch_all($catid);echo "
";print_r($res);echo "
";

C: t ('# portal_category'). do not add the # sign. it will think it is the table model under the plug-in directory, and an error will be reported, prompting you that you do not have the table model.
For example, C: t ('# qqconnect # common_member_qqconnect')-> insert ($ insert_arr );
Discuz will automatically go to the directory \ source \ plugin \ qqconnect \ table to find the table model file table_common_member_qqconnect.php.

Fetch_all ($ catid), $ catid is an array, and it is also a number
You do not need your while, so you can try it as follows:

//$catid=1;//$catid=array(1,2,3);$res = C::t('portal_category')->fetch_all($catid);echo "
";print_r($res);echo "
";



In the above method, when the number in fetch_all (1) can be printed, but only one piece of data is output.
I tried to output all the data, so fetch_all ($ catid) was used)
However, the output is only an Array ();
Somehow,
The code is as follows:
 init();$data = C::t('portal_category')->fetch_all($catid);print_r($data);?>

Er... I have told you before that there is no way to query all data in this table model. both fetch and fetch_all must have a condition that is the primary key catid. Therefore, we use DB to query all data, of course, you can also write a query method in the table model.
For example, open \ source \ class \ table \ table_portal_category.php and add the following method in the class (table_portal_category ).

public function my_fetch_all(){return DB::fetch_all("SELECT * FROM %t",array($this->_table));}

Call: $ data = C: t ('Portal _ category ')-> my_fetch_all (); // no parameter is required
However, I am still used to using DB for direct operations, which is more flexible.

Er... I have told you before that there is no way to query all data in this table model. both fetch and fetch_all must have a condition that is the primary key catid. Therefore, we use DB to query all data, of course, you can also write a query method in the table model.
For example, open \ source \ class \ table \ table_portal_category.php and add the following method in the class (table_portal_category ).

public function my_fetch_all(){return DB::fetch_all("SELECT * FROM %t",array($this->_table));}

Call: $ data = C: t ('Portal _ category ')-> my_fetch_all (); // no parameter is required
However, I am still used to using DB for direct operations, which is more flexible.


So I can also use DB,

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.