I have some questions about handling with array concerning storing in mysql.
I stored an array (of numbers) in my mysql database as text Like: COLUMN_ARRAY: [2,3,4,5,6,7]
( including [ and ] )
My first question: is this a good method or can I work with that for my further steps? or are there any other better methods for storing array in sql column?
My second question: how could it be possible to pick every single element, or go throu all elements in the array with php?
because I want to make a query like if COLUMN_ARRAY includes the number 3 then do blablabla...
maybe you can give me some hint, idea, link or tutorial :)
EDIT:
Ok I did some change and changed everything to something like that:
How to store arrays in MySQL?
the only problem is now: how can I handle with an array (like [1,2,3]) in php - to handle with every single element in that?
Related
I just started working with JSON, so this is a hard way for me. I spent more than 15 hours trying to configure out how I should create and fill an array in PHP to get specified JSON format.
I need to get exactly this example1
{"players":["xZero","Lux"],"points":[{"player":"xZero","points":"20"}]}
and this
example2
{"players":["xZero","Lux"],"points":[]}
PHP will use foreach loops to fill data in example1 from database.
foreach($userdb as $users){
$output["users"][$i]=$users;
$i++;
}
This is just first half, I do not have any idea how to insert data in another half.
To get exact output like example1.
For example 2 I also don't have idea how to fill it out. I known for first half with users, but another half is mistery, like in example1.
As you can see, my experience with JSON is extremely poor. Also I'm not verry experienced with multidimensional arrays.
Thanks in advance for any help!
Based on your JSON your PHP array should look something like this:
array("players"=>array("xZero","Lux"),"points"=>array(array("player"=>"xZero","points"=>20)))
My best advice for you is to populate an array like this or create objects using classes and then using the json_encode() function.
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 am trying to build what I think is a multidimensional array in json through php and a connection to a mysql database.
Here's the output I am looking for:
[{"region":"Americas"},
{"region":"West","division":"Americas","revenues":"100000","headcount":"6"},
{"region":"East","division":"Americas","revenues":"1500000","headcount":"8"},
{"region":"South","division":"Americas","revenues":"1000000","headcount":"7"},
{"office":"San Francisco","location":"50 Kearny Street", "branch":"West"},
{"office":"Los Angeles","location":"200 Sepulveda","branch":"West"},
{"office":"New York","location":"Penn Street","branch":"East"},
{"office":"Philadelphia","location":"155 Trent","branch":"East"},
{"office":"New Jersey","location":"420 Broadway","branch":"East"},
{"office":"Atlanta","location":"39000 Parker","branch":"South"}]
This gives me the feed into a graphing program that creates the proper visualization. My database returns all the values in the JSON above (region, division, revenues, etc.) EXCEPT for "branch" which I had to hand code myself. I can't sure "region" again (because of the graphing algorithm) so I had to rename it to "branch." Under this current configuration, the "Americas" is the top node, with the "regions" underneath, and then the "branches" under each region as appropriate.
So far, I've only been able to build a basic JSON array using PHP and the json_(encode) function ; to get the output above, I edited the file by hand to make sure it would render properly in my graph, which it does.
The question I am looking to answer is: what is the fastest and easiest way to build the above array on the fly? Through a loop in PHP, I would imagine, but I can't get my head around what that would look like.
I probably left out information you need to help, so this is just to get the ball rolling.
Thanks!
Look at this topic: Looping a multidimensional array in php
You can loop through the array using the foreach construct
To give some background, I'm creating a web app using mostly jQuery. I scrape a number of values from a page on another domain; 3 arrays of integers and a date in string format. These values are then displayed on my page. Each of these arrays has 7 numbers.
It makes sense to me to store each array as a single entry in my MySQL database. My original idea was to store the values of each array as a JSON object and insert that into my entry in the DB from reading around SO this doesn't seem like the approach to take. I don't envisage having any need to sort the values at any stage in the future. I will just need the value of each integer and don't need to know how each integer in each array relates to one another.
Can someone point me in the right direction regarding the storing and retrieval of these values?
Usually arrays are kind of iffy to handle. In most cases arrays are values that I need to process later in the front end for the user with javascript (can't think of another use anyway). Therefore what I usually do is store them as text by using
$array = array('one', 'two', 'three');
$textToSaveinDB = implode(',', $array);
When I retrieve this you would just use the other way around
$textFromDB = "one,two,three";
$array = explode(textFromDB, ',')
Afterwards you can do whatever you need to do with the array. For example use JSON_encode() and send it to the user via ajax.
Maybe serialize() and unserialize() would help you. But I won't suggest this as the best approach.
I am new to php and am asking for some coding help. I have little experience with php and have gone to the php.net site and read couple books to get some ideas on how to perform this task.
There seems to be many functions and I am confused on what would be the best fit. (i.e. fgetcsv, explode(), regex??) for extracting data in the file. THen I would need assistance printing/display this information in orderly fashion.
Here is what I need to do:
import, readin txt file that is
delimited (see sample)
The attributes are not always ordered and some records will have missing attributes.
Dynamically create a web table (html)
to present this data
Sample records:
attribute1=value;attribute2=value;attribute3=value;attribute4=value;
attribute1=value;attribute2=value;attribute4=value;
attribute1=value;attribute2=value;attribute3=value;
How do I go about this? What would be best practice for this? From my research it seems I would create an array? multidimensional? Thank you for your time and insight and i hope my question is clear.
Seems like homework, if so best to tag it as such.
You will want to look into file(), foreach() and explode() given that it is delimited by ;
The number of attributes should not matter if they are missing, but all depends on how you setup the display data. Given that they are missing though, you will need know what is the largest amount of attributes to setup the table correctly and not cause issues.
Best of luck!
i would first use the file() method, which will give you an array with each line as an element. Then a couple of explodes and loops to get through it all,first exploding on ';', then loop through each of these and explode on '='.