ADODB5 (PHP): Dynamically Build Table Columns - php

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.

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;
}

Deleting complex array element

So I want to delete an array element from a JSON array based on an id in a sub-array. I know it sounds weird. Here's an example of the array. I want to delete the entire array [0] based on the [dealer][id] array where the [id] = 20220 in this example.
Array
(
[results] => Array
(
[offset] => 1
[length] => 15
[data] => Array
(
[0] => Array
(
[dealer] => Array
(
[id] => 20220
[name] => apple
)
)
)
)
}
In reality there are a lot more elements in the [results] array. I'm not sure how to go about it.
Any help is greatly appreciated!
Loop thru data key first then check if dealer id matches the searched id
$id = 20220;
foreach ($array['results']['data'] as $key => $value) {
if ($value['dealer']['id'] == $id) {
unset($array['results']['data'][$key]);
}
}
use array_filter,
$array['results']['data'] = array_filter($array['results']['data'], function($v){return $v['dealer']['id'] != 20220;});

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');

Can you retrieve nested array by name in php

I have an array like this:
(
[data] => Array
(
[account_id] => 1
[description] => my asset
[value] => Estimate
[value_amount] => 85000
[type] => Vehicle
[owner] => Array
(
[app_id] => 123
[percent] => 100
)
)
)
Clearly I can loop through the array and pull out the nested owner array that way, but is there something similar to array_column that will get the entire owner nested array without having to loop ?
Use the indexes, no function necessary.
$owner = $array['data']['owner']
or..
$percent = $array['data']['owner']['percent']
In php it's call associative array which mean index will not numeric it can be anythink.
So you can retrieve data like
<?php echo $array['data']['owner']['app_id']; ?>

Update an array with fields from matching array

I have Array1 that pulls all of the entries for a Wordpress custom post type, and I have Array2 from another table that shows if that post type has been seen by a specific user. If the ID of Array1 & the PageID of Array2 are the same, then add the [Started] & [Finished] fields from Array2 to Array1. Basically I want to combine them to form Array3. I have tried many different solutions with array_combine but can't get the result I'm looking for.
Array1 (
[0] => stdClass Object ( [ID] => 75 [post_title] => Test Training Video )
[1] => stdClass Object ( [ID] => 80 [post_title] => Test 2 )
[2] => stdClass Object ( [ID] => 85 [post_title] => Test 2 ) )
Array2 (
[0] => stdClass Object ( [PageID] => 75 [Started] => 1 [Finished] => 1 )
[0] => stdClass Object ( [PageID] => 80 [Started] => 1 [Finished] => 0 ) )
Array3 (
[0] => stdClass Object ( [ID] => 75 [post_title] => Test Training Video [Started] => 1 [Finished] => 1 )
[1] => stdClass Object ( [ID] => 80 [post_title] => Test 2 [Started] => 1 [Finished] => 0 )
[2] => stdClass Object ( [ID] => 85 [post_title] => Test 2 ) )
Something like this?
$array3 = array();
foreach( $array1 as $arr1 )
{
foreach( $array2 as $arr2 )
{
if( $arr1["ID"] == $arr2["PageID"] )
{
$array3[] = array( $arr1["Started"], $arr2["Finished"] );
}
}
}
array_combine() is not the right function for this.
In your case, you need to use a foreach-loop to loop over the values of your first array and compare them with your second array.
You also need to rearrange array 2, so that you can easily access the values for started and finished using the pageID as key:
$pageMap = array();
foreach($array2 as $entry) {
$pageMap[$entry['PageID']] = array('started' => $entry['Started'], 'finished' => $entry['Finished']);
}
Then you can do:
$combined_array = array();
foreach($array1 as $post) {
if(!isset($pageMap[$post['ID']])) continue; // do nothing if there are no started/finished entries.
$combined_array[$post['ID']] = array_merge($post, $pageMap[$post['ID']]);
}
You could try a first loop to make your array "ID" match and then combine both arrays
foreach( $array2 as $key=>$value )
{
$value['ID'] = $value['PageId'];
unset($value['PageId']);
$array2[$key] = $value;
}
$array3 = array_merge_recursive($array1,$array2);
After trying the posted solutions I decided to make the change in my $wpdb->get_results sql statement instead of using 2 different sql statements and then combining the 2 different arrays. With help from this post - SQL query to join two tables despite the other table may not have values, I was able to use the following to get the desired result.
$oneBigQuery = $wpdb->get_results("SELECT a.ID, a.post_title, b.PageID, b.Started, b.Finished
FROM $wpdb->posts AS a
LEFT JOIN exampleTableName AS b
ON a.ID = b.PageID
AND b.UserID = 3
WHERE a.post_type = 'custom-post-type'
");

Categories