Database class object - php

I've seen people use this piece of code and I'm trying to understand what it does because I don't see it in any of the codeigniter documenation or in the source code for the database class.
$this->db->ar_orderby

This is an array that holds order by columns..
There should be no reason to use this property directly. Instead call $this->db->order_by('column') which appends to the array automatically.
defined in system/database/DB_active_rec.php Line 42
appended to in method CI_DB_active_record::order_by Line 856
used to generate SQL in CI_DB_active_record::_compile_select Line 1781

You mean
http://phpxref.com/xref/codeigniter/system/drivers/DB_active_record.php.source.html#l781
It allows you to specify the order by.

It still makes sense to use this in the way below -
if (!count($this->db->ar_orderby)) {
$this->db->order_by($this->order_by );
}
It basically means - If an order is not already set by db library when this method was called, use this. Prevents calling of "order by" twice.

You can replace these lines:
if (!count($this->db->ar_orderby)) {
$this->db->order_by($this->order_by );
}
with:
if(!count($this->db->order_by($this->order_by))) {
$this->db->order_by($this->order_by);
}

Related

Number of rows in getItems

I was trying to get all the items from a list using the phpSPO library: https://github.com/vgrem/phpSPO
However in the $remoteList properties in the array of Data there are only 100 rows, and the list has more than 100 items. I saw that it's a common problem, however I don't know which solution I should consider for this library.
https://sharepoint.stackexchange.com/questions/74777/list-api-get-all-items-limited-to-100-rows
$remoteList = $web->getLists()->getByTitle(xxx)
$listData = $remoteList->getItems();
There's a public property for the CamlQuery class called ListItemCollectionPosition defined in this file:
https://github.com/vgrem/phpSPO/blob/b05be0eba6902dabd18784576bb6e1d55426fa99/src/SharePoint/CamlQuery.php
That expects a ListItemCollectionPosition object as a value: https://github.com/vgrem/phpSPO/blob/b05be0eba6902dabd18784576bb6e1d55426fa99/src/SharePoint/ListItemCollectionPosition.php
So I think your code will need to look something like:
$remoteList = $web->getLists()->getByTitle(xxx);
$listItemCollectionPosition = new new ListItemCollectionPosition(); // Either add a use statement for this or use the fully qualified class name here
// ... (set the values according to the items/page you require)
$remoteList->ListItemCollectionPosition = $listItemCollectionPosition;
$listData = $remoteList->getItems();
In terms of how to use it exactly, have a read of the information here:
https://learn.microsoft.com/en-us/previous-versions/office/developer/sharepoint-2010/ee534956(v%3Doffice.14)#retrieving-items-using-list-item-collection-position
The example is in C# but it's not too hard to follow.
It looks as though you set the ListItemCollectionPosition on your query to get subsequent "pages". It also looks as though responses include a ListItemCollectionPosition property themselves that you can then use as a starting point for the next request.

Need to access associative array data created inside a class by json_decode from outside the class

I've been using procedural php for a long time but am still learning OOP. I was able to find a bit of code online that is in the form of a class and I am able to call a new instance of the class and use var_dump on a string to have it print JSON data to a web page. I can look at the results of the var_dump and see that it's returning exactly what I want. I'm then able to use json_decode on the same string to turn it into and associative array and then I can echo the values from within the class. The problem is, I need to use the array values in more code - it's great that I can confirm it by printing it to a web page but I need to use it... but I'm getting errors that state the array is undefined once I try to access it outside of the class.
I'm using this line to convert the data into an array:
$response_array = json_decode($body, true);
I've already confirmed that this works within the class by using this code to print some of the data:
echo $response_array['amount'];
and it works - I see it on the web page.
I've been using this code to create the new instance of the class:
$fdata = new FData();
$fdata->request($order_total, $cc_exp, $cc_number, $cc_name, $order_id, $customer_id);
(the function named 'request' is defined as a public function inside the class)
After that, I want to grab the $response_array so that I can store the returned data into a transactions table, i.e something like this:
$r = mysqli_query($dbc, "CALL add_transaction($order_id, $response_array['transaction_type'], $response_array['amount'], $response_array['exact_resp_code'], $response_array['exact_message'], $response_array['bank_resp_code'], $response_array['bank_message'], $response_array['sequence_no'], $response_array['retrieval_ref_no'], $response_array['transaction_tag'], $response_array['authorization_num'])");
but I keep getting an error saying that the array values are undefined.
Things I have already tried (and which failed) include:
Defining the variables as public inside the class, setting their value in the class, and then calling them outside the class...
public $amount = $response_array['amount'];
then using $amount in my procedure CALL --- I still get the error saying that $amount is undefined.
Using 'return', as in
return $response_array;
and still the error saying the values are undefined.
I tried embedding the entire rest of my code within the class, just copy/paste it in right after the json_decode... but for some reason it can't seem to then make the database calls and other things it needs to do.
I've been reading about __construct but I'm not sure if it applies or how to use it in this case...
I want to stress that I AM able to see the results I want when I use var_dump and echo from within the class.. I need to access the array created by json_decode OUTSIDE of the instance of the class.
Any advice would be greatly appreciated. Thanks.
Assuming your FData::request method ends with something like this...
$response_array = json_decode($body, true);
return $response_array;
and you call it like this...
$response_array = $fdata->request(...);
You should then be able to use $response_array in the calling scope.
Extra note; you should be using prepared statements with parameter binding instead of injecting values directly into your SQL statements.

Can a PHP function dynamically know its name?

I'm trying to get a list of functions that have already run in the context of an operation. And that's where the need arised.
Is there a way to get something like this work?
function funcX()
{
echo this.nameOrSomethingHere; //outputs funcX
}
As this example demonstrates, it is possible to achieve this functionality in Javascript.
I can always go for an alternative solution for taking care of my original need like with an alternative way around as follows;
function funcX()
{
$this_function_name = "funcX";
Add_this_to_the_already_executed_functions_list($this_function_name);
}
And here, the Add_this_to_the_already_executed_functions_list functions job is to take the passed string and add it to a globally defined array so at the end of the shutdown process you can get a view of all the functions that have been run in the last page.
The above method would work, but, obviously, it's not elegant cause it's not dynamic.
It would have been nice to be able to do something like this
function funcX()
{
Add_this_to_the_already_executed_functions_list(this.????);
}
The question is if there is a way to do this in PHP?
You can use __FUNCTION__ to get the current function's name.
http://us2.php.net/manual/en/function.debug-backtrace.php
debug_backtrace will tell you that and a lot more. best for debugging, but you could use it for whatever you wanted.
Use __FUNCTION__
See 'magic constants'

PHP DB access syntax: "$this->db->select..." How do I use "get()"?

I am editing somebody else's code and I am a PHP beginner.
I want to access the contents of a column called "email" in the database "tga_purchase_items" where the "id" of the row is "14". I want to save the output to a variable "$sp_email".
The code I have is thus:
$sp_email = $this->db->select("email")->from("tga_purchase_items")->where("id", 14)->get();
The variable is coming out empty although the database field is definitely populated.
What am I doing wrong? I am not used to this "->" syntax at all.
The syntax used in your database leans towards Codeigniter and active record.
If this is the case, the following code will retrieve the email column.
$result_set = $this->db->select("email")->from("tga_purchase_items")->where("id", 14)->get();
$result_object = $result_set->row();
$sp_email = $result_object->email;
What you're missing is retrieving the data from the result set which you get. The get() functions return a result set which is similar to the native MySQLi Result class. This means that you need to retrieve the data you want from this set. There are various functions available in CodeIgniter to do this, namely row(), row_array(), result() and result_array() which you can read about on their manual page.
I haven't done too much with CodeIgniter and the Active Record (I prefer Zend Framework) but I think something like this would do the trick:
$this->db->select('email');
$sp_email = $this->db->get_where('tga_purchase_items', array('id' => '14'));
Look at this for a more detailed explanation: http://codeigniter.com/user_guide/database/active_record.html

foreach $key=>$value to set ZF form values / OOP PHP

I currently have the following code in a ZF application, to set the values of a Form based on a rowset retrieved from a Db-Table.
$form->forename->setValue($footerContactDetails->forename);
$form->surname->setValue($footerContactDetails->surname);
$form->telephone->setValue($footerContactDetails->telephone);
$form->mobile->setValue($footerContactDetails->mobile);
$form->fax->setValue($footerContactDetails->fax);
$form->email->setValue($footerContactDetails->emailAddress);
$form->address1->setValue($footerContactDetails->address1);
$form->address2->setValue($footerContactDetails->address2);
$form->address3->setValue($footerContactDetails->address3);
$form->townCity->setValue($footerContactDetails->townCity);
$form->region->setValue($footerContactDetails->region);
As the object element names from the rowset match the form elements, what I'd like to do is the following:
foreach ($footerContactDetails as $key=>$value) {
$form->$key->setValue($value);
}
However this provides the following error message:
Fatal error: Call to a member function setValue() on a non-object
I expect this question is more related to OOP PHP in general and not the just ZF.
So how can I set these form values using a foreach?
Many thanks
The foreach is fine, but the names dont match:
$form->email->setValue($footerContactDetails->emailAddress);
So, when iterating over $footerContactDetails, your code will eventually try to
$form->emailAddress->setValue()
but since there is no element of that name in the Zend_Form (it's called email instead), you'll get the error. So change it accordingly and it will work.
On a sidenote, Zend_Form has a method for bulk setting:
$form->populate((array) $footerContactDetails);
It expects an array though, hence the typecast.
Try this:
foreach($footerContactDetails as $key=>$value){
$form->{$key}->setValue($value);
}
Foreach over object variables takes all variables. That means primitive types too. Since primitive type is not an object, you cannot call method on it.
I suggest you use is_object() to check if variable is an object before calling a method, or maybe even think of a different approach.
Did you try something like this?
foreach ($footerContactDetails as $key=>$value) {
$form->${$key}->setValue($value);
}

Categories