Commits

Fausto Carrera committed 2e5f250

fix insert data

Comments (0)

Files changed (3)

app/controllers/crons.php

 		$trades_url = 'https://localbitcoins.com/bitcoincharts/ARS/trades.json';
 		$trades = file_get_contents($trades_url);
 		$trades = json_decode($trades, true);
-		$result = $this->trades_model->createTradeBatch($trades);
-		echo $result.PHP_EOL;
+		foreach ($trades as $trade) {
+			$result = $this->trades_model->createTrade($trade);
+			echo $result.PHP_EOL;
+		}
 	}
 }
 

app/models/trades_model.php

 		parent::__construct();
 	}
 
-	public function createTradeBatch($data) {
+	public function createTrade($data) {
 		$this->db->ignore();
-		$result = $this->db->insert_batch('trades', $data);
+		$result = $this->db->insert('trades', $data);
+		return $result;
 	}
 }

files/cron.php

-<?php
-if (php_sapi_name() != 'cli') {
-    echo 'Opps, run from cli';
-    exit;
-}
-
-$host = 'localhost';
-$user = 'root';
-$pass = '';
-$db = 'sd_trades';
-
-$trades_url = 'https://localbitcoins.com/bitcoincharts/ARS/trades.json';
-//$trades_url = 'http://localhost/playground/php/trades/trades.json';
-try {
-	$trades = file_get_contents($trades_url);
-	$trades = json_decode($trades);
-	// connect
-	$mysqli = new mysqli($host, $user, $pass, $db);
-	// check mysqli connection
-	if(mysqli_connect_errno()) {
-		echo 'Error: Could not connect to database '.$db;
-		exit;
-	}
-	// parse trades
-	foreach ($trades as $trade) {
-		// insert the trades data
-		$query = sprintf('INSERT IGNORE INTO trades SET tid = %s, date = %s, amount = %s, price = %s',
-							$mysqli->real_escape_string($trade->tid),
-							$mysqli->real_escape_string($trade->date),
-							$mysqli->real_escape_string($trade->amount),
-							$mysqli->real_escape_string($trade->price));
-		if($mysqli->query($query)) {
-			echo 'Trade inserted'.PHP_EOL;
-		}else{
-			echo 'Database Error: Unable to create record'.PHP_EOL;
-		}
-		usleep(10);
-	}
-	$mysqli->close();
-} catch(Exception $e) {
-	echo 'Caught exception: '.$e->getMessage().PHP_EOL;
-}