Form (newreports.php) when on submit needs to save to table as well as save hidden data (27 records with 7 columns each) to a BelongsTo table. 7 columns are: id,user_id,reports_id,count,area,area_id,comments. Area needs to pre-fill as 0-26 and reports_id needs to be the same for all (100). User_id should pre-fill from the form entry. Also, id should auto-fill. I think this can be done in my controller for function newreports().
Do I need to write the array like this or is there a simplified way?
$this->Report->saveAll(
Array
(
[Report] => Array
(
[0] => Array
(
[id] => //leave blank because it will auto-fill?
[user_id] => //dynamically from form input
[reports_id] => //dynamically from form input
[area_id] => //dynamically from form input
[area] => 0
[count] => // this should be blank as there are no counts yet
[comments] => // this should be blank as there are no comments yet
)
[1] => Array
(
[id] => //leave blank because it will auto-fill?
[user_id] => //dynamically from form input
[reports_id] => //dynamically from form input
[area_id] => //dynamically from form input
[area] => 1
[count] => // this should be blank as there are no counts yet
[comments] => // this should be blank as there are no comments yet
)
)
)
This is what did it for me
$count = 27;
$v = 0;
do {
######### save report rows to database
$this->Report->create();
$this->Report->set(array('reports_id' => $id , 'area' => $v));
$this->Report->save();
$v++;
} while ($v < $count);
If your form/view code is written the right way, you should be able to get away with $this->Report->saveAll($this->data['Report']);.
View the HTML source for your form. The values of the name attributes for the input tags should be something like data[Report][0][id], data[Report][0][user_id], etc. The second record should have fields looking like data[Report][1][id], data[Report][2][user_id], etc.
When this gets POSTed, it will automatically import in to the correct array structure in $this->data.
If this form will always create new records, then leave out the data[Report][][id] fields (or leave them empty), and saveAll will create new records.
Update to handle first comment.
It's not clear if you want an assignment to $data after or before the controller saves all that hidden data. Let's deal with both parts; first, the save.
So, your own answer nearly gets it right for creating the data.
$count = 27;
$v = 0;
$data = array('Report' => array());
do {
$data['Report'][] = array('reports_id' => $id, 'area' => $v);
$v++;
} while ($v < $count);
// save report rows to database
$this->Report->saveAll($data['Report']);
So, that prepares a data structure, much like you've done in your original question, and in your answer. However, we use saveAll() to create them all in one go.
The next part, is to retrieve the saved data, and put into $this->data, so you can use it in the form that your users will see after the redirect, as per your comment. You'll need something like this in the controller.
$reports = $this->Report->find('all', array(
'conditions' => array(
'reports_id' => $id
)
));
// merge this in with existing data
$this->data = Set::merge($reports, $this->data);
It is assumed that $id is on the URL as a part of the redirect. And that's how you assign that data to $this->data, so you can use it in your form.
And in your form, you can reference the multiple fields like so:
$this->Form->create('Report');
foreach ($data['Report'] as $i => $report) {
$this->Form->input('Report.'.$i.'.id');
$this->Form->input('Report.'.$i.'.user_id');
$this->Form->input('Report.'.$i.'.reports_id');
$this->Form->input('Report.'.$i.'.area_id');
$this->Form->input('Report.'.$i.'.area');
$this->Form->input('Report.'.$i.'.count');
$this->Form->input('Report.'.$i.'.comment');
}
$this->Form->end('Submit);
You can read more about forms for multiple records at the CakePHP manual.
Related
I am writing a page that pulls images and image data out of a multidimensional array. I need to be able to click a button that calls a function to sort out the images by tags(IE tag_GlassDoor & tag_GlassWall) - basically to show only images that do or do not have that particular element (in this case im using 0 and 1 for yes and no), such as a glass door. I can currently make that array display the data, but I cant figure out how to sort the data by one of the array keys, or even really the syntax to pull a single value out at will.
$arrImages[] =
[
'img_sm'=>'image1.jpg',
'tag_GlassDoor'=>0,
'tag_GlassWall'=>1,
];
$arrImages[] =
[
'img_sm'=>'image2.jpg',
'tag_GlassDoor'=>1,
'tag_GlassWall'=>1,
];
Filtering is the answer, it can be used to filter one dimensional Arrays and multidimensional arrays.
the general implementation would be something like this:
$arr = array(
array(
'image' => "data",
'hasObject' => 1
),
array(
'image' => "data",
'hasObject' => 0
),
);
$finteredArray = array_filter($arr, function ($r) {
return (bool) $r['hasObject'];
});
print_r($finteredArray);
// it outputs:
// Array ( [0] => Array ( [image] => data [hasObject] => 1 ) )
am getting all these values from a form submission .
i want to split these repeated values into separate variable and i want to store them into mysql DB.
this the output
array ( 'myField' => 'given value in text area',
'myCheckboxes' => array ( 0 => 'someValue1',
1 => 'someValue2', ),
)
i want to store each value into DB like this
myField = given value in text area
myCheckboxes1 = someValue1
myCheckboxes2 = someValue2
i tried code like this
<?php
$field = $_POST['myField'];
$checkValue1 = $_POST['myCheckboxes.(0)'];
$checkValue2 = $_POST['myCheckboxes.(1)'];
echo "$field"; //getting this output correctly
echo "$checkValue1"; //empty output
echo "$checkValue2"; //empty output
?>
(I'm reposting my comment as an answer since it seems that it solved the problem)
To see why you get empty values use var_dump($_POST) to see the full contents of the $_POST array (that is an useful debug technique for PHP).
You can get several outcomes:
Maybe the checkboxes are not grouped as an array, then you have a problem when you defined its name in the HTML.
Maybe there are grouped but with empty values, then you have a problem with the associated inputs or in the onsubmit() event.
Okay, so generally I wouldn't have a problem doing this and it would be fairly straight forward, but not so much this time.
Here is the relevant code in my controller:
// In order to properly build the form url, we need to include the
// category and product to the view
$this->data = array(
'category' => $category,
'product' => $product
);
// Load the product model and get editable values from the database
$this->load->model('productadmin/products_model');
$productInformation = $this->products_model->get_product_data($product);
// Modular code! Use variable-variables to prevent having to write multiple lines of code
// when we start returning more information from the data
foreach ( $productInformation as $variable => $value )
{
$this->data[$variable] = $value;
}
Now, ideally, I should be able to access $product, $category and any variables returned from the products model. Doing a print_r, I get the following:
Array ( [category] => 1 [product] => 1 [0] => Array ( [id] => 1 [product_name] => Duff ) )
Notice how what was generated by the foreach statement is contained in it's own array. The easiest solution, would be to know how to access that second array from the view, just by passing $this->data.
If that's not do-able, what can I change that would assign the model's associative values inside the data array without creating another array inside of it?
The model simply returns key, value pairs from a get_where statement.
You should use an associative array for your data before it is passed to the view. Try changing this lines:
foreach ( $productInformation as $variable => $value )
{
$this->data[$variable] = $value;
}
with this:
foreach ( $productInformation as $variable => $value )
{
$this->data['product_information'][$variable] = $value;
}
And then in your view you can access your product information using $product_information variable.
Note: I am assuming that you're passing your data to the view using:
$this->load->view('your_view', $this->data);
I cannot seem to get CI's session library to function the way I want it to. Essentially, I am storing 2 different categories of data within the sessions. The data within the 2 categories may contain the same value. Right now my attempt to add a key => value pair to the session is failing, as it is only allowing 1 key => value pair to be associated with the array. It overrides itself each time I do a post.
$arr = array(
'favorite_products' => array(),
'viewed_products' => array()
);
$arr["favorite_products"][] = $fav_id;
$this->session->set_userdata($arr);
This is what the array looks when I print_r it:
Array ( [favorite_products] => Array ( [4f1066c2b7fff] => 1648406 ) [viewed_products] => Array ( ))
Am I doing something wrong, or is this just the way CI's session library works?
Make sure you are destroying your session between attempts, but this code should work just fine...
$arr = array(
'favorite_products' => array(),
'viewed_products' => array()
);
$arr["favorite_products"][] = $fav_id;
$arr["favorite_products"][] = 033333; // another id
$this->session->set_userdata($arr);
should give you...
Array (
[favorite_products] => Array (
[0] => 1648406,
[1] => 033333
),
[viewed_products] => Array ()
)
If you are trying to do this between requests...
// if it doesn't already exist in the session, create an empty array.
if( ! ($favorite_products = $this->session->get_userdata("favorite_products")))
{
$favorite_products = array();
}
$favorite_products[] = "new id or info";
$this->session->set_userdata("favorite_products", $favorite_products);
I've just began using CakePHP and I have a question. I'm creating sort of a Task Manager system.
I have a 'Tasks' table, and this table has a user_id field for assigning a task to a user.
When I baked the application, the generated form has a user_id which was automatically linked to the users table, but I want to show the user's username instead of the user_id - how would I go about doing this?
Also, I need for when the form is saved, it saves the user_id into the field and not the username - So it needs to display the username for ease of use but save the user_id.
I'm still new to this whole Model and Controller so I'm not sure how I'd manage to do this. Thanks.
Update:
Here's where I am so far. In my jobs add.ctp I have added this code to help generate the form field:
foreach ($users as $user):
$options_array[$user['id']] = $user['username'];
endforeach;
print_r($options_array);
echo $this->Form->input('user_id', $options_array);
Unfortunately the print_r gives me:
Array ( [2] => 2 )
And not the desired output:
Array ( [2] => Dan )
$users is defined in the job_controller.php as so:
$users = $this->Job->User->find('list');
$clients = $this->Job->Client->find('list');
$stages = $this->Job->Stage->find('list');
$this->set(compact('users', 'clients', 'stages'));
I'm not sure what I'm doing wrong here.
Dan
If you are only looking for the id and username, then using the list should be cool, but you have to specify the username field.
$users = $this->Job->User->find('list', array('fields' => array('User.username'));
That should return the array as such:
array (
[1] => 'username_for_id_1',
[2] => 'username_for_id_2',
... all the other records...
[114] => 'username for id_114'
)
If you need more information from the User table, then you should use the ->find('all').
$users->Job->User->find('all');
// $users is now:
// array (
// [0] => array (
// 'User' => array(
// 'id' => 1,
// 'username' => 'username',
// ... the rest of the fields
// )
// Other affiliated models will be included with the 'User' if
// you haven't set 'recursive' = -1
// )
// )