I have an array from a database, I just want to be left with the productnames without the array structure to be shown.
public function selectAll ()
{
$stmt = Database::get()->query('SELECT `productname` FROM retrofootball_products');
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
The output of this is
Array ( [0] => Array ( [productname] => Manchester City 1981 Centenary FA Cup Final Shirt ) [1] => Array ( [productname] => Manchester City 1999 Playoff Winning Shirt ) )
I want to be left with
Manchester City 1981 Centenary FA Cup Final Shirt, Manchester City 1999 Playoff Winning Shirt
If you want comma separated use implode
You may use implode :
implode(',', $smt->fetchAll(PDO::FECTH_ASSOC));
Related
I'm building a calendar (FullCalendar) that shows orders for each day and the items that were ordered when you click on an order. When $order_info is put into the $data array, it's missing certain rows of data, as shown below in the 4th code block down. I'm trying to figure out how to put the array together so that every description (key) that is supposed to be shown is paired with the correct quantity (value) and then iterated out using an $.each function in jquery.
The $desc & $qty variables are concatenated when I query them from my table so I'm using the explode function to separate their values.
foreach($sql_query as $row) {
$desc = array_map('trim', explode(',', $row['OlineDesc']));
$qty = array_map('trim', explode(',', $row['OlineQty']));
$order_info = array_combine($desc, $qty);
$data[] = array (
'start' => $row['OrderDate'],
'order_id' => $row['OrderID'],
'desc_qty' => $order_info
);
}
Printing out the array after the closing tag of the foreach:
echo "<pre>";
echo print_r($data);
echo "</pre>";
Output:
// Output when print_r($data)
[0] => Array
(
[start] => 2019-05-11 16:00:00
[order_id] => 2509
[description] => Time: 04:00PM
[desc_qty] => Array
(
[#4 Beef and Pizza Special] => 1
[6 Lbs Italian Beef] => 1
[**incl Brd + Au Jus] => 1
[**incl Sweet Peppers] => 1
[**incl Hot Giardiniera] => 1
[XL 18" Pizza] => 1
[SAUSAGE] => 1
)
)
If I use array_merge to join my arrays instead of array_combine, it merges all the keys and values together as all the keys first, then values... but it's at least showing every row as opposed to the limited # of rows:
$order_info = array_merge($desc, $qty);
// Output when print_r($order_info)
[desc_qty] => Array
(
[0] => #4 Beef and Pizza Special
[1] => 6 Lbs Italian Beef
[2] => **incl Brd + Au Jus
[3] => **incl Sweet Peppers
[4] => **incl Hot Giardiniera
[5] => XL 18" Pizza
[6] => SAUSAGE
[7] => XL 18" Pizza
[8] => XL 18" Pizza
[9] => 1
[10] => 1
[11] => 1
[12] => 1
[13] => 1
[14] => 1
[15] => 1
[16] => 1
[17] => 1
)
Then I'm using jquery that's bound to an onClick event to load the information into a div where key is the description of the item and the value is the quantity:
$.each( event.desc_qty, function( key, value ) {
document.getElementById("party_info").innerHTML += "<span class="+key+">" + key + "<span id='sum_"+key+"'>" + value + "</span></span>";
});
My codes current output:
#4 Beef and Pizza Special 1
6 Lbs Italian Beef 1
**incl Brd + Au Jus 1
**incl Sweet Peppers 1
**incl Hot Giardiniera 1
XL 18" Pizza 1
SAUSAGE 1
Expected output that I'm trying to achieve:
#4 Beef and Pizza Special 1
6 Lbs Italian Beef 1
**incl Brd + Au Jus 1
**incl Sweet Peppers 1
**incl Hot Giardiniera 1
XL 18" Pizza 1
SAUSAGE 1
XL 18" Pizza 1
XL 18" Pizza 1
Any help or direction would be much appreciated! Thanks!
EDIT:
This is my SQL query. I'm using 3 different tables to gather all my information where I match the ID's to pull the proper information.
SELECT a.OrderDate, a.OrderID, a.OrdCustName, a.OrdPhone, b.OtotOrder,
b.OtotTotal, c.OlineOrder, GROUP_CONCAT(c.OlineDesc SEPARATOR ',') as
'OlineDesc', GROUP_CONCAT(FLOOR(COALESCE(c.OlineQty,'1'))) as
'OlineQty'
FROM db.tbldeferredorders a, db.tbldeferredordtotals b,
db.tbldeferredordlines c
WHERE a.OrderID = b.OtotOrder AND a.OrderID = c.OlineOrder
AND a.OrderDate >= NOW() - INTERVAL 3 MONTH
GROUP BY a.OrderID
This query will pull the OlineDesc column into a concatenated string that looks like this. Each comma is from a separate row that I pull together based on a.OrderID. So for this particular row, the OrderID is '2536'.
'#4 Beef and Pizza Special,6 Lbs Italian Beef,**incl Brd + Au
Jus,**incl Sweet Peppers,**incl Hot Giardiniera,XL 18" Pizza,XL 18"
Pizza,SAUSAGE,XL 18" Pizza,PEPPERONI,Lrg Caesar Salad,** ceaser
dressing,Lg 3 Cheese Baked Penne'
The OlineQty column looks like this when I run the query:
'1,1,1,1,1,1,1,1,1,1,1,1,1'
It's probably not the most elegant solution but I found a way to solve my issue. I found that array_combine can't have duplicate keys, so instead of having just a single array, I decided on having 2. And since both arrays have an equal number of descriptions to quantities, I took the descriptions array and used an $.each loop to go through it, then I paired it with the qty[key] as it was going through each loop. This matched it line for line.
$.each( event.desc, function( key, value ) {
document.getElementById("party_info").innerHTML += "<span class="+value+">" + key + "<span id='sum_"+value+"'>" + event.qty[key] + "</span></span>";
});
I have an array like this:
Array
(
[year] => 1994, 1995, 1996, 1997
[make] => Ford
[model] => Mustang, Mustang Cobra, Mustang GT
[engine] => 302, 302 H/O, V6
)
I am wanting to insert unique rows into my database with all the possible solutions in the array.
For example:
id year make model engine
1 1994 Ford Mustang 302
2 1994 Ford Mustang Cobra 302
3 1994 Ford Mustang GT 302
4 1994 Ford Mustang 302 H/O
...and so on
I need to be able to break down that array to get all possible solutions into a unique row. Some columns may not have a value. For example, the array may only contain several Make's. I would like to insert all the unique possibilities into an array.
Array
(
[0] => 1994, Ford, Mustang, 302
[1] => and so on...
)
Here is simple way, without database part, but I hope you can do that yourself:
$arr = array
(
'year' => '1994, 1995, 1996, 1997',
'make' => 'Ford',
'model' =>'Mustang, Mustang Cobra, Mustang GT',
'engine' => '302, 302 H/O, V6'
);
$years = explode(',',$arr['year']);
$makes = explode(',',$arr['make']);
$models = explode(',',$arr['model']);
$engines = explode(',',$arr['engine']);
foreach($years as $year) {
foreach($makes as $make) {
foreach($models as $model) {
foreach($engines as $engine) {
echo 'INSERT INTO yourtable '.$year.', '.$make.', '.$model.', '.$engine."\n";
}
}
}
}
If you need "all unique possibilities" (combinations), the most direct approach is to iterate your information array nesting each item the other. And for each combination created from an iteration, create an array with the collected information and add to a final array. Eg:in kind of pseudo code
finalArray = new array()
foreach(year as y)
foreach(make as m)
foreach(model as mo)
foreach(engine as e)
newArray = new array(y,m,mo,e)
add newArray to finalArray
hope it heps you
I have an array generated with CodeIgniter using MySQL. The values in [co_name] => are duplicated and I tried to group_by it using co_id and its not working ,but it also group_by using order_id.CodeIgniter group_by is working for only one group_by or any other method to removes duplicate values form the [co_name] =>.
Array
(
[0] => stdClass Object
(
[order_date] => 1408255199
[cus_id] => 6
[order_deliver_name] => Jankia
[order_deliver_add_no] => 5085
[order_deliver_add_street] => mapanawathura road
[order_deliver_add_city] => Kandy
[order_deliver_contact] => 0716352632
[order_deliver_date] => 2014-08-12
[order_status] => pending
[emp_id] =>
[cmp_name] => Burger Bun
[co_name] => Put Ham
Put Tomato
Put Ham
Put Tomato
Put Chilli Sauce
Put Tomato
Put Chilli Sauce
Put Ham
Put Chilli Sauce
[order_id] => 28
[quantity] => 10
)
Here is my code
function get_customize_orders(){
$this->db->select(array(
'tbl_order_product_details.order_date',
'tbl_order_product_details.cus_id',
'tbl_order_product_details.order_deliver_name',
'tbl_order_product_details.order_deliver_add_no',
'tbl_order_product_details.order_deliver_add_street',
'tbl_order_product_details.order_deliver_add_city',
'tbl_order_product_details.order_deliver_contact',
'tbl_order_product_details.order_deliver_date',
'tbl_order_product_details.order_status',
'tbl_order_product_details.emp_id',
'tbl_customise_main_product.cmp_name',
'GROUP_CONCAT(tbl_customise_optional.co_name SEPARATOR "<br />" ) as co_name ',
'tbl_order_products.order_id',
'tbl_order_products.quantity',
));
$this->db->from('tbl_order_products');
$this->db->join('tbl_customise_join_products', 'tbl_customise_join_products.cmp_id=tbl_order_products.product_id');
$this->db->join('tbl_customise_main_product', 'tbl_customise_main_product.cmp_id=tbl_customise_join_products.cmp_id');
$this->db->join('tbl_customise_order_products', 'tbl_customise_order_products.order_id=tbl_order_products.order_id');
$this->db->join('tbl_customise_optional', 'tbl_customise_optional.co_id=tbl_customise_order_products.co_id');
$this->db->join('tbl_order_product_details', 'tbl_order_product_details.order_id=tbl_order_products.order_id');
$this->db->group_by('tbl_order_products.order_id');
$query = $this->db->get();
return $query->result();
}
Use DISTINCT in GROUP_CONCAT so it will concatenate only distinct co_name per order_id group
GROUP_CONCAT(DISTINCT tbl_customise_optional.co_name SEPARATOR "<br />" ) as co_name
I have gone code blind and don't know how to get the results I need to output values in a desired format.
I have a search function on an internal website which queries MSSQL based on multiple strings. For instance if a user enters "CCENT MCSE" the query will return all users with that have either certification accredited to them.
To be clear this will return all users who have CCENT cerfificate OR MCSE
My boss now want to change this to AND by using an operator (+) sign so when a user enters "CCENT+MCSE" the query should return users who have the CCENT AND MCSE certification.
The way I have built the query is to explode the search string into an array referencing the + sign as the delimiter.
My SQL statement pulls back the following result
Array
(
[0] => Array
(
[username] => mshort
[fname] => Matthew
[lname] => Short
[location_name] => CENTRAL REMOTE
[grade_name] => E6
[skill_name] => CCENT
)
[1] => Array
(
[username] => mark.parry
[fname] => Mark
[lname] => Parry
[location_name] => CENTRAL OFFICE
[grade_name] => E4
[skill_name] => MCSE Messaging
)
[2] => Array
(
[username] => mark.parry
[fname] => Mark
[lname] => Parry
[location_name] => CENTRAL OFFICE
[grade_name] => E4
[skill_name] => CCENT
)
)
What needs to happen in this case is I need to display only unique users who have both CCENT AND MCSE and discard the users who only have one of them.
In the above example I need to output only Mark Parry and discard Matthew Short
I am not sure how to do it. Please can you help?
Thanks
The SQL Statement
SELECT username,fname,lname,el.location_name, eg.grade_name,el.location_name
,sl.skill_nameFROM engineer e
INNER JOIN engineer_skills es ON e.id=es.engineer_id INNER JOIN skills_list sl ON es.skill_id=sl.id
INNER JOIN engineer_grades eg ON e.grade=eg.id
INNER JOIN engineer_location el ON e.location=el.id INNER JOIN team t ON e.team=t.id
WHERE e.id > 0 AND '.$sql_where.' ORDER BY lname'
$sql_where is this
sl.skill_name LIKE '%".$search[$i]."%' OR
and I strip the last OR
You could filter the array (assuming it is names $data)
First, emulate group by username
$result = array();
foreach($data as $item) {
if(!isset($result[$item["username"]])) $result[$item["username"]] = $item;
else $result[$item["username"]]["skill_name"].="+".$item["skill_name"];
}
Second, filter the result
$filter = array("CCENT","MCSE");
function skill_and_filter($item) {
global $filter;
foreach($filter as $f) if(strpos($item["skill_name"],$f)===false) return false;
return true;
}
$result = array_filter($result,"skill_and_filter");
I have an array of mysql records in $allrecords variable.
There are multiple keys for each record and one of the key is called type.
Some of the variable has the same type.
E.g. it looks like this:
Seat
Mercedes
BMW
BMW
Mercedes
Audi
Mercedes
Fiat
Mercedes
Audi
Fiat
Mercedes
The array is unordered now (just grabbed from database) and now I need to order the array items from the most occurance one to the least.
E.g. like this
Mercedes (5)
Audi (3)
BMW (2)
Fiat (2)
Seat (1)
I do not need the numbers (that I get using another foreach loop), so I need it just like this:
Mercedes
Audi
BMW
Fiat
Seat
How to do such this in PHP? Is it possible? I need to group the items based on the type and then order them from the most to the least occurance.
Thanks in advance.
EDIT:
My array look like this:
Array
(
[0] => stdClass Object
(
[id] => 125
[user_id] => 29
[show] => 1
[state] => 1
[type] => 23
[subcat_id] => 11
[category_id] => 2
[name] => SLK 350
)
)
<?php
$groups = array();
foreach($allrecords as $record){
$type = $record->type;
if(empty($groups[$type]))
$groups[$type] = 1;
else
$groups[$type]++;
}
asort ($groups);
$cars = array_keys($groups);
print_r($cars);
usort($allrecords, function($a, $b){ return $a->count >= $b->count; });
where "count" is the name of the property...
var_dump(str_word_count(implode(" ", $all_records))); // Returns array( "Mercedes" => 5 ... )
aaaah i didn't get that!!!
you should use SQL to achieve this!!!
SELECT type, COUNT(1) FROM cars GROUP BY type;