My array is like:
Array (
[show_to_mentees] => 1
[show_profile] => 1
[job_title] => title
[mentor_org] => orgnization
[mentor_gradyear] => 2010
[mentor_sector] => Array (
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
[mentor_interest] => Array (
[0] => 2
[1] => 4
)
[mentor_degree] => 14
[career_summary] => summary
)
I have to insert the bold array values into a MySQL database, and that should be easily searchable.
Either I should insert a new row for each index or create a string of index values and save it in a single row.
I assume that the information in the array is about the 'mentor' itself and the key 'mentor_interest' show the interest of mentor in different sectors.
If above statement is right then, I assume/suggest that you have 'sectors' table in the database.
I would recommend separate tables mentor_sectors and mentor_interests. Also you would need to add each value separately with the mentor_id in another column for linking purpose.
Related
I have a form at http://97.74.37.64/ (visit it first) . It has only one textarea where visitor can fill upto 15000 number of mobile numbers. after submit it should be converted into an array (This one I have done).
Now, this array is like this below..
Array ( [0] => 9810000000 [1] => 9810000001 [2] => 9810000002 [3] => 9810000003 [4] => 9810000004 [5] => 9810000005 [6] => 9810000006 [7] => 9810000007
and so on..
Now, I want to change the key of the array to string "number" so it should be like
Array ( [number] => 9810000000 [number] => 9810000001 [number] => 9810000002 [number] => 9810000003 [number] => 9810000004 [number] => 9810000005 [number] => 9810000006 [number] => 9810000007
I want to do the above mentioned thing. Because I want to insert the mobile numbers into the MySQL table (one mobile number each row). This is the multiple insertion in MySQL table. My table name is srchlist with 2 fields id(its auto_increment & we don't need to mention or insert it) and number for which I am making array keys as number. So finally it should be inserted like below
id | number
---------------
1 9810000000
2 9810000001
3 9810000002
and so on entire array values should be inserted..
What about just organizing these values into an array of arrays? That way you can bulk insert them into your table or insert them as part of a loop if need be:
array(
array("number" => 1), array("number" => 2), array("number" => 3)
)
You can do something like this:
Controller:
// Array ( [0] => 9810000000 [1] => 9810000001 [2] => 9810000002 [3] => 9810000003 [4] => 9810000004 [5] => 9810000005 [6] => 9810000006 [7] => 9810000007)
$number = array('9810000000','9810000001','9810000002','9810000003',,'9810000004','9810000005','9810000006','9810000007');
foreach($number as $row)
{
$number1[]['number'] = $row;
}
$this->M_admin->numbers($number1); //call modal function
Modal:
function numbers($number)
{
// insert into db as batch
$this->db->insert_batch('numbers', $number);
}
how can i insert each element in the array into a separate column in the database and loop it for each array index ?
for example from array index 0,
insert 1 in column s1
insert 3 in column s2
insert 5 in column s3
insert 11 in column s4
format of array i have now
Array
(
[0] => Array
(
[0] => 1
[1] => 3
[2] => 5
[3] => 11
)
[1] => Array
(
[0] => 1
[1] => 3
[2] => 5
[3] => 13
)
)
thank you
Using a simple foreach loop you can achieve the insertion-
foreach($your_arr as $val){
$sql = "INSERT INTO `table_name` (s1, s2, s3, s4) VALUES ('$val[0]', '$val[1]', '$val[2]', '$val[3]')";
mysqli_query($your_db_conn, $sql);
}
I hope this will help you.
I am getting data from an api (that I cannot query agains, just get lump of data), and then I need to query against those data like I would do using database. Only It would be great if I could do it recursively.
Data example
[0] => Array
(
[id] => 1
[url] => https://domain.com/api/1.0/item/1/
[name] => some_item
[category] => some category
[created_by] => Array
(
[id] => 1
[screen_name] => tomino
)
[current_user_domain_access] => Array
(
[is_active] => 1
[is_administrator] => 1
)
[alerts_enabled] => 0
)
(much shortened version)
I receive an array of objects like that and then I need to select/filter/search by values.
Something like this
SomeModel::find(['category'=>'some category','current_user_domain_access' => ['is_administrator' => 1]]);
Is that something that would be possible in PHP? I was thinking about flattening the array, but then there might be key conflicts
1) select data : You can select data by (array_name->id),(array_name->url) and so on...
2)Filter : add conditions according to requirement
3)search : in_array(),array_search
I'm wondering if there is an easy way to match array key to logo_id?
If I cannot find a way to do this, I will need to use array search which can become quite slow with an array of 200 items. Right?
p.s. this is result returned by mysqli fetch result call. Maybe this can be modified to provide array which I need?
Array
(
[0] => Array
(
[logo_id] => 1
[logo_name] => beeline
[logo_level] => 1
[logo_image_path] => logos/1.png
[logo_value] => 2
[logo_hints] =>
)
[1] => Array
(
[logo_id] => 2
[logo_name] => geocell
[logo_level] => 1
[logo_image_path] => logos/2.png
[logo_value] => 4
[logo_hints] =>
)
[2] => Array
(
[logo_id] => 3
[logo_name] => google
[logo_level] => 1
[logo_image_path] => logos/3.png
[logo_value] => 5
[logo_hints] =>
)
[3] => Array
(
[logo_id] => 5
[logo_name] => coca cola
[logo_level] => 1
[logo_image_path] => logos/5.png
[logo_value] => 2
[logo_hints] =>
)
)
Did I explain it good? phh, sorry for bad wording.
this is result returned by mysqli fetch result call. Maybe this can be modified to provide array which I need?
Yes. I assume you mean mysqli_result::fetch_all. Use mysqli_result::fetch_row in a loop instead and construct your array manually with whatever keys you like to.
I have four tables: followers, users, mixes, songs I am trying to get all the mixes from all the followers of one user, I have that part figured out, but I also want to get the songs from each of those mixes, currently my query is giving me results but each result is for one song on the mix, rather than an array of songs within each result for one mix ... any help would be amazing, my sql skills aren't the greatest and I have spent a lot of time trying to figure this out!
my current query is:
SELECT followers.following_id, users.id, users.user_username, mixes.id, mixes.mix_created_date, mixes.mix_name,songs.song_artist
FROM followers, users, mixes,songs
WHERE followers.user_id = 46
AND users.id = followers.following_id
AND mixes.user_id = followers.following_id
AND mixes.id > 0
ORDER BY mixes.mix_created_date DESC
LIMIT 10
the current result is (from running this through a cakephp custom query)
Array
(
[0] => Array
(
[followers] => Array
(
[following_id] => 47
)
[users] => Array
(
[id] => 47
[user_username] => someguy
)
[mixes] => Array
(
[id] => 45
[mix_created_date] => 2012-07-21 2:42:17
[mix_name] => this is a test
)
[songs] => Array
(
[song_artist] => Yo La Tengo
)
)
[1] => Array
(
[followers] => Array
(
[following_id] => 47
)
[users] => Array
(
[id] => 47
[user_username] => someguy
)
[mixes] => Array
(
[id] => 45
[mix_created_date] => 2012-07-21 2:42:17
[mix_name] => this is a test
)
[songs] => Array
(
[song_artist] => Animal Collective
)
)
as you can see the mix id's are the same, I am trying to get the songs to be an array inside of each result like :
Array
(
[0] => Array
(
[followers] => Array
(
[following_id] => 47
)
[users] => Array
(
[id] => 47
[user_username] => someguy
)
[mixes] => Array
(
[id] => 45
[mix_created_date] => 2012-07-21 2:42:17
[mix_name] => this is a test
)
[songs] => Array
(
[0]=>array(
['song_artist'] => Yo La Tengo
),
[1]=>array(
['song_artist'] => Animal Collective
)
)
)
Really hoping this can be done with just one sql statement! thanks in advance!
You can use the SQL join command to make multiple queries together..
Use this...
sql_join
first a note: it looks like you have a missing condition. according to the above query, every song in songs table will be joined with every result possible. probably there should be a condition similar to the following added: (column names can be different based on your tables):
...
and mix.song_id=songs.song_id
...
as for your question: I don't know php so i regard mysql alone: I don't think it is possible to do it with mysql. mysql returns rows in the result set and each row can contain a single value in each column. to add a group of values (song names) in one column, they must be concatenated (and that is possible: Can I concatenate multiple MySQL rows into one field?), and later you split them back in your php script. this is not a good idea as you will need to choose a separator that you know will never appear in the values that are concatenated. therefore I think its better to remove the songs table from the query and after getting the mix id, run a second query to get all songs in that mix.