Extract values from a textbox to show in an option value - php

Hello I have a box with values 2012-21-04. What I would like to do is use 3 select options to display year, month, and day. I would use php to extract each value. How would i approach this?

If you always have the same type of string to work with, just split the strings, using "-" as delimiter:
$arr_date = explode("-",$date); // $date being "2012-21-04"
$arr_date will be an array of three elements containing the three seperated numerical values
Regards,
STEFAN

Related

storing 1,2,3,4 numbers with comma into DB

I was used implode function for an array to insert the value into DB.
$day1=implode(',',$day);
echo "day:".$day1;
$day have a value of array like {100,100}; now i change this like 100,100 string to insert into the table. because I can't store array directly into the table.if any option instead of implode() to load the array into DB please post that.
else help me which datatype is used to store in DB . i cann't use int because of commas.
Under the assumption you are using MySQl, you should be utilizing the TEXT data type, because this data type offers no assumptions on what size your insert will be. Now, of course if you want to calculate a fixed length, feel free to use varchar, but because of the unknown width of your numbers, you would have to overallocate regardless.
https://dev.mysql.com/doc/refman/5.5/en/blob.html
Storing it as a string with numbers delimited by a comma is an adequate storage format, and should work fine, as you are dealing with numbers there would be no conflict when retrieving the data and then exploding the value back into an array using the same comma delimiter.
The implode() returns the array items concatenated to a single string (http://php.net/manual/en/function.implode.php).
Your database field needs to be of a text type (char, varchar, text) to store the value.
In this situation you have to convert array into json and store in db.
ex:
$data=("a","b","c");
$json_encode=json_encode($data);
then after retrieved from db convert back to array.
ex:
$json_decode=json_decode($data,true);
$array = array('lastname', 'email', 'phone');
$comma_separated = implode(",", $array);
echo $comma_separated; // lastname,email,phone
// Empty string when using an empty array:
var_dump(implode('hello', array())); // string(0) ""

how to use php method like "explode" in sql

I am trying to get some data, and in the table, there is a field named "sysload". However, it is a var(string) type. The data in it is like "0.0, 0.2, 0.5",three numbers split by comma. However, in the sql, I only need the last number(in this example:0.5) to compare in "where". So how can I use it ? My code:
$termquery=mysql_query("SELECT a.terminal FROM terminal_server_log a
inner join
(
SELECT terminal, MAX(timestamp) timestamp
FROM terminal_server_log
group by terminal
) b on (b.terminal=a.terminal and a.timestamp=b.timestamp)
WHERE explode(',', sysload)[2]>$number");
The last line is the most important one, i want to compare with '$number', but it seems i cannot use 'explode'. Thanks
You can probably use regular expressions to match the last element of your sysload column: https://dev.mysql.com/doc/refman/8.0/en/regexp.html#function_regexp-instr
Look also at CAST() function, as the extracted substring should be converted to a numeric value to allow comparison with a number.
PHP functions will not work inside SQL queries.
You can remove this filter from your query and filter at the php side, or you can make a custom explode function using some mysql string functions.
In this link has an example: (I didn't check if it's working)
Equivalent of explode() to work with strings in MySQL

Inserting an array into a mysql database column

I am trying to insert values from a multi select drop down in a form to a mysql db column. For example: The drop down would have one or more choices chosen then when the form is posted it would insert the data into one column in a mysql db. I'm stuck at how to insert the data.
If you want to insert in single row then you can use implode() to generate comma separated data, or you can do json_encode() and add to your colum.
Say you get the data as
$data = array("one", "two", "tree");
// output one, two, three
$insert_data = implode(",", $data);
or
$insert_data = json_encode($data);
Thats for inserting data in single column. While retrieving you can do explode() or json_decode() to get the return data and can use them in the multi-select again.
If you want one row for each item then just loop through the array and add them
you can turn the array into a single string with http://us1.php.net/function.implode
$comma_separated = implode(",", $array);
set the type of the column to string, then use the function serialize($array) to convert the array into a string. When you want to get the string back to an array, use unserialize($string)
A couple of things to think about:
If there is a one to many relationship - it shouldn't be in one column, look into multiple tables and changing your database structure.
If you really want to pass in an array you will need to convert it to string using the php built in function implode, then using the built in function explode to retrieve the column from the db
$arr = array('val1','val2');
$string = implode(',',$arr);
//Do db insert
//Do db retrieve
$arr = explode(',',$string);
http://php.net/function.implode

SQL - Inserting multiple row values into a single column

I need help on a method of inserting values into a single column on different rows.
Right now, I have an imploded array that gives me a value such as this:
('12', '13', '14')
Those numbers are the new IDs of which I wish to insert into the DB.
The code I used to implode the array is this:
$combi = "('".implode("', '",$box)."')"; // Where $box is the initial array
The query of which I plan to use gets stuck here:
mysql_query("INSERT INTO studentcoursedetails (studentID) VALUES
One option would be to repeat this, but I cant, because the array will loop; there might be 3 IDs, there might be 20.
A loop doesn't seem right. Any help would be appreciated.
For inserting more than one value into a table you should use (value1), (value2) syntax:
$combi = "('".implode("'), ('",$box)."')";
PS: This feature is called row value constructors and is available since SQL-92
Can you not do something like this:
for($x = 0; $x < count($box); $x++)
{
mysql_query("INSERT INTO studentcoursedetails (studentID) VALUES ($box[$x]);
}
This will work directly on your array, insert a new row for each value in $box and also prevent the need to implode the array to a comma delimited string
Storing ids as a comma delimited string might initially seem like a simple model but in the long term this will cause you no end of trouble when trying to work with a non-normalised database.
Some flavors of sql allow compound inserts:
insert into studentcoursedetails (studentid) values
(1),
(2),
(3),
If you are using MySQL, you can insert multiple values in a single sentence:
sql> insert into studentcoursedetails (studentID)
> values (('12'), ('13'), ('14'));
So, you just need to build that string in PHP and you are done.
You can still create the statement via implode. Just don't use VALUES; use SELECT instead
$combi = " ".implode(" UNION ALL SELECT ",$box)." "; // Where $box is the initial array
mysql_query("INSERT INTO studentcoursedetails (studentID) SELECT " . $combi)
The SELECT .. union is portable across many dbms.
Note on the IDs - if they are numbers, don't quote them.
Check to see if there is a variant of the mysql_query function that will operate on an array parameter.

How do I compare two array's values?

I have 2 arrays one retrieved from a database (saved results) and the other from an xml (new results)
$fromDB = array('123','124','524','15','616');
$fromXML = array('123','124','524','15','818');
I want to compare those two and see which values are old (fromDB) and which are new (fromXML) so to insert the old value in a different table.
How can i achieve this?
The array_diff function is what you're looking for.
Take a look at the array_diff() function

Categories