Author Topic: PHP Question  (Read 8595 times)

0 Members and 1 Guest are viewing this topic.

Offline Egly

  • Jr. Member
  • **
  • Posts: 71
    • View Profile
PHP Question
« on: September 21, 2005, 12:38:07 AM »
Hi.

I want to print out the total comments of the user that is logged in with that code:

Code: [Select]
define('GET_CACHES', 1);
define('ROOT_PATH', './');
include(ROOT_PATH.'global.php');
require(ROOT_PATH.'includes/sessions.php');
$user_access = get_permission();
include(ROOT_PATH.'includes/page_header.php');

$user_id = $user_info['user_id'];

if ($user_info['user_level'] >= USER) {
$sql = "SELECT COUNT(user_comments) AS comments
         FROM ".USERS_TABLE;"
WHERE  URL_USER_ID = $user_id";
  $row = $site_db->query_firstrow($sql);

  $comments = (isset($row['comments'])) ? $row['comments'] : 0;
  $user_pscomments = "".$lang['total_votes']."<B> ".$row['comments']."</B>\n";
 
  $site_template->register_vars("user_pscomments", $user_pscomments);
unset($user_pscomments);
   
 }

My problem is that  it counts all the time "24" with every account i logg in...
Where is my mistake?
I have to say, that i´m very noob to php... Started coding today  :lol:

TheOracle

  • Guest
Re: PHP Question
« Reply #1 on: September 21, 2005, 12:59:38 AM »
Change :

Quote

define('GET_CACHES', 1);
define('ROOT_PATH', './');
include(ROOT_PATH.'global.php');
require(ROOT_PATH.'includes/sessions.php');
$user_access = get_permission();
include(ROOT_PATH.'includes/page_header.php');

$user_id = $user_info['user_id'];

if ($user_info['user_level'] >= USER) {
$sql = "SELECT COUNT(user_comments) AS comments
         FROM ".USERS_TABLE;"
WHERE  URL_USER_ID = $user_id";
$row = $site_db->query_firstrow($sql);

$comments = (isset($row['comments'])) ? $row['comments'] : 0;
$user_pscomments = "".$lang['total_votes']."<B> ".$row['comments']."</B>\n";

$site_template->register_vars("user_pscomments", $user_pscomments);
unset($user_pscomments);
   
}


replace with :

Code: [Select]

define('GET_CACHES', 1);
define('ROOT_PATH', './');
include(ROOT_PATH.'global.php');
require(ROOT_PATH.'includes/sessions.php');
$user_access = get_permission();
include(ROOT_PATH.'includes/page_header.php');

if ($user_info['user_level'] >= USER) {

$sql = "

SELECT COUNT(user_comments) AS comments
FROM ".USERS_TABLE;

$row = $site_db->query_firstrow($sql);

$num_comments = $row['comments'];
$site_db->free_result();

$comments = (isset($row['comments'])) ? $num_comments : 0;
$user_pscomments = "".$lang['total_votes']."<B> ".$row['comments']."</B>\n";

$site_template->register_vars("user_pscomments", $user_pscomments);
unset($user_pscomments);
unset($num_comments);
   
} ###### End of if statement.


Will this work ? ;)

Offline Xyu BAM

  • Full Member
  • ***
  • Posts: 145
    • View Profile
Re: PHP Question
« Reply #2 on: September 21, 2005, 01:01:59 AM »
replace
Code: [Select]
         FROM ".USERS_TABLE;"
   WHERE  URL_USER_ID = $user_id";
with:
Code: [Select]
         FROM ".USERS_TABLE."
         WHERE  ".get_user_table_field("", "user_id")." = $user_id";


Will this work ? ;)
why dont you try and see for yourself - it would not work as Egly wanted:
Quote
I want to print out the total comments of the user that is logged

Offline Egly

  • Jr. Member
  • **
  • Posts: 71
    • View Profile
Re: PHP Question
« Reply #3 on: September 21, 2005, 09:56:54 AM »
Thanks guys!!
Now it works.
I replaced code how xyu bam told but it always showed me "1".

I had to change this:

Code: [Select]
$sql = "SELECT COUNT (user_comments AS) comments
to

Code: [Select]
$sql = "SELECT user_comments AS comments
I think it counted the number of entries but did not show me the entry itself, am i right with that?

THANKS!!

Offline Xyu BAM

  • Full Member
  • ***
  • Posts: 145
    • View Profile
Re: PHP Question
« Reply #4 on: September 21, 2005, 10:11:54 AM »
Code: [Select]
$sql = "SELECT SUM(user_comments) AS commentsor
Code: [Select]
$sql = "SELECT COUNT(user_comments) AS comments
        FROM ".COMMENTS_TABLE."