Insert array elements separately into database - php

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.

Related

How to store MYSQLI query as multidimensional array in this format [duplicate]

This question already has answers here:
get array of rows with mysqli result
(2 answers)
Closed 4 years ago.
I'm trying to query database A and store results as an array, which I will then insert into database B. I can move simple data directly, but because of the complexity of this particular query (30+ left joins), I have to store it as an array and insert it in a separate query. The examples below contain just two fields, to keep things simple and focus on the core issue.
I have working code to insert an array stored as follows:
$data = array(
array( // record 1, or row 1
"1",
"Value for field 1.2"
),
array( // record 2, or row 2
"2",
"Value for field 2.2"
),
array( // record 3, or row 3
"3",
"Value for field 3.2"
),
// etc...
);
Unfortunately, I can't get my first query to store the data like this. I have used PHP for years, but I never messed with arrays before. I'm not sure what I'm doing wrong. Using "print_r($data), this is what the results look like for how I need the array to be (This is what the $data variable looks like with print_r() ):
Array ( [0] => Array ( [0] => 1 [1] => Value for field 1.2 ) [1] => Array ( [0] => 2 [1] => Value for field 2.2 ) [2] => Array ( [0] => 3 [1] => Value for field 3.2 ) )
My query to create the array doesn't match this pattern. A "print_r($new_array)" command yields this:
Array ( [0] => Array ( [id] => 37 [post_title] => test1 ) [1] => Array ( [id] => 38 [post_title] => test2 ) [2] => Array ( [id] => 35 [post_title] => test3 ) [3] => Array ( [id] => 42 [post_title] => test4 ) [4] => Array ( [id] => 44 [post_title] => test5 ) [5] => Array ( [id] => 46 [post_title] => test6 ) )
This is my MySQL query to put the data into an array:
$test_sql = "SELECT id, post_title FROM wp_posts where post_type LIKE 'test'";
$resultTest = mysqli_query($con, $test_sql);
//$new_array[] = $row;
while ($row = mysqli_fetch_assoc($resultTest)) {
$rows[] = $row;
}
If I understand what is going on, the array I'm creating is making key value pairs for a multidimensional array, but the format I need is not key value, just an array of values. Since I already have the insert query working, I would prefer to use a select query that stores the data in an array without the key value pairs, if that is possible, but I am open to all suggestions.
Thank you in advance for your kindness and help!
All you need to do is change fetch function to fetch_row:
while ($row = mysqli_fetch_row($resultTest)) {
$rows[] = $row;
}
Or to fetch_array with MYSQLI_NUM as second argument:
while ($row = mysqli_fetch_array($resultTest, MYSQLI_NUM)) {
$rows[] = $row;
}

Change default numeric associative array keys to "number" only without affecting array values

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 to insert multi rows from multi dimension array mysql

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.

Codeigniter:query results into associative array

I have two tables in database
1.main_category fields (id,main_name);
2.sub_category fields(id,main_id, sub_name)
Here main_id is used for connecting two tables from this i want to get the result like the following array
Array
(
[CCTV] => Array
(
[0] => Array
(
[id] => 1
[main_id] => 4
[name] => first
)
[1] => Array
(
[id] => 3
[main_id] => 4
[name] => second
)
[2] => Array
(
[id] => 4
[main_id] => 4
[name] => second
)
)
[Security Camera] => Array
(
[0] => Array
(
[id] => 5
[main_id] => 5
[name] => first
)
[1] => Array
(
[id] => 6
[main_id] => 5
[name] => second
)
[2] => Array
(
[id] => 7
[main_id] => 5
[name] => second
)
)
)
Here the key of the array are main_name field which is from the main_category table and the associative array for each key contains the rows which matches the condition
where main_category.id=sub_category.main_id
I want db query to achieve the above result.is it possible with a join query?
This is the structure of the join query you can change it in your own requirement.
function myfun($id){
$query = "select main_cat.*, sub_cat.* from main_category main_cat
Join sub_category sub_cat
ON main_cat.id = sub_cat.main_id
where main_cat.id = $id";
$data = $this->db->query($query);
return $data->result_array();
}
Hope this will help to you.
try to understand the query and make alteration as required.main_cat as table 1 and sub_category as table two . already you have gave half solution sub_category.employee_id = main_cat.employee_id
$this->db->select("main_cat.id,main_cat.main_id,main_cat.name,sub_category.id,sub_category.main_id,sub_category.sub_name");
$this->db->from('main_cat');
$this->db->join('sub_category', 'sub_category.employee_id = main_cat.employee_id');
$this->db->where('id', $id);
$query = $this->db->get();
return $query->result();
Hello You use codeignitor so use codeignitor mysql query structure it's best practise in future
$this->db->select("main_cat.id,main_cat.main_id,main_cat.name,sub_category.id,sub_category.main_id,sub_category.sub_name");
$this->db->from('main_cat');
$this->db->join_using('sub_category', 'employee_id');
$this->db->where(tablename.'id', $id);
$query = $this->db->get();
return $query->result();
employee_id is common in both table so use join_using other wise you use join like this
$this->db->join('sub_category', 'sub_category.employee_id = main_cat.employee_id');

create sub Array on join instead of new entries

I have 4 lectures where each lecture has it's own name:
jbc2014_lezingen
Then for each lecture there will be questions, in this case 2 for each lecture:
jbc2014_vragen
I use this query:
$query = "SELECT * FROM jbc2014_lezingen
INNER JOIN jbc2014_vragen
ON jbc2014_lezingen.id=jbc2014_vragen.lezing_id";
This results in something like:
[0] => Array
(
[id] => 1
[lezing_naam] => lezing 1
[lezing_id] => 1
[vraag] => foobar?
)
[1] => Array
(
[id] => 2
[lezing_naam] => lezing 1
[lezing_id] => 1
[vraag] => foobar?
)
etc.
So the array size is based on the amount of the questions, not the amount of the lectures.
I would like something like:
[0] => Array
(
[id] => 1
[lezing_naam] => lezing 1
[lezing_id] => 1
[questions] Array (
[0] => [vraag] => foobar?
[1] => [vraag] => foobar?
)
)
Where the questions are in an array (vraag means question).
How can this be done?
Later on I need the multiple choice answers in an array inside the questions as well. But I think that won't be hard after having this one fixed.
The closest thing you could do is something like this -
SELECT jbc2014_lezingen.id as lezing_id,
GROUP_CONCAT(vraag) as vraags
FROM jbc2014_lezingen
INNER JOIN jbc2014_vragen
ON jbc2014_lezingen.id=jbc2014_vragen.lezing_i
GROUP BY jbc2014_lezingen.id
and then just get the values with
explode(",",$row['vraags']);
or create another array with something like this
foreach($raw_result as $key => $value){
$new_array[$key]['lezing_id'] = $value['lezing_id'];
$new_array[$key]['vraags'] = explode(",",$value['vraags']);
}

Categories