I saw somewhere how to find out the number of pages on the website

General project discussion. NOT for help questions.
replytomk3

I saw somewhere how to find out the number of pages on the website

Post by replytomk3 »

I remember a discussion somewhere on how to find out the number of pages by counting database entries in _content. Can someone help?
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm
Location: Fernie British Columbia, Canada

Re: I saw somewhere how to find out the number of pages on the website

Post by calguy1000 »

SELECT count(id) FROM _content
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
Wishbone
Power Poster
Power Poster
Posts: 1368
Joined: Tue Dec 23, 2008 8:39 pm

Re: I saw somewhere how to find out the number of pages on the website

Post by Wishbone »

calguy1000 wrote: SELECT count(id) FROM _content
SELECT count(content_id) FROM _content
replytomk3

Re: I saw somewhere how to find out the number of pages on the website

Post by replytomk3 »

Why does this produce a blank result if ran from the backend as a UDT?

Code: Select all

	global $gCms;
	$db = $gCms->db;
		
	$query = "SELECT count(content_id) FROM ".cms_db_prefix()."content";
	$dbresult = $db->Execute($query);

	return $dbresult;

Wishbone
Power Poster
Power Poster
Posts: 1368
Joined: Tue Dec 23, 2008 8:39 pm

Re: I saw somewhere how to find out the number of pages on the website

Post by Wishbone »

Replace your last line with:

Code: Select all

$row = $dbresult->FetchRow();
return($row['count(content_id)']);
I got 33
replytomk3

Re: I saw somewhere how to find out the number of pages on the website

Post by replytomk3 »

370  :)
replytomk3

Re: I saw somewhere how to find out the number of pages on the website

Post by replytomk3 »

OK, I would like to release this code for the benefit of others.

1) What should I add to show only pages that are active?

2) For you experts, what would the code be to return a list of pages with empty (missing) metadata content in the database, or if it contains the default "".

I have already released something based on "last modified pages": http://dev.cmsmadesimple.org/projects/lastcreatedpage. These features should help those who have not specified metadata when they should have.
replytomk3

Re: I saw somewhere how to find out the number of pages on the website

Post by replytomk3 »

For the benefit of others, the code that you can run as a UDT to find out the number of pages you have (including hidden and inactive) is:

Code: Select all

	global $gCms;
	$db = $gCms->db;
		
	$query = "SELECT count(content_id) FROM ".cms_db_prefix()."content";
	$dbresult = $db->Execute($query);

	$row = $dbresult->FetchRow();
        return($row['count(content_id)']);
Wishbone
Power Poster
Power Poster
Posts: 1368
Joined: Tue Dec 23, 2008 8:39 pm

Re: I saw somewhere how to find out the number of pages on the website

Post by Wishbone »

replytomk3 wrote: 1) What should I add to show only pages that are active?

Code: Select all

$query = "SELECT count(content_id) FROM ".cms_db_prefix()."content WHERE active=1";
Wishbone
Power Poster
Power Poster
Posts: 1368
Joined: Tue Dec 23, 2008 8:39 pm

Re: I saw somewhere how to find out the number of pages on the website

Post by Wishbone »

replytomk3 wrote: 2) For you experts, what would the code be to return a list of pages with empty (missing) metadata content in the database, or if it contains the default "".

Code: Select all

global $gCms;
$db = $gCms->db;
		
$query = "SELECT content_name FROM ".cms_db_prefix()."content WHERE metadata='' OR metadata LIKE '%Add code here%'";
$dbresult = $db->Execute($query);

while ($dbresult && $row = $dbresult->FetchRow()) {
  echo("<p>$row['content_name']<p>\n");
}
Wishbone
Power Poster
Power Poster
Posts: 1368
Joined: Tue Dec 23, 2008 8:39 pm

Re: I saw somewhere how to find out the number of pages on the website

Post by Wishbone »

We might even make it simpler by using a call or two from the ContentOperations class.

http://www.cmsmadesimple.org/apidoc/CMS ... tions.html

I'll look at it tonight when I have a bit more time.
Wishbone
Power Poster
Power Poster
Posts: 1368
Joined: Tue Dec 23, 2008 8:39 pm

Re: I saw somewhere how to find out the number of pages on the website

Post by Wishbone »

Here's a UDT (named 'page_info'), using API calls to get the same info, all at once.

Code: Select all

global $gCms;
$cntnt = $gCms->getContentOperations();

$results['page_count'] = 0;

foreach ($cntnt->GetAllContent() as $page) {
  if ($page->mActive) {
    $results['page_count']++;
  }

  if (!$page->mMetadata || preg_match('/Add code here/', $page->mMetadata)) {
    $results['no_metadata'] .= $page->mName . ' ';
  }
}

$smarty->assign($params['assign'], $results);
Here's how to use it in your template or page:

Code: Select all

{page_info assign='info'}
<p>Number of active pages: {$info.page_count}</p>
{if $info.no_metadata}
<p>The following pages are missing metadata: {$info.no_metadata}</p>
{else}
<p>All pages have metadata</p>
{/if}
replytomk3

Re: I saw somewhere how to find out the number of pages on the website

Post by replytomk3 »

I'm waiting approval on two modules for the Forge. I'll post the code there as a plugin.
replytomk3

Re: I saw somewhere how to find out the number of pages on the website

Post by replytomk3 »

You are doing all the work.

I have modified it to be on separate lines, and to not show inactive pages (those for me are pages in progress, I don't know about others. Anyways, if you cannot navigate to them, I don't care about metadata for those pages).

Code: Select all

global $gCms;
$cntnt = $gCms->getContentOperations();

$results['page_count'] = 0;

foreach ($cntnt->GetAllContent() as $page) {
  if ($page->mActive) {
    $results['page_count']++;
  }

  if (($page->mActive && !$page->mMetadata) || preg_match('/Add code here/', $page->mMetadata)) {
    $results['no_metadata'] .=  ' <p>' . $page->mName;
  }
}

$smarty->assign($params['assign'], $results);
replytomk3

Re: I saw somewhere how to find out the number of pages on the website

Post by replytomk3 »

How do I also ignore pages that are links?
Locked

Return to “General Discussion”