Backend Development 2 min read

Counting Records with Specific Conditions in ThinkPHP5

This article demonstrates how to count records that meet a specific condition in ThinkPHP5 using an aggregate query, showing both backend PHP code to retrieve the count and frontend template code to display the result, along with screenshots of the implementation.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Counting Records with Specific Conditions in ThinkPHP5

Description: Counting records with specific conditions in ThinkPHP5.

Application Example: Counting the number of orders.

Solution: Use an aggregate query. Refer to the official documentation for details.

Backend Code:

Query the record count using the model:

$recordNum = \app\index\Model\Reservation::where('username', '=', session('username'))->count();

Assign the result to the view:

$this->assign([ 'recordNum' => $recordNum, ]);

Frontend Code:

<p>A total of {$recordNum}</p>

Result screenshots:

Result image
Result image

For the full article, click the link below to read the original content.

Read original
Read original
DatabaseBackend DevelopmentPHPThinkPHP5Aggregate Query
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.