<?php
include 'dbconn.php';
include 'func.php';

$data = array("setting_name","setting_value");
$data_length = count($data);

$stmt = $conn->prepare("SELECT setting_name, setting_value FROM $game_settings WHERE game_type = '$gameType' AND setting_status = 1");
$stmt->execute();
$stmt->store_result();
$num_of_rows = $stmt->num_rows;
$stmt -> bind_result($setting_name, $setting_value);
$users = [];
while ($stmt->fetch()) {
	$users[] = array(
		'setting_name' => $setting_name,
		'setting_value' => $setting_value
	);
}
$stmt->free_result();
$stmt->close();

$setting_data = '';

for($i = 0; $i < $num_of_rows; $i++)
{
	$setting_name = $users[$i]['setting_name'];	
	$comma = ',';
	if($i == ($num_of_rows-1)){
		$comma = '';	
	}
	
	$table_val = '';
	for($c = 0; $c < $data_length; $c++){
		$value = '';
		$commaInner = ',';
		if($c == ($data_length-1)){
			$value = trim(preg_replace('/\s+/', ' ', $users[$i][$data[$c]]));
			$commaInner = '';	
		}else{
			$value = '"'.$users[$i][$data[$c]].'"';			
		}
		$table_val .= '"'.$data[$c].'":'.$value.$commaInner;
	}
	$setting_data .= '{ '.$table_val.' }'.$comma;
}

if($num_of_rows > 0){
	echo '{"status":true, "datas":['.$setting_data.']}';
}else{
	echo '{"status":false}';
}

// Close connection
mysqli_close($conn);
?>