I'm trying to save multipliable ids in the database using loop but it only save the last id 3 times so I made for loop to test it
for ($i = 0; $i < 10; $i ++)
{
$this->product_id = $i;
$this->shop_name = $shop;
$this->save();
}
it saves number 9 three times and stops ????
This issue never happened before
You need to create a new instance with each iteration, for example:
for ($i = 0; $i < 10; $i++) {
$this->create(['product_id' => $i, 'shop_name' => $shop]);
}
Your code updates a single instance multiple times.
{"":[{"aprjun":"50","janmar":"31","julsep":"42","octdec":"7","year":"2010"},
{"aprjun":"19","janmar":"6","julsep":"15","octdec":"68","year":"2011"},
{"aprjun":"16","janmar":"4","julsep":"12","octdec":"14","year":"2012"},
{"aprjun":"97","janmar":"9","julsep":"36","octdec":"157","year":"2013"},
{"aprjun":"","janmar":"11","julsep":"","octdec":"","year":"2014"}]}
i am having an json array like this which i need to give input to d3js bt i want it like this
[{"aprjun":"50","janmar":"31","julsep":"42","octdec":"7","year":"2010"},
{"aprjun":"19","janmar":"6","julsep":"15","octdec":"68","year":"2011"},
{"aprjun":"16","janmar":"4","julsep":"12","octdec":"14","year":"2012"},
{"aprjun":"97","janmar":"9","julsep":"36","octdec":"157","year":"2013"},
{"aprjun":"","janmar":"11","julsep":"","octdec":"","year":"2014"}]
how do i get it?
Imagine your json data is like this:
var k = {"":[{"aprjun":"50","janmar":"31","julsep":"42","octdec":"7","year":"2010"},{"aprjun":"19","janmar":"6","julsep":"15","octdec":"68","year":"2011"},{"aprjun":"16","janmar":"4","julsep":"12","octdec":"14","year":"2012"},{"aprjun":"97","janmar":"9","julsep":"36","octdec":"157","year":"2013"},{"aprjun":"","janmar":"11","julsep":"","octdec":"","year":"2014"}]}
so you need to do : k[""] to give you the desired JSON
[{"aprjun":"50","janmar":"31","julsep":"42","octdec":"7","year":"2010"},{"aprjun":"19","janmar":"6","julsep":"15","octdec":"68","year":"2011"},{"aprjun":"16","janmar":"4","julsep":"12","octdec":"14","year":"2012"},{"aprjun":"97","janmar":"9","julsep":"36","octdec":"157","year":"2013"},{"aprjun":"","janmar":"11","julsep":"","octdec":"","year":"2014"}]
Use array indexing. for (var i=0; i < 5; i++) { array [i] = {...} } where 'array' is the name of your variable. Incidentally, why not use the year as a key?
Given that I have a class representing a booking with a start date and an end date, how do i check that a new booking does not clash with one of the existing bookings within the database?
I can do this in SQL, but being new to Linq I am having problems with the || or operator. I can't work out how to do something like (cond1 && cond2) || (cond3 && cond4)
public partial class Booking
{
private int id;
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
}
private string DoubleBooked(Booking booking)
{
var doubles = db.Bookings.Where(p => p.?????? );
// Code to process double bookings
}
You approach many conditions in LINQ the same way you would construct an if statement. In your case the new booking overlaps if there is an old booking, and the new start or new end falls between start and end of the old one or new start is before old start and new end is after old end:
var doubles = db.Bookings
.Where(p => (booking.EndDate <= p.EndDate && booking.EndDate >= p.StartDate) ||
(booking.StartDate >= p.StartDate && booking.StartDate <= p.EndDate) ||
(booking.StartDate <= p.StartDate && booking.EndDate >= p.EndDate))
.ToList();
New end is lower than old end and higher than old start or new start is higher than old start and lower than old end.
Let's have a time arrow where there is an old event marked:
-----------------oldStart-----------------------oldEnd----------------->
Overlapping happens in any of the four following scenarios:
1) ----newStart----oldStart----------newEnd--------oldEnd----------------->
2) --------------oldStart----------newStart-------oldEnd-------newEnd----->
3) -------oldStart----newStart----------newEnd---------oldEnd------------->
4) -------newStart----oldStart----------oldEnd---------newEnd------------->
These lines:
(booking.EndDate <= p.EndDate && booking.EndDate >= p.StartDate) ||
(booking.StartDate >= p.StartDate && booking.StartDate <= p.EndDate)
Cover scenarios 1, 2, and 3.
This line:
(booking.StartDate <= p.StartDate && booking.EndDate >= p.EndDate)
Covers the last scenario.
EDIT: Since you mentioned you feel more comfortable with SQL syntax linq supports a similar syntax as well. Here's the same solution using linq query syntax:
var doubles = from p in Bookings
where (booking.EndDate < p.EndDate && booking.EndDate > p.StartDate) ||
(booking.StartDate > p.StartDate && booking.StartDate < p.EndDate) ||
(booking.StartDate <= p.StartDate && booking.EndDate >= p.EndDate)
select p;
Does this work for you?
private string DoubleBooked(Booking booking)
{
var doubles = db.Bookings.Where(p => p.StartDate >= booking.StartDate && p.StartDate <= booking.EndDate);
// Code to process double bookings
}
With ASP .NET MVC3.
In my controller I have this portion of code
MasterMindDnetEntities context = new MasterMindDnetEntities();
List<MasterMindDnet.Games> Games = (from g in context.Games
join u in context.Users on g.g_u_Id equals u.u_Id
where u.u_Login == User.Identity.Name
select g).ToList();
#ViewBag.Games = Games;
#ViewBag.GameCount = Games.Count;
return View("Index");
In the controll, Games works fine as a list.
In my view I have:
#for (int i = 0; i < ViewBag.GameCount; i++)
{
ViewBag.Games = ViewBag.Games[i];
At line ViewBag.Games = ViewBag.Games[i];, I get the exception :
Cannot apply indexing with [] to an expression of type
'System.Data.Entity.DynamicProxies.
In a previous version I had i < ViewBag.Games.Count; instead of i < ViewBag.GameCount; and it said that ViewBag.Games does not have a definition for Count.
How could I solve this ?
Thank you for your help.
Problem of dynamic proxies mean that you have to declare "statically" what's in your ViewBag.
So in your View, just make :
#{
List<MasterMindDnetGames> games = ViewBag.Games;
for (var i = 0; i < games.Count; i++) {
var game = games[i];//declare it in the scope you need
//bla bla bla
}
}
this is more or less a duplicate of the ones below:
How can i sort java JTable with an empty Row and force the Empty row always be last?
http://www.java.net/node/645655
the only difference is that i would like to insert the new row after the row that is currently selected. the default behavior of DefaultRowSorter would sort immediately when the row is inserted, while what i need is to turn off sorting temporarily so that i could insert the row in a position that i specified, not a position from the sorter
i noticed that there were 2 links provided in How can i sort java JTable with an empty Row and force the Empty row always be last?, but they are not valid anymore, that's why i created this new question.
I copied completely DefaultRowSorter and TableRowSorter from JDK and changed insertInOrder() like below and it seems now working as I expected.
for (int i = 0; i < max; i++) {
if(sortOnInsert)
{
index = Arrays.binarySearch(current, toAdd.get(i));
if (index < 0) {
index = -1 - index;
}
}
else
{
Row rowBeforeAdd = new Row(this, toAdd.get(i).modelIndex - 1);
index = Arrays.binarySearch(current, rowBeforeAdd);
if (index < 0) {
index = viewToModel.length - 1;
}
else
{
index += 1;
}
}