export data in excel from mysql in codeigniter - mysql

I am exporting data from mysql to excel in codeigniter it's working fine on localhost
but it's not working on server .on server it prints only array like this.
Array
(
[0] => Array
(
[id] => 135
[type] => 3
[product_service] =>
[email] => bewcontrols#gmail.com
[first_name] => gurwinder sigh
[last_name] =>
[company_name] =>
[country] =>
[state] =>
[city] =>
[phone_number] => 91-011-09971587701
[mobile] => 09971587701
[fax_number] =>
[address] => f-127 mayapuri indl area phase2 new delhi
[zipcode] =>
[message] => Subject: Fwd to purchase/ maint. for repairing of SOLENOID VALVE and PNEUMATIC cylinders ,DELHI,BHUPINDER ENGG.
Dear Sir,
I'd like to take this opportunity to introduce BEW Pneumatic
Controls,Delhi, to you.
We manufacture Heavy Duty Solenoid Valves in Spool construction
using all-Aluminium fittings.While most manufacturers use plastic
fittings,plastic cages, we at BEW pioneered Aluminium fittings and
have incorporated them across our product line.
Please find attached our latest product catalogue for your perusal and
we look forward to hearing from you soon.
Best Regards,
Gurwinder Singh
Mobile-09811019362,
and 09971587701
[status] => 1
[reply_status] => N
[receive_date] => 2014-11-16 18:22:06
[order_quantity] =>
)
[1] => Array
(
[id] => 134
[type] => 3
[product_service] =>
[email] => mohamed#gmail.com
[first_name] => mohamed
[last_name] =>
[company_name] =>
[country] =>
[state] => 0
[city] =>
[phone_number] =>
[mobile] => 9944556611
[fax_number] =>
[address] =>
[zipcode] =>
[message] => jhkhjh
[status] => 1
[reply_status] => N
[receive_date] => 2014-11-11 15:42:47
[order_quantity] => 20
)

<?php
// output headers so that the file is downloaded rather than displayed
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=filename.csv');
// create a file pointer connected to the output stream
$output = fopen('php://output', 'w');
// output the column headings
//fputcsv($output, array('Column 1', 'Column 2', 'Column 3'));
// fetch the data
mysql_connect('localhost', 'user', 'pass');
mysql_select_db("database");
$rows = mysql_query('SELECT something, something, something FROM table');
// loop over the rows, outputting them
while ($row = mysql_fetch_assoc($rows)) fputcsv($output, $row);
?>

ok its simple in CI
Just download csv library
then
$this->db->select('*')->from('yourtabl')->where(array('your_conditions'));
$query = $this->db->get();
$this->load->helper('csv');
query_to_csv($query, TRUE, 'yourfilename.csv');
and bingo

Related

An Issue with saving SalesOrders data from REST API call to Exact Online

I have been using PHP Client library for Exact Online for a long time.
After saving the customer Accounts, Addresses, Contacts and then filtering out the PaymentConditions based on WooCommerce orders, items are successfully reflecting in the Exact Online dashboard.
But unfortunately calling the SalesOrders post request API. I'm unable to store into the Exact Online dashboard,
even though in order to store only OrderedBy itself is enough which is given in the official documentation
$ordersn = $order->save();
Picqer\Financials\Exact\ApiException : Error 403: Forbidden
$order = new SalesOrder($connection);
$lines = new SalesOrderLine($connection);
....
echo'<pre>'; print_r($order);
$order->SalesOrderLines = $lines;
$ordersn = $order->save();
if ($ordersn->ID)
{
$orderitem['sync_flag'] = true;
}
Here is the details of an order array
Picqer\Financials\Exact\SalesOrder Object
(
...
[attributes:protected] => Array
(
[WarehouseID] => 26ca2016-453f-499a-8a34-c986009bc78d
[OrderID] => FC290B7D-766B-4CBB-B7A2-47327AA3841F
[OrderedBy] => 764a4f6d-4b39-43b4-a86c-265e5478afbd
[DeliverTo] => 764a4f6d-4b39-43b4-a86c-265e5478afbd
[OrderDate] => 2019-02-17T19:29:53
[YourRef] => 75591901YP220320G
[OrderedByName] => Peter Kerner
[Description] => 16031_PayPal/Lastschrift/Kreditkarte
[Remarks] => Order is processing
[PaymentReference] => 16031
[PaymentCondition] =>
[SalesOrderLines] => Picqer\Financials\Exact\SalesOrderLine Object
(
[attributes:protected] => Array
(
[OrderID] => FC290B7D-766B-4CBB-B7A2-47327AA3841F
[VATAmount] => 5,58
[Description] => Goodies Box
[Quantity] => 1,00
[UnitPrice] => 29,37
[Item] => 418d43d6-55fe-410a-8df2-b05cbb72cea5
)
...
)
)
...
)
Do we need to upload VAT code or Am I missing something else data to be resided first shown from the above order-array or what else should we need to call appropriate API. Since in-order to reflect on the Exact Online dashboard. what should we need to follow?
From the built-In function call addItem() below snippets of code:
$soLines = array(
'OrderID' => $lines->OrderID,
'Item' => $lines->Item,
'Description' => $lines->Description,
'Quantity' => $lines->Quantity,
'UnitPrice' => $lines->UnitPrice,
'VATAmount' => $lines->VATAmount,
'VATCode' => $lines->VATCode
);
$order->addItem($soLines);
Generates the results with LineNumber to be included in SalesOrderLines array
[attributes:protected] => Array
(
[WarehouseID] => 26ca2016-453f-499a-8a34-c986009bc78d
[OrderID] => 65F93F56-97A8-4D54-AE37-C0BDDE774E67
[OrderedBy] => 9b048b81-f729-413a-b196-526436f11fe7
[DeliverTo] => 9b048b81-f729-413a-b196-526436f11fe7
[OrderDate] => 2019-02-17T20:45:34
[YourRef] => 9Y9593859V795183K
[OrderedByName] => Katrin Lenk
[Description] => 16033_PayPal Express
[Remarks] => Order is processing
[PaymentReference] => 16033
[PaymentCondition] =>
[SalesOrderLines] => Array
(
[0] => Array
(
[OrderID] => 65F93F56-97A8-4D54-AE37-C0BDDE774E67
[Item] => 5c415369-615c-4953-b28c-c7688f61cfaa
[Description] => ABC Classic
[Quantity] => 2,00
[UnitPrice] => 15,08
[VATAmount] => 5,73
[VATCode] =>
[LineNumber] => 1
)
)
)
Also note I haven't created Journals, GLAccounts, Documents & DocumentAttachments API. Does this actually affects storing of SalesOrders
EDIT:
In much simpler
$salesOrder = new \Picqer\Financials\Exact\SalesOrder($connection);
$salesOrder->WarehouseID = '26ca2016-453f-499a-8a34-c986009bc78d';
$salesOrder->OrderID = '65F93F56-97A8-4D54-AE37-C0BDDE774E67';
$salesOrder->OrderedBy = '9b048b81-f729-413a-b196-526436f11fe7';
$salesOrder->DeliverTo = '9b048b81-f729-413a-b196-526436f11fe7';
$salesOrder->OrderDate = '2019-02-17T20:45:34';
$salesOrder->YourRef = '9Y9593859V795183K';
$salesOrder->OrderedByName = 'Katrin Lenk';
$salesOrder->Description = '16033_PayPal Express';
$salesOrder->Remarks = 'Order is processing';
$salesOrder->PaymentReference = '16033';
$salesOrder->PaymentCondition = 'PP';
$soLines = array(
'Item' => '5c415369-615c-4953-b28c-c7688f61cfaa',
'Description' => 'ABC Classic',
'Quantity' => '1,00',
'UnitPrice' => '29,37',
'OrderID' => '65F93F56-97A8-4D54-AE37-C0BDDE774E67'
);
echo '<pre>'; print_r($soLines);
$salesOrder->addItem($soLines);
echo '<pre>'; print_r($salesOrder);
$salesOrder->save();
Resulting value stored from the soLines array
[attributes:protected] => Array
(
[WarehouseID] => 26ca2016-453f-499a-8a34-c986009bc78d
[OrderID] => 65F93F56-97A8-4D54-AE37-C0BDDE774E67
[OrderedBy] => 9b048b81-f729-413a-b196-526436f11fe7
[DeliverTo] => 9b048b81-f729-413a-b196-526436f11fe7
[OrderDate] => 2019-02-17T20:45:34
[YourRef] => 9Y9593859V795183K
[OrderedByName] => Katrin Lenk
[Description] => 16033_PayPal Express
[Remarks] => Order is processing
[PaymentReference] => 16033
[PaymentCondition] =>
[SalesOrderLines] => Array
(
[0] => Array
(
[OrderID] => 65F93F56-97A8-4D54-AE37-C0BDDE774E67
[Item] => 5c415369-615c-4953-b28c-c7688f61cfaa
[Description] => ABC Classic
[Quantity] => 2,00
[UnitPrice] => 15,08
[VATAmount] => 5,73
[VATCode] =>
[LineNumber] => 1
)
)
)
Actual Result:
Picqer\Financials\Exact\ApiException : Error 403: Forbidden
The reason was OrderID value was included twice in the SalesOrderLine as well as in SalesOrder for the two REST API calls and removing the OrderID entry from SalesOrder worked perfectly and reflecting in the Exact Online Dashboard
You cannot assign $order->SalesOrderLines = $lines; directly resulting in a collection to array error.
The only way to do this was to call via in-built function called addItem() passing array objects into it.

How will i print the result in view in CodeIgniter?

I am trying to get a result in view returned as array in controller.This is a result of an api call using the json.
I need to display the results in the view. There is design integrated in view to display it.
How could i do it in CodeIgniter?
Function call:
$data['nearme_values'] = $this->swsdk->nearMe($lat, $lon, $radius, $page, $size);
This is the var_dumb result of the function call.
stdClass Object
( [content] => Array
( [0] => stdClass Object
( [id] => 1 [name] => Make America Great
[screenShotUrl] => http://52.77.190.221/SW/speaker_wire_images/streamSShot/stream-1479113014667.jpg
[type] => PUBLIC [status] => RECORDED [userId] => 1 [userHandle] => abhilekh
[channelId] => 1 [channelName] => Sports [likes] => 0 [views] => 0
[liveViewers] => 0
)
[1] => stdClass Object
( [id] => 2 [name] => Make America Great
[screenShotUrl] => http://52.77.190.221/SW/speaker_wire_images/streamSShot/stream-1479110563909.jpg
[type] => PUBLIC [status] => RECORDED [userId] => 1 [userHandle] => abhilekh
[channelId] => 2 [channelName] => Politics [likes] => 0 [views] => 0
[liveViewers] => 0
)
[2] => stdClass Object
( [id] => 3 [name] => Make America Great
[screenShotUrl] => http://52.77.190.221/SW/speaker_wire_images/streamSShot/stream-1479109601008.jpg
[type] => PUBLIC [status] => RECORDED [userId] => 2 [userHandle] => sudhanshu
[channelId] => 3 [channelName] => Fun [likes] => 0 [views] => 0
[liveViewers] => 0
)
[3] => stdClass Object
( [id] => 4 [name] => Make America Great
[screenShotUrl] => http://52.77.190.221/SW/speaker_wire_images/profiles/pic-130.jpg
[type] => PUBLIC [status] => RECORDED [userId] => 2 [userHandle] => sudhanshu
[channelId] => 4 [channelName] => Art [likes] => 0 [views] => 0
[liveViewers] => 0
)
) [last] => 1 [totalPages] => 1 [totalElements] => 4
[numberOfElements] => 4 [sort] => [first] => 1
[size] => 10 [number] => 0
)
I am new to the CodeIgniter. Please support with sample code.
if you are gonna call as API, no need to put in view.
Just echo like this in controller's method.
$nearme_values = $this->swsdk->nearMe($lat,$lon,$radius,$page,$size);
echo json_encode($nearme_values);
Displaying in View.
// in Controller.
$data['nearme_values'] = $this->swsdk->nearMe($lat, $lon, $radius, $page, $size);
$this->load->view('ur_view_file', $data);
// in view
foreach ($nearme_values->content as $nearme) {
echo $nearme->id .' / '. $nearme->screenShotUrl .' / '. $nearme->type .'<br>';
}

insert array data to mysql codeigniter 3

How can I insert array data to mysql using code igniter ??
I tried example from CI documentation like this
$data = array(
'id_kls' => 'id_kls',
'fk__id_kls' => 'fk__id_kls',
'id_reg_pd' => 'id_reg_pd',
'nm_pd' => 'nm_pd',
'asal_data' => 'asal_data',
'nilai_angka' => 'nilai_angka',
'nilai_huruf' => 'nilai_huruf',
'nilai_indeks' => 'nilai_indeks',
);
$this->db->insert('master_nilai', $data);
// Executes: REPLACE INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date')
But not working ..
I have array data like this
Array
(
[error_code] => 0
[error_desc] =>
[result] => Array
(
[0] => Array
(
[id_kls] => f77294f7-2a5a-4876-860b-824d227d5b19
[fk__id_kls] => 02
[id_reg_pd] => 001be76b-4e58-4cea-96cf-fee2d8e0abdc
[nm_pd] => SUYATNO
[asal_data] => 9
[nilai_angka] =>
[nilai_huruf] => B
[nilai_indeks] => 3.00
)
Make sure you've enable config/autoload.php/$autoload['libraries'] = array('database');
and config/database.php/your hostname,username,dbname!

Set and fetch bindModel query in cake template file

$categ = $this->FreeadsCategory->bindModel( array( 'hasMany' => array( 'Subcategory' => array('foreignKey' => 'category_id', 'order'=>'id ASC') ) ) );
$data = $this->FreeadsCategory->findById($i);
$this->set("datas", $data);
I am not able to fetch the datas in view page using cakephp
If i give pr($datas); showing nothing in ctp file
If i print the data in controller i am getting the following array structure
Array
(
[FreeadsCategory] => Array
(
[id] => 1
[uuid] => 51512434-e4c4-441b-b90e-16f8732d5573
[category] => Automobiles
[status] => Active
)
[Subcategory] => Array
(
[0] => Array
(
[id] => 1
[uuid] => 4ea15f22-adf0-4020-b35d1-052ff9ff9a27
[category_id] => 1
[subcategory] => Cars/Cabs/Jeeps
[status] => Active
)
[1] => Array
(
[id] => 5
[uuid] => 51cec363-e7ac-4095-a86b-0ccdf260d1b4
[category_id] => 1
[subcategory] => Buses/Lorries
[status] => Active
)
)
You don't fetch data in views, that violates the MVC pattern. Technically there are ways to do it but it's plain wrong, you'll end up with unmaintanable garbage code.
I really recommend you to get started by reading about the MVC design pattern and to do the CakePHP blog tutorial first to get a minimum of understanding of how CakePHP works.

Retrieving a Movie's Showtimes grouped by Theater: Movie->Showtimes<-Theater in CakePHP

When you view a movie, I want to show the showtimes (movie_times table) organized by Theater. I can get all the data just fine, but it's returning the Theater data with EVERY MovieTime like this:
[0] =>
[Movie] =>
[name] => Herp de Derp
[MovieTime] =>
[0] =>
[datetime] => 2011-07-06 18:30:00
[Theater] =>
[name] => Awesome Cinemas
[address] => 1234 Street Ln
[1] =>
[datetime] => 2011-07-06 20:30:00
[Theater] =>
[name] => Awesome Cinemas
[address] => 1234 Street Ln
[2] =>
[datetime] => 2011-07-06 20:30:00
[Theater] =>
[name] => Crappy Movies 10
[address] => 678 Avenue St
As you can imagine, there is a TON of duplication with the theater data.
Is there a way I can get this same data, but instead, get the Theater data grouped with the movie_times within it like this?:
[0] =>
[Movie] =>
[name] => Herp de Derp
[Theater] =>
[0] =>
[name] => Awesome Cinemas
[address] => 1234 Street Ln
[MovieTime] =>
[0] =>
[datetime] => 2011-07-06 18:30:00
[1] =>
[datetime] => 2011-07-06 20:30:00
[2] =>
[datetime] => 2011-07-06 22:30:00
[1] =>
[name] => Awesome Cinemas
[address] => 1234 Street Ln
My associations are the following:
Movie hasMany MovieTime
MovieTime belongsTo Movie
Theater hasMany MovieTime
MovieTime belongsTo Theater
As you can see, there's no direct link between Movie and Theater - it's using the MovieTime as a go-between. The associations make logical sense (to me at least) - but if I have to change them to a different (but also logical) setup, I'm certainly willing to hear it.
I'm using this to get my data currently:
$data = $this->find("first", array(
'conditions' => array(
array('Movie.slug' => $slug)
),
'fields' => array('id', 'name', 'slug', 'url', 'runtime'),
'contain' => array(
'MovieTime' => array(
'fields' => array('datetime', 'screen_number'),
'Theater' => array(
'fields' => array('id', 'slug', 'name', 'address', 'phone'),
'City' => array(
'fields' => array('id', 'name', 'st')
),
),
),
),
)
);
Try the GROUP BY. You just need to add 'group' => 'Put here de Model.Id' into your find.
Try this:
$data = $this->find("first", array(
'conditions' => array(
array('Movie.slug' => $slug)
),
'fields' => array('id', 'name', 'slug', 'url', 'runtime'),
'contain' => array(
'MovieTime' => array(
'fields' => array('datetime', 'screen_number'),
'group' => 'Theater.id',
'Theater' => array(
'fields' => array('id', 'slug', 'name', 'address', 'phone'),
'City' => array(
'fields' => array('id', 'name', 'st')
),
),
),
),
),
);
For more information, check this two links:
http://debuggable.com/posts/how-to-group-by-in-cakephps-new-release-part-2:4850d2c9-edbc-4f0e-9e0e-07d64834cda3
http://nuts-and-bolts-of-cakephp.com/2008/06/03/new-way-to-group-by-in-cakephp