G
G
Gera112020-05-20 17:30:55
PHP
Gera11, 2020-05-20 17:30:55

How to count and display the number of records in a database cell in PHP?

There is such a query in the database. If I understand correctly, this is the main request to the _users database

$row = $db->super_query( "SELECT * FROM " . USERPREFIX . "_users WHERE user_id = '{$id}'" );

From here the question.
In this _users database there is a cell 'pay_news' And in this cell the id of the news is written separated by commas.
How to count the number of records (id) and display it on the page?

This is a piece from CMS DLE

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman, 2020-05-20
@Gera11

When querying the database, the $row variable will contain an array with user data. The array will contain the $row['pay_news'] element, which in turn will contain the string value "news id separated by commas".
You need to
1) Split the string into an array using a comma separator
2) Count the number of elements in the resulting array

$idNewsArray = explode(',' , $row['pay_news']);
$countNews = count($idNewsArray );

The $count variable will contain the number of entries, then all you need to do is simply display this value on the page:
echo $countNews;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question