4images Forum & Community

General / Allgemeines => Programming => Topic started by: MrAndrew on September 15, 2010, 06:33:11 PM

Title: Help me with code please...
Post by: MrAndrew 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.
Title: Re: Help me with code please...
Post by: V@nо 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";
Title: Re: Help me with code please...
Post by: MrAndrew 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
Title: Re: Help me with code please...
Post by: V@no 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";
Title: Re: Help me with code please...
Post by: MrAndrew 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'
Title: Re: Help me with code please...
Post by: V@no 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...
Title: Re: Help me with code please...
Post by: MrAndrew 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);
Title: Re: Help me with code please...
Post by: V@no on September 24, 2010, 09:16:07 PM
you put sql query inside "while", it should be outside.
Title: Re: Help me with code please...
Post by: MrAndrew 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
Title: Re: Help me with code please...
Post by: Rembrandt 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";

Title: Re: Help me with code please...
Post by: MrAndrew 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:
Title: Re: Help me with code please...
Post by: V@no 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;
Title: Re: Help me with code please...
Post by: MrAndrew 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!
Title: Re: Help me with code please...
Post by: V@nо 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...
Title: Re: Help me with code please...
Post by: MrAndrew 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!
Title: Re: Help me with code please...
Post by: V@no on September 27, 2010, 01:35:13 AM
Oh, now I understand :)
In that case scratch my previous post ;) $user_id - is what you need.

You can simplify your code with with:

$sql = "SELECT SUM(image_hits) AS sum
        FROM ".IMAGES_TABLE."
        WHERE user_id = $user_id";
$result = $site_db->query_firstrow($sql);
$tot_hits = $lang['total_hits']." <B>".($result['sum'] ? $result['sum'] : 0)."</B>\n";
$site_template->register_vars("tot_hits", $tot_hits);
unset($tot_hits);
Title: Re: Help me with code please...
Post by: MrAndrew on October 23, 2010, 06:51:13 PM
I have this code. Work fine! But...

$most_viewed_name .= ""; 
$most_viewed_name .= time() - 60 * 60 * 24 * 7;       
        $sql = "SELECT i.image_name as name, SUM(i.image_hits) AS hit_sum
        FROM ".IMAGES_TABLE." i
        WHERE i.image_date >= ".$most_viewed_name."
        GROUP BY name ORDER BY hit_sum DESC
        LIMIT 5";


$result = $site_db->query($sql);

$sum = (isset($row['hit_sum'])) ? $row['hit_sum'] : 0;
   
   while ($row = $site_db->fetch_array($result)) {
$most_viewed_name .= "<tr>";
$most_viewed_name .= "<td align=\"left\">";
$most_viewed_name .= '<a href ="'.ROOT_PATH.'/search.htm?search_fields=image_name&search_keywords='.$row['name'].'" title="'.$row['hit_sum'].' views today"><b>'.$row['name'].'</b></a>';
$most_viewed_name .= "</td>";
$most_viewed_name .= "</tr>";
}

$site_template->register_vars(array(
"most_viewed_name" => $most_viewed_name,
));
unset($most_viewed_name);

(http://drandrew.jino.ru/5.jpg)

What is the number and how to delete it??? (Yellow mark)
Title: Re: Help me with code please...
Post by: Rembrandt on October 23, 2010, 07:11:00 PM
$most_viewed_name .= ""; 
$time= time() - 60 * 60 * 24 * 7;       
        $sql = "SELECT i.image_name as name, SUM(i.image_hits) AS hit_sum
        FROM ".IMAGES_TABLE." i
        WHERE i.image_date >= ".$time."
        GROUP BY name ORDER BY hit_sum DESC
        LIMIT 5";

.
.
.


mfg Andi
Title: Re: Help me with code please...
Post by: MrAndrew on October 23, 2010, 08:08:15 PM
Fine! Thanks!  :D