public/lib/reviews.php
/*
    * @author Derek Melchin
    * @abstract Add album review attributes to a review in the database. Things like tags, hometowns, subgenres, labels)
    * @param string $attNAme      The name of the album attribute you are adding
    * @param array  $attValueList List of values to add to the attribute
    * @param int    $reviewID     ID number of the review in the database
    * @param string $idCol        The name of the id column for the attribute in the database on table {$attName}
    * @param string $nameCol      The name of the name column for the attribute in the database on table {$attName}
    */
@author Derek Melchin
@abstract Add album review attributes to a review in the database.
@author Derek Melchin @abstract Add album review attributes to a review in the database .
public function applyReviewAttributes($attName, $attValueList, $reviewID, $idCol='id', $nameCol='name') {
	$strings = [$attName, $idCol, $nameCol];
	$this->sanitizeStrings($strings);
	$this->sanitizeStrings($attValueList);
	if (count($attValueList) > 0) {
	    $valueListStr = "('" . implode("', '", $attValueList) . "')"; 
	    $stmt = $this->mysqli->query("SELECT * FROM $attName WHERE name IN $valueListStr;");
	    $attDict = array_fill_keys($attValueList, NULL);
	    while ($row = $stmt->fetch_array(MYSQLI_ASSOC))
		if (in_array($row[$nameCol], $attValueList))
		    $attDict[$row[$nameCol]] = $row[$idCol];
	    $valuesToAdd = array_keys($attDict, NULL);
	    if (count($valuesToAdd) > 0) {
	        $valuesToAddStr = "('" . implode("', 0), ('", $valuesToAdd) . "', 0)";
	        $this->mysqli->query("INSERT INTO $attName (name, confirmed) VALUES $valuesToAddStr;");
	        $insertID = $this->mysqli->query("SELECT LAST_INSERT_ID() as id")->fetch_array(MYSQLI_ASSOC)['id'];
	        foreach ($attDict as $name => &$id)
		    if (is_null($id)) {
		        $id = $insertID;
			$insertID++;
		    }
	    }
	    $values = '';
	    $elementNum = 0;
	    foreach ($attDict as $attID) {
		if ($elementNum != 0)
		   $values .= ", ";
		$values .= "($reviewID, $attID)";
		$elementNum++;
	    }
	    $this->mysqli->query("INSERT IGNORE INTO review_$attName VALUES $values;");
	}
    }
public function applyReviewAttributes ( $ attName , $ attValueList , $ reviewID , $ idCol = 'id' , $ nameCol = 'name' ) { $ strings = [ $ attName , $ idCol , $ nameCol ] ; $ this -> sanitizeStrings ( $ strings ) ; $ this -> sanitizeStrings ( $ attValueList ) ; if ( count ( $ attValueList ) > 0 ) { $ valueListStr = " (' " . implode ( " ', ' " , $ attValueList ) . " ') " ; $ stmt = $ this -> mysqli -> query ( " SELECT * FROM  $ attName  WHERE name IN  $ valueListStr ; " ) ; $ attDict = array_fill_keys ( $ attValueList , NULL ) ; while ( $ row = $ stmt -> fetch_array ( MYSQLI_ASSOC ) ) if ( in_array ( $ row [ $ nameCol ] , $ attValueList ) ) $ attDict [ $ row [ $ nameCol ] ] = $ row [ $ idCol ] ; $ valuesToAdd = array_keys ( $ attDict , NULL ) ; if ( count ( $ valuesToAdd ) > 0 ) { $ valuesToAddStr = " (' " . implode ( " ', 0), (' " , $ valuesToAdd ) . " ', 0) " ; $ this -> mysqli -> query ( " INSERT INTO  $ attName  (name, confirmed) VALUES  $ valuesToAddStr ; " ) ; $ insertID = $ this -> mysqli -> query ( " SELECT LAST_INSERT_ID() as id " ) -> fetch_array ( MYSQLI_ASSOC ) [ 'id' ] ; foreach ( $ attDict as $ name => & $ id ) if ( is_null ( $ id ) ) { $ id = $ insertID ; $ insertID ++ ; } } $ values = '' ; $ elementNum = 0 ; foreach ( $ attDict as $ attID ) { if ( $ elementNum != 0 ) $ values .= " ,  " ; $ values .= " ( $ reviewID ,  $ attID ) " ; $ elementNum ++ ; } $ this -> mysqli -> query ( " INSERT IGNORE INTO review_ $ attName  VALUES  $ values ; " ) ; } }
@author Derek Melchin
 @abstract Add album review attributes to a review in the database.
@author Derek Melchin @abstract Add album review attributes to a review in the database .
// Sanitize input strings // If the review was submitted with {$attName} // Make an associative array with the {$attNAme} name as the key and the id as the value // If a {$attNAme} doesn't have an id, leave it's value in the array as null // Foreach {$attName} that doesn't have an id, insert it into the {$attName} table and // add the assigned database id to the associative array // Insert all of the {$attName} ids and $reviewID combos submitted in the review to the  // review_${attName} table