I am using datatables without a problem, but some pages crash due to the large amount of data that it's fetching from the database. So I tried to implement server side processing. I followed the examples on the site, but I just don't seem to receive the json data sent from the php file. Can someone please tell me what's wrong?
What I get on console is: jquery.min.js:5 POST http://example.com//datatables-script.php net::ERR_EMPTY_RESPONSE.
Response at Network tab says: Failed to load response data
This is my ajax call:
$(document).ready(function(){
$('#sort').dataTable({
"ajax": {
url: "datatables-script.php",
type: "POST",
data: {storeid:'<?php echo $stid; ?>'},
//dataType: 'json',
success: function(gotback) {
//var JSONArray = JSON.stringify(data);
//console.log(JSONArray);
console.log(gotback);
}
},
"processing": true,
"serverSide": true,
"bServerSide": true,
});
This is my script: Sorry for the long post.
<?php
mb_internal_encoding('UTF-8');
$aColumns = array( 'Rec_Id', 'Br_Id', 'C_Id', 'SubcId', 'ProdId', 'Prodme', 'URL', 'Im0', 'Price' );
$sIndexColumn = 'Rec_Id';
$sTable = 'tb';
$gaSql['user'] = 'ss';
$gaSql['password'] = 'sss';
$gaSql['db'] = 's';
$gaSql['server'] = 's';
$gaSql['port'] = 3306; // 3306 is the default MySQL port
// Input method (use $_GET, $_POST or $_REQUEST)
$input =& $_POST;
$gaSql['charset'] = 'utf8';
//get store id
$storeid = $_POST['storeid'];
$db = new mysqli($gaSql['server'], $gaSql['user'], $gaSql['password'], $gaSql['db'], $gaSql['port']);
if (mysqli_connect_error()) {
die( 'Error connecting to MySQL server (' . mysqli_connect_errno() .') '. mysqli_connect_error() );
}
if (!$db->set_charset($gaSql['charset'])) {
die( 'Error loading character set "'.$gaSql['charset'].'": '.$db->error );
}
$sLimit = "";
if ( isset( $input['iDisplayStart'] ) && $input['iDisplayLength'] != '-1' ) {
$sLimit = " LIMIT ".intval( $input['iDisplayStart'] ).", ".intval( $input['iDisplayLength'] );
}
$aOrderingRules = array();
if ( isset( $input['iSortCol_0'] ) ) {
$iSortingCols = intval( $input['iSortingCols'] );
for ( $i=0 ; $i<$iSortingCols ; $i++ ) {
if ( $input[ 'bSortable_'.intval($input['iSortCol_'.$i]) ] == 'true' ) {
$aOrderingRules[] =
"".$aColumns[ intval( $input['iSortCol_'.$i] ) ]." "
.($input['sSortDir_'.$i]==='asc' ? 'asc' : 'desc');
}
}
}
if (!empty($aOrderingRules)) {
$sOrder = " ORDER BY ".implode(", ", $aOrderingRules);
} else {
$sOrder = "";
}
$iColumnCount = count($aColumns);
if ( isset($input['sSearch']) && $input['sSearch'] != "" ) {
$aFilteringRules = array();
for ( $i=0 ; $i<$iColumnCount ; $i++ ) {
if ( isset($input['bSearchable_'.$i]) && $input['bSearchable_'.$i] == 'true' ) {
$aFilteringRules[] = "".$aColumns[$i]." LIKE '%".$db->real_escape_string( $input['sSearch'] )."%'";
}
}
if (!empty($aFilteringRules)) {
$aFilteringRules = array('('.implode(" OR ", $aFilteringRules).')');
}
}
// Individual column filtering
for ( $i=0 ; $i<$iColumnCount ; $i++ ) {
if ( isset($input['bSearchable_'.$i]) && $input['bSearchable_'.$i] == 'true' && $input['sSearch_'.$i] != '' ) {
$aFilteringRules[] = "".$aColumns[$i]." LIKE '%".$db->real_escape_string($input['sSearch_'.$i])."%'";
}
}
if (!empty($aFilteringRules)) {
$sWhere = " WHERE sid=$storeid ".implode(" AND ", $aFilteringRules);
} else {
$sWhere = "";
$aQueryColumns = array();
foreach ($aColumns as $col) {
if ($col != ' ') {
$aQueryColumns[] = $col;
}
}
$sQuery = "
SELECT SQL_CALC_FOUND_ROWS ".implode(", ", $aQueryColumns)."
FROM ".$sTable."".$sWhere.$sOrder.$sLimit;
//echo $sQuery;
$rResult = $db->query( $sQuery ) or die($db->error);
// Data set length after filtering
$sQuery = "SELECT FOUND_ROWS()";
$rResultFilterTotal = $db->query( $sQuery ) or die($db->error);
list($iFilteredTotal) = $rResultFilterTotal->fetch_row();
// Total data set length
$sQuery = "SELECT COUNT(".$sIndexColumn.") FROM ".$sTable."";
$rResultTotal = $db->query( $sQuery ) or die($db->error);
list($iTotal) = $rResultTotal->fetch_row();
$output = array(
"sEcho" => intval($input['sEcho']),
"iTotalRecords" => $iTotal,
"iTotalDisplayRecords" => $iFilteredTotal,
"aaData" => array(),
);
while ( $aRow = $rResult->fetch_assoc() ) {
$row = array();
for ( $i=0 ; $i<$iColumnCount ; $i++ ) {
if ( $aColumns[$i] == 'version' ) {
// Special output formatting for 'version' column
$row[] = ($aRow[ $aColumns[$i] ]=='0') ? '-' : $aRow[ $aColumns[$i] ];
} elseif ( $aColumns[$i] != ' ' ) {
// General output
$row[] = $aRow[ $aColumns[$i] ];
}
}
$output['aaData'][] = $row;
}
echo json_encode( $output );
Check this example, I am using the same in my project:
JS:
$('#datatable_location_details').dataTable({
"sServerMethod": "GET",
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "getdetails.php,
// "aoColumns": [null, null, null, null, { "bSortable": true, "sClass": "align_center" }, { "bSortable": true, "sClass": "align_center" }, { "bSortable": false, "sClass": "align_center" }]
});
Php:
$start = $_REQUEST['iDisplayStart'];
$length = $_REQUEST['iDisplayLength'];
$col = $_REQUEST['iSortCol_0'];
$arr = array(0 => 'state', 1 => 'city', 2 => 'institute_name', 3 => 'address', 4 => 'student_count', 5 => 'ranking');
$sort_by = $arr[$col];
$sort_type = $_REQUEST['sSortDir_0'];
$qry = "select id, institute_name, address, state, city, student_count, ranking, latitude, longitude from location_details where (state LIKE '%".$state."%' and city LIKE '%".$city."%' and institute_name LIKE '%".$name."%' and address LIKE '%".$address."%') and (address != '' and state != '' and city != '') ORDER BY ".$sort_by." ".$sort_type." LIMIT ".$start.", ".$length;
$res = mysqli_query($con, $qry);
while($row = mysqli_fetch_assoc($res))
{
$data[] = $row;
}
$qry = "select count(id) as count from location_details";
$res = mysqli_query($con, $qry);
while($row = mysqli_fetch_assoc($res))
{
$iTotal = $row['count'];
}
$rec = array(
'iTotalRecords' => $iTotal,
'iTotalDisplayRecords' => $iTotal,
'aaData' => array()
);
$k=0;
if (isset($data) && is_array($data))
{
foreach ($data as $item)
{
$action = 'Show Map';
$tooltip= 'Show Map';
$class = 'show_details';
if($item['latitude'] == '' && $item['longitude'] == '')
{
$action = 'Modify Address';
$tooltip= 'Latitude & Longitude are not available for this address';
$class = 'modify_address';
}
$rec['aaData'][$k] = array(
0 => $item['state'],
1 => $item['city'],
2 => htmlspecialchars($item['institute_name'], ENT_QUOTES),
3 => htmlspecialchars($item['address'], ENT_QUOTES),
4 => $item['student_count'],
5 => $item['ranking'],
6 => ''.$action.'',
);
$k++;
}
}
echo json_encode($rec);
For more info check this Github Project
Related
I am getting error while updating my sales order using for each.
foreach( $_POST['productId'] as $key => $val){
$invoice_arrs = array(
"product_name" => $_POST['productname'][$key],
"purchaseQty" => $_POST['purchaseQty'][$key],
"sellingPrice" => $_POST['sellingPrice'][$key],
"discount" => $_POST['discount'][$key],
"dis_type" => $_POST['discounttype'][$key],
"total" => $_POST['total'][$key],
"net_price" => $_POST['net_price'][$key]
);
$temp=UpdateRecords("sales_order","sales_order_id=$id",$invoice_arrs,1);
//here is my update record function
function UpdateRecords($table, $condition, $updata, $debug = "") {
global $conn;
foreach ( $updata as $key => $value ) {
if ($value != "now()") {
$fv [] = "$key = \"" . "$value" . "\"";
} else {
$fv [] = "$key = " . "$value" . "";
}
}
$fv_list = trim ( implode ( ", ", $fv ) );
$query = "UPDATE $table SET " . "$fv_list" . " WHERE $condition";
if ($debug == 1) {
echo $query;
}
$result = executeQuery( $query );
if (! mysqli_affected_rows ( $conn )) {
global $errormessage;
$errormessage = mysqli_error ( $conn );
if (! empty ( $errormessage ))
return 0;
}
return 1;
}
You can try below query -
INSERT INTO $table (product_name,
purchaseQty,
sellingPrice,
discount,
dis_type,
total,
net_price)
VALUES($_POST['productname'][$key],
$_POST['purchaseQty'][$key],
$_POST['sellingPrice'][$key],
$_POST['discount'][$key],
$_POST['discounttype'][$key],
$_POST['total'][$key],
$_POST['net_price'][$key])
ON DUPLICATE KEY UPDATE product_name = VALUES($_POST['productname'][$key]),
purchaseQty = VALUES($_POST['purchaseQty'][$key]),
sellingPrice = VALUES($_POST['sellingPrice'][$key]),
discount = VALUES($_POST['discount'][$key]),
dis_type = VALUES($_POST['discounttype'][$key]),
total = VALUES($_POST['total'][$key]),
net_price = VALUES($_POST['net_price'][$key]);
I want to search data using ajax method with multiple fields search option (e.g. name, college, department, year, nationality e.t.c ). I have insert name for searching and rest of fields are empty than it went to foreach loop but this if (isset($_GET[$field]) && !empty($_GET['$field'])) condition not successful and went to else loop
$fields = array(
'name' => TRUE,
'gender' => TRUE,
'colf' => TRUE,
'deptf' => TRUE,
'natf' => TRUE,
'fstatusf' => TRUE,
'fyearf' => TRUE
);
foreach ($fields as $field => $like) {
if (isset($_GET[$field]) && !empty($_GET['$field'])) {
$value = $_GET[$field];
$search[] = $field . ( $like ? ('LIKE "%' . $value . '%"') : ('="' . $value . '"') );
}
}
if ($search) {
$sql = 'SELECT * FROM fmaf WHERE ' . implode(' or ' . $search);
}
else{
$sql="SELECT * FROM fmaf";
}
At last i have found the solution and thanks to cFreed and other who help me. My main concern is that if user want to search with one field only or more than 1 field in that case below answer is helpful for me and may be also for someone:
if (empty($_GET['name']) && empty($_GET['gender']) && empty($_GET['colf']) && empty($_GET['deptf']) && empty($_GET['natf']) && empty($_GET['fstatusf']) && empty($_GET['fyearf']))
{
$sql="select * from fmaf ";
}
else
{
$wheres = array();
$sql = "select * from fmaf where ";
if (isset($_GET['name']) and !empty($_GET['name']))
{
$wheres[] = "name like '%{$_GET['name']}%' ";
}
if (isset($_GET['gender']) and !empty($_GET['gender']))
{
$wheres[] = "gender = '{$_GET['gender']}'";
}
if (isset($_GET['colf']) and !empty($_GET['colf']))
{
$wheres[] = "college = '{$_GET['colf']}' ";
}
if (isset($_GET['deptf']) and !empty($_GET['deptf']))
{
$wheres[] = "department = '{$_GET['deptf']}' ";
}
if (isset($_GET['natf']) and !empty($_GET['natf']))
{
$wheres[] = "nationality = '{$_GET['natf']}' ";
}
if (isset($_GET['fstatusf']) and !empty($_GET['fstatusf']))
{
$wheres[] = "finalstatus = '{$_GET['fstatusf']}' ";
}
if (isset($_GET['fyearf']) and !empty($_GET['fyearf']))
{
$wheres[] = "fyear = '{$_GET['fyearf']}' ";
}
foreach ( $wheres as $where )
{
$sql .= $where . ' AND '; // you may want to make this an OR
}
$sql=rtrim($sql, "AND ");
}
You need to build the query depending on the request.
A toy example is this:
$sql = "select * from student where 1 = 1".(isset($name)?" AND name like '%$name%":"").(isset($country)?" AND country = '$country'":"").";";
You may use a simple way, being able to face any case, like this:
// define searchable fields, with option for LIKE|EQUAL (TRUE|FALSE)
$fields = [
'name' => TRUE,
'country' => TRUE,
'address' => TRUE,
'gender' => FALSE,
'state' => FALSE
];
foreach ($fields as $field => $like) {
if (isset($_GET[$field]) AND !empty($_GET['$field'])) {
$value = $_GET[$field];
// prepare WHERE condition item, depending on LIKE option
$search[] = $field . (
$like ? ('LIKE "%' . $value . '%"') : ('="' . $value . '"')
);
}
}
if ($search) {
$sql = 'SELECT * FROM student WHERE ' . implode(' AND ' . $search);
}
Hi i have a counter code. I want to stop when (example) 11 result. how can i stop curl when script count 11 result?
<?php
class instaAuth
{
public function _userlistesi() {
$klasor = opendir("cookieboq/");
while(false !== ($veriler = readdir($klasor))) {
if(strstr($veriler,'selco')) {
$bir_veri = str_replace('.selco','',$veriler);
$user_ekle .= "$bir_veri,";
}
}
$userler = substr($user_ekle,0,-1);
$users_arr = explode(",",$userler);
return $users_arr;
}
public function _authLike($_ID,$userlist, $DELETE = NULL)
{
if ( !empty($userlist) )
{
$type = ($DELETE == TRUE) ? 'unlike' : 'like';
$members = array();
$params = array();
foreach ( $userlist as $username )
{
$_cookie = $this->_cookieFolder . $username . $this->_cookieExtension;
$randomProxy = ( count($this->_proxy) > 0 ) ? $this->_proxy[rand(0, (count($this->_proxy) - 1))] : NULL;
if ( !file_exists($_cookie) )
{
continue;
}
$members[] = $username;
$fake_ip = $this->fakeIP();
$header = array(
"Accept: */*",
"Accept-Language: tr;q=1",
"HTTP_CLIENT_IP" => $fake_ip,
"HTTP_FORWARDED" => $fake_ip,
"HTTP_PRAGMA" => $fake_ip,
"HTTP_XONNECTION" => $fake_ip,);
$options = array(
CURLOPT_POST => TRUE,
CURLOPT_POSTFIELDS => $this->signed(json_encode(array())),
CURLOPT_HTTPHEADER => $header,
CURLOPT_COOKIEJAR => $_cookie,
CURLOPT_COOKIEFILE => $_cookie,
CURLOPT_USERAGENT => $this->_useragent,
);
$params[] = array(
'url' => "https://i.instagram.com/api/v1/media/{$_ID}/{$type}/",
'options' => $options
);
}
} else {
throw new Exception('Error: _authLike() - Beğeni yaptırabilmeniz için {cookies} tanımlamalısınız.');
}
$multiCURL = new \RollingCurl\RollingCurl();
foreach ($params as $param) {
$request = new \RollingCurl\Request($param['url'], 'POST');
$request->setOptions($param['options']);
$multiCURL->add($request);
}
$this->_result = 0;
$multiCURL
->setCallback(function(\RollingCurl\Request $request, \RollingCurl\RollingCurl $rollingCurl) {
$result = $request->getResponseText();
if ( is_string($result) )
{
$result = json_decode($result);
if ( is_object($result) )
{
if ($result->status == 'ok') $this->_result++;
}
}
})
->setSimultaneousLimit(100)
->execute()
;
$result = array(
'success' => $this->_result,
'errors' => NULL
);
return json_decode(json_encode($result));
}
}
and this insterested other code page
<form action="" method="POST">
Pictures ID : <input type="text" name="apifotoId">
<br>
Limits : <input type="text" name="limit">
<br>
<input type="submit" value="Like Send">
</form>
<?php
include("class.php")
if(isset($_POST['apifotoId'])) {
$start = time();
$y = new instaAuth();
$user_list = $y->_userlistesi();
$limit = $_POST["limit"];
$_ID = $_POST['apifotoId'];
$y->_authLike($_ID,$user_list);
$fark = round(time()-$start)/60;
echo "Likes Complete (".$fark." dk.)";
}
?>
I want to stop when (example) 11 result. how can i stop curl when script count 11 result?
you can make an if statement only in function "authLike" condition if(result ==11) break;
and you if you clear your answer to help you more .
So, here is my problem on login through steam id it creates an account on my website but it also decides on next login to skip an Auto Increment causing the next registered member to gain a ton of Auto Incremented member id's
<?php
require ("common.php");
class SteamSignIn
{
const STEAM_LOGIN = 'https://steamcommunity.com/openid/login';
public static function genUrl($returnTo = false, $useAmp = true)
{
$returnTo = (!$returnTo) ? (!empty($_SERVER['HTTPS']) ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'] : $returnTo;
$params = array(
'openid.ns' => 'http://specs.openid.net/auth/2.0',
'openid.mode' => 'checkid_setup',
'openid.return_to' => $returnTo,
'openid.realm' => (!empty($_SERVER['HTTPS']) ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'],
'openid.identity' => 'http://specs.openid.net/auth/2.0/identifier_select',
'openid.claimed_id' => 'http://specs.openid.net/auth/2.0/identifier_select',
);
$sep = ($useAmp) ? '&' : '&';
return self::STEAM_LOGIN . '?' . http_build_query($params, '', $sep);
}
public static function validate()
{
$params = array(
'openid.assoc_handle' => $_GET['openid_assoc_handle'],
'openid.signed' => $_GET['openid_signed'],
'openid.sig' => $_GET['openid_sig'],
'openid.ns' => 'http://specs.openid.net/auth/2.0',
);
$signed = explode(',', $_GET['openid_signed']);
foreach($signed as $item)
{
$val = $_GET['openid_' . str_replace('.', '_', $item)];
$params['openid.' . $item] = get_magic_quotes_gpc() ? stripslashes($val) : $val;
}
$params['openid.mode'] = 'check_authentication';
$data = http_build_query($params);
$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' =>
"Accept-language: en\r\n".
"Content-type: application/x-www-form-urlencoded\r\n" .
"Content-Length: " . strlen($data) . "\r\n",
'content' => $data,
),
));
$result = file_get_contents(self::STEAM_LOGIN, false, $context);
preg_match("#^http://steamcommunity.com/openid/id/([0-9]{17,25})#", $_GET['openid_claimed_id'], $matches);
$steamID64 = is_numeric($matches[1]) ? $matches[1] : 0;
return preg_match("#is_valid\s*:\s*true#i", $result) == 1 ? $steamID64 : '';
}
}
$steam_login_verify = SteamSignIn::validate();
if(!empty($steam_login_verify))
{
// Grab Data From Steam API
$json = file_get_contents('http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=' . $sapik . '&steamids='. $steam_login_verify .'&format=json');
//Decode Data From Steam API
$data = json_decode($json);
foreach($data->response->players as $player)
{
$query = "INSERT INTO steam (steamid, personaname, profileurl, avatar, avatarmedium, avatarfull ) VALUES ( :steamid, :personaname, :profileurl, :avatar, :avatarmedium, :avatarfull) ";
$query_params = array(
':steamid' => $player->steamid,
':personaname' => $player->personaname,
':profileurl' => $player->profileurl,
':avatar' => $player->avatar,
':avatarmedium' => $player->avatarmedium,
':avatarfull' => $player->avatarfull,
);
}
try
{
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch(PDOException $ex)
{
switch( $ex->errorInfo[1] )
{
case 1062:
$ps = $db->prepare("SELECT * FROM `steam` WHERE steamid = :sid");
$ps->bindParam(':sid', $steam_login_verify);
$ps->execute();
$ps->setFetchMode(PDO::FETCH_ASSOC);
foreach ($ps as $row)
{
$_SESSION['sid'] = $row['steamid'];
}
header('Location:'.$basedir);
die('redirecting to'.$basedir);
;
}
}
$ps = $db->prepare("SELECT * FROM `steam` WHERE steamid = :sid");
$ps->bindParam(':sid', $steam_login_verify);
$ps->execute();
$ps->setFetchMode(PDO::FETCH_ASSOC);
foreach ($ps as $row)
{
$_SESSION['sid'] = $row['steamid'];
}
header('Location:'.$basedir);
die('redirecting to'.$basedir);
} else {
$steam_sign_in_url = SteamSignIn::genUrl();
}
Hy, I designed an array, and I need some help because I've got aaaloot of headaches. Can anyone give me a simple idea how to work with this in a simple way?
Most of the errors when I try to add some values in my array are array offset error.
My array:
$info = array
(
"id" => "",
"city" => "",
"university" => array
(
"name" => "",
"address" => "",
"phone" => "",
"fax" => "",
"email" => "",
"website" => "",
"type" => "",
"faculty" => array
(
"name" => "",
"adress" => "",
"phone" => "",
"fax" => "",
"email" => "",
"website" => "",
"type" => "",
)
)
);
CODE:
<?php
// HTML DOM PARSER LIBRARY
include('hdp.php');
//error_reporting(false);
/*
* Returns an array with field associative name and
* extracted value, using a specified delimiter.
*/
function extractField($str, $delim)
{
$val = '';
// All possible fields
$posFlds = array
(
'Adresa' => 'address',
'Telefon' => 'phone',
'Fax' => 'fax',
'Email' => 'email',
'Website' => 'website',
'Tip' => 'type'
);
foreach($posFlds as $fldName => $assocName)
{
if(strstr($str,$fldName))
{
$val = #explode($delim,$str);
if (!$val[1])
{
print 'Delimiter `'.$delim.'` not found!';
return false;
}
return array($assocName, $val[1]);
}
}
return false;
}
// DB->Table query structure
$info = array
(
"id" => "",
"city" => "",
"university" => array
(
"name" => "",
"address" => "",
"phone" => "",
"fax" => "",
"email" => "",
"website" => "",
"type" => "",
"faculty" => array
(
"name" => "",
"adress" => "",
"phone" => "",
"fax" => "",
"email" => "",
"website" => "",
"type" => "",
)
)
);
// City identifier
$cityid = 0;
// Rows, columns and deep identifiers
$i = 0; $j = 0; $k = 0;
// Base, secondary and third crawl url
$baseUrl = 'XXX';
$secondaryUrl = ''; $thirdUrl ='';
// Html contents without stripped tags (as is)
$htmlCu = file_get_html($baseUrl);
$htmltUn = ''; $htmltFa = '';
// Selectors for third level deep check
$selectCu = $htmlCu->find('td[width="100%"] table td');
$selectUn = ''; $selectFa ='';
// Text formats (raw and cleaned)
$rtext =''; $ftext = '';
// Contoare
$a=0; $b=0; $c=0;
// Select each row from parent td -> child table -> child td
foreach($selectCu as $row)
{
// Skip first row result
if($i != 0)
{
$rtext = $row->plaintext;
$ftext = trim($rtext,' • ');
if($ftext != '-')
{
// If string contains a dot it is a city
if(strstr($rtext, '•'))
{
print "CITY = ".$ftext."<br>";
$cityid++;
$info[$a]["city"] = $ftext;
}
// If string is not a city, then is a university
else
{
$info[$a]["city"][$b]['university']['name'] = $ftext;
echo "<b>----ID [".$i."]; NAME:</b> ".$ftext."<br>";
$secondaryUrl = $row->find('a',0)->href;
$htmlUn = file_get_html($secondaryUrl);
$selectUn = $htmlUn->find('td[width="100%"] table tr');
foreach($selectUn as $col)
{
// Skip first row result
if($j != 0)
{
$rtext = $col->plaintext;
$ftext = trim($rtext,' • ');
echo "--------ID[".$j."] NAME: <b>".$ftext."</b>; TYPE: ".gettype($col)."<br>";
// key 0 -> associative name; key 1 -> value
$field = extractField($ftext,': ');
if($field)
{
echo "--------<b>Field name:</b> ".$field[0]."<b>, value:</b> ".$field[1]."<br>";
}
/////////////////////////////TESTTTTTT ZONEEEEEEEEEEEEEEE/////////////////////////////
// If string contains a dot it is a faculty
if(strstr($rtext, '•'))
{
$thirdUrl = $col->find('a',0)->href;
$htmlFa = file_get_html($thirdUrl);
$selectFa = $htmlFa->find('td[width="100%"] table tr');
foreach($selectFa as $deep)
{
$rtext = $deep->plaintext;
$ftext = trim($rtext,' • ');
//echo "------------id[".$k."] <-> <b>".$ftext."</b> <-> ".gettype($deep)."<br>";
$field = extractField($ftext,': ');
if($field)
{
echo "------------<b>Field name:</b> ".$field[0]."<b>, value:</b> ".$field[1]."<br>";
}
$k++;
}
echo "<br><br>";
}
}
$j++;
}
echo "<br><br>";
}
}
}
$i++;
if($cityid == 2) break;
}
print "<h1>".($i-1)."</h1>";
print_r($info);
You are accessing the array the wrong way in those calls:
$info[$a]["city"] = $ftext;
$info[$a]["city"][$b]['university']['name'] = $ftext;
Correct would be:
$info["city"] = $ftext;
$info["city"]['university']['name'] = $ftext;
It seems you want to handle multiple $info-arrays. For this you have to create an array of those info-arrays.
Final and working code, if anyone fiind this usefull:
// HTML DOM PARSER LIBRARY
include('hdp.php');
error_reporting(true);
// Session to know what last city was
session_start();
if (!$_SESSION['LAST']) $_SESSION['LAST'] = NULL;
/*
* Returns an array with field associative name and
* extracted value, using a specified delimiter.
*/
function extractField($str, $delim)
{
$val = '';
// All possible fields
$posFlds = Array
(
'Adresa' => 'address',
'Telefon' => 'phone',
'Fax' => 'fax',
'Email' => 'email',
'Website' => 'website',
'Tip' => 'type'
);
foreach($posFlds as $fldName => $assocName)
{
if(strstr($str,$fldName))
{
$val = #explode($delim,$str);
if (!$val[1])
{
print 'Delimiter `'.$delim.'` not found!';
return false;
}
return array($assocName, $val[1]);
}
}
return false;
}
// Trying to connect to local server
$conn = mysqli_connect("localhost","root","","educatie") or die("Could not connect to MySQL server!");
if (mysqli_connect_errno($conn))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// DB->Table Query structure
$info = Array
(
"city" => Array
(
"name" => "",
"university" => Array
(
"name" => "",
"address" => "",
"phone" => "",
"fax" => "",
"email" => "",
"website" => "",
"type" => "",
"faculty" => Array
(
"name" => "",
"adress" => "",
"phone" => "",
"fax" => "",
"email" => "",
"website" => "",
"type" => "",
)
),
)
);
// City identifier
$cityid = 0;
// Rows, columns and deep identifiers
$i = 0; $j = 0; $k = 0;
// Base, secondary and third crawl url
$baseUrl = 'http://infoportal.rtv.net/informatii_universitati.html';
$secondaryUrl = ''; $thirdUrl ='';
// Html contents without stripped tags (as is)
$htmlCu = file_get_html($baseUrl);
$htmltUn = ''; $htmltFa = '';
// Selectors for third level deep check
$selectCu = $htmlCu->find('td[width="100%"] table td');
$selectUn = ''; $selectFa ='';
// Text formats (raw and cleaned)
$rtext =''; $ftext = '';
// Ignore tokens
$banned = array(" ","•","""," ");
// Counters A, B and C
$a=0; $b=0; $c=0;
// Temporary variables
$tmpVar = '';
// Select each row from parent td -> child table -> child td
foreach($selectCu as $row)
{
// Skip first row result
if($i != 0)
{
$rtext = $row->plaintext;
$ftext = trim($rtext,' • ');
if($ftext != '-')
{
// If string contains a dot it is a city
if(strstr($rtext, '•'))
{
if(($_SESSION['LAST'] !== $ftext) && ($_SESSION['LAST'] !== NULL)) continue;
print "<h1>-> Oras: ".$ftext."</h1><br>";
echo "SESSION:".$_SESSION['LAST']."|<br />";
$tmpVar = $ftext;
}
// If string is not a city, then is a university
else
{
$a--;
$info[$a]['city']['name'] = $tmpVar;
$info[$a]['city']['university'][$b]['name'] = $ftext;
$secondaryUrl = $row->find('a',0)->href;
$htmlUn = file_get_html($secondaryUrl);
$selectUn = $htmlUn->find('td[width="100%"] table tr');
foreach($selectUn as $col)
{
// Skip first row result
if($j != 0)
{
$rtext = $col->plaintext;
$ftext = trim($rtext,' • ');
// key 0 -> associative name; key 1 -> value
$field = extractField($ftext,': ');
if($field)
{
if(strstr($field[1],'de stat'))
$info[$a]['city']['university'][$b][$field[0]] = 'de stat';
else
$info[$a]['city']['university'][$b][$field[0]] = $field[1];
}
/////////////////////////////TESTTTTTT ZONEEEEEEEEEEEEEEE/////////////////////////////
// If string contains a dot it is a faculty
if(strstr($rtext, '•'))
{
$tempVar = trim(str_replace($banned,'',$ftext),' ');
$thirdUrl = $col->find('a',0)->href;
$htmlFa = file_get_html($thirdUrl);
$selectFa = $htmlFa->find('td[width="100%"] table tr');
foreach($selectFa as $deep)
{
$rtext = $deep->plaintext;
$ftext = trim($rtext,' • ');
$info[$a]['city']['university'][$b]['faculty'][$c]['name'] = $tempVar;
$field = extractField($ftext,': ');
if($field)
{
if($field[0]=='website')
{
$info[$a]['city']['university'][$b]['faculty'][$c][$field[0]] = $deep->find('a',0)->href;
}
else
{
$info[$a]['city']['university'][$b]['faculty'][$c][$field[0]] = $field[1];
}
}
$k++;
}
$facName = #$info[$a]['city']['university'][$b]['faculty'][$c]['name'];
$facAddr = #$info[$a]['city']['university'][$b]['faculty'][$c]['address'];
$facPhone = #$info[$a]['city']['university'][$b]['faculty'][$c]['phone'];
$facFax = #$info[$a]['city']['university'][$b]['faculty'][$c]['fax'];
$facEmail = #$info[$a]['city']['university'][$b]['faculty'][$c]['email'];
$facWeb = #$info[$a]['city']['university'][$b]['faculty'][$c]['website'];
$facTax = #$info[$a]['city']['university'][$b]['faculty'][$c]['type'];
echo "<b>ID UNIVERSITATE:</b> ".$a."<br />";
echo "<b>Nume facultate:</b> ".$facName."<br />";
echo "<b>Adresa:</b> ".$facAddr."<br />";
echo "<b>Telefon:</b> ".$facPhone."<br />";
echo "<b>Fax:</b> ".$facFax."<br />";
echo "<b>Email:</b> ".$facEmail."<br />";
echo "<b>Pagina web:</b> ".$facWeb."<br />";
echo "<b>Tip taxa:</b> ".$facTax."<br /><br />";
mysqli_query($conn,"
INSERT INTO faculty
VALUES ('$a','$facName','$facAddr','$facPhone','$facFax','$facEmail','$facWeb','$facTax')
");
$c++;
}
}
$j++;
}
$univCity = #$info[$a]['city']['name'];
$univName = #$info[$a]['city']['university'][$b]['name'];
$univAddr = #$info[$a]['city']['university'][$b]['address'];
$univPhone = #$info[$a]['city']['university'][$b]['phone'];
$univFax = #$info[$a]['city']['university'][$b]['fax'];
$univEmail = #$info[$a]['city']['university'][$b]['email'];
$univWeb = #$info[$a]['city']['university'][$b]['website'];
$univTax = #$info[$a]['city']['university'][$b]['type'];
echo "<b>ID UNIVERSITATE:</b> ".$a."<br />";
echo "<b>Oras:</b> ".$univCity."<br />";
echo "<b>Nume universitate:</b> ".$univName."<br />";
echo "<b>Adresa:</b> ".$univAddr."<br />";
echo "<b>Telefon:</b> ".$univPhone."<br />";
echo "<b>Fax:</b> ".$univFax."<br />";
echo "<b>Email:</b> ".$univEmail."<br />";
echo "<b>Pagina web:</b> ".$univWeb."<br />";
echo "<b>Tip taxa:</b> ".$univTax."<br />";
mysqli_query($conn,"
INSERT INTO university
VALUES ('$a','$univCity','$univName','$univAddr','$univPhone','$univFax','$univEmail','$univWeb','$univTax')
");
$b++;
echo "<br><br>";
}
$a++;
}
}
// SESSION IT, IF THIS JOB IS COMPLETE
if($tmpVar) $_SESSION['LAST'] = $tmpVar;
$i++;
}
print "<h1>".($i-1)."</h1>";
print_r($info);