using Foreach to make a table - foreach

Im new to PHP and I made this to display to my website what has been uploaded in the fdpp portal
<?php $resp = file_get_contents("http://fdpp.blgs.gov.ph/api/documents?source=datatable&sSearch=kalinga");
$clean = json_decode($resp);
print_r($clean); ?>
this is the result :
stdClass Object
(
[iTotalRecords] => 130035
[iTotalDisplayRecords] => 879
[sEcho] => 0
[aaData] => Array
(
[0] => stdClass Object
(
[lgu] => <a href=' http://fdpp.blgs.gov.ph/documents/view/129293'>CAR<br/>Kalinga<br />Balbalan</a>
[document] => <a href=' http://fdpp.blgs.gov.ph/documents/view/129293'>Local Disaster Risk Reduction and Management Fund Utilization (LDRRMF)</a>
[period] => Quarter 1 2014
[status] => Required • SUBMITTED
[desc] => The atng.
)
[1] => stdClass Object
(
[lgu] => <a href=' http://fdpp.blgs.gov.ph/documents/view/129188'>CAR<br/>Kalinga<br />Balbalan</a>
[document] => <a href=' http://fdpp.blgs.gov.ph/documents/view/129188'>Manpower Complement</a>
[period] => Quarter 1 2014
[status] => Required • SUBMITTED
[desc] => The file ag.
)
what shall I add to my code to put this in a table with column name lgu, document, period? I tried reading foreach manual but I cant figure it out can someone help me?

I have to agree with PeeHaa (+1) with not enough information given, but I'm going to attempt to help you out.
The following assumptions were made:
You have a local MySQL DB named "testdb"
You have the PDO extension installed
In "testdb", you have a table named "test_table" with 4
columns (id, lgu, document, period)
First, you need to set the second parameter in json_decode to true to return an associative array rather than an object (PHP: json_decode).
So, based upon the limited information, here is a working version:
<?php
$resp = file_get_contents("http://fdpp.blgs.gov.ph/api/documents?source=datatable&sSearch=kalinga");
$clean = json_decode($resp,true);
// Open connection to your MySQL DB
$db = new PDO('mysql:host=localhost;dbname=testdb', 'username', 'password');
// Parse the now-associative array and insert into table
foreach($clean['aaData'] as $doc){
$stmt = $db->prepare("INSERT INTO test_table(lgu,document,period) VALUES(:lgu,:document,:period)");
$stmt->execute(array(':lgu' => $doc['lgu'], ':document' => $doc['document'], ':period' => $doc['period']));
}
?>
You should really provide some more information for a more exact answer.
But, this should set you in the right direction.

Related

How change BO product list to also show wholesale price Prestashop 1.6

Someone who has made this change?
Today the BO product list show the end customer price excl and incl vat.
I want if possible to show our wholesale price and mour end customer price excl vat instead. This is probably a change in AdminProductController but I dont know how. I run PS 1.6.1.6
Regards
Anders Yuran
Open /controllers/admin/AdminProductsController.php from your PrestaShop root directory and in the __construct() function, you will see the following code:
$this->fields_list['price'] = array(
'title' => $this->l('Base price'),
'type' => 'price',
'align' => 'text-right',
'filter_key' => 'a!price'
);
This code is responsible for showing the price in products list, try replacing the code with the following:
$this->fields_list['price'] = array(
'title' => $this->l('WholeSale price'),
'type' => 'price',
'align' => 'text-right',
'filter_key' => 'a!wholesale_price'
);
Hope it helps.

Foreach syntax on smarty 1.6

I use Smarty v.1.6,
in php :
$groups = $user_groups['data'];
$user_groups['data'] derived from $user_groups = $facebook->api('/'.$fbuser.'/groups');
(I retrieve user facebook group data and it returns an array). Then I assign $groups in tpl file :
$template->assign("userGroupsData", $groups);
I want to print only the name of each group, so in tpl :
{foreach from=$userGroupsData item=member}
{$member->name}
{/foreach}
but the result shows nothing. {$member->name|#print_r} will produce :
11111111111 (as the number of my group)
What's wrong with my code? Please help..
Here is the array result of {$member|print_r} :
Array (
[name] => beasiswa dirmawa ugm
[unread] => 25
[bookmark_order] => 999999999
[id] => 148164981898119 ) 1
Array (
[name] => Diskusi Jurusan IKE
[unread] => 1
[bookmark_order] => 999999999
[id] => 131266600402713 ) 1
Array (
[name] => ILMU KOMPUTER - UGM
[unread] => 7
[bookmark_order] => 9
[id] => 392009304179631 ) 1
Array (
[name] => Facebook Developers
[unread] => 25
[bookmark_order] => 999999999
[id] => 146797922030397 ) 1
...etc
Any help would greatly appreciated, thank you.
If it's an array you should use the period symbol (in smarty 2 and up, at least):
{$member.name}
http://www.smarty.net/docsv2/en/language.variables.tpl#language.variables.assoc.arrays
but it may be different in the version you're using

Displaying choice based on foreign key / join

I have folowing schema:
Template
UserTemplate
template_id
Costs
template_id
amount
value
What I'm trying to do, is to create a sfWidgetFormDoctrineChoice that displays the shipping costs based on a UserTemplate id.
$this->widgetSchema['cost'] = new sfWidgetFormDoctrineChoice(array(
'model' => 'Costs',
'key_method' => 'getValue',
'method' => 'getAmount',
'add_empty' => 'Please Select Amount',
'expanded' => false,
'multiple' => false
));
This displays all of the Costs.
Ideally, I'd like it to limit it to the UserTemplate.
I have looked at creating a custom query and passing that into the widget, but I'm not sure if this would be the correct way of doing this
So If I have a bunch of costs assigned to the template id of 12 and the user template references 12, when I'm on example.com/user-template/12 - I'd expect to see the costs for this in my form widget.
Creating a custom query and passing it to the widget is exaclty what you're looking for. You will have to build the query depending on the template_id you use in the URL.

how do I generate po file from php array?

I've a php language file, that lists values in an array.
I want to transform this into .po file.
php file looks like this:
<?php
$LANG_ = array(
// advanced search buttons
"_search1" => "Start Search",
"_search2" => "Hide Search Box",
"_search3" => "Search",
// page numbering
"_pn1" => "Page %CURRENT_PAGE% of %TOTAL_PAGES%",
"_pn2" => "<< First",
"_pn3" => "Last >>",
// _tpl_article-add.php
"_tpl_article-add1" => "Article Submission Form",
"_tpl_article-add2" => "Submit Article",
"_tpl_article-add3" => "Article Submitted Successfully",
// errors
"_err14" => "Delete",
"_err144" => "Display Image",
"_err15" => "Edit",
"_err16" => "View",
);
?>
This is just an example, file itself is huge, over 3000 lines. It would kill me to insert every single one of these into a po catalog manually. Is there something that can automate this for me?
I'm using poedit.
Thanks, I'm new to this, so any insight will be useful...
Try php2po which will convert a PHP array into PO files and will also make sure that all of your escaping is correct. The PO file can then be edited in a tool like Poedit or Virtaal.
The first entry is the file header, look into an existing gettext file (-po) what is needed. The escaping I did with addslashes; maybe you need to do more.
$fh = fopen("en.po", 'w');
fwrite($fh, "#\n");
fwrite($fh, "msgid \"\"\n");
fwrite($fh, "msgstr \"\"\n");
foreach ($LANG_ as $key => $value) {
$key = addslashes($key);
$value = addslashes($value);
fwrite($fh, "\n");
fwrite($fh, "msgid \"$key\"\n");
fwrite($fh, "msgstr \"$value\"\n");
}
fclose($fh);

In nuSOAP, how to return simple data type having multiple occurance as response?

Suppose my response message can be like this
<Response>
<ResponseCode>false</ResponseCode>
<ResponseMessage>reason 1</ResponseMessage>
<ResponseMessage>reason 2</ResponseMessage>
<ResponseMessage>reason 3</ResponseMessage>
</Response>
this (xsd:string) item is having multiple occurance.
How to add and configure this kind of rsponse message in nuSOAP server?
Thanks in advance :)
I found this example on another forum. It helped me solving a similar issue with multiple elements:
<inventory>
<car>
<make>Nissan</make>
<model>Maxima</model>
<year>2005</year>
<quantity>3</quantity>
</car>
<car>
<make>Nissan</make>
<model>Maxima</model>
<year>2006</year>
<quantity>1</quantity>
</car>
</inventory>
NuSOAP takes the approach that "car", which is repeated, is an array, so
the "car" element in the associative array points to a simple array:
$car[] = array('make' => 'Nissan', 'model' => 'Maxima', 'year' => 2005,
'quantity' => 3);
$car[] = array('make' => 'Nissan', 'model' => 'Maxima', 'year' => 2006,
'quantity' => 1);
$inventory = array('car' => $car);

Resources