Jump to content

PHP Syntax using SQL - alreadyrated - Help??


SoLoGHoST

Recommended Posts

Hello, I'm trying to use this and wondering if 'alreadyrated' will work as an array in $context['downloads_files']['alreadyrated'] when calling it in the template.  I need 'alreadyrated' to check in a different table to determine if that user has already rated this file, if so, I need $context['downloads_files']['alreadyrated'] to store an array of the list of files that have been already rated by that user.

 

	// Show the downloads

	$dbresult = $smcFunc['db_query']('', "

	SELECT

		p.ID_FILE, p.totalratings, p.rating, p.commenttotal,

	 	p.filesize, p.views, p.title, p.id_member, m.real_name,

	 	 p.date, p.description, p.totaldownloads, p.allowcomments, p.fileurl, p.orginalfilename

	FROM {db_prefix}down_file as p

		LEFT JOIN {db_prefix}members AS m ON (p.id_member = m.id_member)

	WHERE  p.ID_CAT = $cat AND p.approved = 1

	ORDER BY $sortby $orderby

	LIMIT $context[start]," . $modSettings['down_set_files_per_page_cool']);

	$context['downloads_files'] = array();

	while($row = $smcFunc['db_fetch_assoc']($dbresult))

	{

		$context['downloads_files'][] = array(

		'ID_FILE' => $row['ID_FILE'],

		'title' => $row['title'],

		'totalratings' => $row['totalratings'],

		'rating' => $row['rating'],

		'commenttotal' => $row['commenttotal'],

		'filesize' => $row['filesize'],

		'views' => $row['views'],

		'id_member' => $row['id_member'],

		'real_name' => $row['real_name'],

		'date' => $row['date'],

		'description' => $row['description'],

		'totaldownloads' => $row['totaldownloads'],

		'allowcomments' => $row['allowcomments'],

		'fileurl' => $row['fileurl'],

		'orginalfilename' => $row['orginalfilename'],

		'alreadyrated' ->query( "SELECT id_member, ID_FILE
					FROM {db_prefix}down_rating 
					WHERE id_member = " . $user_info['id'] . " AND ID_FILE = " . $row['ID_FILE']) );
		);



	}

	$smcFunc['db_free_result']($dbresult);	

 

 

I feel that there is something wrong with the particular syntax below, but not sure, can someone please confirm/deny this and help me with the correct syntax for this??

 

		'alreadyrated' ->query( "SELECT id_member, ID_FILE
				FROM {db_prefix}down_rating 
				WHERE id_member = " . $user_info['id'] . " AND ID_FILE = " . $row['ID_FILE']) );

 

I don't know how to make it so that alreadyrated = the Affected rows as in = ['db_affected_rows']();  then in the template I'll just check if the affected rows == 0 or != 0.  Does this make sense to you?  If so, can you please give me a heads up on how to do this.  I am calling

foreach ($context['downloads_files'] as $i => $file)

within the template file so, 'alreadyrated' should be within this group of variables I assume.  That way, I can just check if

if ($files['alreadyrated'] == 0)
// User did not rate this file yet, allow them to rate it...
else if ($files['alreadyrated'] != 0)
// User already rated this file, do not allow them to rate it again...

in the template.

 

Can someone please help with this??  Am I even doing this the right way??

 

Should 'alreadyrated' be defined like this instead??

		'alreadyrated' =>  $dbresult2 = $smcFunc['db_query']('', "SELECT id_member, ID_FILE
			FROM {db_prefix}down_rating
			WHERE id_member = " . $user_info['id'] . " AND ID_FILE = " . $row['ID_FILE']) ? $smcFunc['db_affected_rows']() : $smcFunc['db_affected_rows'](); 
                                                                 $smcFunc['db_free_result']($dbresult2);

 

PLEASE Somebody HELP ME! :'( :'(

Link to comment
https://forums.phpfreaks.com/topic/148560-php-syntax-using-sql-alreadyrated-help/
Share on other sites

Then there is this way of doing it I suppose by merging the 2 tables together into 1 SELECT Case and giving each table a different letter, but I don't know how to get around id_member and ID_FILE coming up in both tables...  Can I do something like $row['p.id_member'] and $row['r.id_member'] like I have it in the code below??

	// Show the downloads

	$dbresult = $smcFunc['db_query']('', "

	SELECT

		p.ID_FILE, p.totalratings, p.rating, p.commenttotal,

	 	p.filesize, p.views, p.title, p.id_member, m.real_name,

	 	 p.date, p.description, p.totaldownloads, p.allowcomments, p.fileurl, p.orginalfilename, r.id_member, r.ID_FILE

	FROM {db_prefix}down_file as p, {db_prefix}down_rating as r

		LEFT JOIN {db_prefix}members AS m ON (p.id_member = m.id_member)

	WHERE  p.ID_CAT = $cat AND p.approved = 1

	ORDER BY $sortby $orderby

	LIMIT $context[start]," . $modSettings['down_set_files_per_page_cool']);

	$context['downloads_files'] = array();

	while($row = $smcFunc['db_fetch_assoc']($dbresult))

	{

		$context['downloads_files'][] = array(

		'ID_FILE' => $row['ID_FILE'],

		'title' => $row['title'],

		'totalratings' => $row['totalratings'],

		'rating' => $row['rating'],

		'commenttotal' => $row['commenttotal'],

		'filesize' => $row['filesize'],

		'views' => $row['views'],

		'id_member' => $row['id_member'],

		'real_name' => $row['real_name'],

		'date' => $row['date'],

		'description' => $row['description'],

		'totaldownloads' => $row['totaldownloads'],

		'allowcomments' => $row['allowcomments'],

		'fileurl' => $row['fileurl'],

		'orginalfilename' => $row['orginalfilename'],

		'alreadyrated' => $row['r.id_member'] == $user_info['id'] && $row['r.ID_FILE'] == $row['p.ID_FILE'] ? 1 : 0;

		);



	}

	$smcFunc['db_free_result']($dbresult);	

 

I'm very confused here :( Any Help on this is greatly appreciated!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.