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');
Related
This question already has answers here:
Is there a function to extract a 'column' from an array in PHP?
(15 answers)
Closed 6 months ago.
I'm wondering what the most efficient way to reduce this array down by a level, ideally without loops in PHP. It's a result from mysqli_fetch_all().
Array
(
[0] => Array
(
[ID] => 648546
)
[1] => Array
(
[ID] => 648552
)
[2] => Array
(
[ID] => 650046
)
[3] => Array
(
[ID] => 652732
)
[4] => Array
(
[ID] => 652738
)
[5] => Array
(
[ID] => 652756
)
)
The result I would like is
array(648546,648552,650046,652732,...)
The simple query example it comes from is something as easy as:
SELECT mytable.ID FROM mytable WHERE status =1
This should do it for PHP 5.5+
$result = array_column($array, 'ID');
You can use array_map():
$new_array = array_map(function($v){return $v['ID'];}, $old_array);
You might try this:
SELECT GROUP_CONCAT(mytable.ID)
FROM mytable
WHERE status = 1
GROUP BY status
It should return 1 row with the ID as a comma separated list.
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;
}
I have an SQL recordset that I wish to convert into an array that I can then change the ages without changing the db data.
I wish to be able to reference the ages by name. For example my source data is simply two columns with name and age.
// Initialise the pupils array
$query = "SELECT name, age FROM class";
$result = pg_query($query) or die('Query failed: ' . pg_last_error());
$pupils = pg_fetch_all($result);
print_r($pupils);
This gives me:
Array
(
[0] => Array
(
[name] => Bob
[age] => 3
)
[1] => Array
(
[name] => Sue
[age] => 5
)
[2] => Array
(
[name] => Mary
[age] => 7
)
)
What I want is:
Array
(
[Bob] => 3,
[Sue] => 5,
[Mary] => 7
}
Use array_column() to build this array:
$pupils = pg_fetch_all($result);
$students = array_column($pupils, 'age', 'name');
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']);
}
I'm wanting to know how to dynamically generate the table columns in adodb5.
Here's my current code:
<?php
$sql = "SELECT id FROM customers";
$query = $db->Execute($sql);
$rows = $query->GetRows();
$fields = $query->fields;
foreach($rows as $row) {
print_r($row). '<br />';
}
?>
The output I recieve is:
Array
(
[id] => 280
)
Array
(
[id] => 1024
)
Array
(
[id] => 474
)
Array
(
[id] => 476
)
Array
(
[id] => 564
)
Array
(
[id] => 569
)
Array
(
[id] => 594
)
Array
(
[id] => 385
)
Array
(
[id] => 304
)
Array
(
[id] => 700
)
Array
(
[id] => 285
)
Array
(
[id] => 205
)
Array
(
[id] => 536
)
Array
(
[id] => 140
)
I'm literally just wanting it to grab all the columns in the query and build the table headers. So basically, I will have a table that has all the "ID"'s under one columns. I actually want the columns labeled, so in case I have more than one columns, for example, ID, Name, Date, Comment; it would dynamically know how to make the headers for each columns. Is this someone that can help me with?
You first need to modify your query that gets the data rows:
select * from customers
or if you only want certain columns:
select id, name from customers
you can then iterate through the rows and see you have an object or an associative array (I forget which) that allows you to lay out your table. For the headers you already have the $fields so just iterate to form the header:
foreach ($fields as $name) {
echo $name;
}
Hope this helps.