Iterate throw checkbox list, even if not checked, in Laravel - php

I have a table of items, and if the user selects an item I need to know how many units are assigned to this item. So I created a check box list coming back with the selected items ids and another one for units.
<td>{{$item->name}}</td>
<td>{{Form::checkbox('itemchks[]', $item->id)}}</td>
<td>{{Form::text('units[]','0')}}</td>
The problem is that, unless the user checks all items we get 2 different-size lists.
For ex, when choosing 3 items, and assigning them some units values, the itemchks come back with these ids
["4","15","23"]
but the units list returns
["15","0","18","0","0","0","0","0","20"]
so I can't know, exactly, each id and its correspondent units.
I'm using laravel 4.2
Any ideas will be appreciated.

Instead of letting the browser set the key in your units[] array you could do that yourself using the $item->id:
<td>{{$item->name}}</td>
<td>{{Form::checkbox('itemchks[]', $item->id)}}</td>
<td>{{Form::text('units['.$item->id.']','0')}}</td>

Related

Check for duplicates in arrays with mysql

I have a column in my table that will always contain an array of three items (they come from 3 selects with the same items that the user chooses from). I need to make a query that will return if any user has selected the same combination of 3 items that another user has selected too, doesn't matter the order he chose them in.
So I have 3 selects with the same content in them, the user needs to chose an item from each one (it's validated so all of them are different) and I need to check if that combination has been chosen before or not.
In my database the array looks like this:
a:4:{i:0;s:8:"item1";i:1;s:8:"item3";i:2;s:8:"item5"}
The database will have many users so I'm afraid that checking this will be very resource intensive, what is the best way to approach it?

PHP- Get index/associated value of option selected in dropdown box

I want to show values in a dropdown box (either from an array or database - please advise on which option is method)
Then
when a user selects a value in dropdown box, i want to have and get its associated index value (like 1, 2).
For example: dropdown box shows values:
"Car"
"Bicycle"
If user selects "Car", when i get dropdown selected value, i should get 1, similarly for "Bicycle" i get 2 .. and so on.
Please advise easy and simplest method to implement this
Thanks
You can use two methods:
1) Set up prior to display a code that holds associative array with key -> value i.e: 1 => Car (you can keep it in config file if it doesn't change frequently, you can pull it from database or you can keep it in some other form: serialized, file etc.) and use it when the submitted form is being processed.
2) Use array with key and value with the same string i.e: Car => Car and when the form is processed you will have value right away. This solution has some limitations and be troublesome with using more words or other characters that need to be sanitized.
I would advise option 1, you will have to set up the list before and use it after form has been processed but it allows more freedom and its less maintenance.

MySQL editable sort index in CakePHP

I am trying to implement a way to sort elements in a list based on the order they were added from oldest to newest. This would be easy to do in MySQL using an ORDER BY on a created DATETIME. The problem is I then want to be able to move elements up and down the list and have that order saved in the database, but also make sure that new elements get put on the end of the list.
I have thought of using an INT index and just increment that for each new item that is added to the list. Then when an existing item is moved up or down in the list, swap the index numbers. Does this sound like the best way to achieve this result? If not, would anyone be able to provide some insight of a better way. Thanks
So I ended up solving this by using an INT field in the MySQL table called order. When new items are added, the order field is just incremented from the previous largest order in the table. When items are moved up or down in order, all other items orders are shifted up or down accordingly so that the order numbers are always continuous. On delete, orders are also shifted down.

Combining multiple rows or results with the same title to form drop down menus with PHP and MySQL

So I am picking up a project that was quit halfway through by the last guy so that I could get some more practice with PHP and databases. I have run into a problem, and I am sure it is common enough that there is a standard solution, but I am unable to find one.
The db I am working with has 4,600, so reorganizing is out of the question. It is a db of liquers for a wholesaler. Here is what the results page looks like currently:
What I am trying to set it up so the results are returned in list form, with only one title and dropdown menus for the different sizes/prices of products that looks like this:
The problem is that there are multiple entries in the db for each product. In this example there are 3, while some have 1, and some have 2.
I am really not sure how to go about this, and any help would be greatly appreciated. Thank you.
I'm not sure about the PHP syntax, but pseudocode here's what you could do:
allProductsReturnedFromMySQL = QueryYourDatabaseForAllProducts()
Hashtable[productId, List[productSizes]] dropDownsByProduct;
Hashtable[productId, commonProductInformation] uniqueProducts;
foreach (product in allProductsReturnedFromMySQL) {
if product.productId not in uniqueProducts
then add it with the product information that does not vary
if product.productId not in dropDownsByProduct
then add it with an empty list
append the size of this product to the corresponding list in dropDownsByProduct
}
After that little bit of logic you'll have all your unique products with the common properties for each one, and a way to fetch the corresponding sizes drop down. If you wanted to do this purely in SQL to minimize the data that's transferred, you could do something like this:
-- this would get you your products
select distinct id, property1, property2 from product
-- this would get you your drop downs by product
select id, size from product order by id
You can then build the same drop down hashtable by iterating through the second result set.
I'm not sure if this is the best way, but I've always approached this by altering the query so that it is sorted by product name. Then as you iterate through the rows, check to see if the product name matches the one you just processed. If it's the same, then this row is a different size of the same project.

php dynamic checkboxes

Currently I have a form that submits an image with textfields such as
title, description and another field that autoincrements for imageID, another
area for the actual file , called vfile, and *** another part that has
3 checkboxes and a text field.
Everything works fine, and this is what it does. Submits the data to a database so that it can pull the information to a page on the website.
The only part I am trying to update is:
The 3 checkboxes and the textfield.
Lets say the first checkbox reads: Apples
The second : Oranges
The Third: Grapes
And in the other category is a blank textfield that if you add something, it would add it to a category called "Other".
So the database design has 4 fields: 1 - apples, 2 - oranges, 3 - grapes, 4 - other.
When I click a checkbox, it would add checked to the database under the correct one, either apples, oranges, or grapes.
If I add a field to the textbox such as: Bannanas, then it would add "Bannanas" to the database field vother and show that in the database.
This is all fine, but what if the next picture has all 4 items, plus another one? Such as if the next picture had Apples, Oranges, Grapes, Bannanas, and Plums?
How could I have the "Bannanas" other category, change into a checkbox category that could be chosen for the next pics when I go to the add images page next time.
So that when I go to the second picture to submit, it would give me the option of not just 3 checkboxes, but 4 checkboxes now, that I could check the first 4, "Apples, Oranges, Grapes, Bannanas" and then put Plums in the other category.
Basically upon submit it takes what is in the other feild and addes a new category to the database, which is then displayed in the array of checkbox choices and it is removed from the Other Category now, for it is a checkbox. (thus it would not want the value left in the old field, for it would keep creating the same category over and rewriting the old data possibly.
Anyway, any suggestions?
Thanks in advance.
(It sounds like this is more of a database design question and not a php question, but I may be misunderstanding what it is you are looking for advice on)
It sounds like you are saying that these attributes (Apples, Orange, etc) are stored as columns in your main table; but the situation you are describing sounds more like Tagging. Typically you would maintain a list of things that get tagged (your images), and a separate list of all possible tags (Which would be a table containing the rows : Apple, Orange, Grape). Your UI has the option to select from pre-existing tags (rows in the tag table) or add a new tag using the "Other" box. New tags would be added as a new row to the tag table. Since tags and tagged items have a many-to-many relationship you would create a third table (called a join table) that stores keys of tagged items and keys of tags; that way you can select either side of the relationship easily : get all the tags for a given item; get all the items with a given tag.
Does that help?
(EDIT : for comments)
So, Activities sounds like the list of Tags. If I want to show a form with checkboxes for all the Activities I can query the activities table for them. Each of those checkboxes can have a name attribute or something that captures the ID of the row that its bound to.
Also I would select from the join table the ids of the tags that my currently viewed image has selected. As I am populating the checkbox list I can check this result set to see if the id of the checkbox I'm putting on the page is in the list of tags for the image.
To store this back to the db on submit, the easiest thing is probably to (in a transaction) delete all the entries for the image from the join table and replace them with new entries based on the state of the check boxes in the form.
Drop the apples, oranges and grapes columns.
Create a second table with two fields: imageID and itemtype.
Don't make any of the two a key. Now you can list as many different types of items for each image as you need. It will be comparatively expensive to get the list of item types in use from the database but unless you have millions of items this shouldn't be a problem. (Mikeb is suggesting to keep the list of item types in use in a separate table to speed this up.)
The alternative is to dynamically add a column to the first table for each item type you encounter. This will create a very sparse table. I wouldn't do this.
You need to change your database schema to support an infinite number of attributes.
You should move your list of attributes from a set of fields in the record to a list of related records in a new table. This new table requires 2 columns. The first is the primary key from the existing table so you can establish the relationship between the tables. The second is the attribute field ('Bananas' or 'Apples' or 'Plums', etc.) You can have as many records in the attributes table as you like for each record in your main table. You could also have no attribute records if none are checked.
This kind of relationship between two tables is called a one-to-many relationship.

Categories