I have some code which pulls from multiple tables of a MS SQL database, arranges the data and outputs an array, I then need to use that array to populate a worksheet telling someone what items are needed on a day to day basis. Right now the output looks like the below, where the fields are ID, Name, Date, number (they are asterisk deliminated because some names include commas).
74*xxxx*2017-06-04*1
74*xxxx*2017-06-05*1
74*xxxx*2017-06-06*2
74*xxxx*2017-06-07*2
74*xxxx*2017-06-08*2
74*xxxx*2017-06-09*2
74*xxxx*2017-06-10*2
75*yyyy*2017-06-05*123
75*yyyy*2017-06-06*111
75*yyyy*2017-06-07*124
75*yyyy*2017-06-08*107
75*yyyy*2017-06-09*194
75*yyyy*2017-06-10*195
I am trying to get them to merge them into a single value per ID so that I can print it out to a grid to look similar to:
| ID | NAME | 06-04 | 06-05 | 06-06 | 06-07 | 06-08 | 06-09 | 06-10 |
| 74 | xxxx | 1 | 1 | 2 | 2 | 2 | 2 | 2 |
| 75 | yyyy | - | 123 | 111 | 124 | 107 | 194 | 195 |
my intent was to merge the array values into a single array with the dates as the keys, the (most recent) code I was trying is:
$schedule[] = $PID . "*" . $program . "*" . $date . "*" . $num;
$grid = array();
foreach ($schedule as $item ) {
$ex = explode('*', $item);
$grid[$ex[0]] = array(
'name' => $ex[1],
$ex[2] => $ex[3],
);
}
with the intent that that would merge them into a single array line to look like:
[74] => Array ( [name] => xxxx [2017-06-04] => 1 [2017-06-05] => 1 [2017-06-06] => 2 [2017-06-07] => 2 [2017-06-08] => 2 [2017-06-09] => 2 [2017-06-10] => 2)
after which I could just use it line by line with $grid[$i][1] etc to pull the value and print to each cell. I've tried other methods than the above, that is the only one I presently have the code snippet for and I've only been able to get it to keep the last value I add on a per ID basis.
I'm certain there is a piece I am missing or simply not understanding, if anyone could offer some input it would be appreciated.
If I understood your requirement correctly, the below code will give the desired array.
$schedule[] = '74*xxxx*2017-06-04*1';
$schedule[] = '74*xxxx*2017-06-05*1';
$schedule[] = '74*xxxx*2017-06-06*2';
$schedule[] = '74*xxxx*2017-06-07*2';
$schedule[] = '74*xxxx*2017-06-08*2';
$schedule[] = '74*xxxx*2017-06-09*2';
$schedule[] = '74*xxxx*2017-06-10*2';
$schedule[] = '75*yyyy*2017-06-05*123';
$schedule[] = '75*yyyy*2017-06-06*111';
$schedule[] = '75*yyyy*2017-06-07*124';
$schedule[] = '75*yyyy*2017-06-08*107';
$schedule[] = '75*yyyy*2017-06-09*194';
$schedule[] = '75*yyyy*2017-06-10*195';
$grid = array();
foreach ($schedule as $item ) {
$ex = explode('*', $item);
$grid[$ex[0]]['name'] = $ex[1];
$grid[$ex[0]][$ex[2]] = $ex[3];
}
print_r($grid);
And out put is
Array
(
[74] => Array
(
[name] => xxxx
[2017-06-04] => 1
[2017-06-05] => 1
[2017-06-06] => 2
[2017-06-07] => 2
[2017-06-08] => 2
[2017-06-09] => 2
[2017-06-10] => 2
)
[75] => Array
(
[name] => yyyy
[2017-06-05] => 123
[2017-06-06] => 111
[2017-06-07] => 124
[2017-06-08] => 107
[2017-06-09] => 194
[2017-06-10] => 195
)
)
Related
I am trying to load dynamic table data, my URI is;
example.coma/admin/view/form/<form_id>
My model query is;
public function getRecords($table, $form_id) {
$this->db->select('*');
$this->db->from($table);
$this->db->where('form_id', $form_id);
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->result_array();
}
}
This returns an array of data, I need to build a HTML table based on this array.
I'll show an example of two different arrays returned by the query.
Array 1.
(select * from members where form_id = 123)
Array
(
[0] => Array
(
[id] => 104
[member_no] =>
[firstname] => Peter
[lastname] => Keys
[address] => 17 main road
[email] => P3TER#HOTMAIL.CO.UK
[postcode] => UK123
[city] => London
[telnum] => 123123123
[fk_form_submission_id] => 123
)
)
Array 2.
(select * from orders where form_id = 123)
Array
(
[0] => Array
(
[colour] => blue
[type] => shirt
[age] => 34
[size] => medium
[quantity] => 2
[discount] => Y
[posted] => N
)
)
What I want to achieve is display a vertical table to display the dataset. Obviously each table will have different row names, example below;
Table 1.
+---------------+-------+
| ID | 104 |
+---------------+-------+
| Member Number | |
+---------------+-------+
| First Name | Peter |
+---------------+-------+
| Last Name | Keys |
+---------------+-------+
| etc | etc |
+---------------+-------+
Table 2.
+--------+--------+
| Colour | blue |
+--------+--------+
| P Type | shirt |
+--------+--------+
| Age | 34 |
+--------+--------+
| Size | medium |
+--------+--------+
| etc | etc |
+--------+--------+
How can I set these table row names? Do I need to create another array of table headers and merge both arrays?
Any advice is appreciated.
I'm not sure I fully understand - but if you want to set the keys of the array dynamiclly you can do that with foreach loop as:
<table>
<tr><th>Key</th><th>Value</th></tr>
<?php foreach($res[0] as $key => $val)
echo '<tr><td>'. $key . '</td><td>' . $val . '</td></tr>'; ?>
</table>
Edit:
If you want to change the keys name to something more displayable I would recommend using another array for swap (most of the time it done for translation...).
$displayName = array("id" => "ID", "member_no" => "Member Number", "firstname" => "First Name" ..., "type" => "P type", ...);
foreach($res[0] as $key => $val)
echo '<tr><td>'. $displayName[$key] . '</td><td>' . $val . '</td></tr>';
You can also use array_combine but that will need to know which kind of keys you have...
Notice that this solution will work only if the display name are unique for all kind of keys
Here's a sample of data
Date | Source | Amount
-----------------------
01A | S1 | 12
01A | S4 | 2
01A | S7 | 134
02A | S1 | 126
03A | S4 | 10
02A | S7 | 0
02A | S1 | 3
02A | S4 | 4
02A | S7 | 5
The resulting array needs to look like:
Array
(
[01A] => Array
(
[S1] => 12
[S4] => 2
[S7] => 134
)
[02A] => Array
(
[S1] => 126
[S4] => 10
[S7] => 0
)
[03A] => Array
(
[S1] => 3
[S4] => 4
[S7] => 5
)
)
I've tried looking at pages such as PHP Adding multiple associative arrays to new array based on key, PHP multiple arrays to one associative array and Merge multiple associative arrays to a single array of associative arrays and playing around with code as below (not using the same data - but same theory) but it's not producing what I need. The code below just shows A and B
$array = Array();
$a=array( "ABC" => array("A"=>"RED","B"=>"GREEN"));
$b=array( "ABC" => array("C"=>"BLUE","D"=>"YELLOW"));
$array = $a + $b;
echo '<pre>';
print_r($array);
echo '</pre>';
KISS
$result = array();
foreach($rows as $row)
{
if(!isset($result[$row['Date']])) $result[$row['Date']] = array();
$result[$row['Date']][$row['Source']] = $row['Amount'];
}
Assuming you already have the data in the $input array, the code is as easy as:
$result = array();
foreach ($input as $row) {
$date = $row['Date'];
if (! array_key_exists($date, $result)) {
$result[$date] = array();
}
$result[$date][$row['Source']] = $row['Amount'];
}
If you don't have the data in $input but you fetch it from the database just replace the line:
foreach ($input as $row) {
with something that matches your data retrieval loop, f.e.:
while ($row = mysqli_fetch_assoc($result)) {
two tables Location and Routes. Location table contains following fields and values
id | name
1 | bangalore
2 | mumbai
3 | kolkatta
4 | delhi
Routes table contains following fields and values
id | source | desination
1 | 1 | 4
2 | 1 | 2
3 | 1 | 3
4 | 2 | 4
5 | 2 | 3
6 | 3 | 4
want to find all possible routes from source to destination like
bangalore-delhi
bangalore-mumbai-delhi
bangalore-mumbai-kolkatta-delhi
bangalore-kolkatta-delhi
please help me to achieve this result
Using your existing data from your two tables, you could LEFT JOIN columns source and destination from Routes meaning your INT values will match up to the Location.name column. Then make sure you order the data by source column for so it's easier to sort with PHP later on.
MySQL:
SELECT locStart.name as start, locFinish.name as finish FROM `routes`
LEFT JOIN location as locStart
ON routes.source = locStart.id
LEFT JOIN location as locFinish
ON routes.destination = locFinish.id
ORDER BY routes.source
Output:
array (
0 =>
array (
'start' => ' bangalore',
'finish' => 'delhi',
),
1 =>
array (
'start' => ' bangalore',
'finish' => 'mumbai',
),
2 =>
array (
'start' => ' bangalore',
'finish' => 'kolkatta',
),
//Etc....
Then add your DB results into multidimensional array using the Source name as the key.
$stmt = $pdo->query($query);
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
$newArr;
foreach ($result as $v) {
$newArr[$v['start']][] = $v['finish'];
}
Output:
array (
' bangalore' =>
array (
0 => 'delhi',
1 => 'mumbai',
2 => 'kolkatta',
),
'mumbai' =>
array (
0 => 'delhi',
1 => 'kolkatta',
),
'kolkatta' =>
array (
0 => 'delhi',
),
)
For outputting the new array, you can use a recursive function:
function recurse($newArr, $key=NULL) {
if($key !== NULL) {
foreach ($newArr[$key] as $k=>$v) {
echo '<li>Destination: ' . $v . '</li>';
}
echo '</ul>';
} else {
foreach ($newArr as $k=>$v) {
echo 'Source: ' . $k . '<br><ul>';
recurse($newArr, $k);
}
}
}
Output:
Source: bangalore
Destination: delhi
Destination: mumbai
Destination: kolkatta
Source: mumbai
Destination: delhi
Destination: kolkatta
Source: kolkatta
Destination: delhi
Please can I have some help on this. I'm trying to delete SQL row in a table that are not match the data in an array.
SQL Table:
| id | Name | No
------------------
| 1 | john | 14
| 2 | rui | 156
| 3 | ted | 148
| 4 | alex | 123
| 5 | depay | 56
Array:
Array
(
[0] => Array
(
[name] => john
[no] => 14
)
[1] => Array
(
[name] => ted
[no] => 148
)
[2] => Array
(
[name] => depay
[no] => 56
)
)
This may help you
[akshay#localhost tmp]$ cat test.php
<?php
$array = array (
array (
'name' => 'john',
'no' => '14'
),
array (
'name' => 'ted',
'no' => '148'
),
array (
'name' => 'depay',
'no' => '56'
)
);
$table = "some_table";
$sql = "DELETE FROM ".$table." WHERE (NAME,NO) NOT IN (". implode(",",array_map(function($_){ return sprintf("('%s',%d)",$_['name'],$_['no']);},$array)).")";
print $sql."\n";
?>
Output
[akshay#localhost tmp]$ php test.php
DELETE FROM some_table WHERE (NAME,NO) NOT IN (('john',14),('ted',148),('depay',56))
It would be much easier if you can return "id" aka primary key of the table as array. In that case you can just grab those ids and during delete operation exclude those from the equation.
$selectQ = 'select id from table_name'; // and no>10
$result = mysql_result($selectQ);
$ids = array();
while ($info = mysql_fetch_assoc($result)) {
$ids[] = $info['id'];
}
if($ids) {
$deleteQ = 'delete from table_name where id not in ('.implode(",", array_values($ids)).')';
mysql_query($deleteQ);
}
P.S : During select query make sure to include logic for finding those users. For example, who's "no" is greater than X.Otherwise this query in default will return all ids and eventually nothing will be deleted.
So I have an array like the following:
Array
(
[0] => Array
(
[user_id] => 684
[sec_id] => 2
[rank_id] => 1
[rank] => usr
)
[1] => Array
(
[user_id] => 693
[sec_id] => 3
[rank_id] => 5
[rank] => usr
)
)
And I have another array like this
Array
(
[0] => 2
[1] => 7
[2] => 27
)
I want the value of the second array to be added at the end of each arrays of the 1st array, and it should be multiplied. I mean, if I have 100 arrays in the first array, and 3 elements in the second array, I should have 300 in the resulting array.
Taking example of the above, I would like to have something as follows:
user_id | sec_id | rank_id | rank | menu_id
684 | 2 | 1 | usr | 2
684 | 2 | 1 | usr | 7
684 | 2 | 1 | usr | 27
693 | 3 | 5 | usr | 2
693 | 3 | 5 | usr | 7
693 | 3 | 5 | usr | 27
I tried with the following function, but it's not working.
function getR($arr_one,$arr_two) {
foreach ($arr_one as $k=>&$v) {
foreach ($arr_two as $x=>&$y) { $v['menu_id'] = $y; }
}
return $arr_one;
}
This is just making an array like this:
user_id | sec_id | rank_id | rank | menu_id
684 | 2 | 1 | usr | 27
693 | 3 | 5 | usr | 27
Means, it's just adding menu_id at the end of each element of the first array, but not multiplying. Any idea, I'm surely missing something.
Thanks guys.
function getR($arr_one,$arr_two) {
$new_arr = array();
foreach ($arr_one as $k=>$v) {
foreach ($arr_two as $x=>$y) {
$this_item = $v;
$this_item['menu_id'] = $y;
$new_arr[] = $this_item;
}
}
return $new_arr;
}
I'm not going to ask... but try this:
<?php
function crazy ($arr1,$arr2) {
foreach ($arr1 as $key=>$value) {
foreach ($arr2 as $value2) {
$nvalue=$value;
$nvalue[]=$value2;
$new[]=$nvalue;
}
}
return $new;
}
$arr1=array(array('user'=>1,'dude'=>2),array('user'=>2,'dude'=>3));
$arr2=array(2,7,27);
print_r(crazy($arr1,$arr2));
this is tested too, http://www.ideone.com/Of126
Without testing (eek!) I imagine something like this:
function getR( $arr_one, $arr_two )
{
$second_key = 0;
foreach ( $arr_one as $k => &$v )
{
$v['menu_id'] = $second_key++;
if ( 3 == $second_key ) $second_key = 0;
}
return $arr;
}
Presumably, you're passing the first array by reference? Not sure what $arr is that you're returning though...