view php/read/updateTierLink.php @ 35:86f79bc1d142

refactor to split out shareable fn to actually do an Amazon API request
author Charlie Root
date Fri, 04 Jan 2019 12:54:12 -0500
parents 077b0a0a3e6d
children 385ddd7c4b55 a67bf725e87b
line wrap: on
line source

<?PHP

$isbn1 = strval($_GET['isbn1']);
echo $isbn1;
$isbn2 = strval($_GET['isbn2']);
echo $isbn2;
$state = strval($_GET['state']);
echo $state;
if (isset($_GET['userID'])) {
  $userID=$_GET['userID'];
 }
$loc = strval($_GET['loc']);
echo $loc;

include "../../../private/db.php";  
//$addState = 0;

$query1 = "CALL b_getLinkID('$isbn1', '$isbn2', $loc)";  // "select LinkID from scoretable where BookID1 = '$isbn1' and BookID2 = '$isbn2' and LocID = $loc";
//echo $query1;
$data1 = mysqli_query($link, $query1) or exit( mysqli_error( $link ));
mysqli_close($link);	//do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop
include "../../../private/db.php"; 

if ( mysqli_num_rows( $data1 ) < 1 )		//does the link exist
{
	if($state == 7)
		$queryL = "CALL b_addNewSearchLink('$isbn1','$isbn2',0, $loc)";
	else
		$queryL = "CALL b_addNewLink('$isbn1','$isbn2',0, $loc)";
	//echo $queryL;
	$results = mysqli_query($link, $queryL);
	
	$rows=mysqli_fetch_array($results, MYSQLI_ASSOC);
	$linkid=$rows['LinkID'];
	
	//reset connection
	mysqli_close($link);	//do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop
	include "../../../private/db.php"; 
			
	
	//if(isset($userID))
	//	$addState = 1;
}
else
{
	$rows=mysqli_fetch_array($data1, MYSQLI_ASSOC);
	$linkid=$rows['LinkID'];
}
	
	$query3 = "CALL b_getLinkIDLinks($linkid)";  //"select LinkID, Positive, Buy, StrongInterest, MiddleInterest, WeakInterest, SearchRel, PositiveLogIn, BuyLogIn, StrongInterestLogIn, BuyLogIn, StrongInterestLogIn, MiddleInterestLogIn, WeakInterestLogIn, SearchRelLogIn from links where LinkID = $linkid";
	//echo $query3;
	$data = mysqli_query($link, $query3) or exit( mysqli_error( $link ));
	
	//reset connection
	mysqli_close($link);	//do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop
	include "../../../private/db.php"; 
	
while($line = mysqli_fetch_assoc($data))
{
	$linkID = $line["LinkID"];
	$pos = $line["Positive"];
	$buy = $line["Buy"];
	$strong = $line["StrongInterest"];
	$middle = $line["MiddleInterest"];
	$weak = $line["WeakInterest"];
	$searchrel = $line["SearchRel"];
	
	$posLogIn = $line["PositiveLogIn"];
	$buyLogIn = $line["BuyLogIn"];
	$strongLogIn = $line["StrongInterestLogIn"];
	$middleLogIn = $line["MiddleInterestLogIn"];
	$weakLogIn = $line["WeakInterestLogIn"];
	$searchrelLogIn = $line["SearchRelLogIn"];
	
	/*    
	RECOMM = 1;
    BUY = 2;
    STRONG = 3;
    WEAK = 4;
    CONTEMPLATE = 5;
    BROWSE = 6;
    SEARCH = 7;*/

	if(isset($userID))
	{
		if($state == 1)
			$posLogIn += 1;
		else if($state == 2)
			$buyLogIn += 1;
		else if ($state == 3)
			$strongLogIn += 1;
		else if ($state == 4)
			$weakLogIn += 1;
		else if ($state == 5)
			$middleLogIn += 1;
		else if ($state == 7)
			$searchrelLogIn += 1;
	}	
	else
	{
		if($state == 1)
			$pos += 1;
		else if($state == 2)
			$buy += 1;
		else if ($state == 3)
			$strong += 1;
		else if ($state == 4)
			$weak += 1;
		else if ($state == 5)
			$middle += 1;
		else if ($state == 7)
			$searchrel += 1;
	}
	
	$updateQuery = "CALL b_increaseTierLink($linkID,$pos,$buy,$strong,$weak,$middle,$searchrel,$posLogIn,$buyLogIn,$strongLogIn,$weakLogIn,$middleLogIn,$searchrelLogIn)";
	//echo $updateQuery;
	$dataFinal = mysqli_query($link, $updateQuery);
	
	if($state == 1)
	{
		$stateQuery = "CALL b_insertThumbState($userID,$linkID,1)";		
		//echo $stateQuery;
		$stateFinal = mysqli_query($link, $stateQuery);
	}
}

mysqli_close($link);

?>