Author Topic: Help me with code please...  (Read 21027 times)

0 Members and 1 Guest are viewing this topic.

Offline MrAndrew

  • Sr. Member
  • ****
  • Posts: 302
    • View Profile
    • Aviation PhotoBase
Help me with code please...
« on: September 15, 2010, 06:33:11 PM »
Good day

I have this code

	
$sql "SELECT SUM(image_hits) AS sum 
          FROM "
.IMAGES_TABLE;
  
$row $site_db->query_firstrow($sql);

  
$sum = (isset($row['sum'])) ? $row['sum'] : 0;
  
$total_hits "".$lang['total_hits']."<B> ".$row['sum']."</B>\n";
  
  
$site_template->register_vars("total_hits"$total_hits);
unset(
$total_hits);


But this code extract hits from all database... I need to extract total hits of one user.
« Last Edit: September 15, 2010, 06:44:05 PM by MrAndrew »

Offline V@nо

  • Addicted member
  • ******
  • Posts: 1.223
    • View Profile
Re: Help me with code please...
« Reply #1 on: September 16, 2010, 01:50:13 AM »
$user_id 1//replace with your user id
$sql "SELECT SUM(image_hits) AS sum 
        FROM "
.IMAGES_TABLE."
        WHERE user_id = 
$user_id";
Your first three "must do" before you ask a question:
If I asked you to PM me, I meant PM to my primary account, this account doesn't accept PMs.

Offline MrAndrew

  • Sr. Member
  • ****
  • Posts: 302
    • View Profile
    • Aviation PhotoBase
Re: Help me with code please...
« Reply #2 on: September 24, 2010, 03:26:32 PM »
This code give me the next error:

Code: [Select]
DB Error: Bad SQL Query: SELECT user_t_images, user_name, user_id, SUM(image_hits) AS sum FROM 5images_users, 5images_images WHERE user_level >= '2' ORDER BY user_t_images DESC LIMIT 10
Column 'user_id' in field list is ambiguous

$sql "SELECT user_t_images, user_name, user_id, SUM(image_hits) AS sum FROM ".USERS_TABLE.", ".IMAGES_TABLE."
WHERE user_level >= '"
.USER."'
ORDER BY user_t_images DESC
LIMIT 10"
;


Help me please

Offline V@no

  • If you don't tell me what to do, I won't tell you where you should go :)
  • Global Moderator
  • 4images Guru
  • *****
  • Posts: 17.849
  • mmm PHP...
    • View Profile
    • 4images MODs Demo
Re: Help me with code please...
« Reply #3 on: September 24, 2010, 04:55:16 PM »
It's because you need an unique alias (see extra "i" and "u" below) for each table and it's fields when used two or more tables in the same query.

Code: [Select]
$sql = "SELECT i.user_t_images, u.user_name, u.user_id, SUM(i.image_hits) AS sum
        FROM ".USERS_TABLE." u, ".IMAGES_TABLE." i
        WHERE u.user_level >= '".USER."' AND u.user_id = i.user_id
        ORDER BY i.user_t_images DESC
        LIMIT 10";
Your first three "must do" before you ask a question:
Please do not PM me asking for help unless you've been specifically asked to do so. Such PMs will be deleted without answer. (forum rule #6)
Extension for Firefox/Thunderbird: Master Password+    Back/Forward History Tweaks (restartless)    Cookies Manager+    Fit Images (restartless for Thunderbird)

Offline MrAndrew

  • Sr. Member
  • ****
  • Posts: 302
    • View Profile
    • Aviation PhotoBase
Re: Help me with code please...
« Reply #4 on: September 24, 2010, 05:10:04 PM »
Right! Thanks V@no, will do that now


Ohhh, i have this error  :cry:
Code: [Select]
DB Error: Bad SQL Query: SELECT i.user_t_images, u.user_name, u.user_id, SUM(i.image_hits) AS sum FROM 5images_users u, 5images_images i WHERE u.user_level >= '2' AND u.user_id = i.user_id ORDER BY i.user_t_images DESC LIMIT 10
Unknown column 'i.user_t_images' in 'field list'

Offline V@no

  • If you don't tell me what to do, I won't tell you where you should go :)
  • Global Moderator
  • 4images Guru
  • *****
  • Posts: 17.849
  • mmm PHP...
    • View Profile
    • 4images MODs Demo
Re: Help me with code please...
« Reply #5 on: September 24, 2010, 05:51:16 PM »
user_t_images is not part of 4images, I don't know where it came from in your code...
Your first three "must do" before you ask a question:
Please do not PM me asking for help unless you've been specifically asked to do so. Such PMs will be deleted without answer. (forum rule #6)
Extension for Firefox/Thunderbird: Master Password+    Back/Forward History Tweaks (restartless)    Cookies Manager+    Fit Images (restartless for Thunderbird)

Offline MrAndrew

  • Sr. Member
  • ****
  • Posts: 302
    • View Profile
    • Aviation PhotoBase
Re: Help me with code please...
« Reply #6 on: September 24, 2010, 08:46:56 PM »
I`m very tired. I`m trying and trying, trying and trying. But nothing to give me a sum  :cry: For one user this is not a problem. But to show list with 10 users impossible! Please, please help me!!!  :cry: :cry: :cry: :cry:

Code below nothing to show... Not errors, not results...

$tot_hits "";
while (
$row $site_db->fetch_array($result)) {
$user_id $row['user_id'];
$sql "SELECT SUM(image_hits) AS sum, user_id
        FROM "
.IMAGES_TABLE.", ".USERS_TABLE."
        user_id = 
$user_id
        ORDER BY user_id DESC
        LIMIT 10"
;
      
$result $site_db->query($sql);



  
$sum = (isset($row['sum'])) ? $row['sum'] : 0;
  
$tot_hits "".$lang['total_hits']."<B> ".$row['sum']."</B>\n";
 } 
  
$site_template->register_vars("tot_hits"$tot_hits);
unset(
$tot_hits);

Offline V@no

  • If you don't tell me what to do, I won't tell you where you should go :)
  • Global Moderator
  • 4images Guru
  • *****
  • Posts: 17.849
  • mmm PHP...
    • View Profile
    • 4images MODs Demo
Re: Help me with code please...
« Reply #7 on: September 24, 2010, 09:16:07 PM »
you put sql query inside "while", it should be outside.
Your first three "must do" before you ask a question:
Please do not PM me asking for help unless you've been specifically asked to do so. Such PMs will be deleted without answer. (forum rule #6)
Extension for Firefox/Thunderbird: Master Password+    Back/Forward History Tweaks (restartless)    Cookies Manager+    Fit Images (restartless for Thunderbird)

Offline MrAndrew

  • Sr. Member
  • ****
  • Posts: 302
    • View Profile
    • Aviation PhotoBase
Re: Help me with code please...
« Reply #8 on: September 24, 2010, 09:19:59 PM »
$tot_hits "";


$sql "SELECT SUM(image_hits) AS sum, user_id
        FROM "
.IMAGES_TABLE.", ".USERS_TABLE."
        user_id = 
$user_id
        ORDER BY user_id DESC
        LIMIT 10"
;
      
$result $site_db->query($sql);

while (
$row $site_db->fetch_array($result)) {
  
$user_id $row['user_id'];
  
$sum = (isset($row['sum'])) ? $row['sum'] : 0;
  
$tot_hits "".$lang['total_hits']."<B> ".$row['sum']."</B>\n";

 } 
  
$site_template->register_vars("tot_hits"$tot_hits);
unset(
$tot_hits);


This give me next and nothing result:

Code: [Select]
Notice: Undefined variable: user_id in /home/users2/d/drandrew/domains/skyphotos.ru/includes/page_header.php on line 863

DB Error: Bad SQL Query: SELECT SUM(image_hits) AS sum, user_id FROM 5images_images, 5images_users user_id = ORDER BY user_id DESC LIMIT 10
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 '= ORDER BY user_id DESC LIMIT 10' at line 3

Rembrandt

  • Guest
Re: Help me with code please...
« Reply #9 on: September 25, 2010, 05:09:00 AM »

$sql 
"SELECT SUM(image_hits) AS sum, user_id
        FROM "
.IMAGES_TABLE."
        WHERE user_id = 
$user_id
        ORDER BY user_id DESC
        LIMIT 10"
;


Offline MrAndrew

  • Sr. Member
  • ****
  • Posts: 302
    • View Profile
    • Aviation PhotoBase
Re: Help me with code please...
« Reply #10 on: September 25, 2010, 09:24:04 AM »
$tot_hits "";


$sql "SELECT SUM(image_hits) AS sum, user_id
        FROM "
.IMAGES_TABLE."
        WHERE user_id = 
$user_id
        ORDER BY user_id DESC
        LIMIT 10"
;
      
$result $site_db->query($sql);

while (
$row $site_db->fetch_array($result)) {
  
$user_id $row['user_id'];
  
$sum = (isset($row['sum'])) ? $row['sum'] : 0;
  
$tot_hits "".$lang['total_hits']."<B> ".$row['sum']."</B>\n";

 } 
  
$site_template->register_vars("tot_hits"$tot_hits);
unset(
$tot_hits);


Code: [Select]
Notice: Undefined variable: user_id in /home/users2/d/drandrew/domains/skyphotos.ru/includes/page_header.php on line 863

DB Error: Bad SQL Query: SELECT SUM(image_hits) AS sum, user_id FROM 5images_images WHERE user_id = ORDER BY user_id DESC LIMIT 10
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 'ORDER BY user_id DESC LIMIT 10' at line 4

Error again  :cry: :cry:

Offline V@no

  • If you don't tell me what to do, I won't tell you where you should go :)
  • Global Moderator
  • 4images Guru
  • *****
  • Posts: 17.849
  • mmm PHP...
    • View Profile
    • 4images MODs Demo
Re: Help me with code please...
« Reply #11 on: September 25, 2010, 10:58:19 PM »
You need specify in $user_id variable what user you want to get data for. So on top of your code you must have something like:
$user_id = 123;
Your first three "must do" before you ask a question:
Please do not PM me asking for help unless you've been specifically asked to do so. Such PMs will be deleted without answer. (forum rule #6)
Extension for Firefox/Thunderbird: Master Password+    Back/Forward History Tweaks (restartless)    Cookies Manager+    Fit Images (restartless for Thunderbird)

Offline MrAndrew

  • Sr. Member
  • ****
  • Posts: 302
    • View Profile
    • Aviation PhotoBase
Re: Help me with code please...
« Reply #12 on: September 26, 2010, 11:06:41 AM »
Ok. I fully completed my code. That`s will be easy then i thought!

$sql "SELECT SUM(image_hits) AS sum 
        FROM "
.IMAGES_TABLE."
        WHERE user_id = 
$user_id";
$result $site_db->query($sql);

while (
$row $site_db->fetch_array($result)) {
  
$sum = (isset($row['sum'])) ? $row['sum'] : 0;
  
$tot_hits "".$lang['total_hits']." <B>".$row['sum']."</B>\n";

 } 
  
$site_template->register_vars("tot_hits"$tot_hits);
unset(
$tot_hits);


This had not be in page_header!

Offline V@nо

  • Addicted member
  • ******
  • Posts: 1.223
    • View Profile
Re: Help me with code please...
« Reply #13 on: September 26, 2010, 06:54:31 PM »
are you trying show to a logged in user sum of image hits for his images?
In that case you might want consider use $user_info['user_id'] instead of $user_id...
Your first three "must do" before you ask a question:
If I asked you to PM me, I meant PM to my primary account, this account doesn't accept PMs.

Offline MrAndrew

  • Sr. Member
  • ****
  • Posts: 302
    • View Profile
    • Aviation PhotoBase
Re: Help me with code please...
« Reply #14 on: September 27, 2010, 01:21:20 AM »
V@no,  I wanted to show sum of total images in the profile of user profile! I can do this MOD now! Do it?

Thanks!