post multidimensional array with Zend Form - php

i created a zend form which is able to generate subform with the elements like: items[0][price], items[0][count]; items[1][price] etc.
in each row of a table i need to display certain data. so, in my view i created a html table and tried to iterate through the 'items' and insert certain form element in certain column of a table.
<table>
<?php foreach($this->form->getSubform('items') as $item) :?>
<tr>
<td>just some data like product name</td>
<td><?php echo $item->getElement('price');?></td>
<td><?php echo $item->getElement('count');?></td>
</tr>
<?php endforeach;?>
</table>
after that, each form element item do NOT have array name anymore, as i was expecting, but just like 'items' or 'count' in each row, so multidimensional array will not be returned.
if i generate the form in view (echo $this->form;), then form element names are ok, but how should i add some data in the table then..? preferably it should be done in view.

Instead of adding elements always with price and count, since you're generating the field dinamically, try to name them price1, count1, price2, count2, and so on. Then you can retrieve them in your table using their new name (with the count appended).

Prepend your form decorator stack with the PrepareElements decorator. This decorator will loop through your form and set the appropriate belongsTo names.

Related

two tables joined, select option , how to fill a value automaticly

hello i have two tables joined , and i'm trying to fetch some values into a select box, making it so that when a user selects one of the options , when submitting the form it will post the other values associated
<select name="matricula[]">
<?php
// use a while loop to fetch data
// from the $all_categories variable
// and individually display as an option
while ($matricula = mysqli_fetch_array(
$result,MYSQLI_ASSOC)):;
?>
<option value="<?php echo $matricula["matricula"];
// The value we usually set is the primary key
?>">
<?php echo $matricula["matricula"];
// To show the category name to the user
?>
</option>
<?php
endwhile;
// While loop must be terminated
?>
</select>
in this code i need to add the value automatically and insert on form submission : "tipocomb" it has already got a table named "combustivel" created with the values and when i use json encode it displays the two arrays connected in the view source
it's just a question of how to echo the "tipocomb"?
If you want to do something like that you need js to post form that has hidden inputs with related data. Simpler solution would be when you read the post in your controller fetch related data and do further logic there.

Dynamically added form elements

This question is basically for my understanding, so kindly guide me. I dont require any code but just the approach for solving this type of problem.
I am using jquery to dynamically add form elements. For example, I want user to enter his favourite movie. I have given him an add button to add more movies. Now if user adds 10 times for 10 movies, how will i save this data to database ? i mean i will have only one column for movie but user is entering 10 (some might add 100) so how is this data stored ?
I have asked this question to basically understands how to save data when the form elements are dynamically added ? shall i save them to column movies and seperate them by commas or something else ?
There are many approach to solve this problem. One solution to this is AJAX. say example you are using php
1) Sort form data with each use press button; jQuery Form.
2) a php function to process sql queries.
3) call php function in ajax in success method.
4) store value in database.
In the form elements that you add, you would make the name= value into an array, so that the various identical items could be processed.
For example, suppose you are cloning a table row. Suppose this is the template row that you are cloning:
<tr>
<td>
Movie Name: <input type="text" name="mov[]" />
</td>
</tr>
After the form was posted, the data would appear something like this:
POST DATA:
Array
(
[mov] => Array
(
[0] => (the initial, template row would have no value)
[1] => Gladiator (whatever user typed in)
[2] => Troy (the next movie entered by user...
The code I used to view the POST output above looks like this:
If (empty($_POST)===false) {
echo '<pre>';
echo 'POST DATA:<br />';
print_r($_POST);
echo '<br />*********************************************************<br /><br />';
echo 'UPLOADED FILES:<br />';
print_r($_FILES);
echo '</pre>';
die('Dying immed inside IF EMPTY($_POST)===FALSE...');
}

Adding Nested form rows PHP/Javascript

Overview:
I am creating a dynamic web invoicing system,
Most of the page are textareas and I have used tables to nest as they will post data into seperate tables after the form is submitted. When the "Nested" Add a Row is selected, it should add a row inside with form field that has details on which row and which position so it can be gathered in a for loop when it is posted.
Image:
Problem:
Adding a row in the circled area does not add it in the last row, It add's it as it is in the image. Additionally it only works on the first item, the second add a row does not even register a click.
I am unsure how to get the variables to create the form name[][] in this particular index size and the parents index number (located in javascript line 2-3)
Relevent Code:
Javascript
$("#additemrow").click(function(){
var currentListItem = $(this).length;
var currentJobItem = $(this).parents('#items').index('#items');
currentListItem++;
$("#listitem:last").after(/*Blank Form Row*/);
bind();
})
PHP
<table id="items">
<tr class="item-row"><td>/*form elements*/</td></tr>
<tr class="list-row">
<td colspan=5>
<table class="itemlist">
<?php
$itemCount = 0;
foreach($jobListItem as $items) {
foreach($items as $item) {
if($problem['item_id'] == $item['item_id']) {
?>
<tr id="listitem">
<td class="list-item">
<div class="delete-wpr">
<textarea class="item" name="item[/*A PARENT ITEM NUMBER*/][<?php echo $itemCount;?>][item]"></textarea>
<a class="deleteitem" href="javascript:;" title="Remove row">X</a>
</div>
</td>
</tr>
<?php
}
$itemCount++;
}
}
?>
<tr>
<td colspan="5"><a id="additemrow" href="javascript:;" title="Add a row">Add a row</a></td>
</tr>
</table>
//NEXT ITEMLIST GOES HERE
</table>
EDIT: Foreach Loop is just used to get variables from a array.
Please let me know if you need more details or bits of code as im not sure if I have given enough information.
Regarding the problem where your row gets insertet at the top:
Like Bertrand Lefort already said: in JQuery you can append nodes to the end of another (parent) node by calling parent_node.append(child_node) (more information here)
You can also add elements before/after another element by calling another_element.before(element) / another_element.after(element) or (be aware of reversed syntax!) element.insertBefore(another_element) / element.insertAfter(another_element)
Regarding the problem where only the first "add a row" registers a click:
You're selecting by id. Id's are (or should be) unique, so when you call $("#additemrow").click(function(){ ... }) the onClick-function will only be attached to the first element that matches the given id (as far as I remember).
If you want to attach the same onClick-function to multiple elements use class instead. However, if you do so, you got to make sure you insert the new element relative to the clicked element (eg. by using .parent(), .siblings() or similar functions).
Regarding your problem #2, I don't really know what you're trying to do, but this is what I noticed:
currentListItem and currentJobItem seem to be unused in their scope, since you are declaring them as local variables but not using them
.parents('#items').index('#items') is redundant because .parents('#items') already selects only the parent elements that match '#items'. If you want to get the index of the matched element, use index() (without parameters, see jquery API for more)
Some other notes:
If you have the time (and the need of a clean project) I recommend using template engines like Smarty to separate your logic from the output. This improves the readability of your code and also makes it easier to find/eliminate bugs.
I hope this helped a little bit. If you provide more information on what you're trying to do and what doesn't work as you expext, I will try to provide further help and update my answer.
You could add an id to the parent html element, then in your click handler:
$("#additemrow").click(function(){
$("#parent").append(/*Blank Form Row*/);
})

codigniter how to make a drop down table from database values

I need to create a drop-down menu in Codigniter and the values will pulled from a database table. The data contains a group of movies, I need to be able to populate the data into the menu.
How should I do this?
just iterate through your data and create a select list
<select name="movie_id">
<?php foreach ($rows as $row): ?>
<option value="<?= $row['id']; ?>"><?= $row['name']; ?></option>
<?php endforeach; ?>
</select>
This will obviously be different based on your table schema
According to Codeigniter documentation
see this url
Code Igniter - form_dropdown selecting correct value from the database
Codeigniter form_helper getting database rows to be values in select menu
The first parameter will contain the name of the field, the second parameter will contain an associative array of options, and the third parameter will contain the value you wish to be selected. You can also pass an array of multiple items through the third parameter, and CodeIgniter will create a multiple select for you.
Your admin controller should have something like
$data['selected'] = $this->salary_expectation->get_salary_selected();
According to this, the admin view should be like this
<?php echo form_dropdown('salaries', $salaries, $selected_value); ?>

How to store field Id in a row?

I write a web site with jquery and lot of ajax request to get data for table and ask data modifications with PHP/MySql on server side.
Currently, I use id attribute to store the id of the field of the table (which is an autoincrement int value).
And it works fine.
BUT I have recently learned that id should be unique (and start with a letter...).
AND I have different tables that could have the same id value (for different sql table)
Then I am not html (nor xhtml) compliant...
How could I correct my code ?
By using .data() function of jQuery ?
An hidden html element with the id as value (<span class="id">3</span>) ?
Other solution ?
Additional informations:
I have wrote a widget to manage my tables.
To add a new row, I do:
row = $('<div class="row" id="'+item.id+'"/>');
[...] // I add fields to my row
row.appendTo(tableData);// tableData is the html element where rows are
When a field element is changed, I trigger an event to the table that will ask the modification to the server with the right id:
$(e.target).closest(".row").attr("id")
If you are able to use jQuery 1.4.3 or greater look at using the html 5 data-* attributes. jQuery 1.4.3 will automatically use those data- attributes and place them in the .data() collection on the element.
Example:
<table>
<tr data-rowId="1">
</tr>
</table>
$("tr:first").data("rowId") would print 1
This method would also allow you to store json objects as well.
<table>
<tr data-row='{"Id" : 1, "Name": "Smith"}'>
</tr>
</table>
And than in your data()
var row = $("tr:first").data("row")
You can reference row.Id and row.Name
You can prefix your id with the table name :
<div id="mytable_1234"></div>
It's easy to extract the table name and the id from the field and this is HTML compliant.
var values = $(element).attr('id').split('_');
// values[0] is the table name and values[1] is the id.
You can use any other separator if you're already using underscores in your table names.
you can also use jQuery metadata .....
Its awesome to store data in html
Instead of using id use data-id and use the .data('id') (on that element) to retrieve it with jQuery.
I think... I understand your question - multiple data tables, all with autoincrement ids?
My solution would be pre-appending the ID with a letter (like you said) to differentiate it.
Example would be a dataset for 'cars' I would do:
<table>
<tr id="cars_1">
...
</tr>
<tr id="cars_2">
...
</tr>
<tr id="cars_3">
...
</tr>
</table>
Later if you have another table, bikes, you would do:
<table>
<tr id="bike_1">
...
</tr>
<tr id="bike_2">
...
</tr>
<tr id="bike_3">
...
</tr>
</table>
Your end result would be unique ID's while keeping the ID db value in mind, so you would do a simple check (if cars, then do this, etc), then to seperate the prefix (cars from the id 1, you would use something like the PHP expolode() fn).
Hope that clarifies it, and that I understood your question.

Categories