I am currently facing a dilemma within the project I am currently completing. I have a database where two tables are linked through foreign keys which are of data type int.
What I am trying to do is retrieve a value from tblProductColour and using the INNER JOIN function add the information to the other table which is tblProducts.
Here is the controller code,
public ActionResult Index()
{
var db = WebMatrix.Data.Database.Open("Database");
if (Session["AdministrationTeam"] != null)
{
View = "ViewAllProducts";
}
else
{
return RedirectToAction("Login", "Home");
}
return View(View, odb.tblproducts.SqlQuery("SELECT * FROM tblproducts t INNER JOIN tblProductColour ot ON t.ID = ot.ProductColour"));
}
The getters and setters that are stored within the model itself are as shown below,
public int? ProductColour {get;set;}
Here is the code I am using in my View to retrieve the information from the model,
#foreach (var item in Model) {
<div class="col-lg-3 col-md-6 col-sm-6 ">
<img src="#item.ProductImagePath" alt="Image Should Show Here" style="margin:auto auto; background-color:#fff;" width="100%" height="20%"/>
<p>Product Id: #Html.DisplayFor(modelItem => item.Id) </p>
<p> Product Name: #Html.DisplayFor(modelItem => item.ProductName)</p>
<p>Supplier: #Html.DisplayFor(modelItem => item.ProductSupplier) </p>
<p>Colour: #Html.DisplayFor(modelIteem => item.ProductColour) </p>
<p>Product Quantity: #Html.DisplayFor(modelItem => item.ProductQuantity)</p>
<p> Product Description: #Html.DisplayFor(modelItem => item.ProductDescription)</p>
</div>
When retrieving the information, I am faced with this error shown below,
Conversion failed when converting the varchar value 'Red' to data type int.
Here are the entities,
tblproducts
tblProductColour
If anyone has any solutions to how I can display the information it would be appreciated.
You're joining the wrong items
"SELECT * FROM tblproducts t INNER JOIN tblProductColour ot ON t.ID = ot.ProductColour"
should be
"SELECT * FROM tblproducts t INNER JOIN tblProductColour ot ON t.ProductColour = ot.ID"
You then need to change
#Html.DisplayFor(modelIteem => item.ProductColour)
in your view to (assuming the correct reference name for the table is tblProductColours) to
#Html.DisplayFor(modelIteem => item.tblProductColours.ProductColour)
- or
#Html.DisplayFor(modelIteem => item.ProductColours.ProductColour) -
may also work - just depends on how it is referenced, hopefully autocomplete will help you out.
Related
To quickly summarize, my first table's rows gets multiplied by the amount of rows in the second table possibly due to my join:
Table 1:(bbr_group) I only have two rows
1
Table 2:(bbr_group_type) I only need to get group_type_name, joined by group_type_id
2
Controller:
public function fetchgroup(){
$all_groups = HmsBbrGroup::join('hms_bbr_group_type', 'hms_bbr_group.group_type_id', '=', 'hms_bbr_group.group_type_id')
->orderBy('group_id', 'ASC')->get();
return response()->json([
'all_groups'=>$all_groups,
]);
}
Table: (with ajax)
success: function (response){
var tbody="";
$.each(response.all_groups, function (key, group) {
tbody+=`
<tr>
<td><p class="font-weight-bold mb-0">${group.group_name}</p>${group.group_description}</td>
<td><p>${group.group_type_name}</p></td>
<td><p>users here</p></td>
<td><p>status here</p></td>
<td>
<button type="button" value="${group.group_id}" class="edit_group btn btn-outline-secondary"><i class="fas fa-edit"></i> Edit</button>
<button type="button" value="${group.group_id}" class="delete_group btn btn-outline-secondary"><i class="fas fa-trash"></i> Delete</button>
</td>
</tr>`;
});
$('#main-group-list tbody').html(tbody)
}
Here is the situation of my output: As you can see even if there are only two rows, it shows the row with every group name from the second table
3
I need to show the table with only the two rows in hms_bbr_group with the left join group_type_name. In this case, both are "General"
I am not sure if the join in the controller or the table is the reason for the duplicates. I'd like some feed back if this line is correct:
$all_groups = HmsBbrGroup::join('hms_bbr_group_type', 'hms_bbr_group.group_type_id', '=', 'hms_bbr_group.group_type_id')
Any help would be great. thank you
Your joining condition is wrong. You are using hms_bbr_group table in both side of equal. I think this would be like following:
$all_groups = HmsBbrGroup::join('hms_bbr_group_type', 'hms_bbr_group.group_type_id', '=', 'hms_bbr_group_type.group_type_id')
->orderBy('group_id', 'ASC')->get();
I just moved to the laravel framework and am starting to migrate some legacy sites and I have hit a problem with SQL or blade - dunno which.
I have to display a load of rows 'sports classes' which are grouped by year and then month. each needs to show attendance etc.
I am unsure which way to proceed.
I am able to display all rows and sort by date - easy squeezy
I am able to groupBy year AND month - fiddly but sorted it.
These are all displayed in an accordian.
Click the month - the individual rows drop down - you get the idea
I can get a number of rows per month/year
What I am unable to figure out is how to actually display the rows.
The groupBy is this:
$LinkClasses = DB::table('classes_lists')
->select('id, class, teacher, size')
->select(DB::raw('YEAR(date) AS year, MONTH(date) AS month, MONTHNAME(date) AS month_name, COUNT(*) post_count'))
->groupBy('year')
->groupBy('month')
->orderBy('year', 'desc')
->orderBy('month', 'desc')
->orderBy('id', 'desc')
If the code you provided is within your controller, then you can append ->get() after your last ->orderBy(). This will return a Collection. You can then do whatever you want with the Collection (http://laravel.com/api/master/Illuminate/Support/Collection.html), including conversion to an array using ->toArray(), but I think it would be best to utilize the Eloquent ORM if possible.
Anyway, once you have it in the format you want, just pass it to the view like so:
return view('your.view', compact('LinkClasses'));
Then, inside the your.view blade template, you can access this by using the following:
#foreach ($LinkClasses as $currentRow)
<tr>
<td>{{ $currentRow['id'] }}</td>
<td>{{ $currentRow['class'] }}</td>
<td> ... </td>
</tr>
#endforeach
Best guess I can offer without seeing the blade template to get a better idea of what you're doing. Hope that helps!
UPDATE BASED ON OP FEEDBACK:
Since you are only receiving a single record, it seems as though the issue lies in your query. I suggest you simplify your query to fetch all records and then do your sorting within an array. Something like this in your controller:
$allClasses = DB::table('classes_lists')->all();
foreach ($allClasses as $currentClass) {
$yearMonth = date('Y-m', $currentClass['date']);
$classesByYearMonth[$yearMonth][] = $currentClass;
}
ksort($classesByYearMonth);
/* now you have an array of all classes sorted by year-month like this:
// $classesByYearMonth[2014-01] = array(
// [0] => array(1, 'class name', 'teacher name', 23),
// [1] => array(2, 'another class', 'different teacher', 25),
// ...
// );
//
// $classesByYearMonth[2014-02] = ...
*/
return view('your.view', compact('classesByYearMonth'));
Then, inside your blade template:
#foreach ($classesByYearMonth as $yearMonth => $classListArray)
Found {{ sizeof($classListArray) }} classes for {{ $yearMonth }}
#foreach ($classListArray as $currentClass)
<div>
<div>ID: {{ $currentClass['id'] }}</div>
<div>Class: {{ $currentClass['class'] }}</div>
<div>Teacher: {{ $currentClass['teacher'] }}</div>
<div>Size: {{ $currentClass['size'] }}</div>
</div>
#endforeach
#endforeach
I will leave it to you to fix the formatting to make your accordion work. But hopefully that will get you on the right path.
DNoe - thank you so much.
Your reply put me on exactly the right track.
I had to mod some bits due to laravel ambiguities and add the strtotime but the logic was all there.
foreach ($allClasses as $currentClass) {
$ym = $currentClass['date'];
$yearMonth = date("Y-m",strtotime($ym));
$classesByYearMonth[$yearMonth][] = $currentClass;
}
krsort($classesByYearMonth);
return View::make('classes.index', compact('classesByYearMonth'));
The css is simple from here.
I owe you some beers. And thanks for helping me take my head from my butt!
Send me a pm and i would be very very happy to forward beer donation :o
Great work and thank you again. :)
Also, part of the problem was that the results were throwing an stdObject rather than an array.
Being able to compare your code with my own has enabled me to create a dbquery with multiple joins from which meaningfull data is selected and then converted to an array.
$classes = DB::table('table2')
->join('table1', 'table2.id', '=', 'table1.id2' )
->join('table3', 'table1.id3', '=', 'table3.id' )
->orderBy('classes_lists.date','DESC')
->get(array('table1.id', 'teacher', 'date', 'size', 'students', 'fname', 'classname', 'table1.notes'));
$cfr = count($classes);
foreach($classes as $object)
{
$arrays[] = (array) $object;
}
foreach ($arrays as $currentClass){
$ym = $currentClass['date'];
$yearMonth = date("Y-m",strtotime($ym));
$clazByYearMonth[$yearMonth][] = $currentClass;
}
krsort($clazByYearMonth);
This was the output into blade:
Not formatted :
#foreach ($clazByYearMonth as $yearMonth => $classListArray)
Found {{ sizeof($classListArray) }} classes for {{ $yearMonth }}
#foreach ($classListArray as $currentClass)
<div>
date: {{ $currentClass['date'] }} | class: {{ $currentClass['classname'] }} | Size: {{ $currentClass['size'] }} Teacher: {{ $currentClass['fname'] }} |
</div>
#endforeach
#endforeach
So I am deeply confused. I have two tables, one is locations assignments which consists of: location id, type and type id. I also have a table called services, which consists of name, id , description and icon.
The idea is to say, get me back all 13 services, from that we create 13 checkboxes. then we say, check the location assignments table, if this services (based on type, id and location id) matches a service in that list, check the checkbox, else leave it unchecked.
What I ahve so far is:
public static IEnumerable<Constants.Assignable> getAllService(int id)
{
List<Constants.Assignable> assign = new List<Constants.Assignable>();
using (var db = new Context())
{
var serv = from s in db.Services
join la in db.LocationAssignments on s.id equals la.typeId into LocationAssignments
from la in LocationAssignments
where la.locationId == id && s.id == la.typeId && la.type == Constants.SERV
select s;
foreach(var s in serv)
{
assign.Add(new Constants.Assignable(){
id = s.id, name = s.name
});
}
return assign;
}
}
which returns me, currently, two services, when it should return me 13. So there is something wrong with my join.
from there we do:
<h3 class="muted">Services Nearby</h3>
IEnumerable<UFA.Location.Core.Constants.Assignable> ServicesNearby = UFALocationApp.Helpers.LocationHelper.QueryHelper.getAllServicesNearby(Model.id);
foreach (var servicenb in ServicesNearby)
{
<div class="control-group">
<label class="control-label" for="serviceNearBy">
#servicenb.name
</label>
<div class="controls">
<input type="checkbox" id="Locationservice" value="#servicenb.id" name="serviceNB" checked="#(servicenb.assigned ? "checked" : "")" />
</div>
</div>
}
which prints out the two check boxes that are, in this case checked. there should be 11 more that are unchecked.
What do I have to change in my query to say: get me all services and only check off the ones associated with this location?
To make it a LEFT JOIN, you need to use DefaultIfEmpty(), it seems that the component you're missing to make this work;
var serv = from s in db.Services
join la in db.LocationAssignments on s.id equals la.typeId
into LocationAssignments
from la in LocationAssignments.DefaultIfEmpty()
where la.locationId == id && s.id == la.typeId
&& la.type == Constants.SERV
select s;
If I read you well you're after something like this:
var assign = (from s in db.Services
select new Constants.Assignable
{
id = s.id,
name = s.name,
checked= db.LocationAssignments.Any(la => la.typeId == s.id)
}).ToList();
Now you can change the value of checked by clicking the checkbox and process the changes when you post back.
I'm obviously missing something obvious here but I cant for the life of me work out what, I've setup a view to display a custom SQL query, but the screen is showing nothing, here's what I've got
Controller
def queueBreakdown(){
String SQLQuery = "select state, count(test_exec_queue_id) as 'myCount' from dbo.test_exec_queue group by state"
def dataSource
def list = {
def db = new Sql(dataSource)
def results = db.rows(SQLQuery)
[results:results]
}
}
If I run this manually I get a set of results back like so
state myCount
1 1
test 2
test2 1
The queueBreakdown.gsp has the following...
<body>
<g:message code="default.link.skip.label" default="Skip to content…"/>
<div class="nav" role="navigation">
<ul>
<li><a class="home" href="${createLink(uri: '/')}"><g:message code="default.home.label"/></a></li>
</ul>
</div>
<div id="queueBreakdown-testExecQueue" class="content scaffold-list" role="main">
<h1><g:message code="Execution Queue Breakdown" /></h1>
<table>
<thead>
<tr>
<g:sortableColumn property="Run State" title="Run State"/>
<g:sortableColumn property="Count" title="Count" />
</tr>
</thead>
<tbody>
<g:each in="${results}" status="i" var="it">
<tr class="${(i % 2) == 0 ? 'even' : 'odd'}">
<td>${it.state}</td>
<td>${it.myCount}</td>
</tr>
</g:each>
</tbody>
</table>
</div>
</body>
But when I view the page I get nothing... The table has been built but there are no lines in it, what am I being thick about here?
Cheers
your controller code is really confusing, what is the action here ? queueBreakdown() or list() ? It seems like you have mixed up 2 actions together, and queueBreakdown() is not returning any model...
class SomeController {
def dataSource
def queueBreakdown() {
String SQLQuery = "select state, count(test_exec_queue_id) as 'myCount' from dbo.test_exec_queue group by state"
def db = new Sql(dataSource)
def results = db.rows(SQLQuery)
[results:results]
}
}
Many thanks for reading this.
I have asked as well in Adobe forums with no luck.
I am building a small library application for school books.
I have created a database with lets say 2 tables
Books ( ID_Book , Writer , Title , Copies) and
Loans ( ID_Book , Load_ID , Loan_Date ) etc
I have used correctly spry to create easily a table which print the book list in a table
with pagination .
var ds1 = new Spry.Data.XMLDataSet("ajaxquery.php", "root/row", {sortOnLoad: "Writer", sortOrderOnLoad: "ascending"});
ds1.setColumnType("ID_Book", "number");
var pv1 = new Spry.Data.PagedView( ds1 ,{ pageSize:10 });
var pv1PagedInfo = pv1.getPagingInfo();
pv1.setColumnType("ID_Book", "number");
I have made the necessary declarations to produce the dataset for the Loans
var ds3 = new Spry.Data.XMLDataSet("ajaxallloans", "root/row", {sortOnLoad: "ID_Book", sortOrderOnLoad: "ascending"});
ds3.setColumnType("ID_Book", "number");
ds3.setColumnType("ID_Dan", "number");
I would like to find a way to change the table row color for the BOOKS table IF an ID_Book is within the Loans table - ds3.
The table is created
<div spry:region="pv1" id="bibliapv">
<div spry:state="loading" class="loading" >Loading...</div>
<div spry:state="ready">
<table>
<tr >
<th width="75" spry:sort="ID_Book"> Book No</th>
<th width="123" spry:sort="Writer">Writer </th>
etc...
</tr>
<tr spry:repeat="pv1" spry:select="mySelectClass" spry:hover="hover">
<td >{ID_Book}</td>
<td>{writer}</td>
etc ..
</tr>
</table>
</div>
</div>
<div>
Many thanks again.
Dinos - Greece
Many thanks again for reading .
I found a solution based on the ideas drawn from
labs.adobe.com/technologies/spry/samples/data_region/CustomColumnsSam ple.html
I have added the following code:
created a css rule
lets say
.match {
background-color: #0CF;
}
In spry:region add the class {cssrule} which is added dynamically shortly after <tr class="{cssrule}" spry:repeat="pv1" spry:select="mySelectClass" spry:hover="hover">
3.
Then just before closing tag added (you could put it earlier in code)
<script type="text/javascript">
ds2.addObserver({
onPostLoad:function( ds2, data ){
var data = ds2.getData();
var pv1data = pv1.getData();
for( var i = 0; i < pv1data.length; i++ )
{
for (var j =0 ; j< data.length ; j++)
{ if ((data[j].Writer).toString()== (pv1data[i].Writer).toString() ) //or whatever you like!
{pv1data[i].cssrule="match"; }
}
}
}
});
</script>