Linking arrays according to common values - laravel

I have some array like
0: [id: '1',department: 'xyz',date: '10-10-2019',time: '12:50']
1: [id: '1',department: 'xyz',date: '11-10-2019',time: '10:30']
2: [id: '2',department: 'abc',date: '09-09-2019',time: '09:50']
3: [id: '2',department: 'abc',date: '07-07-2019',time: '03:20']
I want them to be merged according to the id and department
so the 0 and 1 array will be merged together and the output should be something like
0:[id: '1',department: 'xyz',[[date: '10-10-2019',time: '12:50'],[date: '11-10-2019',time: '10:30']]]
and the 2 and 3 array will be merged together and the output should be something like
1:[id: '2',department: 'abc',[[date: '09-09-2019',time: '09:50'],[date: '07-07-2019',time: '03:20']]]
how can I do it?

Try this..
$input = [
0 =>['id' => '1','department' => 'xyz','date' => '10-10-2019','time' => '12:50'],
1 => ['id' => '1','department' => 'xyz','date' => '11-10-2019','time' => '10:30'],
2 => ['id' => '2','department' => 'abc','date' => '09-09-2019','time' => '09:50'],
3 =>['id' => '2','department' => 'abc','date' => '07-07-2019','time' => '03:20']];
$output = [];
$id_array = [];
foreach ($input as $values)
{
$id_array [] = $values['id'];
}
$unique_id_array = array_unique($id_array);
foreach($input as $key => $in)
{
if(array_key_exists($key,$unique_id_array))
{
$output[] = [
'department' => $in['department'],
'id' => $in['id']
];
}
}
foreach($input as $in)
{
foreach($output as $key => $out)
{
if($out['id'] == $in['id'] && $out['department'] == $in['department'])
{
$output[$key]['date_time'][] = ['date' =>$in['date'],'time' => $in['time']];
}
}
}
print_r($output);die();
The output is going to be
Array
(
[0] => Array
(
[department] => xyz
[id] => 1
[date_time] => Array
(
[0] => Array
(
[date] => 10-10-2019
[time] => 12:50
)
[1] => Array
(
[date] => 11-10-2019
[time] => 10:30
)
)
)
[1] => Array
(
[department] => abc
[id] => 2
[date_time] => Array
(
[0] => Array
(
[date] => 09-09-2019
[time] => 09:50
)
[1] => Array
(
[date] => 07-07-2019
[time] => 03:20
)
)
)
)

Hello i think this will works fine,and it's easy and best way to achieve your output.
i tried many codes in my system with data given by you,just try this
i hope this will work for you as well.
<?php
$inputArr = [0 =>['id' => '1','department' => 'xyz','date' => '10-10-2019','time' => '12:50'],1 => ['id' => '1','department' => 'xyz','date' => '11-10-2019','time' => '10:30'],2 => ['id' => '2','department' => 'abc','date' => '09-09-2019','time' => '09:50'],3 =>['id' => '2','department' => 'abc','date' => '07-07-2019','time' => '03:20']];
print_r($inputArr);
$second_copy = $inputArr;
foreach ($inputArr as $key => $single)
{
$goalArr[$single['id']] = getRow($single['id'],$single['department'],$second_copy);
}
function getRow($id,$department,$second_copy)
{
$returnArr['id'] = $id;
$returnArr['department'] = $department;
foreach ($second_copy as $key => $single)
{
if($id == $single['id'] && $department == $single['department'])
{
$returnArr['date'][] = $single['date'];
$returnArr['time'][] = $single['time'];
}
}
return $returnArr;
}
echo "<br>Output is: <br>";
print_r($goalArr);
?>

Related

laravel how to add price product variations in laravel cart session

this is taking more than i anticipated,
On my shopping cart app i'm able to add product to app cart session with no problem, But now i need to add product variations like (color, size, ... etc) for every product added to the cart, These variation can change to product price according to users selection on product details page.
The code that i have currently is working for me except that i can't add a list of variation for a single product i can only create one variation then the code is replacing it with new variation value.
This is my cart object when running {{print_r($cart)}}
App\Cart Object
(
[items] => Array
(
[27] => Array
(
[id] => 27
[name] => product with variation and colors
[slug] => iphone-pro13
[price] => 100
[prefix] => QAR
[qty] => 1
[poster] => /image/md/9964677a-c957-4510-9eb4-f41578c069b3
[subtotal] => 730
[quotable] => 0
[variations] => Array // these are the list of product variations
(
[16] => Array
(
[id] => 16
[var_name] => This is the variation
[var_price] => 365.00
[var_qty] => 1
[var_subtotal] => 365
[color_code] => #e6bf00
)
)
)
[12] => Array
(
[id] => 12
[name] => Dolore quis sunt reiciendis.
[slug] => iste-autem-beatae-eaque-natus-distinctio
[price] => 96.08
[prefix] => QAR
[qty] => 1
[poster] => /media/default/product-placeholder.jpg
[subtotal] => 350.692
[quotable] => 0
[variations] => Array // these are the list of product variations
(
[0] => Array
(
[var_name] => This is the variation 2
[var_price] => 335.00
[var_qty] => 1
[var_subtotal] => 365
[color_code] => #e6bf00
)
)
)
)
the is my cart.php class
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Cart extends Model
{
public $items = null;
public $itemsCount = 0;
public $grandTotal = 0;
public $variations = null;
public function __Construct($oldCart = null) {
if($oldCart) {
$this->items = $oldCart->items;
$this->itemsCount = $oldCart->itemsCount;
$this->grandTotal = $oldCart->grandTotal;
$this->variations = $oldCart->variations;
}
}
public function add($product) {
if (isset($this->items)) {
if( array_key_exists($product->id, $this->items) ) {
$qty = $this->items[$product->id]['qty'] += $product->qty;
$subtotal = $qty * $product->price;
}else{
$qty = $product->qty;
$subtotal = $product->subtotal;
}
}else{
$qty = $product->qty;
$subtotal = $product->subtotal;
}
$item = [
'id' => $product->id,
'name' => $product->name,
'slug' => $product->slug,
'price' => $product->price,
'prefix' => $product->prefix,
'qty' => $qty,
'poster' => $product->poster,
'subtotal' => $subtotal,
'quotable' => $product->quotable,
];
$variations = [
'id' => $product->variation_id,
'var_name' => $product->variation,
'var_price' => $product->variation_price,
'var_qty' => $qty,
'var_subtotal' => $qty * $product->variation_price,
'color_code' => $product->color,
];
$this->items[$product->id] = $item; // <-- this adds new product
$this->items[$product->id]['variations'][$product->variation_id] = $variations; // <-- this adds new product variation
$this->itemsCount +=1;
$this->grandTotal += $product->price * $product->qty;
}
}
Not sure where my code went wrong but i need to be able to add list of product variations instead of replacing current ones.
any ideas?

Laravel 1 row missing while inserting and

$purchase_line_datas = PurchaseLine::where('transaction_id',
$transaction->id)->get(); $i = 0;
$input = [];
foreach ($purchase_line_datas as $key => $purchase_line_data) {
if (!$enable_stock_transfer) {
$qty_available = $this->productUtil->num_uf($purchases[$i]['quantity']);
} else {
$qty_available = 0;
}
$item = array(
"business_id" => $business_id,
"transaction_id" => $purchase_line_data->transaction_id,
"purchase_line_id" => $purchase_line_data->id,
"variation_id" => $purchase_line_data->variation_id,
"contact_id" => $transaction_data['contact_id'],
"product_id" => $purchase_line_data->product_id,
"ref_no" => $ref_no,
"new_barcode" => $purchase_line_data->barcode,
"default_sell_price" => $this->productUtil->num_uf($purchases[$i]['default_sell_price']),
"purchase_qty" => $this->productUtil->num_uf($purchases[$i]['quantity']),
"qty_available" => $this->productUtil->num_uf($qty_available),
"created_at" => Carbon::now()
);
array_push($input, $item);
$i++;
}
ProductPurchaseSl::insert($input);
in this code I am inserting an array but after inserting.. it's showing that one row is missing.And it's happening very often.I couldn't be able to solve this problem.

Accessing image property of K2 item

I'm developing a module that displays a specific number of k2 items based on its category id. I can get those item but I'm not able to access the image on it. When I try $item->image returns the following notice:
Notice: Undefined property: stdClass::$image
Here is the object I get from my sql query:
stdClass Object
(
[id] => 5
[title] => News test item
[alias] => news-test-item
[catid] => 4
[published] => 1
[introtext] => Some intro text
[fulltext] =>
[video] =>
[gallery] =>
[extra_fields] => []
[extra_fields_search] =>
[created] => 2014-03-28 21:42:01
[created_by] => 252
[created_by_alias] =>
[checked_out] => 0
[checked_out_time] => 0000-00-00 00:00:00
[modified] => 0000-00-00 00:00:00
[modified_by] => 0
[publish_up] => 2014-03-28 21:42:01
[publish_down] => 0000-00-00 00:00:00
[trash] => 0
[access] => 1
[ordering] => 1
[featured] => 0
[featured_ordering] => 0
[image_caption] =>
[image_credits] =>
[video_caption] =>
[video_credits] =>
[hits] => 0
[params] => {"catItemTitle":"","catItemTitleLinked":"","catItemFeaturedNotice":"","catItemAuthor":"","catItemDateCreated":"","catItemRating":"","catItemImage":"","catItemIntroText":"","catItemExtraFields":"","catItemHits":"","catItemCategory":"","catItemTags":"","catItemAttachments":"","catItemAttachmentsCounter":"","catItemVideo":"","catItemVideoWidth":"","catItemVideoHeight":"","catItemAudioWidth":"","catItemAudioHeight":"","catItemVideoAutoPlay":"","catItemImageGallery":"","catItemDateModified":"","catItemReadMore":"","catItemCommentsAnchor":"","catItemK2Plugins":"","itemDateCreated":"","itemTitle":"","itemFeaturedNotice":"","itemAuthor":"","itemFontResizer":"","itemPrintButton":"","itemEmailButton":"","itemSocialButton":"","itemVideoAnchor":"","itemImageGalleryAnchor":"","itemCommentsAnchor":"","itemRating":"","itemImage":"","itemImgSize":"","itemImageMainCaption":"","itemImageMainCredits":"","itemIntroText":"","itemFullText":"","itemExtraFields":"","itemDateModified":"","itemHits":"","itemCategory":"","itemTags":"","itemAttachments":"","itemAttachmentsCounter":"","itemVideo":"","itemVideoWidth":"","itemVideoHeight":"","itemAudioWidth":"","itemAudioHeight":"","itemVideoAutoPlay":"","itemVideoCaption":"","itemVideoCredits":"","itemImageGallery":"","itemNavigation":"","itemComments":"","itemTwitterButton":"","itemFacebookButton":"","itemGooglePlusOneButton":"","itemAuthorBlock":"","itemAuthorImage":"","itemAuthorDescription":"","itemAuthorURL":"","itemAuthorEmail":"","itemAuthorLatest":"","itemAuthorLatestLimit":"","itemRelated":"","itemRelatedLimit":"","itemRelatedTitle":"","itemRelatedCategory":"","itemRelatedImageSize":"","itemRelatedIntrotext":"","itemRelatedFulltext":"","itemRelatedAuthor":"","itemRelatedMedia":"","itemRelatedImageGallery":"","itemK2Plugins":""}
[metadesc] =>
[metadata] => robots=
author=
[metakey] =>
[plugins] =>
[language] => *
[categoryname] => News-test
[categoryid] => 4
[categoryalias] => news-test
[categoryparams] => {"inheritFrom":"0","theme":"","num_leading_items":"2","num_leading_columns":"1","leadingImgSize":"Large","num_primary_items":"4","num_primary_columns":"2","primaryImgSize":"Medium","num_secondary_items":"4","num_secondary_columns":"1","secondaryImgSize":"Small","num_links":"4","num_links_columns":"1","linksImgSize":"XSmall","catCatalogMode":"0","catFeaturedItems":"1","catOrdering":"","catPagination":"2","catPaginationResults":"1","catTitle":"1","catTitleItemCounter":"1","catDescription":"1","catImage":"1","catFeedLink":"1","catFeedIcon":"1","subCategories":"1","subCatColumns":"2","subCatOrdering":"","subCatTitle":"1","subCatTitleItemCounter":"1","subCatDescription":"1","subCatImage":"1","itemImageXS":"","itemImageS":"","itemImageM":"","itemImageL":"","itemImageXL":"","catItemTitle":"1","catItemTitleLinked":"1","catItemFeaturedNotice":"0","catItemAuthor":"1","catItemDateCreated":"1","catItemRating":"0","catItemImage":"1","catItemIntroText":"1","catItemIntroTextWordLimit":"","catItemExtraFields":"0","catItemHits":"0","catItemCategory":"1","catItemTags":"1","catItemAttachments":"0","catItemAttachmentsCounter":"0","catItemVideo":"0","catItemVideoWidth":"","catItemVideoHeight":"","catItemAudioWidth":"","catItemAudioHeight":"","catItemVideoAutoPlay":"0","catItemImageGallery":"0","catItemDateModified":"0","catItemReadMore":"1","catItemCommentsAnchor":"1","catItemK2Plugins":"1","itemDateCreated":"1","itemTitle":"1","itemFeaturedNotice":"1","itemAuthor":"1","itemFontResizer":"1","itemPrintButton":"1","itemEmailButton":"1","itemSocialButton":"1","itemVideoAnchor":"1","itemImageGalleryAnchor":"1","itemCommentsAnchor":"1","itemRating":"1","itemImage":"1","itemImgSize":"Large","itemImageMainCaption":"1","itemImageMainCredits":"1","itemIntroText":"1","itemFullText":"1","itemExtraFields":"1","itemDateModified":"1","itemHits":"1","itemCategory":"1","itemTags":"1","itemAttachments":"1","itemAttachmentsCounter":"1","itemVideo":"1","itemVideoWidth":"","itemVideoHeight":"","itemAudioWidth":"","itemAudioHeight":"","itemVideoAutoPlay":"0","itemVideoCaption":"1","itemVideoCredits":"1","itemImageGallery":"1","itemNavigation":"1","itemComments":"1","itemTwitterButton":"1","itemFacebookButton":"1","itemGooglePlusOneButton":"1","itemAuthorBlock":"1","itemAuthorImage":"1","itemAuthorDescription":"1","itemAuthorURL":"1","itemAuthorEmail":"0","itemAuthorLatest":"1","itemAuthorLatestLimit":"5","itemRelated":"1","itemRelatedLimit":"5","itemRelatedTitle":"1","itemRelatedCategory":"0","itemRelatedImageSize":"0","itemRelatedIntrotext":"0","itemRelatedFulltext":"0","itemRelatedAuthor":"0","itemRelatedMedia":"0","itemRelatedImageGallery":"0","itemK2Plugins":"1","catMetaDesc":"","catMetaKey":"","catMetaRobots":"","catMetaAuthor":""}
)
What am I missing here?
Any help will be appreciated.
Edit: I'm using Joomla v2.5.18 and K2 v2.6.8
This is how K2 Content module adds the image to the item Object (taken from mod_k2_content/helper.php):
//Images
if ($params->get('itemImage'))
{
$date = JFactory::getDate($item->modified);
$timestamp = '?t='.$date->toUnix();
if (JFile::exists(JPATH_SITE.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$item->id).'_XS.jpg'))
{
$item->imageXSmall = JURI::base(true).'/media/k2/items/cache/'.md5("Image".$item->id).'_XS.jpg';
if ($componentParams->get('imageTimestamp'))
{
$item->imageXSmall .= $timestamp;
}
}
if (JFile::exists(JPATH_SITE.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$item->id).'_S.jpg'))
{
$item->imageSmall = JURI::base(true).'/media/k2/items/cache/'.md5("Image".$item->id).'_S.jpg';
if ($componentParams->get('imageTimestamp'))
{
$item->imageSmall .= $timestamp;
}
}
if (JFile::exists(JPATH_SITE.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$item->id).'_M.jpg'))
{
$item->imageMedium = JURI::base(true).'/media/k2/items/cache/'.md5("Image".$item->id).'_M.jpg';
if ($componentParams->get('imageTimestamp'))
{
$item->imageMedium .= $timestamp;
}
}
if (JFile::exists(JPATH_SITE.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$item->id).'_L.jpg'))
{
$item->imageLarge = JURI::base(true).'/media/k2/items/cache/'.md5("Image".$item->id).'_L.jpg';
if ($componentParams->get('imageTimestamp'))
{
$item->imageLarge .= $timestamp;
}
}
if (JFile::exists(JPATH_SITE.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$item->id).'_XL.jpg'))
{
$item->imageXLarge = JURI::base(true).'/media/k2/items/cache/'.md5("Image".$item->id).'_XL.jpg';
if ($componentParams->get('imageTimestamp'))
{
$item->imageXLarge .= $timestamp;
}
}
if (JFile::exists(JPATH_SITE.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$item->id).'_Generic.jpg'))
{
$item->imageGeneric = JURI::base(true).'/media/k2/items/cache/'.md5("Image".$item->id).'_Generic.jpg';
if ($componentParams->get('imageTimestamp'))
{
$item->imageGeneric .= $timestamp;
}
}
$image = 'image'.$params->get('itemImgSize', 'Small');
if (isset($item->$image))
$item->image = $item->$image;
}
You should have everything you need, otherwise take a look at the K2 Content module.
You didn't mention which Joomla and K2 versions you are using.
For Joomla > 1.6 You should parse the "params" property like this:
$params = new JRegistry($item->params);
And get the image as follows
J 1.6 <=> J 2.5.14
$params->getValue('itemImage')
J >= 2.5.14
$params->get('itemImage')
Reference: http://api.joomla.org/cms-3/classes/JRegistry.html

Deleting categories in magento

I want to delete empty categories, using the following code:
`<?php
require "app/Mage.php";
umask(0);
Mage::app();
$categoryCollection = Mage::getModel('catalog/category')->getCollection()->addFieldToFilter('level', array('gteq' => 2));
foreach($categoryCollection as $category) {
if ($category->getProductCount() === 0) {
print_r ($category);
echo "<br><hr><br>";
$category->delete();
}
}
echo 'End!';
?>`
Upon executing this code, it crashes at delete.
print_r gives the following result:
Mage_Catalog_Model_Category Object (
[_eventPrefix:protected] => catalog_category
[_eventObject:protected] => category
[_cacheTag:protected] => catalog_category
[_useFlatResource:protected] => 1
[_designAttributes:Mage_Catalog_Model_Category:private] => Array (
[0] => custom_design
[1] => custom_design_from
[2] => custom_design_to
[3] => page_layout
[4] => custom_layout_update
[5] => custom_apply_to_products
)
[_treeModel:protected] =>
[_defaultValues:protected] => Array (
)
[_storeValuesFlags:protected] => Array (
)
[_lockedAttributes:protected] => Array (
)
[_isDeleteable:protected] => 1
[_isReadonly:protected] =>
[_resourceName:protected] => catalog/category_flat
[_resource:protected] =>
[_resourceCollectionName:protected] => catalog/category_flat_collection
[_dataSaveAllowed:protected] => 1
[_isObjectNew:protected] =>
[_data:protected] => Array (
[entity_id] => 53
[level] => 4
[path] => 1/2/27/39/53
[position] => 3
[is_active] => 1
[is_anchor] => 1
[product_count] => 0
)
[_hasDataChanges:protected] => 1
[_origData:protected] => Array (
[entity_id] => 53
[level] => 4
[path] => 1/2/27/39/53
[position] => 3
[is_active] => 1
[is_anchor] => 1
)
[_idFieldName:protected] => entity_id
[_isDeleted:protected] =>
[_oldFieldsMap:protected] => Array (
)
[_syncFieldsMap:protected] => Array (
)
)
What am I doing wrong, and how should I do it?
Any help would be greatly appreciated.
<?php
require "app/Mage.php";
umask(0);
Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));
$categoryCollection = Mage::getModel('catalog/category')->getCollection()->addFieldToFilter('level', array('gteq' => 2));
foreach($categoryCollection as $category) {
if ($category->getProductCount() === 0) {
print_r ($category->entity_id);
echo "<br><hr><br>";
$category->delete();
}
}
echo 'End!';
?>
You have to add this line
Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));
you write this code print_r ($category); so its reruns the array. That is not error.

CodeIgniter Multidimensional Object - ActiveRecord

I have an object called 'events', that's created via $data['events'] = function (the function pulls information out of an events table and others using active record).
The events object looks like:
Array
(
[0] => stdClass Object
(
[id] => 2
[course_name] => Course 3
[course_description] => Course
[course_price] => 995
[supplier_name] => Supplier 3
[location_country_code] => GB
[location_country] => United Kingdom
[location_city] => London
[venue_name] => Venue 2
[venue_address] => 2 Street
[venue_postcode] => EC2M 7PQ
[venue_city] => London
[venue_county] =>
[venue_country] => United Kingdom
[venue_locality] =>
[event_type] => Materials Only
[event_status] => Confirmed
[course_id] => 2
[event_duration] => 3
[event_start_date] => 2013-09-12
[event_date_added] => 2013-02-26 14:36:06
[event_status_id] => 2
[event_type_id] => 4
[tutor_id] => 0
[tutor_confirmed] => 0
[event_featured] => 0
[event_push] => 0
[event_active] => 0
[invigilator_id] => 0
[event_discount] =>
[event_max_delegates] => 16
[location_id] => 1
[venue_id] => 1
[supplier_id] => 2
)
[1] => stdClass Object
(
[id] => 1
[course_name] => Course Name
[course_description] => Course Description
[course_price] => 995
[supplier_name] => Supplier 1
[location_country_code] => GB
[location_country] => United Kingdom
[location_city] => London
[venue_name] => Venue Name
[venue_address] => Street
[venue_postcode] => EC2M 7PQ
[venue_city] => London
[venue_county] =>
[venue_country] => United Kingdom
[venue_locality] =>
[event_type] => Private Venue
[event_status] => Provisional
[course_id] => 1
[event_duration] => 3
[event_start_date] => 2013-11-13
[event_date_added] => 2013-02-26 09:56:17
[event_status_id] => 1
[event_type_id] => 3
[tutor_id] => 0
[tutor_confirmed] => 0
[event_featured] => 0
[event_push] => 0
[event_active] => 0
[invigilator_id] => 0
[event_discount] => 395
[event_max_delegates] => 16
[location_id] => 1
[venue_id] => 1
[supplier_id] => 1
)
)
I'd like to add a nested object under the key 'delegates' for each row using ActiveRecord, that pulls through the delegates attached to the event using a bridge table 'events_delegates_bridge' by comparing the 'event_id' and 'delegate_id columns in that table.
Essentially so that the object looks like so:
Array
(
[0] => stdClass Object
(
[id] => 2
[course_name] => Course 3
[delegates] => Array
(
[0] => stdClass Object
(
[id] => 1
[name] => Joe Bloggs
)
[1] => stdClass Object
(
[id] => 2
[name] => Joe Smith
)
[3] => stdClass Object
(
[id] => 3
[name] => Jane Doe
)
)
[course_description] => Course
[course_price] => 995
[supplier_name] => Supplier 3
[location_country_code] => GB
[location_country] => United Kingdom
[location_city] => London
[venue_name] => Venue 2
[venue_address] => 2 Street
[venue_postcode] => EC2M 7PQ
[venue_city] => London
[venue_county] =>
[venue_country] => United Kingdom
[venue_locality] =>
[event_type] => Materials Only
[event_status] => Confirmed
[course_id] => 2
[event_duration] => 3
[event_start_date] => 2013-09-12
[event_date_added] => 2013-02-26 14:36:06
[event_status_id] => 2
[event_type_id] => 4
[tutor_id] => 0
[tutor_confirmed] => 0
[event_featured] => 0
[event_push] => 0
[event_active] => 0
[invigilator_id] => 0
[event_discount] =>
[event_max_delegates] => 16
[location_id] => 1
[venue_id] => 1
[supplier_id] => 2
)
)
Any ideas how best to achieve this? Thanks.
Event Model
class Event_Model extends CI_Model {
public function get_events() {
$this->db->select( '*' );
$this->db->from( 'courses' );
$this->db->from( 'suppliers' );
$this->db->from( 'locations' );
$this->db->from( 'venues' );
$this->db->from( 'event_type' );
$this->db->from( 'event_status' );
$this->db->join( 'events', 'events.course_id = courses.id AND events.supplier_id = suppliers.id AND events.location_id = locations.id AND events.venue_id = venues.id AND events.event_type_id = event_type.id AND events.event_status_id = event_status.id', 'inner' );
$this->db->order_by( 'events.event_start_date', 'asc' );
$query = $this->db->get();
return $query->result();
}
}
Dashboard Controller
$data['events'] = $this->event_model->get_events();
Delegates Model
I've created this to get the delegate data. Do you think it can be used to add the correct delegates to the events object?
class Delegate_Model extends CI_Model {
public function get_delegates() {
$this->db->select( '*' );
$this->db->from( 'delegates' );
$this->db->from( 'events_delegates_bridge' );
$this->join( 'delegates', 'delegates.id = events_delegates_bridge.delegate_id', 'inner' );
$query = $this->db->get();
return $query->result();
}
}
Just tested this and it shows a blank page.
You're best off doing it with 2 separate queries.
$events = array();
$result = $this->db->query('SELECT * FROM events WHERE ...');
foreach($result->result_array() as $event) {
$events[$event['id']] = $event;
}
$result = $this->db->query('
SELECT * FROM events_delegates_bridge
JOIN delegates ON (delegate_id = delegates.id)
WHERE ...
');
foreach($result->result_array() as $delegate) {
if (!empty($events[$delegate['event_id']])) {
$events[$delegate['event_id']]['delegates'][] = $delegate
}
}
This bit of code just queries the events and puts them in an array indexed by the event id.
Then, a separate query runs to pull up the delegates, and attaches them to the appropriate event.
use $name=$variable->result_array();var_dump($name); I think This work

Resources