Dynamic query retrieval - php

I am using NOTORM to generate queries, and there is one case on the application where the user can select fields, conditions, etc and thus basically come up with custom queries. That way the user can generate a report like 'people with first name = Jack that have spent > 800'.
I am using NOTORM to generate this query, but I'd like to store the query for usage by another part of the application that is not using NOTORM. Is there any function to retrieve the query generated?
This is how the NOTORM object looks after the queries were generated and executed:
object(NotORM_Result)#55 (33) {
["single":protected]=>
bool(false)
["select":protected]=>
array(0) {
}
["conditions":protected]=>
array(7) {
[0]=>
string(9) "name = ? "
[1]=>
string(12) "surname = ? "
[2]=>
string(15) "custom_field_19"
[3]=>
string(31) "custom_field_20 BETWEEN ? AND ?"
[4]=>
string(21) "custom_field_18 <= ? "
[5]=>
string(15) "custom_field_16"
[6]=>
string(10) "email = ? "
}
["where":protected]=>
array(7) {
[0]=>
string(9) "name = ? "
[1]=>
string(12) "surname = ? "
[2]=>
string(52) "custom_field_19 IN ('Yes', 'Sent lender info', 'No')"
[3]=>
string(31) "custom_field_20 BETWEEN ? AND ?"
[4]=>
string(21) "custom_field_18 <= ? "
[5]=>
string(28) "custom_field_16 IN ('Buyer')"
[6]=>
string(10) "email = ? "
}
["parameters":protected]=>
array(6) {
[0]=>
string(1) "a"
[1]=>
string(6) "Franco"
[2]=>
int(1218146400)
[3]=>
int(1249682400)
[4]=>
string(3) "800"
[5]=>
string(1) "?"
}
["order":protected]=>
array(0) {
}
["limit":protected]=>
NULL
["offset":protected]=>
NULL
["group":protected]=>
string(0) ""
["having":protected]=>
string(0) ""
["lock":protected]=>
NULL
["union":protected]=>
array(0) {
}
["unionOrder":protected]=>
array(0) {
}
["unionLimit":protected]=>
NULL
["unionOffset":protected]=>
NULL
["data":protected]=>
NULL
["referencing":protected]=>
array(0) {
}
["aggregation":protected]=>
array(0) {
}
["accessed":protected]=>
NULL
["access":protected]=>
NULL
["keys":protected]=>
array(0) {
}
["connection":protected]=>
NULL
["driver":protected]=>
NULL
["structure":protected]=>
NULL
["cache":protected]=>
NULL
["notORM":protected]=>
object(NotORM)#3 (12) {
["connection":protected]=>
object(PDO)#2 (0) {
}
["driver":protected]=>
string(5) "mysql"
["structure":protected]=>
object(NotORM_Structure_Convention)#4 (4) {
["primary":protected]=>
string(2) "id"
["foreign":protected]=>
string(5) "%s_id"
["table":protected]=>
string(2) "%s"
["prefix":protected]=>
string(0) ""
}
["cache":protected]=>
NULL
["notORM":protected]=>
NULL
["table":protected]=>
NULL
["primary":protected]=>
NULL
["rows":protected]=>
NULL
["referenced":protected]=>
array(0) {
}
["debug":protected]=>
bool(false)
["freeze":protected]=>
bool(false)
["rowClass":protected]=>
string(10) "NotORM_Row"
}
["table":protected]=>
string(5) "users"
["primary":protected]=>
string(2) "id"
["rows":protected]=>
NULL
["referenced":protected]=>
array(0) {
}
["debug":protected]=>
bool(false)
["freeze":protected]=>
bool(false)
["rowClass":protected]=>
string(10) "NotORM_Row"
}
I could do some kind of script that iterates over 'where' and 'parameters' but seems not too elegant, especially since the order of those arrays is not correct (I would have to do something like replace each "?" with the next item in "parameters")... Any NOTORM functions? Or any more elegant way that you suggest?
This is the 'where' and 'parameters' part in case that you have something elegant for the last option:
["where":protected]=>
array(7) {
[0]=>
string(9) "name = ? "
[1]=>
string(12) "surname = ? "
[2]=>
string(52) "custom_field_19 IN ('Yes', 'Sent lender info', 'No')"
[3]=>
string(31) "custom_field_20 BETWEEN ? AND ?"
[4]=>
string(21) "custom_field_18 <= ? "
[5]=>
string(28) "custom_field_16 IN ('Buyer')"
[6]=>
string(10) "email = ? "
}
["parameters":protected]=>
array(6) {
[0]=>
string(1) "a"
[1]=>
string(6) "Franco"
[2]=>
int(1218146400)
[3]=>
int(1249682400)
[4]=>
string(3) "800"
[5]=>
string(1) "?"
}

Just convert the NotORM_Result object to an string
$sql = (string)$result;
//or
$sql = $result->__toString();

Related

php foreach with additional condition

I Have a Json output like
{ ["id"]=> int(1) ["name"]=> string(7) "SH-Mini" ["currency"]=> int(1) ["monthly"]=> string(4) "4.99" } [79]=> object(stdClass)#301 (4)
{ ["id"]=> int(1) ["name"]=> string(7) "SH-Mini" ["currency"]=> int(2) ["monthly"]=> string(4) "345.23" } [80]=> object(stdClass)#300 (4)
{ ["id"]=> int(1) ["name"]=> string(7) "SH-Mini" ["currency"]=> int(6) ["monthly"]=> string(5) "4.01" } [81]=> object(stdClass)#299 (4)
{ ["id"]=> int(19) ["name"]=> string(8) "SH-Basic" ["currency"]=> int(1) ["monthly"]=> string(4) "5.99" } [82]=> object(stdClass)#298 (4)
{ ["id"]=> int(19) ["name"]=> string(8) "SH-Basic" ["currency"]=> int(2) ["monthly"]=> string(6) "443.44" } [83]=> object(stdClass)#297 (4)
{ ["id"]=> int(19) ["name"]=> string(8) "SH-Basic" ["currency"]=> int(6) ["monthly"]=> string(4) "5.03" } [84]=> object(stdClass)#296 (4)
I want to get the value with id as 19 and currency as 2. The output I require is 443.44
I was trying using PHP foreach for the above json decoded output. It always gives me the first value of 5.99
I am using the below code.
$pid = 19;
$cur = 2;
foreach($products as $product){
$getproductrrice[$product->id] = $product->monthly;
}
return $getproductrrice[$pid];
I need to add one more condition so that I can pass currency and get the output as 443.44.
AS you are looping, test each product to see if it is one you want
$pid = 19;
$cur = 2;
foreach($products as $product){
if ( $product->id == $pid && $product->currency == $cur ) {
$getproductrrice[$product->id] = $product->monthly;
}
}

Concrete5 - PHP: Cannot get desired sort order from array using usort

So I'm trying to alphabetically order my article titles in concrete5, however, they always show the default order. I've attempted different methods of sorting before finding this documentation but it still doesn't seem to work. I'm newer to the concept of sorting something like this.
If someone could clarify how usort works --I was a bit confused by the php documentation I read. How is it making a comparison between $a and $b? I imagine this might not even be the way to go since I want its sort order to be alphabetical.
Any wisdom would be greatly appreciated.
Here is the code I'm working with:
<?php
function orderNode($a, $b) {
return strcmp($a->value, $b->value);
}
usort($artCategoryOptions, "orderNode");
?>
<?php if($urlparam == '?category=Articles' || strpos($urlparam, '?article_category') !== false) {?>
<h3>ARTICLE CATEGORIES</h3>
<ul class="nav nav-pills nav-stacked space-bottom">
<?php foreach ($artCategoryOptions as $cat) { ?>
<li class="<?php if ($artCategoriesFilter == $cat->value) echo 'active'; ?>">
<a href="/newsroom/?article_category=<?php echo $cat->value ?>">
<strong><?php echo $cat->value ?></strong>
</a>
</li>
<?php } ?>
</ul>
<?php } ?>
The vardump of $artCategoryOptions --which is what I'm trying to sort is:
object(SelectAttributeTypeOptionList)#327 (2)
{ ["options":"Concrete5_Model_SelectAttributeTypeOptionList":private]=> array(7)
{ [0]=> object(SelectAttributeTypeOption)#328 (6)
{
["error"]=> string(0) ""
["ID"]=> string(2) "49"
["value"]=> string(10) "Employment"
["th"]=> object(TextHelper)#23 (0) { }
["displayOrder"]=> int(0)
"usageCount"]=> string(1) "6"
}
[1]=> object(SelectAttributeTypeOption)#329 (6)
{
["error"]=> string(0) ""
["ID"]=> string(2) "52"
["value"]=> string(18) "Products Liability"
["th"]=> object(TextHelper)#23 (0) { }
["displayOrder"]=> int(1)
["usageCount"]=> string(1) "3"
}
[2]=> object(SelectAttributeTypeOption)#330 (6)
{
["error"]=> string(0) ""
["ID"]=> string(2) "50"
["value"]=> string(10) "Litigation"
["th"]=> object(TextHelper)#23 (0) { }
["displayOrder"]=> int(2)
["usageCount"]=> string(1) "3"
}
[3]=> object(SelectAttributeTypeOption)#331 (6)
{
["error"]=> string(0) ""
["ID"]=> string(2) "48"
["value"]=> string(12) "Construction"
["th"]=> object(TextHelper)#23 (0) { }
["displayOrder"]=> int(3)
["usageCount"]=> string(1) "2"
}
[4]=> object(SelectAttributeTypeOption)#332 (6)
{
["error"]=> string(0) "" ["ID"]=> string(2) "53"
["value"]=> string(11) "Health Care"
["th"]=> object(TextHelper)#23 (0) { }
["displayOrder"]=> int(4)
["usageCount"]=> string(1) "2"
}
[5]=> object(SelectAttributeTypeOption)#333 (6)
{
["error"]=> string(0) ""
["ID"]=> string(2) "54"
["value"]=> string(11) "Real Estate"
["th"]=> object(TextHelper)#23 (0) { }
["displayOrder"]=> int(5)
["usageCount"]=> string(1) "1"
}
[6]=> object(SelectAttributeTypeOption)#334 (6)
{
["error"]=> string(0) ""
["ID"]=> string(2) "55"
["value"]=> string(17) "Creditor's Rights"
["th"]=> object(TextHelper)#23 (0) { }
["displayOrder"]=> int(6)
["usageCount"]=> string(1) "1" }
}
["error"]=> string(0) ""
}
I believe if you go into the Dashboard and change the "Option Order" for the attribute to "Alphabetical" it would return them in the order you want without you having to re-sort them.

Unshift the array on condition

I have written below lines of code
public function shiftarray($cursor, $arg)
{
$keyarguments = array("first_name","roll_no");
$arrayStudents =array();
foreach ($cursor as $k => $row)
{
foreach ($keyarguments as $key)
{
if (strcasecmp($row[$key], $arg) == 0)
{
array_unshift($arrayStudents, $row);
}
else
{
}
}
}
return $arrayStudents;
}
I have array of students in $cursor like
{ [0]=> array(50) { ["_id"]=> object(MongoId)#23 (1) { ["$id"]=> string(24) "58131c7799fbad4c1d000202" } ["student_id"]=> float(2) ["registration_temp_perm_no"]=> string(1) "1" ["roll_no"]=> float(1) ["admission_date"]=> string(10) "01/07/2016" ["first_name"]=> string(7) "Neil" ["middle_name"]=> string(4) "David" ["last_name"]=> string(6) "Stephan" ["dob"]=> string(10) "12/03/1981" ["gender"]=> string(6) "Female" ["blood_group"]=> string(2) "A+" ["birth_place"]=> string(11) "Sadar Bazar" ["nationality"]=> string(6) "Indian" ["language"]=> string(7) "English" ["religion"]=> string(8) "Agnostic" ["address_line1"]=> string(20) "4148 Hazelcrest Hill" ["address_line2"]=> string(20) "22883 Memorial Place" ["city"]=> string(11) "Sadar Bazar" ["state"]=> string(13) "Uttar Pradesh" ["pincode"]=> string(6) "190010" ["country"]=> string(5) "India" ["phone1"]=> string(10) "9039180419" ["phone2"]=> string(10) "7681559402" ["email"]=> string(24) "educianstudent#gmail.com" ["is_sms_enabled"]=> string(3) "Yes" ["is_active"]=> int(1) ["has_finished"]=> int(0) ["student_category"]=> string(1) "5" ["course"]=> string(24) "58131c7099fbad4c1d0001c2" ["Biometric_ID"]=> string(1) "1" ["siblings"]=> string(14) "Cynthia Taylor" ["guardian_name"]=> string(14) "Cynthia Taylor" ["guardian_occupation"]=> string(13) "Senior Editor" ["guardian_qualification"]=> string(20) "Research Assistant I" ["guardian_email_id"]=> string(23) "educianparent#gmail.com" ["gaurdain_contact_details"]=> string(10) "9419513603" ["guardian_relationship"]=> string(6) "Father" ["height"]=> string(3) "4.9" ["weight"]=> string(4) "34.9" ["allergies"]=> string(0) "" ["batch"]=> int(2) ["academicyear"]=> string(4) "2015" ["batchhistory"]=> array(1) { [0]=> array(5) { ["batchid"]=> float(2) ["academic_year"]=> string(4) "2015" ["course"]=> string(24) "58131c7099fbad4c1d0001c2" ["sequenceno"]=> int(1) ["courseId"]=> object(MongoId)#24 (1) { ["$id"]=> string(24) "58131c7099fbad4c1d0001c2" } } } ["uploads"]=> array(1) { ["profile_pic"]=> string(39) "58131c7799fbad4c1d000202schoolgirl2.jpg" } ["created_at"]=> NULL ["updated_at"]=> string(0) "" ["routearray"]=> array(2) { [0]=> array(5) { ["routeid"]=> int(2) ["academicyear"]=> string(4) "2016" ["current"]=> int(0) ["vehicleno"]=> string(9) "JK01S8764" ["dateofassignment"]=> string(10) "09/28/2016" } [1]=> array(5) { ["routeid"]=> int(3) ["academicyear"]=> string(4) "2016" ["current"]=> int(1) ["vehicleno"]=> string(9) "JK01S8764" ["dateofassignment"]=> string(10) "11/17/2016" } } ["HostelAlloted"]=> array(7) { ["Food Preferences"]=> string(4) "Both" ["Hostel"]=> object(MongoId)#25 (1) { ["$id"]=> string(24) "58138aee99fbade41e000031" } ["Floor"]=> string(7) "Floor_1" ["RoomNumber"]=> int(11) ["Approved"]=> string(3) "yes" ["Approved On"]=> object(MongoDate)#26 (2) { ["sec"]=> int(1472322600) ["usec"]=> int(0) } ["Academic Year"]=> string(4) "2016" } ["HostelAllotmentHistory"]=> array(1) { [0]=> array(7) { ["Food Preferences"]=> string(4) "Both" ["Hostel"]=> object(MongoId)#27 (1) { ["$id"]=> string(24) "58138aee99fbade41e000031" } ["Floor"]=> string(7) "Floor_1" ["RoomNumber"]=> int(11) ["Approved"]=> string(3) "yes" ["Approved On"]=> object(MongoDate)#28 (2) { ["sec"]=> int(1472322600) ["usec"]=> int(0) } ["Academic Year"]=> string(4) "2016" } } ["courseId"]=> object(MongoId)#29 (1) { ["$id"]=> string(24) "58131c7099fbad4c1d0001c2" } }
...
...
I am trying to bring those student on the beginning of array whose first name is brian and rest of students should at the bottom.
Now the above code place the students on the top of the array but the rest of the students are ommited/removed. I don't want them to be removed.
I am trying to tweak what code to write in else condition.
Please help!!!
Fill $arrayStudents before the loop otherwise only matches will be collected and unshifted.
Extend example of php.net/unshift
$add = array('big');
$queue = array(
array("orange"),
array("banana"),
array("apple1"),
array("raspberry")
);
array_unshift($queue, $add);
print_r($queue);
In your case: search for keys you want to unshift, the rest goes to the standard $list = array(/list of studends/):
$list = array();
$bringToTop = array('brian', 'elvis');
foreach($data as $item) {
if (/* in in the list to bring on top*/) {
$memory[] = $item;
} else {
$list[] = $item;
}
}
foreach($memory as $item) {
array_unshift($list, $item);
}
print_r($list);
Kind regards
Add $arrayStudents[] = $row; to your else scope. Here is a simple demo to illustrate it.
When matches add the element to the result from the start of array, unmatches add them from the end of the array.

Access multidimensional array in php?

array(10) {
["table_name"]=>
string(0) ""
["chart_type"]=>
string(5) "Table"
["chart_name"]=>
string(9) "Veterans "
["dashboard_name"]=>
string(7) "Default"
["data_option"]=>
string(0) ""
["limit_value"]=>
string(1) "0"
["view_status"]=>
bool(true)
["view_priority"]=>
string(0) ""
["chart_data"]=>
array(72) {
[0]=>
array(11) {
[0]=>
string(4) "Rank"
[1]=>
string(6) "Branch"
[2]=>
string(12) "Level 3 Name"
[3]=>
string(12) "Level 4 Name"
[4]=>
string(23) "Logged Premia Score MTD"
[5]=>
string(26) "Converted Premia Score MTD"
[6]=>
string(21) "Persistancy Score MTD"
[7]=>
string(23) "Logged Premia Score YTD"
[8]=>
string(26) "Converted Premia Score YTD"
[9]=>
string(21) "Persistancy Score YTD"
[10]=>
string(5) "Score"
}
[1]=>
array(11) {
[0]=>
float(1)
[1]=>
string(22) " GWALIOR DIRECTSegment"
[2]=>
string(29) "G00563 - GOVIND SINGH KUSHWAH"
[3]=>
string(21) "S03643 - Shishir Jain"
[4]=>
float(1)
[5]=>
float(1)
[6]=>
float(1)
[7]=>
float(0.9994)
[8]=>
float(1)
[9]=>
float(1)
[10]=>
float(9.89901)
}
[2]=>
array(11) {
[0]=>
float(2)
[1]=>
string(26) " KURUKSHETRA DIRECTSegment"
[2]=>
string(23) "A02311 - Amarjeet Singh"
[3]=>
string(21) "S00927 - Sachin Kumar"
[4]=>
float(1)
[5]=>
float(1)
[6]=>
float(1)
[7]=>
float(0.953)
[8]=>
float(1)
[9]=>
float(1)
[10]=>
float(9.82245)
}
}
["color_scheme_name"]=>
string(7) "Default"
}
How do i display the chart_data in php
$arr=json_decode($row[0],TRUE);
var_dump($arr);
foreach($arr as $row)
{
foreach($row['chart_data'] as $k)
{
echo $k['Rank'];
echo $k['Branch'];
}
}
EDIT:
for ($x=0; $x<=count
($arr['chart_data']); $x++) {
foreach($arr['chart_data'][$x] as $key=>$val)
{
echo $val;
}
}
You are having 2 foreach calls.
The first one passes all keys of the outer array, amongst which chart_data.
The second one looks at the children of the outer array, and looks for chart_data in each child. But is obviously not finding any.
Change your code to this:
$arr=json_decode($row[0],TRUE);
foreach($arr['chart_data'] as $k){
echo $k['Rank'];
echo $k['Branch'];
}
edit:
I see. (Thanks for indenting your array) Your problem is off course that $arr['rank']and $arr['branch'] don't exist. They are mere strings inside $k[0] and $k[1] and themselves don't contain any other data.
What would you have like to see outputted?
Maybe this is solution.
$arr = json_decode($row[0],TRUE);
foreach($row['chart_data'] as $chart_data)
{
foreach($chart_data[0] as $k)
{
echo $k['Rank'];
echo $k['Branch'];
}
}
Update :
Rank, Branch is values. Your array is bad format.
$arr = json_decode($row[0],TRUE);
foreach($row['chart_data'] as $chart_data)
{
foreach($chart_data[0] as $k)
{
echo $k[0];
echo $k[1];
}
}

counting foreach loop items

i have the following array:
[2]=>
object(stdClass)#9 (4) {
["ID"]=>
string(32) "43c845f895a56fbe8aea9435ef8fa806"
["Type"]=>
string(8) "Campaign"
["Name"]=>
string(28) "An unmissable invitation for"
["Actions"]=>
array(5) {
[0]=>
object(stdClass)#10 (4) {
["Event"]=>
string(4) "Open"
["Date"]=>
string(19) "2013-05-07 17:00:00"
["IPAddress"]=>
string(12) "109.239.93.2"
["Detail"]=>
string(0) ""
}
[1]=>
object(stdClass)#11 (4) {
["Event"]=>
string(4) "Open"
["Date"]=>
string(19) "2013-05-07 09:01:00"
["IPAddress"]=>
string(12) "109.239.93.2"
["Detail"]=>
string(0) ""
}
[2]=>
object(stdClass)#12 (4) {
["Event"]=>
string(4) "Open"
["Date"]=>
string(19) "2013-04-30 22:29:00"
["IPAddress"]=>
string(14) "94.171.192.216"
["Detail"]=>
string(0) ""
}
[3]=>
object(stdClass)#13 (4) {
["Event"]=>
string(5) "Click"
["Date"]=>
string(19) "2013-04-30 17:43:00"
["IPAddress"]=>
string(12) "109.239.93.2"
["Detail"]=>
string(60) "http://www.rbh.co.uk/rbhevent/?name=[fullname]&email=[email]"
}
[4]=>
object(stdClass)#14 (4) {
["Event"]=>
string(4) "Open"
["Date"]=>
string(19) "2013-04-30 17:43:00"
["IPAddress"]=>
string(12) "109.239.93.2"
["Detail"]=>
string(0) ""
}
}
}
i am trying to count the events that are the same. So for example ["Event"] = Open =4 / ["Event"] = Click =1.
I am trying to achieve this via counting a foreach loop:
$i=0;
foreach($entry->Actions as $actions ) {
echo $i++;
}
Im not quite sure how to approach this? Can someone suggest a best practice?
$counts = array();
foreach($entry->Actions as $actions) {
if(!isset($counts[$actions->Event])) {
$counts[$actions->Event] = 0;
}
++$counts[$actions->Event];
}
print_r($counts);
<?php
$amounts = array(); // Events as key
foreach($entry->Actions as $actions)
{
if (isset($amounts[$actions["Event"]])) $amounts[$actions["Event"]]++;
else $amounts[$actions["Event"]] = 1;
}
print_r($amounts);
echo "<br>".$amounts["Open"];
?>

Categories