I am creating a very basic photo uploading site that will allow users to:
1) select a file to upload
2) choose an category from a drop down
3) tag the photo with names
Users will then be able to search using their name to find pictures.
Since there will be situations where the uploading user will want to tag multiple people who appear in a single photo, is there a simple way to allow them to type names separated by commas and then have PHP create unique entries in the database? If not, what's the best procedure for having multiple text fields to accomplish this?
Thanks
chance
If I understand you, there are a couple of ways.
First, you can, as you've said, have a single text field, comma separated. In this case, the PHP after the form is submitted can simply explode the string by ",".
Alternatively, you can have an array of input items using e.g. "inputName[]" as the input name. Once this is submitted, PHP will create it as an array of items which can be looped through.
Once that's done, you do whatever you need to with the data.
There is a php function called explode() that you can use to turn a comma delimted string into an array. For example, if the people to be tagged are separated by a comma you can use $people = explode(',',$names); Where $names is the string that the use typed. Then you will have an array where each item is a tagged person that you can iterate over and do what you need to with it.
Related
I've created a search while typing method using this demo:
https://codeforgeek.com/2014/09/ajax-search-box-php-mysql/
But now i'm stuck with processing the results.
I want to receive back a list of data (that works) but also per dataline an id which should not be visible, but
which i can use as post data in the submit.
I probably have to change the json array and put the id in it, but i then i don't know how to seperate the data in the post form and use the id as hidden id.
To clearify my example: I have a table with strings (can be duplicate) and unique number id's.
I want the list to contain the strings and i want to submit with just the id.
I also found a lot of simular projects all looking a bit different, i especially like the looks of this one, creating a hovering results box and not changing anything to my form.
use json_decode on result
json_decode($json) will give you object in return
json_decode($json,true) vill give you array in return
I have a 5 level multidimensional array. The number of keys in the array fluctuates but I need to store it in a database so I can access it with PHP later on. Are there any easy ways to do this?
My idea was to convert the array into a single string using several different delimiters like #* and %* and then using a series of explode() to convert the data back into an array when I need it.
I haven't written any code at this point because I'm hoping there will be a better way to do this. But I do have a potential solution which I tried to outline below:
here's an overview of my array:
n=button number
i=item number
btn[n][0] = button name
btn[n][1] = button desc
btn[n][2] = success or not (Y or N)
btn[n][3] = array containing item info
btn[n][3][i][0] = item intput type (Default/Preset/UserTxt/UserDD)
btn[n][3][i][1] = array containing item value - if more than one index then display as drop down
Here's a run-down of the delimiters I was going to use:
#*Button Title //button title
&*val1=*usr1234 //items and values
&*val2=*FROM_USER(_TEXT_$*name:) //if an items value contains "FROM_USER" then extract the data between the perenthesis
&*val3=*FROM_USER(_TEXT_$*Time:) //if the datatype contains _TEXT_ then explode AGAIN by $* and just display a textfield with the title
&*val4=*FROM_USER($*name1#*value1$*name2#*value2) //else explode AGAIN by $* for a list of name value pairs which represent a drop box - name2#*value2
//sample string - a single button
#*Button Title%*val1=*usr1234&*val2=*FROM_USER(_TEXT_$*name:)&*val3=*FROM_USER(_TEXT_$*date:)&*val4=*FROM_USER($*name1#*value1$*name2#*value2)
In summary, I am seeking some ideas of how to store a multidimensional array in a single database table.
What you want is a data serialization method. Don't invent your own, there are plenty already out there. The most obvious candidates are JSON (json_encode) or the PHP specific serialize. XML is also an option, especially if your database may support it natively to some degree.
Have a look at serialize or json_encode
The best decision for you is json_encode.
It has some advantages for json_encode beside serialize for storing in db.
taking smaller size
if you
must modify data manually in db there will be some problems with serialize, because this format stores size of values that has been serialized and modifying this values you must count and modify this params.
SQL (whether mySQL or any other variant) does not support array data types.
The way you are supposed to deal with this kind of data in SQL is to store it across multiple tables.
So in this example, you'd have one table that contains buttonID, buttonName, buttonSuccess, etc fields, and another table that contains buttonInputType and buttonInputValue fields, as well as buttonID to link back to the parent table.
That would be the recommended "relational" way of doing things. The point of doing it this way is that it makes it easier to query the data back out of the DB when the time comes.
There are other options though.
One option would be to use mySQL's enum feature. Since you've got a fixed set of values available for the input type, you could use an enum field for it, which could save you from needing to have an extra table for that.
Another option, of course, is what everyone else has suggested, and simply serialise the data using json_encode() or similar, and store it all in a big text field.
If the data is going to be used as a simple block of data, without any need to ever run a query to examine parts of it, then this can sometimes be the simplest solution. It's not something a database expert would want to see, but from a pragmatic angle, if it does the job then feel free to use it.
However, it's important to be aware of the limitations. By using a serialised solution, you're basically saying "this data doesn't need to be managed in any way at all, so I can't be bothered to do proper database design for it.". And that's fine, as long as you don't need to manage it or search for values within it. If you do, you need to think harder about your DB design, and be wary of taking the 'easy' option.
I'm using smarty for work with html and faced one problems: I have table of different elements (few select and many input). I need to check the values of all elements in the table. With input no problem ($('input[type="text"]')), but how to check in one time select and input values.the problem is that the smart does not provide any additional options for {html_options}. May be there is some AND structure for jQuery selectors?
I see two possible routes here:
Extend your selector with add, e.g.
$('input[type="text"]').add('select').doSomething()
Use a common class for all input and select elements, e.g.
$('.my_input_class').doSomething()
Of course this assumes that you are using common properties/methods on your selection afterwards. For instance, you'd be able to call val() to retrieve the values.
If you want to add the same functionality to two selectors you can this approach..
$('input[type=text] , select').addClass('classname')
You can use a comma to add the same functionality to multiple selectors..
I'm trying to find out if its possible to send several different form values thru one textarea.
So instead of have one text input field for first name, one for last name, one for street address etc. I want to be able to add all info in one text area and seperate each value with a line break.
Each value need to be inserted in different columns in the assign table in the database.
It should be done i php.
Any pointers or tips of how to achieve this?
Thanks,
Fredrik
I think it should work with
$array = explode("\n", $text);
so you have all values in an array.
It is certainly possible - though I would question the user experience of doing it this way and the amount of extra work you are giving yourself. Remember that users are generally not used to having one big textbox for everything - and you will need to trust that they do actually put the line break in, which I guarantee they won't.
In any event, you could take the content and then split it on \n using preg_split with a regular expression. I think the following regular expression would work
$lines = preg_split("/(\R+)/", $textbox_content)
I am looking best solution in PHP for a search form which has multiple fields and person can also choose operation on that field. Here is how it will work for example. Also allow me to set what operations are possible on field without hard-coding. There will be many fields on page i am explaining on few on them.
Scenario examples :
Lets field "age" there can be three scenario. age can be greater than
or equal to or less than given field. So on page for a field person
will be able to select operation type from drop-down
"equal,greater,lesser" and then specify value.
Similarly for subject it can have following scenarios. subject equals
selected, or in multiple selected subjects, or not in selected
subjects.
I don't wanna hard-code solution for each field, i want something that's more flexible with which i can add or remove fields later without changing major part of code.
Was able to resolve it with collaborated code between PHP and Javascript. Created array in PHP with name of field, then what type of operations it support and other optined needed.
Then in jQuery hide display and restrict field options based on options that i output from PHP array.