I have a form which where I receive some $_POST data. In a submitAction.php (for example) I did 2 steps, first I assign to a variable what I receive in $_POST for each input field, and second I push the variable to an array of fields; this:
first:
$pais= $_POST['pais'];
$horario = $_POST['horario'];
second:
$fields = array(
'pais' => "eeuu",
'horario' => "pdt",
);
I have simplify the number of fields but In reality I have more. I have an array with the names of the $fields array keys, that came from another part of the application.
The problem is that I am trying to automatize this process, because if not I have to edit this submitAction.php for each form that I have in my application.
I am trying to code a for each statement to iterate and did the assign of the $_POST['whatever'] to the $fields key that match, but I am not sure how to do it.
Any help? Thanks in advance :)
I had some difficulty understanding your question, but I presume that you wanted to take the field names and their corresponding values from the POST request (but only from the fields which you have stated in an array) and assign them to a multidimensional array. The code below should help you.
$keyNames = array("pais", "horario");
$fields = array();
foreach($keyNames as $keyName){
$fields[$keyName] = $_POST[$keyName];
}
Related
I am trying to create a table and populate the table with the following fields with the help of database seeder:
option a
option b
option c
option d
correct option
First four fields will be assigned random word, and the last field 'correct option' will contain any one of the first four.
I could not find any solution to do it with Laravel database seeder. Can anyone help?
Something like this?
use faker random element function in your factory or seeder.
$optionA = $faker->word;
$optionB = $faker->word;
$optionC = $faker->word;
$optionD = $faker->word;
return [
'option_a' => $optionA,
'option_b' => $optionB,
'option_c' => $optionC,
'option_d' => $optionD,
'correct_option' => $faker->randomElement([$optionA,$optionB,$optionC,$optionD]),
];
Create a factory and use Faker to generate the random words you're after
This sounds like an ideal use case for JSON columns (both for questions and answers). For instance, you might decide to have multiple valid answers to a single multiple choice question.
In your migration:
// create_questions_table.php
...
$table->json('choices')->default(new Expression('(JSON_ARRAY())'));
$table->json('answer')->default(new Expression('(JSON_ARRAY())'));
From https://laravel.com/docs/7.x/migrations#column-modifiers:
Using an Expression instance will prevent wrapping the value in quotes and allow you to use database specific functions. One situation where this is particularly useful is when you need to assign default values to JSON columns.
Then create a factory:
// QuestionFactory.php
$factory->define(Location::class, function (Faker $faker) {
$choices = $faker->words(4);
$answer = [ $choices[rand(1,4)] ];
return [
'choices' => $choices,
'answer' => $answer,
];
});
Using the Faker library included in Laravel, we can pick 4 words and randomly assign one of them to be the answer.
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.
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.
In cakephp, I executed this query to find the id field value of the corresponding username
$count = $this->User->find('all',array('conditions'=>array('User.username' => $n)))
How I will get take the value of a field from this array result to a variable?
I am new to cakephp and any help will be very useful.
Well, after calling find, you will notice that $count will be populated if something was brought from the database. I would change something, though, I would use "first" instead of "all" because you are finding only one record.
You can either use this
//in your controller
$count = $this->User->find('first',
array('conditions'=>array('User.username' => $n)));
//and set the variable to be used in the view in this way
$this->set('yourId', $count['User']['id']);
Then in your view
echo $yourId;
Or, you can also do this
$yourId = $this->User->field('id', array('User.username' => $n));
$this->set(compact('yourId'));
and then in your view
echo $yourId
I have searched high and low on how to just get a total of a field called points. I just need one total figure but the best I can get is a list of records from the Points table with the associated records from the Members.
$totalPoints = $this->Member->Point->find('all', array(
array('fields' => array('sum(Point.points) AS Point.ctotal'))));
Why not using virtualFields as documented and suggested by the docs?
http://book.cakephp.org/2.0/en/models/virtual-fields.html
$this->Member->Point->virtualFields['total'] = 'SUM(Point.points)';
$totalPoints = $this->Member->Point->find('all', array('fields' => array('total')));
This is way cleaner.
Also note the double array you got there in your $options array (...find('all', array(array(...). And how I used only a single/flat array.
This is the reason why your SUM() call as fields does not work.
mark's answer above is right. I just want to add that you can do this:
$totalPoints = $this->Member->Point->find('first', array(
array('fields' => array('sum(Point.points) AS Point__ctotal'))));
$totalPoints will be have this:
$totalPoints['Point']['ctotal']
Another clean way without outputting an array
$this->Member->Point->virtualFields = array('total' => 'SUM(Point.points)');
$total = $this->Member->Point->field('total');