Slow php function, Putting a lot of data in to array - php

I have a function that get data from an SQL Server database. The result set is around 5900 records with two columns (Code and Name) in each record.
However this function can take quite a while to run 2-5 or even longer in some cases.
Here is the code:
public function GetAllProductCodes()
{
$Connection = Database::odbc("sage");
$sql = "SELECT Code, Name FROM STKStockItemView";
$ResultSet = Database::execute($Connection, $sql);
$i = 1;
$array = array();
while ($row = odbc_fetch_array($ResultSet)) {
foreach ($row AS $key => $value) {
$array[$i][$key] = $row[$key];
}
$i++;
}
return $array;
}
What would be a more optimum way of running this? Here is a dump of the output array:
array (size=5867)
1 =>
array (size=2)
'Code' => string '0010' (length=4)
'Name' => string 'Product name' (length=14)
2 =>
array (size=2)
'Code' => string '0957' (length=4)
'Name' => string 'Product name' (length=27)
3 =>
array (size=2)
'Code' => string '0958' (length=4)
'Name' => string 'Product name' (length=20)
4 =>
array (size=2)
'Code' => string '1050' (length=4)
'Name' => string 'Product name' (length=16)

I believe the problem lies in your database wrapper. Also, are you making a new database connection every time on your function?
$Connection = Database::odbc("sage");
because if so, that's not a good idea. You should make the database connection once, in another file (say config.php).
Another thing to keep under consideration is to implement a cache system to deal with large amount of data, that way you won't have to query the database as much. If you're not familiar with cache, a simple cache class to use would be "phpfastcache".

Related

Unable To Show Data in PHP Using ASP.NET Web Service

I've created a simple web service in ASP.NET and want that service to be consumed in a PHP application. The web service is as follows:
[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public List<Customer> GetCustomers(int id)
{
id = Convert.ToInt32(HttpContext.Current.Request.QueryString["id"]);
List<Customer> lst = null;
using (var context = new DemoEntities())
{
lst = (from c in context.Customer
where c.CustomerID == id
select c).ToList();
}
return lst;
}
In the above web service, a customer id is passed to retrieve customer details. So to consume this service in PHP, I've tried to do the following using nuSoap library as follows and it works almost:
<?php
require_once("lib/nusoap.php"); //Using the nuSoap library
$client = new nuSoap_Client('http://localhost:1284/MyCustomers.asmx?wsdl', TRUE); //Passed the ASP.NET web service and an object created
$result = $client->call('GetCustomers', array('id' => 1)); //Called the GetCustomers method and passed a default parameter
foreach($result as $item) //Tried to iterate in a foreach loop
{
echo $item; //Here is the issue - The output returns or returned only the name 'Array'
}
?>
I've done PHP programming a long time ago and trying to figure the issue searching google. I've even tried to access the array index with the web service property directly like the below but seems like missing something or may be not the correct way: Any idea would be appreciated
echo $item[1]->CustName;
echo $item[1]; //Even this
Right now, I am getting the Xml data as follows using the Soap web service:
<ArrayOfCustomer>
<Customer>
<CustomerID>2</CustomerID>
<CustName>John</CustName>
<CustAddress>On Earth</CustAddress>
<CustLocation>On Earth</CustLocation>
<CustSex>Male</CustSex>
<CustType>3</CustType>
<CustStatus>2</CustStatus>
<CustDetails>Great guy - Always regular.</CustDetails>
</Customer>
</ArrayOfCustomer>
Update 1: Used var_dump($item) and currently getting the array elements as follows:
array
'Customer' =>
array
'CustomerID' => string '2' (length=1)
'CustName' => string 'John' (length=7)
'CustAddress' => string 'On Earth' (length=25)
'CustLocation' => string 'On Earth' (length=10)
'CustSex' => string 'Male' (length=4)
'CustType' => string '3' (length=1)
'CustStatus' => string '2' (length=1)
'CustDetails' => string 'Great guy - Always regular.' (length=47)
But when tried with this $item->Customer->CustName, getting this error again - Trying to get property of non-object.
Update 2: Again used var_dump($item) and the result is as follows with PHP programming:
<?php
require_once("lib/nusoap.php");
$client = new nuSoap_Client('http://localhost:1284/MyCustomers.asmx?wsdl', TRUE);
$result = $client->call('GetAllCustomers'); //Without any parameter
foreach($result as $item)
{
echo var_dump($item);
}
?>
Output:
array
'Customer' =>
array
0 =>
array
'CustomerID' => string '1' (length=1)
'CustName' => string 'Jack' (length=7)
'CustAddress' => string 'On Earth' (length=25)
'CustLocation' => string 'On Earth' (length=10)
'CustSex' => string 'Male' (length=4)
'CustType' => string '3' (length=1)
'CustStatus' => string '2' (length=1)
'CustDetails' => string 'Regular Customer and always happy to cooperate.' (length=47)
1 =>
array
'CustomerID' => string '2' (length=1)
'CustName' => string 'John' (length=4)
'CustAddress' => string 'On Earth' (length=7)
'CustLocation' => string 'On Earth' (length=10)
'CustSex' => string 'Male' (length=4)
'CustType' => string '3' (length=1)
'CustStatus' => string '2' (length=1)
'CustDetails' => string 'Great guy - Always regular.' (length=25)
Again, tried to use two loops to get the values that's as follows but it only returns first two results and there are total 10 records in the database:
<?php
require_once("lib/nusoap.php");
$client = new nuSoap_Client('http://localhost:1284/MyCustomers.asmx?wsdl', TRUE);
$result = $client->call('GetAllCustomers');
$count = count($result);
foreach($result as $item)
{
for($i = 0; $i <= $count; $i++)
{
echo 'Name: ' . $item['Customer'][$i]['CustName'].'<br/>';
}
}
?>
Output:
Name: Jack //Returns only first two records though it should return all the records
Name: John
Simply do a var_dump($item); in php to see how they array structure is.. currently I do not know what the response object is, but for example you can access the keys as such: echo $item->Customer->CustName;
If it is an array response, not an object, then: $item['Customer']['CustName'];

SQL/PHP sort fields from a form to make insert

I've got a form which users can add new rows by clicking on a button and automatically, it adds +1 on the field's name.
So for example, I've got my train_id_1, train_type_1 and my user wants to add a new one, so now I've got train_id_2 and train_type_2.
In order to save this in my database, I would like to sort and seperate train_type_1 / train_type_2... to make a foreach and then to save in my database.
So, the var_dump of my $_POST looks like :
array (size=60)
'train_id_1' => string ' 07:36' (length=6)
'train_type_1' => string ' -Z' (length=3)
'user_id_1' => string 'CPN' (length=3)
'event_criter_1' =>
array (size=3)
0 => string 'test' (length=4)
1 => string '234' (length=3)
2 => string '532' (length=3)
'train_id_2' => string ' 08:32' (length=6)
'train_type_2' => string ' -X' (length=3)
'user_id_2' => string 'CPN' (length=3)
'event_criter_2' =>
array (size=3)
0 => string 'TESTG' (length=5)
1 => string 'GGG' (length=3)
2 => string 'AETG' (length=4)
'train_id_3' => string ' 08:36' (length=6)
'train_type_3' => string ' -Z' (length=3)
'user_id_3' => string 'CPN' (length=3)
'event_criter_3' =>
array (size=1)
0 => string '' (length=0)
'train_id_4' => string ' 09:04' (length=6)
'train_type_4' => string ' -X' (length=3)
'user_id_4' => string 'CPN' (length=3)
'event_criter_4' =>
array (size=1)
0 => string '' (length=0)
Do you know how I can make abcd_1 separate from abcd_2 to make my foreach (or another solution) ?
Thank you!
Loop through your array, matching _1 and insert the posted elements as one row. Then increment and match _2 etc..
$postedElements = $_POST;
$elementsPerRow = 4;
$numRows = count($postedElements)/$elementsPerRow;
// loop through the number of 'rows' to insert
for($i=1;$i<=$numRows;$i++){
// Build an array to store matched elements to insert
$elementsToInsert = array();
//process the complete _POST array each time...
foreach($postedElements as $name => $value){
// ...get the 'row' (the bit after the underscore)...
//list($value,$row) = explode('_',$name); // doesn't work for 2 underscores..
$row = end(explode($name)); // This will
if($row == $i){
// ...and add elements that match to an insert array
// $elementsToInsert[] = $name;
$elementsToInsert[$name] = $value;
}
}
// insert $elementsToInsert into DB
}
I think, at first You need to have a amount of records in array through count($arr). Then use usual for loop:
//output of field names
for ($i = 0; $i < count($arr); $i++){
//Work with array
}
Make a new array.
Then add a data from loop into new array.
I think it could help You.
And by which reason You need to sort them?

make serialized form POST to php

my problem is that I'm sending html form data via jquery here's the code :
$('#search_button').click(function(event) {
var track_load = 0; //total loaded record group(s)
var loading = false; //to prevents multipal ajax loads
var total_groups = <?php echo $total_groups; ?>; //total record group(s)
var Data = $("#srch_form").serializeArray();
console.log(Data);
$('#results').load("autoload_process.php", {
data:Data,
'group_no':track_load},
function() {track_load++;}); //load first group
});
and in the php receiving page I'm getting an array with one array per name&value pair inside like this :
array (size=4)
0 =>
array (size=2)
'name' => string 'srch_word' (length=9)
'value' => string '' (length=0)
1 =>
array (size=2)
'name' => string 'srch_cat-2' (length=10)
'value' => string '2' (length=1)
2 =>
array (size=2)
'name' => string 'srch_cat-5' (length=10)
'value' => string '5' (length=1)
3 =>
array (size=2)
'name' => string 'srch_cat-6' (length=10)
'value' => string '6' (length=1)
But before with html only form post I was getting one array with the pairs inside :
array (size=4)
'srch_word' => string '' (length=0)
'srch_cat-2' => string '2' (length=1)
'srch_cat-5' => string '5' (length=1)
'srch_cat-6' => string '6' (length=1)
And I use to access the values like an associative array would do ($_POST['srch_word'], or even just $srch_word would work ), now I can't do that.
¿How can I get one array from the jquery post or how can I reduce this array of arrays to one array in the php file or what should I do in this case?
I would appreciate your help.
this is sample maybe usefull for you as long number of child array always two
<?php
$keys = array();
$value = array();
$serialize = array(array('name' => 'srch_word', 'value' => ''), array('name' => 'srch_cat-2', 'value' => '2'));
foreach ($serialize as $key) {
array_push($keys, $key['name']);
array_push($value, $key['value']);
}
$final = array_combine($keys, $value);
echo '<pre>',print_r($final),'</pre>';
?>
replace $serialize as your array define
please vote if you like it

php json_decode return [object Object]

I have a problem with JSON data in PHP. I need to use data from this JSON in my SQL statement. When I'm trying to debug it with echo(var_dump, or print_r is not working too) command the output with is
{"records":"tekst","name":"[object Object]"}
This is a JSON structre:
{
records: 'tekst',
name: {
imie: 'imie1',
nazwisko: 'nazwisko1'
}
}
I'm trying to decode this by json_decode(), but I have an error
"Warning: json_decode() expects parameter 1 to be string, array
given".
Does anyone know what's wrong?
PHP manual about JSON and the format required: function.json-decode. basically, double quotes only and names must be quoted.
a demonstration of conversion using PHP.
So, you supply the json string that looks like, with the whitespace removed, like this:
{records:[{id:1,name:'n1'},{id:2,name:'n2'}]}
Which is an object containing an array with two entries that could be arrays or objects.
Except, it is not a valid JSON string as it contains single quotes. And PHP wants all the names in double quotes, as in "id":1.
So, possible PHP code to recreate that, assuming arrays as the inner entries is:
$json = new stdClass();
$records = array();
$entry = array('id' => 1, 'name' => 'n1');
$records[] = $entry;
$entry = array('id' => 2, 'name' => 'n2');
$records[] = $entry;
$json->records = $records;
$jsonEncoded = json_encode($json);
Which, when 'dump'ed looks like:
object(stdClass)[1]
public 'records' =>
array
0 =>
array
'id' => int 1
'name' => string 'n1' (length=2)
1 =>
array
'id' => int 2
'name' => string 'n2' (length=2)
Now, the string that structure produces is:
{"records":[{"id":1,"name":"n1"},{"id":2,"name":"n2"}]}
Which looks similar to yours but is not quite the same. Note the names in double quotes.
However, if your json string looked the same then PHP could decode it, as is shown below:
$jsonDecoded = json_decode($jsonEncoded);
var_dump($jsonDecoded, 'decoded');
Output: Note all objects...
object(stdClass)[2]
public 'records' =>
array
0 =>
object(stdClass)[3]
public 'id' => int 1
public 'name' => string 'n1' (length=2)
1 =>
object(stdClass)[4]
public 'id' => int 2
public 'name' => string 'n2' (length=2)
We may want arrays instead so use the true as the second parameter in the 'decode'
$jsonDecoded = json_decode($jsonEncoded, true);
var_dump($jsonDecoded, 'decoded with true switch');
Output: with arrays rather than objects.
array
'records' =>
array
0 =>
array
'id' => int 1
'name' => string 'n1' (length=2)
1 =>
array
'id' => int 2
'name' => string 'n2' (length=2)
string 'decoded with true switch' (length=24)

PHP compare two objects with same properties but different properties values, get non-matching values

I have these two objects contained in a array:
array (size=2)
0 =>
object(stdClass)[20]
public 'name' => string 'John' (length=4)
public 'surname' => string 'D' (length=1)
public 'id_number' => string '924' (length=3)
public 'file' => string '1001' (length=4)
public 'arrival_date' => string '1368466111' (length=10)
1 =>
object(stdClass)[21]
public 'name' => string 'John' (length=4)
public 'surname' => string 'D' (length=1)
public 'id_number' => string '924' (length=3)
public 'file' => string '1002' (length=4)
public 'arrival_date' => string '1368466190' (length=10)
It would be great to come up with 3 arrays or 3 objects like the following:
array('name'=>'John','surname'=>'D','id_number'=>'924') - contains the matching values
array('file'=>'1001','arrival_date'=>'1368466111') - contains the first set of different values
array('file'=>'1002','arrival_date'=>'1368466190') - 2nd set of not matching values
The story behind the code is that upon the arrival of each person, I open a file, and at some point in time, I want each person to list his name and beneath his name and identification (that are identical for each arrival) to list his arrival files, each in a row.
What do you, think? Is there any neat way to accomplish this? What I did so far is a mess - tons of code with poor results.
There are built in functions for that. You do need to cast the objects to an array for this to work though (which is an array of all public properties of the object). Assuming your variable is called $var:
$a1 = (array)$var[0];
$a2 = (array)$var[1];
$inBoth = array_intersect_assoc($a1, $a2);
$onlyInFirst = array_diff_assoc($a1, $a2);
$onlyInSecond = array_diff_assoc($a2, $a1);

Categories