I have an array of fonts which i displaying in the page using blade templates #foreach like this
#foreach($data as $fonts)
<li class="abc">
<div class="am-actions"><span><i class="icon add"></i></span></div>
Grumpy wizards make toxic brew for the evil Queen and Jack.
</div>
<div class="am-font-details">
<div class="am-font-name">{!! $fonts['font_name'] !!}</div>
<div><?php echo $count = count($fonts['variants']); ?> Styles</div>
</div>
<div class="am-font-options">
<h4>SELECT VARIANTS TO INCLUDE:</h4>
<ul>
#foreach($fonts['variants'] as $variants)
<li><label><input type="checkbox">
{{ $variants }}
</label></li>
#endforeach
</ul>
<h4>LANGUAGE/SCRIPT TO INCLUDE:</h4>
<ul>
#foreach($fonts['subsets'] as $subsets)
<li><label><input type="checkbox">{{ $subsets }} </label></li>
#endforeach
</ul>
</div>
</li>
#endforeach
as you can see i use #foreach for variants and languages inside the main loop the variants and langauges is listed with checkbox so the user can selected their choice and add. on the add the selected variants and languages with the main font array details should be passed through ajax please help me :-(
Since this an ajax question this belongs to Jquery section.
Anyway you can't use ul tag inside li tag. so I made some changes to your html code. I hope this what you need(change php variables appropriately)
/**
* Blade File
**/
#foreach($data as $fonts)
<div class="abc">
<div class="am-actions">
<span><i class="icon add"></i></span>
</div>
Grumpy wizards make toxic brew for the evil Queen and Jack.
</div>
<div class="am-font-details">
<div class="am-font-name">{!! $fonts['font_name'] !!}</div>
<div><?php echo $count = count($fonts['variants']); ?> Styles</div>
</div>
<div class="am-font-options">
<h4>SELECT VARIANTS TO INCLUDE:</h4>
<ul>
#foreach($fonts['variants'] as $variant)
<li><label><input type="checkbox" name="variant" value="{{$variant->id}}">{{ $variant->name }}</label></li>
#endforeach
</ul>
<h4>LANGUAGE/SCRIPT TO INCLUDE:</h4>
<ul id="subsets">
#foreach($fonts['subsets'] as $subsets)
<li><label><input type="checkbox" name"subset">{{ $subsets }} </label></li>
#endforeach
</ul>
</div>
</div>
#endforeach
/**
* Jquery
**/
$(document).on('change', 'input[name=varient], input[name=subset]', function(){
if($('input[name=varient]:checked').length > 0 && $('input[name=subset]:checked').length > 0){
var variants = [];
var subsets = [];
$('input[name=varient]:checked').each(function(index, element){
varients.push($(element).val());
});
$('input[name=subset]:checked').each(function(index, element){
subsets.push($(element).val());
});
$.ajax('<route-to-controller-method>', {
type : 'POST',
data : {
'varients' : variants,
'subsets' : subsets
}
});
}
});
Related
I need your help. Lets say I have array with 10 elements and every record to be shown on every iteration and I want to be done with Livewire, let me share with you some code and will tell you what I have tried till now.
public $content;
public $array = ['first', 'second', 'third' ,'fourth'];
foreach ($array as $item) {
sleep(1);
$this->content[] = "Element ${item}";
}
<div class="modal-body">
#if ($content)
<ul class="listree">
#foreach ($content as $element)
<li>
<div class="listree-submenu-heading">
{!! $element['title'] !!}
</div>
<ul class="listree-submenu-items">
#foreach ($element['elements'] as $sub)
<li>
<div class="listree-submenu-heading">
{!! $sub['title'] !!}
</div>
</li>
#endforeach
</ul>
</li>
#endforeach
</ul>
#endif
</div>
My idea is display first record, wait 1 second, display first and second record, wait 1 second, display first, second and third records and so on... How I can do this with livewire. The problem is that $content is filled with the information after all iteration and then the component is refreshed.
I tried on every iteration to send custom event which will call refresh method, but without succes. I will appreaciate any advice, and if you need more information, I will provide it.
Assumming you're also using alpinejs, this can be done pretty easily.
<x-app-layout>
<div class="text-gray-800 ml-10">
<ul class="bg-green-200">
<!-- The main foreach loop. -->
#foreach (range('a', 'z') as $element)
<!-- render al <li> tags with display:none. -->
<!-- Show the 1st after 0s, the 2nd after 1s, the 3rd after 2s, ... -->
<li class="bg-blue-200 m-5"
x-data="{show: false, index: {{ $loop->index }} }"
x-init="setTimeout(() => show = true, index * 1000)">
<div x-show="show">
{{ $element }}
...
</div>
</li>
#endforeach
</ul>
</div>
</x-app-layout>
$loop is a special object you can access within a #foreach block.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
my controller function is. I passed data through bidder array but array data can't print
public function bidders()
{
$payments = \App\Models\Payment::all();
$posts = Post::all()->where('user_id','=',Auth::user()->id);
foreach ($posts as $post) {
$postid = $post->id;
$bidder[] = Bid::all()->where('post_id','=',$postid);
}
return view('user.student.student-post-bidders',compact('bidder','posts','payments'));
}
Here is my blade in this blade my data print from DB.
How can I print array? indexes of array print in a blade
#php
dd($bidder);
$index = 0;
#endphp
#foreach($bidder as $bid)
#foreach($posts as $post)
#if($post->id == $bid[$index]['post_id'])
<div class="row" >
<!-- Dashboard Box -->
<div class="col-xl-12" >
<div class="dashboard-box margin-top-0">
<!-- Headline -->
<div class="headline">
<h4><strong><span class="bg-danger text-center" style="margin-left: 46px">Your Post Title:</span>{{$bid[$index]->Post->title}}</strong></h4>
</div>
<div class="content">
<ul class="dashboard-box-list">
<li>
<!-- Overview -->
<div class="freelancer-overview manage-candidates">
<div class="freelancer-overview-inner">
<!-- Avatar -->
<div class="freelancer-avatar">
<div class="verified-badge"></div>
#if($bid[$index]->Tutor->profile_Image=='')
<img data-cfsrc="{{asset('asset/images/user-avatar-placeholder.png')}}" alt="" style="display:none;visibility:hidden;">
#else
{{-- {{asset('storage/app/public/profileImages/' . $pic->profile_Image)}}--}}
<img data-cfsrc="{{asset('storage/app/public/profileImages/' . $bid[$index]->Tutor->profile_Image)}}" alt="" style="display:none;visibility:hidden;">
#endif
</div>
<!-- Name -->
<div class="freelancer-name">
<h4>{{$bid[$index]->Tutor->name}}</h4>
<!-- Details -->
<strong><span>Subject:</span></strong>
#foreach($bid[$index]->Tutor->subject as $sub)
<strong><span>{{\App\Models\Subject::find($sub->pivot->subject_id)->subject}}</span></strong>
#endforeach
<!-- Rating -->
<div class="freelancer-rating">
<div class="star-rating" data-rating="{{$bid[$index]->Tutor->rating}}"></div>
</div>
<!-- Bid Details -->
<ul class="dashboard-task-info bid-info">
<li><strong id="{{ $bid[$index]->id}}">PKR:{{$bid[$index]->bid_price}}</strong><span>Fixed Price</span></li>
<li><strong>Days:{{$bid[$index]->days}}</strong><span>Delivery Time</span></li>
</ul>
<!-- Buttons -->
<div class="buttons-to-right always-visible margin-top-25 margin-bottom-0" onclick=" indexID({{$i}})">
<a href="#small-dialog-1"
onclick="moveprice({{ $bid[$index]->id}}), payment({{ $bid[$index]->id}})" class="popup-with-zoom-anim button ripple-effect"
#foreach($payments as $pay) #if($bid[$index]->post_id == $pay->post_id) id="hide" #endif #endforeach><i class="icon-material-outline-check "></i> Accept Bid</a>
{{-- #dd($bid->id)--}}
<i class="icon-feather-mail"></i> Send Message
<a id="btn" class="button gray ripple-effect ico" onclick="removeBid({{$bid[$index]->id}})" title="Remove Bid" data-tippy-placement="top"><i class="icon-feather-trash-2"></i></a>
</div>
</div>
</div>
</div>
</li>
</ul>
</div>
</div>
</div>
</div>
<span style="visibility: hidden">{{$i++}}</span>
#endif
#endforeach
#php
$index++;
#endphp
#endforeach
When I run code error will be occur
offset 1 I need all data print in a blade
How to print array index wise
you should use relations,
if you want to show the list of bidders then in bidders model
app/bidders.php
public function post()
{
return $this->belongsTo('App\Post');
}
Assuming that you have proper standards in table structure for mapping eloquent.
then you need to change your bidders function as below
public function bidders()
{
$payments = \App\Models\Payment::get();
$bidders = Bidder::whereHas('post', function($post) {
return $post->where('user_id','=',Auth::user()->id);
//Provides a filter for bidders, post which are related to user
})->with('post')->get();
return view('user.student.student-post-bidders',compact('bidders', 'payments'));
}
And there should be only one loop for bidders on blade as below.
Here is your answer why array is not appearing in blade check in comments for json_encode
#foreach($bidders as $bid)
{{$bid->id}} //gives you bid parameters
{{json_encode($bid->post)}} //this will get all the post details array/object is not printable on blade so encoded to json just for display purpose
//suppose you need only name from post then
{{$bid->post->name}};
#endforeach
This will lower your code length, as well as improve performance due to eager loading and less looping. Also removes stuff like creating array and maintain index and if condition on blade for id check.
That's it.
Happy Coding!!
I think you just need to refactoring your code ),
first of all use $bid->{$index} instead of this $bid[$index]. you have collection but you can use it as array
do you want to group your bidder by post_id ?
The following Codeigniter controller outputs a JSON when accessed through url.
Codeigniter controller
function ttoday(){
$this->load->model('dashboard_stats');
$tours_today = $this->dashboard_stats->upcoming_tours();
$data_json = json_encode($tours_today);
echo $data_json;
}
Codeigniter Model
function upcoming_tours()
{
mysql_query("SET time_zone = '+3:00'");
$this->db->order_by('from_start_time','asc');
$this->db->where('DATE(from_start_time) = CURDATE()');
$this->db->where('from_start_time > DATE_SUB(NOW(), INTERVAL 90 MINUTE)');
$query = $this->db->get('tours');
return $query->result();
}
Angularjs Controller
translineClassic.controller('homeController', ['$scope', '$http', '$window', '$rootScope', function($scope, $http, $window, $rootScope){
$http.get('http://122.222.22.2/booking/public_html/admin/api/cities')
.success(function(result){
$scope.cities = result;
});
$http.get('http://122.222.22.2/booking/public_html/admin/api/ttoday')
.success(function(result){
$scope.tours = result;
});
}]);
Encoded Json
[{"tour_id":"4260","route_time":"121473438600","tour_route":"12","from_start_time":"2016-09-09 19:30:00","available_seats":"47","start_price":"800.00","additional":"0","book_online":"0","date_created":"2016-08-30 09:50:05"},{"tour_id":"4412","route_time":"111473438600","tour_route":"11","from_start_time":"2016-09-09 19:30:00","available_seats":"47","start_price":"800.00","additional":"0","book_online":"0","date_created":"2016-08-30 11:34:46"}]
In the database where the routes are stored. We have stored the tour_route id and not the route name. There is a separate table that stores the routes, and I can get the route name by using the following code.
function get_route_name($id){
$this->db->select('route_name');
$this->db->where('route_id', $id);
$query = $this->db->get('routes');
if ($query->num_rows() > 0)
{
$row = $query->row();
return $row->route_name;
}
}
Using the code as shown above, I can be able to use the $http for angularjs to get the data and then bind with the view.
The problem arises when I need to show the route name. but instead I have tour_route, I can be able to do this in php using the get_route_name() function, How can I print route name instead of tour_route? Do I have to use the $http again? Please help me on this. I am new to angularJs and any help will be appreciated.
My View
<article class="result">
<div ng-repeat="tour in tours">
<div class="one-fourth heightfix"><img class="img-responsive" src="assets/images/translinebus.JPEG" alt="" /></div>
<div class="one-half heightfix">
<h3>{{ tour.tour_route }} ?</h3>
<ul>
<li>
<span class="ico people"></span>
<p>Max people</strong> <br/>
Seats Remaining </strong> </p>
</li>
<li>
<span class="ico luggage"></span>
<p>Max <strong>3 suitcases</strong> <br />per vehicle</p>
</li>
<li>
<span class="ico time"></span>
<p>Departure Time<br />
<strong><span class="icon-clock" style="color:red;">
{{ tour.from_start_time }} </span>
</strong></p>
</li>
</ul>
</div>
<div class="one-fourth heightfix">
<div>
<div class="price"><small>KES</small> {{ tour.start_price }}</div>
<span class="meta">per passenger</span>
<button value="{{ tour.tour_id }}" type="submit" class="btn grey large">select</button>
</div>
</div>
<div class="full-width information" ng-show="showDetails">
x
<p>Free Wifi, Charging</p>
</div>
</div>
</article>
I have a one to many model.
An WorkoutDetails which has many Exercises.
I was able to display all the data for the Exercises, but I need just the property 'Title'.
In controller I have
public function getDetails($id){
$details = WorkoutDetails::where('parent_id','=',$id)->get();
return View::make('menu_renderer.partials.details')->with('details', $details);
}
And in HTML I have
#foreach($details as $detail)
<div>
<ul>
{{$detail->exercises}}
</ul>
</div>
#endforeach
And the output is:
[{"id":1,"type_id":0,"title":"Exercise one","details":"Details
exercise one","media":"5","created_at":"-0001-11-30
00:00:00","updated_at":"-0001-11-30 00:00:00"}]
[{"id":2,"type_id":0,"title":"Exercise two","details":"Details
exercise two","media":"2","created_at":"-0001-11-30
00:00:00","updated_at":"-0001-11-30 00:00:00"}]
[{"id":3,"type_id":0,"title":"Exercise three","details":"Details
Exercise three","media":"6","created_at":"-0001-11-30
00:00:00","updated_at":"-0001-11-30 00:00:00"}]
[{"id":4,"type_id":0,"title":"Exercise four","details":"Details
Exercise four","media":"6","created_at":"-0001-11-30
00:00:00","updated_at":"-0001-11-30 00:00:00"}]
Which is not bad, but I need just the title or details.
I tried
#foreach($details as $detail)
<div>
<ul>
{{$detail->exercises->title}}
</ul>
</div>
#endforeach
or
#foreach($details as $detail)
<div>
<ul>
{{$detail->exercises.title}}
</ul>
</div>
#endforeach
or
#foreach($details as $detail)
<div>
<ul>
{{$detail->exercises[title]}}
</ul>
</div>
#endforeach
But nothing...
Can anyone give me a hint how can I display just the title or details of each Exercise which belongs to WorkoutDetails model ?
Thank you!
From the dump provided, it seems another array is holding the inner data.
hence try :
#foreach($details as $detail)
<div>
<ul>
{{$detail->exercises[0][title]}}
// or :
{{$detail->exercises[0]->title}}
</ul>
</div>
#endforeach
You can use laravel query function first() to pickup the first object.
#foreach($details as $detail)
<div>
<ul>
{{ $detail->exercises->first()->title }}
</ul>
</div>
#endforeach
I have an array with 38 records.
After iterating over the first 10 I want it to start on a new column.
This is very simple and an example is below, however, I need to add in HTML which makes it difficult:
#for($i = 0; $i < count($records); $i++)
#if($i % 10 == 0)
//start new column
#endif
<li>{{ $records[$i]['name'] }}</li>
#endfor
What the HTML looks like without looping and what it should look like after looping properly:
<li class="col-sm-3">
<li class="dropdown-header">
Record Set
</li>
<li>Record Name</li>
<li>Record Name</li>
<li>Record Name</li>
</li>
Problem is, after 10 records I need it to break out of col-sm-3 and start a new col-sm-3 without the dropdown-header but with each iteration's record name.
How can this be done? Please ask questions if clarification is needed.
If it's an array then you may use array_chunk:
#foreach(array_chunk($records, 10) as $ten_arrays)
<li class="col-sm-3">
#foreach($ten_arrays as $record)
{{ $record['field_name'] }}
#endforeach
</li>
#endforeach
This will output lis like this:
<li class="col-sm-3">
<!-- Ten Items -->
</li>
<li class="col-sm-3">
<!-- Ten Items -->
</li>