8 Replies - 430 Views - Last Post: 27 August 2015 - 05:07 PM Rate Topic: -----

#1 JeremyBenson11  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 230
  • Joined: 10-January 14

Return Thumb Rating

Posted 27 August 2015 - 09:14 AM

Hey,

I'm working on a thumb rating system, but when I return the values to show on the page both of them say 1 even though there's only 1 db entry for thumbs up. I should return 1 and 0.. No matter what I write, so wondering if someone can help me simplify the query.

html
echo '<a href="php/scripts/product_thumb_rate.php?username='.$user->return_username().'&type=up&token='.$_GET['token'].'">
			<span class="glyphicon glyphicon-thumbs-up"></span>'.$product->return_thumbs_up().'
		</a> 
		<a href="php/scripts/product_thumb_rate.php?username='.$user->return_username().'&type=down&token='.$_GET['token'].'" class="pdleft">
			<span class="glyphicon glyphicon-thumbs-down"></span>'.$product->return_thumbs_down().'
		</a>';


query
public function set_thumb_rating($tokenSet)
	{
		
		$db = new PDO($this->db['dsn'],$this->db['username'], $this->db['password'], array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
		
		try{
			
			$queryUp = 'SELECT COUNT(`ID`) FROM `products_thumb_ratings` WHERE `type` = \'up\' AND `token` = \''.$tokenSet.'\'';
			$queryDown = 'SELECT COUNT(`ID`) FROM `products_thumb_ratings` WHERE `type` = \'down\' AND `token` = \''.$tokenSet.'\'';
				
			$this->thumbs['up'] = $db->query($queryUp)->execute();
			$this->thumbs['down'] = $db->query($queryDown)->execute();
					
		}catch(\PDOException $e){}
	
	}[code]

returning functions
[code]
// return thumbs up
	
	public function return_thumbs_up()
	{
		
		return $this->thumbs['up'];
		
	}
	
	// return thumbs down
	
	public function return_thumbs_down()
	{
		
		return $this->thumbs['down'];
		
	}



Is This A Good Question/Topic? 0
  • +

Replies To: Return Thumb Rating

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3714
  • Posts: 13,467
  • Joined: 08-August 08

Re: Return Thumb Rating

Posted 27 August 2015 - 09:23 AM


This post has been edited by CTphpnwb: 27 August 2015 - 09:24 AM

Was This Post Helpful? 0
  • +
  • -

#3 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13484
  • View blog
  • Posts: 53,826
  • Joined: 12-June 08

Re: Return Thumb Rating

Posted 27 August 2015 - 09:24 AM

Why is column 'type' a varchar?

How is this table structured?

At worst you would have an ID for what ever you are hooking this to, an integer for 'up', and an integer column for 'down'... then you would SELECT the ID, the sum of column up, and the sum for column down GROUPED BY the id.... probably WHERE the id equals something
Was This Post Helpful? 0
  • +
  • -

#4 JeremyBenson11  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 230
  • Joined: 10-January 14

Re: Return Thumb Rating

Posted 27 August 2015 - 09:42 AM

View Postmodi123_1, on 27 August 2015 - 09:24 AM, said:

Why is column 'type' a varchar?

How is this table structured?

At worst you would have an ID for what ever you are hooking this to, an integer for 'up', and an integer column for 'down'... then you would SELECT the ID, the sum of column up, and the sum for column down GROUPED BY the id.... probably WHERE the id equals something


ID Username Type Token
1 'Bob' 'up' SDF4F34f

each user can only rate once, either up or down, I may make it so the user can edit their rating by clicking the other thumb type. Token corresponds to the product which also has the same token.
Was This Post Helpful? 0
  • +
  • -

#5 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1828
  • View blog
  • Posts: 5,755
  • Joined: 15-January 14

Re: Return Thumb Rating

Posted 27 August 2015 - 09:45 AM

$this->thumbs['up'] = $db->query($queryUp)->execute();


You're running query, and then execute. Execute is for prepared statements, you don't have a prepared statement. Execute also returns a boolean value, and that's what you're saving. It's printing 1 because it is returning boolean true. If you want to get the result of the query then use one of the fetch methods, like fetch_column.
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13484
  • View blog
  • Posts: 53,826
  • Joined: 12-June 08

Re: Return Thumb Rating

Posted 27 August 2015 - 09:48 AM

That doesn't make sense on a few levels. Why is there a 'user' name in there? It should be a USER ID. You would figure the key for the row would be a USER ID and item ID.

Again - it's not a best practice to have varchars when a simple number would work. 1 = up, 2 = down... etc.
Was This Post Helpful? 0
  • +
  • -

#7 JeremyBenson11  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 230
  • Joined: 10-January 14

Re: Return Thumb Rating

Posted 27 August 2015 - 01:45 PM

Actually storing the username makes more sense to me. Makes things readable. I don't have to do any extra queries in my db to find information if I know the user that made the action just by looking at the name.Pretty much all my tables store an associative user. Makes things easier to keep track of.

Thanks for the replies. I'm gonna check into fetch_column, and see how proper query calls work,
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13484
  • View blog
  • Posts: 53,826
  • Joined: 12-June 08

Re: Return Thumb Rating

Posted 27 August 2015 - 01:51 PM

Typically you want keys that are unique and divorced from any business value. Numbers happen to be great because they work well on joins, auto increment, make things more flexible, etc.

https://en.wikipedia...i/Surrogate_key

*shrug* .. but what evs.. it's your project.
Was This Post Helpful? 0
  • +
  • -

#9 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1828
  • View blog
  • Posts: 5,755
  • Joined: 15-January 14

Re: Return Thumb Rating

Posted 27 August 2015 - 05:07 PM

I always store a numeric user ID instead of something like a username. If a user changes their username you only want to have to do that in one table. Their ID will never change. If you need human-readable output use a join. And, yeah, the indexes are smaller also. My databases have gigabytes of indexes, and those are only integers. If the indexes had to be strings that would be much larger. I'm not sure if there's a great way to check the total index size on a database, but I notice it with an export. I might have a database that phpMyAdmin claims is 7.5GB, but when it gets dumped to a .sql file where most of the file is the text data the file might be only 5GB. The major difference is the indexes.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1