Symfony DomCrawler can't get attribute - laravel

For this div:
<section class='menu'>
<div class="some beautiful classes" link="/some/path/in/web">
<div class="contImgHome">
<img src="/images/icon.jpg alt="">
</div>
<h5>
Go to Section
</h5>
</div>
<div class="some beautiful classes" link="/another/path/in/web">
<div class="contImgHome">
<img src="/images/icon.jpg alt="">
</div>
<h5>
Go to another section
</h5>
</div>
</section>
I make:
$response = $guzzleClient->request('GET', $url_base);
$crawler = new Crawler( (string) $response->getBody() );
$crawler->filter('section.menu > div')->each(function( Crawler $div, $i )
{
$xx = $div->extract( [ 'class', 'link'] );
print_r( $xx );
echo PHP_EOL;
die;
});
And return this:
Array
(
[0] => Array
(
[0] => some beautiful classes
[1] =>
)
)
I also tried with:
$div->attr('classes');
$div->attr('link');
But 'link' attribute is always empty. ¿Why I cant get "link" attribute?
I'm using Laravel 5.2 and installed Symfony Crawler vía composer.

It was my mistake!
Web set that "link" attribute with javascript using Document Ready, so attr is not existing when Crawler gets page DOM.

Related

in laravel error Invalid argument supplied for foreach() (View: i cant see $value

I am trying to make a blog foreach that see this erro
Invalid argument supplied for foreach() (View:
my site https://weadam.com/
my code is:
controller:
public function index()
{
$localetmp = Session::get('locale');
$blogs = #json_decode(file_get_contents("https://www.weadam.com/blogs/wp-json/wp/v2/posts?per_page=4&lang=" . $localetmp));
$blogsold = Blog::orderBy('id','desc')->take(100)->get();
foreach ((array)$blogs as $blog){
// $blog->elapsed = $this->time_elapsed_string('#'.$blog->date);
// $blog->date = date("d F Y",$blog->date);
}
$error = "";
return view('home-new',['blogs' => $blogs]);
}
and view is:
<div class="eng-home-blog">
<div class="col-sm-9 home-post-disp bx-shadow brd-radius bg-white">
#foreach($blogs as $blog)
#if($blog->sticky)
<img src="{{$blog->fimg_url}}" alt="" class="img-responsive brd-radius bx-shadow">
<div class="hm-post-date">{{$blog->date}}</div>
<h3>{{$blog->title->rendered}}</h3>
<p>{!!$blog->excerpt->rendered!!}</p>
<span>{{__("READ MORE")}}</span>
#endif
#endforeach
</div>
</div>
pass true as secondary param at json_decode function
$blogs = #json_decode(file_get_contents("https://www.weadam.com/blogs/wp-json/wp/v2/posts?per_page=4&lang=" . $localetmp),true);
now you have to fetch this blog data as associative data. not as an object. here's the example code for it
<div class="eng-home-blog">
<div class="col-sm-9 home-post-disp bx-shadow brd-radius bg-white">
#foreach($blogs as $blog)
#if($blog['sticky'])
<img src="{{$blog['fimg_url']}}" alt="" class="img-responsive brd-radius bx-shadow">
<div class="hm-post-date">{{$blog['date']}}</div>
#endif
#endforeach
</div>
</div>
You can simply use forelse it will take care of the empty case
#forelse($blogs as $blog)
// do what ever you want to do
#empty
<div> Nothing to show </div>
#endforelse
Hope it helps reference.
Thanks.

Populated Array Throws Undefined Offset: 1 In Laravel 5.6

I have a laravel 5.6 query that populates an array with a single result to pass to a view.
This is the query :
$farms=DB::select("select pic,shop_name,StateName,amount,
duration,ror,farm_shop.id from farm_shop join states on states.id=farm_shop.state_id where
farm_shop.id=:id",['id' => $id]);
A print_r command returns this:
Array ( [0] => stdClass Object ( [pic] =>
092720180421565bac5ae4974f9MAIN.jpg
[shop_name] => Pig Farm [StateName] => STATEGTR
[amount] => 3000000 [duration] => 11 [ror] => 28 [id] => 4 ) )
My view accesses it like this :
#foreach($farms->as $farmshop)
<div class="row course-set courses__row col-md-6 col-offset-md-3">
#php
$pic="http://xxxxxx/storage/".$farmshop['pic'];
#endphp
<section class="col-md-3">
<img src="{{$pic}}" alt="" width="1200" height="1200" title="xxx">
<div class="text-center"><b>{{$farmshop['shop_name']}}</b><br/>
Location: {{$farmshop[['StateName']}}
<br/>
Amount (X):<span style="color:red">
<b> {{number_format($farmshop['amount'],2)}}</b></span><br/>
Period (Months): {{$farmshop['duration']}}<br/>
Rate (%): {{$farmshop['ror']}} <div class="text-
center">
<div class="col-md-12 text-center">
Invest
</div>
<div> </div>
</div>
</section>
</div>
#endforeach
The error that now gets thrown is :
Undefined offset: 1
And the below line gets highlighted in the inbuilt laravel function compileForEach($expression):
$iteratee = trim($matches[1]);
Please how do i resolve this?..Thanks
The problem was in this line of code: #foreach($farms->as $farmshop)
Correct code is supposed to be : #foreach($farms as $farmshop)
Thanks

Illegal string offset Laravel 5.3

I'm trying to build a 'breadcrumb' thing dynamically using an array and this pass it to the view
In my controller
//build the breadcrumbs
$breadcrumbs = [
[
'link' => 'test link',
'name' => ''
]
];
return view('home',compact('breadcrumbs'));
the 'home.blade.php' extends the 'master.blade.php' where the
#include('_breadcrumbs')
resides. And here's the contents of '_breadcrumbs.blade.php'
<!-- Page Heading -->
<div class="row">
<div class="col-lg-12">
<ol class="breadcrumb">
<li #if(!isset($breadcrumbs))class="active"#endif>
<i class="fa fa-dashboard"></i> Dashboard
</li>
#if(isset($breadcrumbs))
#foreach($breadcrumbs as $b)
<li>{{$b['name']}}</li>
#endforeach
#endif
</ol>
</div>
</div>
<!-- /.row -->
but it gives me this error
Illegal string offset 'link' (View:
C:\laravel-projects\dinhi_ecommerce\resources\views_breadcrumbs.blade.php)
(View:
C:\laravel-projects\dinhi_ecommerce\resources\views_breadcrumbs.blade.php)\
If I do
dd(var_dump($breadcrumbs));
it gives me the contents of the array 'breadcrumbs' which means it does work in the controller part.
Any ideas, help please?
UPDATE:
I actually have a line where I modify the index 'name' in the array which I forgot to include in my post. Anyway I change it from
$breadcrumbs['name'] = categories::where('cat_id',$id)->first()->cat_name;
to
$breadcrumbs[0]['name'] = categories::where('cat_id',$id)->first()->cat_name;
and now its working :)

How Can I check if Page is single page of Custom Post Type in Wordpress

On the Homepage I have called the content of Custpm Post Type with Ajax with this code
jQuery(document).ready(function($){
$.ajaxSetup({cache:false});
$(".sector_item a").click(function(){
var post_link = $(this).attr("href");
$(".sector_item_content").html("<div class='load' style='position:relative;margin-top:50px;padding-bottom:50px;'><img src='<?php bloginfo("template_url"); ?>/images/ajax-loader.gif'></div>");
$(".sector_item_content").load(post_link);
return false;
});
});
But In the single Page of this same Post Type I need to have another design with content so how I can detect the single page. I tried with Wordpess is_single() function but it doesnt work it displayed anyway in ajax.How Can I fix this?
Here is my single template.php
<?php if(is_singular('sector' )){
echo 'Single page' ;
}else{
while (have_posts() ) : the_post(); ?>
<div class="container single_page_inner">
<div class="row clearfix">
<div class="col-md-10 column" style="float:none;margin:auto;">
<div class="sector_single_title">
<?php the_title();?>
</div>
<div class="single_sector_contentmain"> <?php the_content();?></div>
<div class="single_sector_more">
<img src="<?php bloginfo(template_url);?>/images/single_secormoreline.png"/>
<div class="single_sector_button">
<span class="single_sec_leftline"></span>
<span class="single_sec_rightline"></span>
Click Here To Read More
<div class="more_line"></div>
</div>
</div>
</div>
</div>
</div>
<?php endwhile; ?>
If you want to be able to do different things when your single.php files is loaded from Ajax, you need to tell it somehow when it is getting an Ajax request.
One approach would be to add a parameter to your call, like this:
var post_link = $(this).attr("href") + '?isajax=1';
Then in your single.php file, test for this parameter:
<?php if(!empty($_GET['isajax'])){
// This is Ajax - display in Ajax format
} else {
// This is not Ajax - display in standard single.php format
}
use this:
is_singular('YOUR_POST_TYPE' );
CODEX

Magento - Four Products on Homepage - Random Order - Foreach Loop

I'm trying to get 4 random products on the homepage using PHP within a TPL file I've created. I'd like to be able to format the products in a foreach loop as I'm using some formatting in the code seen below...
<div class="three columns">
<div class="product_container no_border">
<div class="product">
<img src="<?php echo $this->getSkinUrl('images/products/place_holder.jpg'); ?>" alt=" ">
</div>
<div class="product_title">
240 Serving Package
</div>
<div class="price_hp">$454.99</div>
<div class="free_shipping">
<div class="fs"></div>
Free shipping for this package
</div>
<div class="shop_btn">
ADD TO CART
</div>
</div>
</div>
I don't know what PHP to use though to grab the 4 products from any category and randomize the order. May I have some guidance please?
Thanks!
use following code in your phtml file for randomize products..
$categoryid = 15;
$category = new Mage_Catalog_Model_Category();
$category->load($categoryid);
$products = $category->getProductCollection();
$products->addAttributeToSelect('*');
$products->getSelect()->order('RAND()');
$products->getSelect()->limit(4);
foreach($products as $prod)
{
echo $prod->getName() ."<br>";
$img=$prod->getSmallImageUrl();
echo "<img src='$img'>" ."<br>";
}

Resources