I've got the following json object:
$dati = array(
"data" => array(
'address_complete'=>$data->results[0]->formatted_address,
'address_square'=>$data->results[0]->address_components[1]->long_name,
'location'=>$data->results[0]->address_components[2]->long_name,
'postal_code'=>$data->results[0]->address_components[7]->long_name,
'data_ora'=>$tmp_date
),
"str" => array ("n1"=>"fabio",
"n2"=>"marcolino",
"n3"=>"lauretta")
);
$p = json_encode($dati);
echo $p;
How can I enter the n1 element to show fabio as output?
$p.str.n1 doesn't work. How can I do this?
Besides, how can I show fabio,marcolino,lauretta using a for loop?
for ($i=1; $i<=3; $i++) {
$dati = array(
"data" => array(
'address_complete'=>$data->results[0]->formatted_address,
'address_square'=>$data->results[0]->address_components[1]->long_name,
'location'=>$data->results[0]->address_components[2]->long_name,
'postal_code'=>$data->results[0]->address_components[7]->long_name,
'data_ora'=>$tmp_date
),
"str" => array (
"n".$i=>"fabio",
"n".$i=>"marcolino",
"n".$i=>"lauretta")
);
//$p = json_encode($dati);
echo $dati[str]["n".$i];
}
How can I fix this code?
Finally I've got another question: if "str" => array ("n1"=>"fabio", "n2"=>"marcolino", "n3"=>"lauretta") are saved in a database and I want to get them from it, is correct to write in the following way?
"str" => array("n".$i=>"Ti sei incrociato con ".$array_db[username]),
and to call them with Ajax:
... success:function(msg){
if(msg){
$("#location").html(Object.keys(msg.str).map(x => msg.str[x]).join(", "));
}else{
$("#location").html('Not Available');
}
Besides How can I show "fabio","marcolino","lauretta" using a loop
for?
<?php
$dati = array(
"data" => array(
'address_complete'=>$data->results[0]->formatted_address,
'address_square'=>$data->results[0]->address_components[1]->long_name,
'location'=>$data->results[0]->address_components[2]->long_name,
'postal_code'=>$data->results[0]->address_components[7]->long_name,
'data_ora'=>$tmp_date
),
"str" => array ("n1"=>"fabio",
"n2"=>"marcolino",
"n3"=>"lauretta")
);
?>
<script type="text/javascript">
<?="var p = " . json_encode($dati) ?>
// print individual elements
for (i in p.str) {
console.log(p.str[i])
}
// print together
console.log(Object.keys(p.str).map(x => p.str[x]).join(", "))
</script>
Related
I have array in controller file:
$total_data = array();
$totals = $this->model_sale_order->getOrderTotals($order_id);
foreach ($totals as $total) {
$total_data[] = array(
'title' => $total['title'],
'text' => $this->currency->format($total['value'], $order_info['currency_code'], $order_info['currency_value']),
);
}
How pass 'text' in this same controller file?
for example:$text = $total['text'];I getting an error undefined index: text in....
Where is a problem?
I don't know what you want to do, but you can send a variable from controller to view by $data in Opencart 2.x
You can send $total_data to tpl file this way:
$data['total_data'] = $total_data;
so your code must be:
$total_data = array();
$totals = $this->model_sale_order->getOrderTotals($order_id);
foreach ($totals as $total) {
$total_data[] = array(
'title' => $total['title'],
'text' => $this->currency->format($total['value'], $order_info['currency_code'], $order_info['currency_value'])
);
}
$data['total_data'] = $total_data;
and use it in view (tpl file):
<?php
foreach($total_data as $total){
echo $total['text'];
}
?>
I'm trying to create a function test that would be used for several buildings types but I can't get the variables to be passed into the View.
Controller code:
public function index($data = NULL){
$data1 = $this->test('hotel');
$data2 = $this->test('restaurant');
$data = array_merge($data1,$data2);
$this->load->view('templates/default',$data);
}
public function test($building_type){
$data[$building_type]['title'] = 'this is a title for '.$building_type;
for ($i=1;$i<=3;$i++) {
$data[$building_type][$i] = $building_type.' button';
}
$data['building_type_array'] = ['hotel', 'restaurant'];
return $data;
}
View code:
foreach ($building_type_array as $value) {
echo $value; // echoes 'hotel' and 'restaurant'
echo $value['title']; // throws 'Illegal string offset'
echo $value[3]; // echoes the 4th letter of 'hotEl' and 'resTaurant'
}
echo $building_type['title']; // Throws 'Undefined variable: building_type'
echo $hotel['title']; // echoes 'this is a title for hotel'
echo $hotel[3]; // echoes 'hotel button'
The first four echo are attempts that do not give the expected result. The last two echo of the View give the expected result but I would like to use a generic variable to avoid writing $hotel['title'], $restaurant['title'] ... for each building type.
Try something like this...
Controller:
public function index($data = NULL) {
$data['building_types']['hotel'] = $this->test('hotel');
$data['building_types']['restaurant'] = $this->test('restaurant');
$this->load->view('templates/default', $data);
}
View:
foreach ($building_types as $building) {
foreach ($building as $value) {
echo $value; // whatever
}
}
I don`t know if i understand you, but i understand that you have an array
like
$m_array = //come from model
array(
array(
'title' => 'title',
'1' => 'value1',
'2' => 'value2',
'3' => 'value3'
),
array(
'title' => 'title2',
'1' => 'value1-2',
'2' => 'value2-2',
'3' => 'value3-2'
),
array(
'title' => 'title3',
'1' => 'value1-3',
'2' => 'value2-3',
'3' => array('one' => 'v-one', 'two' => 'v-two')
),
//etc
);
in your view
foreach($m_array as $item => $value){
if( is_array($value)){
foreach($value as $row => $value2){
echo "<td>$item</td>";
echo "<td>$row</td>";
echo "<td>$value2</td>";
}
}
else{
echo "<td>$item</td>";
echo "<td>$value</td>";
echo "<td>-</td>";
}
}
i am using this array for tag friends in a photo but no one is tagged , help me if i wrong.
$tags = array(
"tag_uid" => $id,
"tag_text" => $name,
"x" => 20,
"y" => 20
);
$facebook->api('/' . $new . '/tags?','post', array( $tags ));
this is because you are not doing the correct tags arrays. Try the following codes:
// Set tags limit
$f1 = $facebook->api('me/friends?limit=10');
// Get access token
$access_token = $facebook->getAccessToken();
// First post photo and then tag id
$post_photo = $facebook->api('/me/photos', 'POST', array(
'source' => '#' . $photo,
'message' => $message
)
);
// Creating Tags arrays for Tagging in the photo
foreach($f1['data'] as $fbu){
$tagx = array('tag_uid' => $fbu['id'],'x' => 30,'y' => 30 );
$ftags[] = $tagx;
}
// Tags generated now giving variable
$tagargs = array (
'tags' => json_encode($ftags),
'access_token' => $access_token,
);
// Posting the tags in photo
$result = $facebook->api('/' . $post_photo['id'] . '/tags', 'post', $tagargs);
In my hook_preprocess_node function I am changing the links by themeing and adding a t() function to allow translation.
The problem is when I render out in my node I get the word "ARRAY" printed out, this is using either
<?php print render($field_downloads); ?> or <?php print $field_downloads); ?>
in my node.
Code in template.php
$list_of_paths = array();
foreach($field_downloads as $index => $data)
{
$file_uri = $data['uri'];
$file_path = file_create_url($file_uri);
$list_of_paths[] = '<strong> >>'. t('DOWNLOAD'). '</strong> '.l(t($data['description']), $file_path);
}
$variables['field_downloads'] .= theme("item_list", array(
'items' => $list_of_paths,
'type' => 'ul',
'attributes' => array('class' => 'downloads'),
));
}
What I am trying to do is display a table with checkboxes on the press of a button by ajax. The table should be initially hidden and get populated on the fly by a function call.
If initially I load $options1 with some dummy values , then after ajax call it throws in an error saying-
Notice: Undefined index: red in theme_tableselect() (line 3285 of
D:\wamp\www\drupal7\includes\form.inc).
where 'red' is the index of a dummy row value and #options don't get populated with the new values. What is the way to get this working ?
Here is the code for the form-
$form['mltag_new']['tag'] = array(
'#type' => 'button',
'#value' => t("Suggest Tags"),
'#ajax' => array(
'callback' => 'mltag_suggest_tags_ajax',
'wrapper' => 'mltag_suggest_tags_table_div',
'effect' => 'slide',
),
);
$options1 = array(); //initial dummy values
$options1['red']['tag'] = "A red row";
$options1['red']['chi'] = "A red row";
$form['mltag_new']['myselector'] = array (
'#type' => 'tableselect',
'#title' => 'My Selector',
'#header' => $header,
'#options' => $options1,
'#prefix' => '<div id="mltag_suggest_tags_table_div">',
'#suffix' => '</div>',
);
return $form;
and the Ajax callback looks something like this-
function mltag_suggest_tags_ajax($form, $form_state) {
//$content has some content
//pass the content to a function
include_once 'includes/content_tag.inc';
$tags = mltag_content_tag($content, variable_get('algo_type'), 20);
if (empty($tags)) {
$output .= t('Content is insufficient to generate Tags using this algorithm. <br>Please choose other algorithm from Settings Page.');
$form['mltag_new']['sample_text']['#markup'] = $output;
return $form['mltag_new']['sample_text'];
}
else {
$algo = variable_get('algo_type');
if ($algo == 1) {
$header = array(
'tag' => t('Tag'),
'frequency' => t('Frequency'),
);
$options = array();
foreach ($tags as $key => $value) {
$options[$key] = array(
'tag' => $key,
'frequency' => $value,
);
}
}
elseif ($algo == 2) {
$header = array(
'tag' => t('Tag'),
'chi' => t('Chi Square Value'),
);
$options = array();
foreach ($tags as $key => $value) {
$options[$key] = array(
'tag' => $key,
'chi' => $value,
);
}
}
$form['mltag_new']['myselector']['#header'] = $header;
$form['mltag_new']['myselector']['#options'] = $options;
return $form['mltag_new']['myselector'];
}
}
I replied to your post on Drupal.org about how I'm working on a somewhat similar problem. Try adding
$form['mltag_new']['myselector'] =
form_process_tableselect($form['mltag_new']['myselector']);
just before your return. Hopefully that helps you more than it did me. Beware that the #options just get rendered when the block reloads from the ajax, but the original $form object doesn't seem to be aware.
I know that this is a few years later, but I found this while searching for my own solution:
The tableselect module creates checkboxes in the $ form that have to be removed. in the example above, they would be in $form['mltag_new']['myselector'] with keys equal to the original $option1 in your original code. If you unset those, then call
$form['mltag_new']['myselector'] = form_process_tableselect($form['mltag_new']['myselector']);
before your return, it will eliminate the dummy rows.