How to access multidimensional array using django templates in html page - php

I have a multidimensional array in php page like this:
$comment=Array
(
[655436] =>
[655435] => Array
(
[id] => 5864928
[shortmessage] => rytrht
)
[655434] => Array
(
[id] => 5864934
[shortmessage] => vcxv
)
[654990] =>
[654989] => Array
(
[id] => 5864948
[shortmessage] => fgfj
)
[654983] => Array
(
[id] => 5864909
[shortmessage] => state
)
[654981] =>
[654979] =>
[654978] => Array
(
[id] => 5864933
[shortmessage] => ggjj
)
[654329] => Array
(
[id] => 5864974
[shortmessage] =>
)
)
echo $renderer->render('sample.html', array( 'index' => commentId,'Comment' => $comments));
I wish to use the $comment array values in HTML page using Django temlates with out looping over the array
sample.html:
<td><input class="Comment" type="text" value="{{Comment[index]['shormessage']}}" /></td>
But {{Comment[index]['shormessage']}} not giving any value and the whole page itself not displaying
I dont know how to access the multidimensional array.
Is there any way to do this?

Use For Loop for this
Try by Using
{% for Comment in Comments %}
<input class="Comment" type="text" value="{{Comment.shormessage}}" />
{%endfor%}

Related

Replacing an array key contents whilst preserving the full array - PHP

I want to replace the contents of array key [menu_order] with my new array part, whilst keeping the entire array together. Which way would be to replace that key?
Original complete array
variable - $full_array
Array
(
[menu_order] => Array
(
[0] => Array
(
[row_position] => 1
[row_identifier] => home
[original_row_position] => 1
[button_styling] => off
[tab] => self
)
[1] => Array
(
[row_position] => 2
[row_identifier] => benefits
[original_row_position] => 2
[button_styling] => off
[tab] => self
)
)
[policy] => Array
(
[override_privacy] => no
[privacy_override_textarea] =>
)
)
[menu_order] new data
variable - $menu_order_array_part
Array
(
[0] => Array
(
[row_position] => 1
[row_identifier] => home
[original_row_position] => 1
[button_styling] => off
)
[1] => Array
(
[row_position] => 2
[row_identifier] => benefits
[original_row_position] => 2
[button_styling] => off
)
)
I can't get the syntax correct for editing the original array whilst replacing a multidimensional array
Thanks,
D
You can use the key.
$full_array['menu_order'] = $menu_order_array_part;

PHP: Displaying data from Bigcommerce API

I am trying to display the data from Bigcommerce through php when I run the Bigcommerce::getCategories() i getting a array of data like this:
[0] => Bigcommerce\Api\Resources\Category Object
(
[ignoreOnCreate:protected] => Array
(
[0] => id
[1] => parent_category_list
)
[ignoreOnUpdate:protected] => Array
(
[0] => id
[1] => parent_category_list
)
[fields:protected] => stdClass Object
(
[id] => 88
[parent_id] => 0
[name] => Dell
[description] =>
[sort_order] => 0
[page_title] =>
[meta_keywords] =>
[meta_description] =>
[layout_file] =>
[parent_category_list] => Array
(
[0] => 88
)
[image_file] =>
[is_visible] => 1
[search_keywords] =>
[url] => /dell/
)
[id:protected] => 88
[ignoreIfZero:protected] => Array
(
)
[fieldMap:protected] => Array
(
)
)
but when I try to pass this to JQuery so that I can display it using this statement: <?php echo json_encode($categories); ?> I am getting an array of empty objects, what is the right way to get the array of objects in Bigcommerce API? Thanks.
From the first line of your code:
[0] => Bigcommerce\Api\Resources\Category Object
You have an object, not an array. Try casting it to an array first:
$array = (array) $yourObject;

PHP Nested Array loop nested results

I am experimenting with my first API and getting stuck with the results. I am getting an Array Back:
Array
(
[GetOrderListResult] => Array
(
[Status] => Success
[MessageCode] => 0
[ResultData] => Array
(
[OrderResponseItem] => Array
(
[0] => Array
(
[NumberOfMatches] => 2
[OrderTimeGMT] => 2014-05-05T03:23:00
[LastUpdateDate] => 2014-05-28T11:41:45.953
[TotalOrderAmount] => 12.7800
[OrderState] => Active
[DateCancelledGMT] =>
[OrderID] => 138711
[ClientOrderIdentifier] => 138711
[SellerOrderID] =>
[OrderStatus] => Array
(
[CheckoutStatus] => NotVisited
[CheckoutDateGMT] => 1900-01-01T00:00:00
[PaymentStatus] => NotSubmitted
[PaymentDateGMT] => 1900-01-01T00:00:00
[ShippingStatus] => Unshipped
[ShippingDateGMT] => 1900-01-01T00:00:00
[OrderRefundStatus] => NoRefunds
)
)
[1] => Array
(
[NumberOfMatches] => 2
[OrderTimeGMT] => 2014-05-05T03:23:00
[LastUpdateDate] => 2014-05-28T12:59:01.78
[TotalOrderAmount] => 6.3900
[OrderState] => Active
[DateCancelledGMT] =>
[OrderID] => 138750
[ClientOrderIdentifier] => 138750
[SellerOrderID] =>
[OrderStatus] => Array
(
[CheckoutStatus] => NotVisited
[CheckoutDateGMT] => 1900-01-01T00:00:00
[PaymentStatus] => NotSubmitted
[PaymentDateGMT] => 1900-01-01T00:00:00
[ShippingStatus] => Unshipped
[ShippingDateGMT] => 1900-01-01T00:00:00
[OrderRefundStatus] => NoRefunds
)
)
)
)
)
)
Now the onyl way I know how to reference a fied such as the order id in the array is:
echo "Order ID: ".$result['GetOrderListResult']['ResultData']['OrderResponseItem']['0']['OrderID'];
But I want to be able to loop through the array of orders and execute code for each item, could somewbody please point me in the right direction for:
a) is there a better way to refernce these fields?
b) how do I loop through the OrderResponseItem part of the array?
The only loop I could think of was for the whole array not nested items in the array.
Sorry if I'm missing something simple....
Thanks and if you need the data in any other format please let me know.
Since you already know the keys you could just use a foreach to access them an point to that key then loop. Something like this:
foreach($result['GetOrderListResult']['ResultData']['OrderResponseItem'] as $value) {
$order_id = $value['OrderID'];
// your other processes
}

Get JSON value from PHP

All, I have got a JSON response from NEO4J:
Array
(
[columns] => Array
(
[0] => n
)
[data] => Array
(
[0] => Array
(
[0] => Array
(
[outgoing_relationships] => http://localhost:7474/db/data/node/1/relationships/out
[labels] => http://localhost:7474/db/data/node/1/labels
[data] => Array
(
[position] => Developer
[awesome] => 1
[name] => Michael
[children] => 3
)
[traverse] => http://localhost:7474/db/data/node/1/traverse/{returnType}
[all_typed_relationships] => http://localhost:7474/db/data/node/1/relationships/all/{-list|&|types}
[property] => http://localhost:7474/db/data/node/1/properties/{key}
[self] => http://localhost:7474/db/data/node/1
[properties] => http://localhost:7474/db/data/node/1/properties
[outgoing_typed_relationships] => http://localhost:7474/db/data/node/1/relationships/out/{-list|&|types}
[incoming_relationships] => http://localhost:7474/db/data/node/1/relationships/in
[extensions] => Array
(
)
[create_relationship] => http://localhost:7474/db/data/node/1/relationships
[paged_traverse] => http://localhost:7474/db/data/node/1/paged/traverse/{returnType}{?pageSize,leaseTime}
[all_relationships] => http://localhost:7474/db/data/node/1/relationships/all
[incoming_typed_relationships] => http://localhost:7474/db/data/node/1/relationships/in/{-list|&|types}
)
)
)
)
I do not know how to retrieve the value "position" under the data array, because the array is under another array.
Can you tell me how to do it with PHP?
Thx
Access them like this.
echo $yourarr['data'][0][0]['data']['position'];
Tips on how to locate :
Just locate where is the position keyword, Now look up to the array, As you can see the parent of position is data, think of how you reach from the start
(data)to the destination(position) (like a maze).
When you have more than 1 record try this
foreach($var['data'] as $inside){ //inner 1st stage
foreach($inside as $index => $main){ //inner 2nd stage
if($index == 'data'){ //check if index is data
echo $main['position']; //output position
}
}
}

serialise array will inputs of same name

i've got 3 inputs, 2 of which i want in the same array (name="notes['a']" and name="notes['b']") but when i use jquerys' serializeArray() it puts them all at the same 'level'. But i want to use php to serialise the posted notes array into the database using the method thats been working for inputs that aren't part of an array (ie name="basic"
foreach($_POST['data'] as $var => $value) {
if(!strstr(strtolower($value['name']),'added')) $q1 .= "".$value['name']."='".$value['value']."',";
}
echo $q = "UPDATE ".$_POST['table']." SET ".rtrim($q1,',')." WHERE ".$_POST['idField']."=".$_POST['id']." ";
*the array $_POST['data'] is getting posted as;*
[data] => Array
(
[0] => Array
(
[name] => notes[\'a\']
[value] => dan#jynk.net
)
[1] => Array
(
[name] => notes[\'b\']
[value] => Daniel Crabbe
)
[2] => Array
(
[name] => test
[value] => Daniel Crabbe
)
)
but i guess it should be along these lines?
[data] => Array
(
[0] => Array{
Array(
[name] => notes[\'a\']
[value] => dan#jynk.net
)
Array
(
[name] => notes[\'b\']
[value] => Daniel Crabbe
)
)
[1] => Array
(
[name] => test
[value] => Daniel Crabbe
)
)
how can i get jquery to respect the arrays in the input array? any help welcome...
UPdates
no got it like this but want everything on level [0] for easy access...
Array
(
[0] => Array
(
[name] => cm_email_to
[value] => dan#jynk.net
)
[1] => Array
(
[name] => cm_name_to
[value] => Daniel Crabbe
)
[2] => Array
(
[name] => cm_moveToList
[value] =>
)
)
*inputs*
<input id="cm_email_to" name="cm_email_to" value="dan#jynk.net" class="toPostCM" />
<input id="cm_name_to" name="cm_name_to" value="Daniel Crabbe" class="toPostCM" />
*jquery*
var dataCMSettings = $(".toPostCM").serializeArray();
Try taking the array indicies out of the input name:
<input name="notes[]" value="val1" />
<input name="notes[]" value="val2" />
Remove the quotes from your array indices in your HTML, so array['a'] becomes array[a].
ok - this does exactly what i need...
var params = {};
$('.toPostCM').each(function(index,value) {
params[value.name] = value.value;
});
console.log(params);
*gives me*
Array
(
[cm_email_to] => dan#jynk.net
[cm_name_to] => Daniel Crabbe
[cm_moveToList] =>
)
thanks all...

Categories