I have list of departments, against each department there is a button which is UP and another button DOWN
When you click on UP Button, It calls the function up..
Public function actionUP($id, $sort_order){
$allDeps = Department::model()->findAll($criteria);
$depArr = array();
foreach ($allDeps as $department){
$temp = array();
$temp['id'] = $department->id;
$temp['sort_order'] = $department->sort_order;
$depArr[] = $temp;
}
Now if i var dump this array depArr i am getting an array..
array (size=17)
0 =>
array (size=2)
'id' => string '142' (length=3)
'sort_order' => string '1' (length=1)
1 =>
array (size=2)
'id' => string '141' (length=3)
'sort_order' => string '2' (length=1)
2 =>
array (size=2)
'id' => string '144' (length=3)
'sort_order' => string '5' (length=1)
3 =>
array (size=2)
'id' => string '140' (length=3)
'sort_order' => string '6' (length=1)
4 =>
array (size=2)
'id' => string '139' (length=3)
'sort_order' => string '3' (length=1)
5 =>
array (size=2)
'id' => string '143' (length=3)
'sort_order' => string '4' (length=1)
I have list of departments, position is actually sort_order and there are up and down button against department, up buttons calls for actionUP which sends the up?id=143&sort_order=4
If a user clicks on up button against position 4 its position should changed to 3, and the department at 3 should go to 4 and ALSO UPDATE INTO DATABASE..
I am using Yii 1.1 PHP framework
Flow:
Search using
array_search('whateverID', array_column($depArr, 'id'));
Now you have the index and the sort order, you just need to search for sort order - 1 now.
array_search($depArr[x]['sort_order']-1, array_column($depArr,
'sort_order'));
You now have both element's index, swap the sort order or unset and set again. Hope this helps.
As you can see I have a key called dateTime which holds dates. What I need to do is add all the entries together for each date and return the array. So I simply got one dateTime per date with all the entries of that day added together. I do not know the dateTime inputs ahead of time as I have a crawler that constantly inserts data into my database.
How would you go about doing that in the most efficient way? I imagine I would have to do a foreach loop and somehow check if the key value (dateTime) changes from the previous key value. And then create a brand new array that I return
An example of the array is as follows:
array (size=130)
0 =>
array (size=2)
'dateTime' => string '2015-09-01' (length=10)
'entries' => string '225' (length=3)
1 =>
array (size=2)
'dateTime' => string '2015-09-01' (length=10)
'entries' => string '218' (length=3)
2 =>
array (size=2)
'dateTime' => string '2015-09-01' (length=10)
'entries' => string '217' (length=3)
3 =>
array (size=2)
'dateTime' => string '2015-09-01' (length=10)
'entries' => string '225' (length=3)
4 =>
array (size=2)
'dateTime' => string '2015-09-02' (length=10)
'entries' => string '231' (length=3)
5 =>
array (size=2)
'dateTime' => string '2015-09-02' (length=10)
'entries' => string '220' (length=3)
6 =>
array (size=2)
'dateTime' => string '2015-09-03' (length=10)
'entries' => string '223' (length=3)
7 =>
array (size=2)
'dateTime' => string '2015-09-03' (length=10)
'entries' => string '237' (length=3)
8 =>
array (size=2)
'dateTime' => string '2015-09-03' (length=10)
'entries' => string '220' (length=3)
So it returns an array like this
array (size=130)
0 =>
array (size=2)
'dateTime' => string '2015-09-01' (length=10)
'entries' => string '660' (length=3)
1 =>
array (size=2)
'dateTime' => string '2015-09-02' (length=10)
'entries' => string '451' (length=3)
2 =>
array (size=2)
'dateTime' => string '2015-09-03' (length=10)
'entries' => string '680' (length=3)
Edit With the help of you guys, I ended up doing the following to get the right format.
function prepareArrayForGraphDates($array){
$results = [];
$finalResult = [];
foreach ($array as $value) {
if (!isset($results[$value['dateTime']])) {
$results[$value['dateTime']] = 0;
}
$results[$value['dateTime']] += $value['entries'];
}
$keyNames = array_keys($results);
for ($x = 0; $x < sizeof($results); $x++) {
$finalResult[$x]['dateTime'] = $keyNames[$x];
$finalResult[$x]['entries'] = $results[$keyNames[$x]];
}
return $finalResult;
}
Use the fact that array keys are unique...
$results = [];
foreach ($array as $value) {
if (!isset($results[$value['dateTime']])) {
$results[$value['dateTime']] = 0;
}
$results[$value['dateTime']] += $value['entries'];
}
You can use an array like a set or a dictionary, using the date as the key.
$data = [
[
'datetime'=>'2015-09-02',
'entries' => 220
]
// ... other entries snipped ...
];
$results = [];
foreach ($data as $date => $entry)
{
// if the date has already been recorded with an initial value, add to it
if (array_key_exists($date, $results) {
$results[$date] += $entry
} else {
// the date is new; add it to the results and set its initial value
$results[$date] = $entry;
}
}
I am parsing an XML file and creating two arrays: one of the XML tags ($tags), and the other as the values for the tags ($values). As it parses, it adds the tags and values as it goes, when it's done, I implode the arrays and put them into a MySQL statement:
$sql = "INSERT INTO everything ($tags) VALUE ($values)";
This works fine until I have repeating tags, and then the SQL statement doesn't work....
Is there a way to find the first repeated word in the $tags array and split it at that word (Keeping the tags that follow it) and also split the $values array at the same index that $tags was split, so that the information stays in the same order?
So ultimately converting something like this:
INSERT INTO everything (AmazonOrderID,MerchantOrderID,ShipmentID,MerchantFulfillmentID,PostedDate,AmazonOrderItemCode,SKU,Quantity,Principal,Commission,AmazonOrderItemCode,SKU,Quantity,Principal,Commission,AmazonOrderItemCode,SKU,Quantity,Principal,Commission,FBA) VALUE ('1','1','D','A','2015','64','OX','1','18','-2','64','WA','1','23','-2','29','WAG','1','49','77','97');
Into something like:
INSERT INTO everything (AmazonOrderID,MerchantOrderID,ShipmentID,MerchantFulfillmentID,PostedDate,AmazonOrderItemCode,SKU,Quantity,Principal,Commission) VALUES ('1','1','D','A','2015','64','OX','1','18','-2');
INSERT INTO everything (AmazonOrderItemCode,SKU,Quantity,Principal,Commission) VALUES ('64','WA','1','23','-2');
INSERT INTO everything (AmazonOrderItemCode,SKU,Quantity,Principal,Commission,FBA) VALUES ('29','WAG','1','49','77','97');
Thanks in advance!...
I just base from your "something like".. :)
$fields = ['AmazonOrderID', 'MerchantOrderID', 'ShipmentID', 'MerchantFulfillmentID', 'PostedDate', 'AmazonOrderItemCode', 'SKU', 'Quantity', 'Principal', 'Commission', 'AmazonOrderItemCode', 'SKU', 'Quantity', 'Principal', 'Commission', 'AmazonOrderItemCode', 'SKU', 'Quantity', 'Principal', 'Commission', 'FBA'];
$values = ['1','1','D','A','2015','64','OX','1','18','-2','64','WA','1','23','-2','29','WAG','1','49','77','97'];
// i just added this to avoid error produced by: `Undefined offset` error warning
error_reporting(0);
$fields_dup = array();
$values_dup = array();
for ($i = 0, $j = 0; $i < count($fields); $i++)
{
if (in_array($fields[$i], $fields_dup[$j]))
$j++;
$fields_dup[$j][] = $fields[$i];
$values_dup[$j][] = $values[$i];
// or maybe you want to add ` and ' make your statement look like:
// INSERT INTO table (`field1`, `field2`) VALUES ('value1', 'value2')
//
// $fields_dup[$j][] = "`".$fields[$i]."`";
// $values_dup[$j][] = "'".$values[$i]."'";
}
error_reporting(E_ALL);
// just to show what is produced
var_dump($fields_dup);
var_dump($values_dup);
// while you can also construct your statement in a loop like
for ($i = 0; $i < count($fields_dup); $i++)
{
$sql_fields = implode(',', $fields_dup[$i]);
$sql_values = implode(',', $values_dup[$i]);
echo "INSERT INTO everything ($sql_fields) VALUES ($sql_values) <br>";
}
Output would be:
//var_dump($fields_dup);
array (size=3)
0 =>
array (size=10)
0 => string 'AmazonOrderID' (length=13)
1 => string 'MerchantOrderID' (length=15)
2 => string 'ShipmentID' (length=10)
3 => string 'MerchantFulfillmentID' (length=21)
4 => string 'PostedDate' (length=10)
5 => string 'AmazonOrderItemCode' (length=19)
6 => string 'SKU' (length=3)
7 => string 'Quantity' (length=8)
8 => string 'Principal' (length=9)
9 => string 'Commission' (length=10)
1 =>
array (size=5)
0 => string 'AmazonOrderItemCode' (length=19)
1 => string 'SKU' (length=3)
2 => string 'Quantity' (length=8)
3 => string 'Principal' (length=9)
4 => string 'Commission' (length=10)
2 =>
array (size=6)
0 => string 'AmazonOrderItemCode' (length=19)
1 => string 'SKU' (length=3)
2 => string 'Quantity' (length=8)
3 => string 'Principal' (length=9)
4 => string 'Commission' (length=10)
5 => string 'FBA' (length=3)
// var_dump($values_dup);
array (size=3)
0 =>
array (size=10)
0 => string '1' (length=1)
1 => string '1' (length=1)
2 => string 'D' (length=1)
3 => string 'A' (length=1)
4 => string '2015' (length=4)
5 => string '64' (length=2)
6 => string 'OX' (length=2)
7 => string '1' (length=1)
8 => string '18' (length=2)
9 => string '-2' (length=2)
1 =>
array (size=5)
0 => string '64' (length=2)
1 => string 'WA' (length=2)
2 => string '1' (length=1)
3 => string '23' (length=2)
4 => string '-2' (length=2)
2 =>
array (size=6)
0 => string '29' (length=2)
1 => string 'WAG' (length=3)
2 => string '1' (length=1)
3 => string '49' (length=2)
4 => string '77' (length=2)
5 => string '97' (length=2)
// for the last for-statement
INSERT INTO everything (AmazonOrderID,MerchantOrderID,ShipmentID,MerchantFulfillmentID,PostedDate,AmazonOrderItemCode,SKU,Quantity,Principal,Commission) VALUES (1,1,D,A,2015,64,OX,1,18,-2)
INSERT INTO everything (AmazonOrderItemCode,SKU,Quantity,Principal,Commission) VALUES (64,WA,1,23,-2)
INSERT INTO everything (AmazonOrderItemCode,SKU,Quantity,Principal,Commission,FBA) VALUES (29,WAG,1,49,77,97)
Is that what you are trying to do?
Hope this is helpful, Cheers! ;)
I have an array in loop while like this when I var_dump it.
array (size=73)
0 => string '1' (length=1)
'address' => string '1' (length=1)
1 => string '2' (length=2)
'street_no' => string '2' (length=1)
array (size=73)
0 => string 'vbfgh' (length=5)
'address' => string 'vbfgh' (length=5)
1 => string 'fgfd' (length=4)
'street_no' => string 'fgfd' (length=4)
array (size=73)
0 => string 'vbfgh' (length=5)
'address' => string 'vbfgh' (length=5)
1 => string 'fgfd' (length=4)
'street_no' => string 'fgfd' (length=4)
array (size=73)
0 => string 'vbfgh' (length=5)
'address' => string 'vbfgh' (length=5)
1 => string 'fgfd' (length=4)
'street_no' => string 'fgfd' (length=4)
I want disply data on screen for first data is
`AddressMain: 1
streetMain: 1
.....
AddressLeft: vbfgh
street1Left: fgfd
.....
AddressRight: vbfgh
streetRight: fgfd
.....
AddressCenter: vbfgh
streetCenter: fgfd
.....`
How I can change my label like this if this code in loop while ?
And this is my code
while( $row = pg_fetch_array($result)){
echo "AddressMain:".$row['address'];
echo "streetMain:".$row['street_no'];
echo "<br/>";
echo ".......";
}
Plz help me how I can change my label in this loop ?
thanks
Use an array to define labels, then loop around it.
$labels = array('Main','Left','Right','Center');
$i = 0;
while( $row = pg_fetch_array($result)){
echo "Address".$labels[$i].":".$row['address'];
echo "street".$labels[$i].":".$row['street'];
echo "<br/>";
$i++;
}
I have been stumped on this for awhile and was wondering if there was a way to turn this data into a custom multidimensional array.
I run a query on my table and it spits out an array like this
SELECT id_cms_category, name FROM cms_category_lang ORDER BY id_cms_category ASC
The resulted array looks like this.
array
0=>
array
'id_cms_category'=> 1
'name'=>'Home'
1=>
array
'id_cms_category'=> 2
'name'=>'Test'
So then I run my query on my cms table
SELECT cl.id_cms, c.id_cms_category, cl.meta_title
FROM cms_lang cl
LEFT JOIN cms c ON (c.id_cms = cl.id_cms)
LEFT JOIN cms_link ci ON (ci.id_cms = cl.id_cms)
ORDER BY c.id_cms_category, cl.meta_title ASC
and I get an array like this
array
0 =>
array
'id_cms' => string '4' (length=1)
'id_cms_category' => string '1' (length=1)
'meta_title' => string 'About us' (length=8)
1 =>
array
'id_cms' => string '6' (length=1)
'id_cms_category' => string '1' (length=1)
'meta_title' => string 'Contact Us' (length=10)
2 =>
array
'id_cms' => string '1' (length=1)
'id_cms_category' => string '1' (length=1)
'meta_title' => string 'Delivery' (length=8)
3 =>
array
'id_cms' => string '2' (length=1)
'id_cms_category' => string '1' (length=1)
'meta_title' => string 'Legal Notice' (length=12)
4 =>
array
'id_cms' => string '5' (length=1)
'id_cms_category' => string '1' (length=1)
'meta_title' => string 'Secure payment' (length=14)
5 =>
array
'id_cms' => string '3' (length=1)
'id_cms_category' => string '1' (length=1)
'meta_title' => string 'Terms and conditions of use' (length=27)
6 =>
array
'id_cms' => string '10' (length=2)
'id_cms_category' => string '2' (length=1)
'meta_title' => string 'FAQ - UltraTech IBC Spill Pallet Plus' (length=37)
7 =>
array
'id_cms' => string '9' (length=1)
'id_cms_category' => string '2' (length=1)
'meta_title' => string 'FAQ - UltraTech P2 Plus 2-Drum Hard Top Spill Pallet' (length=52)
8 =>
array
'id_cms' => string '7' (length=1)
'id_cms_category' => string '2' (length=1)
'meta_title' => string 'XR-5, Urethane & Copolymer 2000' (length=31)
I am wanting to try and take all this data and somehow create a multidimensional array like so;
array
0 => 'Home'
array
id_cms => meta_title
1 => 'Test'
array
id_cms => meta_title
that way I should be able to run something like this
foreach($title as $key => $value)
{
<li>$value</li>
foreach($value as $id_cms => $page)
{
<li>$page</li>
}
}
to get the output on the page to look something like this
Home
home 1
home 2
home 3
Test
test 1
test 2
test 3
I wouldn't do two queries for this.
I would alter your second query so you've got both the category and page name in the same result set. It should look something like:
SELECT cl.id_cms, c.id_cms_category, catlang.name, cl.meta_title
FROM cms_lang cl
LEFT JOIN cms c
ON (c.id_cms = cl.id_cms)
LEFT JOIN cms_link ci
ON (ci.id_cms = cl.id_cms)
LEFT JOIN cms_category_lang catlang
ON (catlang.id_cms_category = cl.id_cms_category)
ORDER BY c.id_cms_category, catlang.name, cl.meta_title ASC;
Then you could create a loop that simply checked the category in each iteration. If it finds a new one, end the previous block and start a new one:
$currentCategory = '';
$output = '';
foreach ($rows as $row) {
if ($currentCategory != $row['name']) {
$output .= '</li><li>' . $row['name'];
}
$currentCategory = $row['name'];
$output .= '<li><a href="' . $row['id_cms'] . '">' . $row['meta_title'] . '</li>';
}
// Trim off our unnecessary </li>
$output = substr($output, 4);
If your CMS returns a numerically indexed array (and that's what it sounds like from your post), you can re-index the array using whatever key you want.
$array = array(0 => array('id_cms' => 4, 'id_cms_category' => 1, 'meta_title' => 'About us'));
$newArray = array();
foreach ( $array as $v ) {
$newArray[$v['meta_title']] = $v;
}
var_dump($newArray);
Arrays are vital in PHP programming, so you'd be doing yourself a favor to learn them before going any further.
Create a new empty array: $newArray = array();
Go through your result with a foreach, then as you go, check if the category of the current item already exists in your new array: if (isset($newArray[$row['id_cms_category']))
If it doesn't, create it and initialize it with a new array containing the item as a first element: $newArray[$row['id_cms_category'] = array($row);
If it already existed (i.e. you already went through an item belonging to this category), then add it: $newArray[$row['id_cms_category'] []= $row;
That's it!