Welcome to Geeklog, Anonymous Friday, December 27 2024 @ 12:08 am EST

Geeklog Forums

Need A Hack To Insert Number of Articles Into a Static Page


Status: offline

winnerdk

Forum User
Full Member
Registered: 04/24/05
Posts: 339
Location:Panama City, Republic of Panama

I have a static block on the top of my homepage that reads "There are currently more than 4,000 articles in our ever-expanding database..."

Is there any way that I can snatch the number of articles that are actually in the database so that it will appear in this line? That number is generated in the Admins Only block after "Stories" (4,026).

So, what's the snappy hack to have "There are currently (code) articles in our ever-expanding database..." ???

There would probably be all kinds of uses for this kind of a hack. Anyone have any ideas?

Don Winner, Panama-Guide.com

 Quote

Status: offline

jmucchiello

Forum User
Full Member
Registered: 08/29/05
Posts: 985
In a static page? No problem. Add this code somewhere and then put the variable in your output:

$cnt = DB_getItem("SELECT COUNT(*) FROM {$_TABLES['stories']} WHERE draft = 0"Wink;

if you want to be fancy, throw in COM_getPermSQL('and'Wink. And truly fancy COM_getTopicSQL(). You should look in lib-common.php at the COM_AdminMenu function for examples of generating the story counts.
 Quote

Status: offline

Dirk

Site Admin
Admin
Registered: 01/12/02
Posts: 13073
Location:Stuttgart, Germany
A check for dates in the future may also be in order if you use that feature.

bye, Dirk
 Quote

Status: offline

winnerdk

Forum User
Full Member
Registered: 04/24/05
Posts: 339
Location:Panama City, Republic of Panama
Quote by: jmucchiello

In a static page? No problem. Add this code somewhere and then put the variable in your output: $cnt = DB_getItem("SELECT COUNT(*) FROM {$_TABLES['stories']} WHERE draft = 0"); if you want to be fancy, throw in COM_getPermSQL('and'). And truly fancy COM_getTopicSQL(). You should look in lib-common.php at the COM_AdminMenu function for examples of generating the story counts.

Ok, but a couple of questions from a non-programmer... 1. When you say "Add this code somewhere..." where would you suggest? 2. When you say "...put the variable in your output:" What variable? I really appreciate your help, but don't understand the answer. Thanks. Don Winner, Panama-Guide.com
 Quote

Status: offline

LWC

Forum User
Full Member
Registered: 02/19/04
Posts: 818
jmucchiello, please use the CODE tag as that's what it's for and also some things turn to smilies otherwise.

winnerdk, he meant obviously that you put it somewhere in your staticpage and then you can use
Text Formatted Code
echo $cnt;

wherever you want in that staticpage.

Of course, you need to enable PHP in your staticpage and read how to use PHP in staticpages.
 Quote

Status: offline

winnerdk

Forum User
Full Member
Registered: 04/24/05
Posts: 339
Location:Panama City, Republic of Panama
Quote by: LWC

jmucchiello, please use the CODE tag as that's what it's for and also some things turn to smilies otherwise. winnerdk, he meant obviously that you put it somewhere in your staticpage and then you can use

Text Formatted Code
echo $cnt;
wherever you want in that staticpage. Of course, you need to enable PHP in your staticpage and read how to use PHP in staticpages.

Thanks for the help. When enabling PHP I see there are two options, - execute PHP (return) - execute PHP What's the difference. I'm off to read and get smarter now about using PHP in static pages. Thanks again. Don Winner, Panama-Guide.com
 Quote

Status: offline

winnerdk

Forum User
Full Member
Registered: 04/24/05
Posts: 339
Location:Panama City, Republic of Panama
Now I'm getting:

Parse error: syntax error, unexpected '<' in /home/panamax/public_html/plugins/staticpages/functions.inc(841) : eval()'d code on line 1

This is what I'm trying, and it's not working;

<br /><p><b><a href="http://www.panama-guide.com/index.php">Panama Guide</a></b> is the #1 English Language web site about the Republic of Panama. There are currently echo $cnt; articles in our ever-expanding database and we update daily so check back often. About 3,000 people visit <a href="http://www.panama-guide.com/">Panama-Guide.com</a> every day to follow current events and to use the other resources available. We provide English language news about Panama as well as information about all of the other things you need to know if you plan to visit or live here. We focus on those topics and issues which are of greatest importance to the English speaking expatriate community. And if you can't <a href="http://www.panama-guide.com/search.php">find what you need to know</a>, we take <a href="http://www.panama-guide.com/article.php/20060921101146229">requests</a>. Welcome aboard, and tell your friends.

$cnt = DB_getItem("SELECT COUNT(*) FROM {$_TABLES['stories']} WHERE draft = 0"Wink;

 Quote

Status: offline

winnerdk

Forum User
Full Member
Registered: 04/24/05
Posts: 339
Location:Panama City, Republic of Panama
Ok, I am now at the point where I have;

1. php working in the static page.
2. working php before and after the html on the same page;
3. But there's obviously something wrong with the string of code I'm trying to use to get the number from the database;
4. I'm getting this error;

"An error has occurred:
2 - Missing argument 2 for db_getitem() @ /home/panamax/public_html/system/lib-database.php line 250"

5. This error is occuring as a result of this line:

$cnt = DB_getItem("SELECT COUNT(*) FROM {$_TABLES['stories']} WHERE draft = 0"Wink;

So, what can I change to get this to work?


 Quote

Status: offline

Dirk

Site Admin
Admin
Registered: 01/12/02
Posts: 13073
Location:Stuttgart, Germany
Looks like you should use DB_query, not DB_getItem.

DB_query takes a complete SQL query as a string while DB_getItem expects three parameters from which it builds the SQL query by itself.

bye, Dirk
 Quote

Status: offline

winnerdk

Forum User
Full Member
Registered: 04/24/05
Posts: 339
Location:Panama City, Republic of Panama
I made that change, and now I'm getting a different error:

On the webpage: "An SQL error has occurred. Please see error.log for details."

And in the error log: "Sat 19 May 2007 13:54:17 EDT - 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE draft = 0' at line 1. SQL in question: SELECT COUNT(*) FROM WHERE draft = 0 "
 Quote

Status: offline

Dirk

Site Admin
Admin
Registered: 01/12/02
Posts: 13073
Location:Stuttgart, Germany
You need a
Text Formatted Code
global $_TABLES;
in your PHP code.

bye, Dirk
 Quote

Status: offline

winnerdk

Forum User
Full Member
Registered: 04/24/05
Posts: 339
Location:Panama City, Republic of Panama
Still not working. I tried it before, after, and in.
 Quote

Status: offline

Dirk

Site Admin
Admin
Registered: 01/12/02
Posts: 13073
Location:Stuttgart, Germany
Not the greatest piece of code and it doesn't check permissions:
Text Formatted Code
?><p>There are currently <?php
global $_TABLES;
$result = DB_query("SELECT COUNT(*) AS count FROM {$_TABLES['stories']} WHERE draft_flag = 0 AND date <= NOW()");
list($count) = DB_fetchArray($result);
echo $count;
?> stories in our database!</p>
<?php

That's for the "execute PHP" mode.

bye, Dirk
 Quote

Status: offline

winnerdk

Forum User
Full Member
Registered: 04/24/05
Posts: 339
Location:Panama City, Republic of Panama
Quote by: Dirk

Not the greatest piece of code and it doesn't check permissions:

Text Formatted Code
?><p>There are currently <?php
global $_TABLES;
$result = DB_query("SELECT COUNT(*) AS count FROM {$_TABLES['stories']} WHERE draft_flag = 0 AND date <= NOW()");
list($count) = DB_fetchArray($result);
echo $count;
?> stories in our database!</p>
<?php
That's for the "execute PHP" mode. bye, Dirk

:banana: Dude, works like a charm. Here's the final product;
Text Formatted Code
?>
<p><b><a href="http://www.panama-guide.com/index.php">Panama Guide</a></b> is the #1 English Language web site about the Republic of Panama. There are currently
<?php
global $_TABLES;
$result = DB_query("SELECT COUNT(*) AS count FROM {$_TABLES['stories']} WHERE draft_flag = 0 AND date <= NOW()");
list($count) = DB_fetchArray($result);
echo $count;
?>
articles in our ever-expanding database and we update daily so check back often. About 3,000 people visit <a href="http://www.panama-guide.com/">Panama-Guide.com</a> every day to follow current events and to use the other resources available. We provide English language news about Panama as well as information about all of the other things you need to know if you plan to visit or live here. We focus on those topics and issues which are of greatest importance to the English speaking expatriate community. And if you can't <a href="http://www.panama-guide.com/search.php">find what you need to know</a>, we take <a href="http://www.panama-guide.com/article.php/20060921101146229">requests</a>. Welcome aboard, and tell your friends.
 
This is (obviously) a mix of php and html, but it works. Looks like crap here, but functional. Thanks again for taking the time. If you want to see it work, hit the below. This is the "Welcome Aboard" static page that displays at the top of the homepage. Is there any way to get a comma in the number (4,033 instead of 4033)? Don Winner, Panama-Guide.com
 Quote

Status: offline

LWC

Forum User
Full Member
Registered: 02/19/04
Posts: 818
Is there any way to get a comma in the number (4,033 instead of 4033)?

Yes, by using COM_numberFormat:

Text Formatted Code
?>
<p>
<?php
global $_TABLES;

if (COM_getLanguageId() == "he") {
$pre_number = "יש כרגע";
$post_number = "מאמרים במאגר שלנו!";
} else {
$pre_number = "There are currently";
$post_number = "stories in our database!";
}
$result = DB_query("SELECT COUNT(*) AS count FROM {$_TABLES['stories']} WHERE draft_flag = 0 AND date <= NOW()");
list($count) = DB_fetchArray($result);
echo $pre_number . ' ' . COM_numberFormat($count) . ' ' . $post_number;
?>
</p>
<?php


Also added multilingual support there.Wink If you don't need it, just keep the latter $pre_number and $post_number (without the surrounding IF statement),

Dirk, I've also ready asked in the past (because it's needed for my RTL support patch, which lets one disable RTL in non multilingual LTR sites, such as this one), but why don't you add a simple config setting to enable or disable multilingual support?
 Quote

Status: offline

winnerdk

Forum User
Full Member
Registered: 04/24/05
Posts: 339
Location:Panama City, Republic of Panama
Thank you so much! Works like a charm. Wonderful! Thanks again. :banana: Don Winner, Panama-Guide.com
 Quote

All times are EST. The time is now 12:08 am.

  • Normal Topic
  • Sticky Topic
  • Locked Topic
  • New Post
  • Sticky Topic W/ New Post
  • Locked Topic W/ New Post
  •  View Anonymous Posts
  •  Able to post
  •  Filtered HTML Allowed
  •  Censored Content