how to display array of message in magento bundle model - arrays

I'm working on the bundle model to check on the quantity of each bundle .. I want to display the error message in array so I can return all the errors at one time. how can I pass array to this line
Mage::helper('bundle')->('msg')? I want to add my errors array instead of message to be like this
Mage::helper('bundle')->($errors). but when I do so it returns array.
if there is also any other solution I'd be grateful :)

if you want to display in forntend site, look like this.
$message = new array();
foreach( $array in $element )
{
...
$message[] = Mage::helper('bundle')->__('msg');
...
}
Mage::throwException(implode(', ', $message));

Related

Cakephp 4.x Passing Variables to Email Template

In CakePHP 4.x I'm having some trouble with the mailer() function, specifically passing variables from my controller to a template.
I need a user to be able to receive an email with a list of items. I'm able to use ajax to send an array to my controller, but I can't get the controller to pass the variables to the email template. I'm getting the email but not the dynamic content and I just can't get it to work via setViewVars.
My controller function:
public function bar(): void
{
if( $this->request->is('ajax') ) {
$this->request->allowMethod('ajax');
$foo = $this->request->getQuery('data');
// data is a stringified array
$mailer = new Mailer("default");
$mailer->viewBuilder()->setTemplate('default','default');
$mailer
->setEmailFormat('html')
->setFrom(['email#email.com' => 'Comparaciones'])
->setTo('email#email.com')
->setSubject('About')
>setViewVars($foo)
->send();
// ->deliver() doesn't work either
}
}
My template:
// standard cakephp default template
$content = explode("\n", $content);
foreach ($content as $line) :
echo '<p> ' . $line . "</p>\n";
endforeach;
I'm probably tripping up with something simple, I just can't get the array into the template. Any help much appreciated!!!

push an array while foreach other array laravel

I'm using laravel package commentable by faustbrian. i've been getting the comment for a post using just like in documentation in github . I want to search the information for user who post the comment, e.g the user avatar, address, and other information as well. I want to include the user information (which is an array) to the current position index of array (while for each i push it). the problem is, i tried using array_push, array_merge and $data[$key]=>$value as well. but none of them is working when i dd the variable. please help.
public function notaPengurusan($id){
$comments=Complaint::findOrFail($id)->comments->toArray();
foreach ($comments as $comment){
$creator=User::findOrFail($comment['creator_id'])->toArray();
array_push($comment,$creator);
}
dd($comments);
return view('complaint::notapengurusan',compact('comments'));
}
image when i dd the image
You need to use array index and its global variable $comments
foreach ($comments as $key=>$comment){
$creator=User::findOrFail($comment['creator_id'])->toArray();
array_push($comments[$key],$creator);
}
you should add user() relationship in your Comment Model :
public function user(){
return $this->belongsTo('App\Model\User','creator_id');
}
then no need to use for loop and finding user each time , use with('user'):
$comments=Comment::where(['post_id'=>$post_id])->with('user')->orderBy('created_at','desc')->paginate(12);

My code is showing ARRAY

I am using a plugin, using the following code could show user ratings:
<?php $users_rating = RWP_API::get_reviews_box_users_rating( $post_id, $reviews_box_id ); ?>
<?php echo $users_rating; ?>
But instead of showing the actual values, I am getting ARRAY writen on my web page.
Can anyone help me please?
Thank you,
Regards
You need to read the documentation to know how to use the API correctly.
Your call to RWP_API::get_reviews_box_users_rating tells me you're using the "Reviewer WordPress Plugin" from CodeCanyon.
The first step is looking up that method in the documentation: http://evographics.net/WEB_SERVER/reviewer-plugin-v-3-14-2.pdf
The documentation tells you to expect an array to be returned therefore it's up to you to display those values in a usable format.
$users_rating = RWP_API::get_reviews_box_users_rating( $post_id, $reviews_box_id );
// We're going to skip verifying we got back what was expected for the sake of the answer.
// Loop through each user score and display.
foreach ( $users_rating['score'] as $key => $score ) {
// Example of how an individual score could be displayed.
printf( 'Score %d: %d<br />', $key, $score );
}
Based strictly on your example the results of your get_reviews_box_users_rating() method is returning an array. You need to loop through the array itself and echo those values.
foreach($users_rating as $k => $rating){
//IF YOUR ARRAY CONTAINS OBJECTS
echo $rating->userRating;
//IF AN ASSOCIATIVE ARRAY
echo $rating['userRating'];
}
To see the structure of the array being returned you can use this:
print_r($users_rating);
That will let you know if your array is a stdObject Array or an associative array of elements.

Laravel 4 foreach loop - wanting to display the last element

I am currently trying to set up an edit page where an order form is populated using json_decode to decode json information that was saved when the form was created. Because the form's size can change I have to create the correct number of inputs so that all the json data will have a place to be displayed. Fortunately as the inputs are numbered this should not be hard to do. Unfortunately I am not sure how to pick the last element of the json information that has been decoded. Currently I am using:
public function getEdit($id){
$order = Order::where('id', '=', $id);
if($order->count()) {
$order = $order->first();
$order->order_serialized = json_decode($order->order_serialized);
foreach($order->order_serialized as $key => $value){
$order->$key = $value;
}
return View::make('orders.edit')
->with('order', $order);
} else {
return App::abort(404);
}
}
to decode the information and it is working splendidly but I need to be able to pick up the last element to be able to find the total number of inputs and am not sure how I could do this without disturbing the foreach loop. Any and all help would be greatly appreciated!! Thank you so much!
You can use the count and toArray methods to find the last item.
$nItem = $order->count();
$aOrder = $order->toArray();
$aLastItem = $aOrder[$nItem-1];
Collections have a last() function to compliment the first() function.

cakephp invalidate element array

I am using cakephp. I have a form with an element array.
For ex:-
<textarea name="data[User][0][description]>
<textarea name="data[User][1][description]>
From the controller, I need to invalidate (manually) the array field if it is empty and need to show errors to the respective field.
What is the correct syntax for invalidating the field if it an element array ?
I know, the following will work for single element . How it will be for an element array ?
$this->User->invalidate("description");
Unfortunately you cannot invalidate the field with that function.
But what invalidate() does?
function invalidate($field, $value = true) {
if (!is_array($this->validationErrors)) {
$this->validationErrors = array();
}
$this->validationErrors[$field] = $value;
}
It just set validationErrors of the model.
So, you can do following in your Controller (but I also appeal you to move that validation in the Model):
$this->User->validationErrors[1]['description'] = 'Your error message';
The following code will invalidate the second description in the list.
HTH
You can type in view:
<?php
echo $this->Form->error("User.1.description");
?>
Thanks Nik,
your answer helped me, but halfway, because my problem was with a compound field by other subfields.
account_number {
bank_code,
bank_office,
check_digit,
account
}
In this case if we need put in validation error in one subfield, this is the solution:
$this->Model->validationErrors['account_number']['bank_code'][0] = 'Your message error';
I hope this help someone.
Regards.
I had similar problem, since it was for admin panel I showed error message on the first level of field i.e. for this portion only.
If you are validation on controller, just create an array of error with field name and error message, set it in controller and display message if in_array($field, $withErrorArray) in view.

Resources