Shop with two items - php

I have two models. Shop and Item. I am trying to retrieve all of the shops, with 2 items each - however, I cannot get it working. The value of $shop['items'] is not changing.
foreach($shops as &$shop) {
foreach($shop->items as $item) {
$item->formatPreview();
}
$shop = $shop->toArray();
$shop['items'] = array_slice($shop['items'], 2);
}
How can I achieve this, and are there a smarter way using Eloquent?

EDITED
$shops = Shop::with('items')->get();
foreach ($shops as $shop) {
echo 'shop name: '. $shop->name . '<br>';
if($shop->items()->count()) {
foreach ($shop->items()->take(2)->get() as $item) {
echo 'item: ' . $item->name . '<br>';
}
}
echo '<hr>';
}
I was testing this inside a route closure so please format it as you want orfourse. Hope it is working now.

Related

Grouping/Ordering Items in PHP Object Array

I have a sitemap that I'm trying to group by URL and stack them based on URL. Here is the sitemap https://www.sitecentre.com.au/sitemap
Basically, I need this to be dynamic. I can make this easily by doing a case, or if statements, like I've done on the Blog section, but I need it dynamic. It needs to look like this:
<li>Web Design
<ul>
<li>Nested in Web Design</li>
<li>Nested in Web Design</li>
</ul>
</li>
All based on the URL. the URL is /design then the nested would be design/nested
Then the next group would be like branding and nested on branding so it's all organised and laid out perfectly, then alphabetically ordered based on something else.
Here is our current PHP:
<?php
$pages = json_decode(file_get_contents('data/pages.json'));
foreach ($pages as $data){
foreach ($data as $key => $values){
if ($values->status === '1' && $values->visible === '1' && $values->slug !== 'home'){
if ($values->slug !== 'blog'){
echo '<li>' . $values->heading . ' - Last Updated: ' . date('d/m/Y', strtotime($values->date_modified)) . '</li>';
} else {
// Get the latest blogs
$blogs = json_decode(file_get_contents('data/blogs.json'));
echo '<li>' . $values->heading . ' - Last Updated: ' . date('d/m/Y', strtotime($values->date_modified)) . '';
echo '<ul>';
foreach ($blogs as $data){
foreach ($data as $key => $values){
if ($values->status === '1' && $values->visible === '1'){
echo '<li>' . $values->heading . ' - Last Updated: ' . date('d/m/Y', strtotime($values->date_modified)) . '</li>';
}
}
}
echo '</li></ul>';
}
}
}
}
?>
It clearly needs a bit of work in order to make this work, but I cannot for the life of me work out where to start.

For Each loop with 2 parent 'columns'

I am trying to expand on some working code, and not sure how to do it.
Basicly the information can be under either information or some othertitle.
but i dont care for this split, i just want to do a foreach for all of them together.
Basicly i currently run a for each loop like this:
foreach ($info_array['information'] as $item) {... do something }
Would it be possible to somehow say, For each info_array 'information' & 'othertitle' as $item?
array is structured like:
information
random number
price
amount
total
random number
price
amount
total
othertitle
random number
price
amount
total
random number
price
amount
total
i tried this, but that didnt work:
foreach ($item_array['information'] as $item and $item_array['othertitle'] as $item)
The first idea that comes in mind is just use two loops - first one iterates over $item_array['information'], second one - over $item_array['othertitle']. Something like this:
foreach ($item_array['information'] as $item) {
echo $item['key1'] . ' -> ' . $item['key2'];
}
foreach ($item_array['othertitle'] as $item) {
echo $item['key1'] . ' -> ' . $item['key2'];
}
But, if you do the same output for each element of each array, you can do this:
$keys = ['information', 'othertitle'];
foreach ($keys as $key) {
echo 'Key is ' . $key . '<br />';
foreach ($item_array[$key] as $item) {
echo $item['key1'] . ' -> ' . $item['key2'];
}
}
And even it the output for arrays is different - you can solve it in this way:
$keys = ['information', 'othertitle'];
foreach ($keys as $key) {
echo 'Key is ' . $key . '<br />';
foreach ($item_array[$key] as $item) {
if ('information' === $key) {
echo 'Info: ' . $item['key1'] . ' -> ' . $item['key2'];
} else {
echo 'Ttile: ' . $item['key1'] . ' and ' . $item['key2'];
}
}
}
And if you have to iterate over all subarrays of $item_array, the solution is the same as in #AbraCadaver answer:
foreach ($item_array as $key => $items) {
echo 'Key is ' . $key . '<br />';
foreach ($items as $item) {
if ('information' === $key) {
echo 'Info: ' . $item['key1'] . ' -> ' . $item['key2'];
} else {
echo 'Ttile: ' . $item['key1'] . ' and ' . $item['key2'];
}
}
}
Since you know the indexes you could array_merge or use +:
foreach ($item_array['information'] + $item_array['othertitle'] as $item) {
// do something
}
Otherwise you need two loops:
foreach ($item_array as $array) {
foreach($array as $item) {
// do something
}
}

Traverse XML file to find products recursively

I am trying to read remote XML file to find products in a specific category. I am facing issue because XML file has nested categories and each category has products. I need help in preparing a recursive function which will output products from a specific category given the category name.
Here is the code i have started with
<?php
$xml = simplexml_load_file('https://www.deere.com/en/us-en.taxonomy');
getProducts($xml);
function getProducts($xml) {
foreach ($xml as $obj) {
if ($obj->getName() == 'en_us_tractors') { //if category name is en_us_tractors
if ($obj->children()) {
foreach ($obj->children() as $child) {
echo '<h1>' . $child->name . '</h1>';
if ($child->products) {
foreach ($child->products as $product) {
foreach ($product as $p) {
echo 'sku is ' . $p->sku . '<br>';
echo 'path is ' . $p->path . '<br>';
}
}
}
}
}
}
}
}
It's simple to use XPath to find the data, this finds all of the products inside the category you want...
$xml = simplexml_load_file('https://www.deere.com/en/us-en.taxonomy');
$products = $xml->xpath("//en_us_tractors//products/*");
foreach ( $products as $product ){
echo $product->sku."/".$product->path.PHP_EOL;
}

Foreach loop for children skipping to last child in XML file

Okay, my script is supposed to open an XML file, recursively loop through all the tags, children, children of children, and so forth spitting out information as we go. Today I noticed an interesting bug in that my foreach loop that is supposed to loop through the children is skipping directly to the last child and I really have no clue as to why.
function theHunt($node)
{
$tagName = '';
print 'I am starting with ' . $node->getName() . ' It should have ' . $node->count() . ' children. The first child should be: ' . $node->children()->getName() . '<br>';
foreach ($node->children() as $child);
{
print $child->getName() . '<br>';
if (isset($child))
{
print 'I found: ' . $child->getName() . ' I\'ll see if it has kids' . '<br>';
$this->theHunt($child);
}
else
{
print 'No kids here, I\'m going to stop digging.<br>';
}
//Now that I am all the way down or working my way back up. I start gathering my information.
$tagName = $node -> getName();
if($this->rootNode->$tagName[0] !== NULL)
{
foreach ($this->rootNode->$tagName[0]->attributes() as $a => $b) ;
{
//echo $a, '="', $b, "<br>";
}
}
//print_r($node);
print'<br> I kicked out <br>';
}
}
The really weird part is that the line:
print 'I am starting with ' . $node->getName() . ' It should have ' . $node->count() . ' children. The first child should be: ' . $node->children()->getName() . '<br>';
Is outputting all the correct information, but the moment I drop into the foreach loop I skip right to the very last child.
Your code is mishandling the tree structure. You should follow the structure below:
function theHunt($node)
{
foreach ($node->children() as $child);
{
if (isset($child))
{ //has children
theHunt($child); //go one level down
}
else
{ //this is a child
//enum thru its attributes
}
}
}
Okay, I figured it out. Really rookie mistake. I put a semicolon after my foreach. Took it out, I'm running beautifully.

how to get the attribute group in magento

I need to get the attribute group of a certain attribute set , how can i do this ?
i think i got the attribute group id but i can't seem to get the attributes of that group.
$attributes = $_product->getAttributes();
foreach($attributes as $attribute)
{
$group_id = $attribute->getData('attribute_set_info/' . $_product->getAttributeSetId() . '/group_id');
print_r($group_id);
}
I would really appreciate if somebody could help me out , thanx ;)
Just use the ID to instantiate the model you want.
$product = Mage::getModel('catalog/product')->getCollection()->getFirstItem();
foreach($product->getAttributes() as $att)
{
$group_id = $att->getData('attribute_group_id');
//Mage_Eav_Model_Entity_Attribute_Group
$group = Mage::getModel('eav/entity_attribute_group')->load($group_id);
var_dump($group);
}
You can try this, will return all Attribute Group of Magento
$attributeSetCollection = Mage::getResourceModel('eav/entity_attribute_group_collection')
->load();
foreach ($attributeSetCollection as $id=>$attributeGroup) {
echo 'group-name: '; echo $attributeGroup->getAttributeGroupName();
echo '<br>';
echo 'group-id: '; echo $attributeGroup->getAttributeGroupId();
echo '<br>';
echo 'set-id: '; echo $attributeGroup->getAttributeSetId();
echo '<br>';
}

Categories