Run MySQL query inside a Laravel Blade template file - php

I have a Laravel blade file where there is a table and i had loaded data using my web.php file. Now I want to add a table column for the vehicle brand. For that I need to access the brand table's data and I need to print the corresponding column brand in the table following are the codes.
This is my web.php
Route::get('/retailer/spares', function () {
$models = DB::table('models')->get();
$brands = DB::table('brands')->get();
$spares = DB::table('spares')->get();
return View::make('Retailer/spares')->with('models', $models)->with('brands', $brands)->with('spares', $spares);
});
This is my spares.blade.php please see the SQL query that had written. Other fields are working correctly.
<table class="table ">
<tr>
<th>Spare Id</th>
<th>Part Number</th>
<th>Name</th>
<th>Brand</th>
<th>Quantity</th>
<th>Price</th>
<th>Warranty</th>
<th>Spare Image</th>
<th>
<input type="text" class="form-control" id="search" placeholder="Search Spare">
</th>
</tr>
<tbody id="tableModel">
<?php
foreach($spares as $spare){
?>
<tr>
<td ><?php echo $spare->id;?></td>
<td ><?php echo $spare->partNumber;?></td>
<td ><?php echo $spare->description;?></td>
<td>
<?php
$brand="SELECT brandName FROM brands WHERE id=' $spare->brand_id'";
?>
</td>
<td ><?php echo $spare->quantity;?></td>
<td ><?php echo 'Rs.'. $spare->price;?></td>
<td ><?php echo $spare->warranty;?></td>
<td><div class="image"><?php echo ' <img class="img-responsive" src="data:image/jpeg;base64,'.base64_encode( $spare->image ).'"/>';?></div></td>
<td>
<a class=" btn btn-success btn-sm" data-toggle="modal" data-target="#modalEdit" onclick="EditBrand('<?php echo $brand->brandName;?>','<?php echo $brand->id;?>')" >Edit </a>
<a onclick="DeleteBrand(<?php echo $brand->id;?>)" style="" class=" btn btn-danger btn-sm" >Delete </a>
</td>
</tr>
<?php }?>
</tbody>

This is not good practice. You can join two table spares and brands in your controller. Try This.
$data = DB::table('brands')
->join('spares', 'brands.id', '=', 'spares.brand_id')
->select('brands.*', 'spares.id as spare_id', '',''....so on)
->get();
Here brands.* get all brands info you can manual get specific attribute from brands tabl as well as spares. You can get every info of brands and spares in $data array if Brands and spares has relationship

Related

How to read out all subcategories in a nested foreach

I'm trying to add nested foreach Loop and want to show all the sub cats related to the to cat under the table row of the top cat and don't know how can I do exactly as the requirement
if any one can help much appreciated I search a lot but don't get the answer Please Have a look at my code and quid how can I do that.
Here is my view.
<div class="conatiner">
<br/>
<br/>
<button class="accordion">Top Category Name </button>
<div class="panel">
<p>Sub Catorgies list under this top category</p>
</div>
<br/>
</div>
<div class="pag">
<table class="table gap">
<thead>
<tr>
<th>Flag No.</th>
<th>Name</th>
<th>On/Off</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php if ($subcats): ?>
<?php foreach ($subcats as $key => $row): ?>
<tr >
<td><?php echo $row->flag_no?></td>
<td><?php echo $row->cat_name?></td>
<td>
<label class="switch">
<input type="checkbox">
<span class="slider round"></span>
</label>
</td>
<td class="action-cats">
<a style="margin-right: 30px; " href="<?= site_url('Products/delete_cat/' .$row->id);?>"><img src="<?php echo base_url('assets/images1/bin.png');?>" width="20px"> Delete </a>
<img src="<?php echo base_url('assets/images1/edit.png');?>" width="20px"> Edit
</td>
<td>
+
</td>
</tr>
</tr>
<?php foreach ($subt as $row): ?>
<tr>
<?php if ($row->cat_id == $row->cat_id): ?>
<td id="demo<?php echo $row->id?>" class="collapse"> <?php echo $row->sub_cat?></td>
<td id="demo<?php echo $row->id?>" class="collapse"><img src="<?php echo base_url('assets/images1/bin.png');?>" width="20px">
<img src="<?php echo base_url('assets/images1/edit.png');?>" width="20px"></td>
<?php endif ?>
</tr>
<?php continue;?>
<?php endforeach ?>
<?php endforeach ?>
<?php endif;?>
</tbody>
</table>
</div>
</div>
Here is my conroller.
public function categories()
{
$data['subcats']=$this->Pro_model->fetch_categories();
$data['subt']=$this->Pro_model->fetch_sub_categories();
$data['cat']=$this->Pro_model->fetch_cat();
$this->load->view('admin-new/categories.php', $data);
}
Here is my model.
function fetch_categories(){
$this->db->select('*');
$this->db->from('subcat');
$this->db->join('categories', 'subcat.sub_id = categories.id ');
$this->db->group_by('subcat.sub_id');
$this->db->order_by('sflag_no', 'asc');
$query = $this->db->get();
return $query->result();
}
function fetch_sub_categories(){
$this->db->select('*');
$this->db->from('subcat');
$this->db->join('categories', 'subcat.cat_id = categories.id ');
$this->db->group_by('subcat.sub_id');
$this->db->order_by('sflag_no', 'asc');
$query = $this->db->get();
return $query->result();
}
You need to have table structured like this:
<table border="2" bordercolor="green">
<tr>
<td>main cat 1
<table border="2" bordercolor="blue" id="cat-id-1">
<tr class="subRow">
<td>subcat 1 for table 1</td>
</tr>
<tr class="subRow">
<td>subcat 2 for table 1</td>
</tr>
<tr class="subRow">
<td>subcat 3 for table 1</td>
</tr>
</table>
</td>
<td><a herf="#" class="toggle" data-cat-id="1">+</a></td>
</tr>
<tr>
<td>main cat 2
<table border="2" bordercolor="blue" id="cat-id-2">
<tr class="subRow">
<td>subcat 1 for table 2</td>
</tr>
<tr class="subRow">
<td>subcat 2 for table 1</td>
</tr>
</table>
</td>
<td><a herf="#" class="toggle" data-cat-id="2">+</a></td>
</tr>
</table>
And then you can toggle the nested table using the jQuery:
<script type="text/javascript">
$(document).ready(function() {
$('.toggle').on('click', function() {
// Get the id of the table that is going to toggle
catId = $(this).data('cat-id');
// Select the sub-table to toggle
table = $('#cat-id-'+catId);
table.toggle();
});
})
</script>
Note: You'll need to set nested table's id as cat-id-{ID OF MAIN CAT} and data-cat-id attribute of toggle button as {ID OF MAIN CAT}.
Here is the working fiddle: https://jsfiddle.net/5vp9a24r/2/
As for the data part, not sure about your db structure, but I think you should initially get main categories and join subs on them. Something like this:
function getCategories() {
$this->db->select('*');
$this->db->from('categories');
$this->db->join('subcat', 'subcat.cat_id = categories.id ');
$this->db->group_by('categories.id');
$query = $this->db->get();
return $query->result();
}
Using this method in your Model, you should get categories that also have respective sub categories with them.
Update:
example of embaded foreach.
<table>
<?php foreach ($categories as $categoryItem) : ?>
<tr>
<td>main cat 1
<table id="cat-id-<?= $categoryItem->id; ?>">
<?php foreach ($categoryItem->subCats as $subcatItem) : ?>
<tr class="subRow">
<td><?= $subcatItem->title ?></td>
</tr>
<?php endforeach; ?>
</table>
</td>
<td><a herf="#" class="toggle" data-cat-id="<?= $categoryItem->id ?>">+</a></td>
</tr>
<?php endforeach; ?>

Get php checkbox data from multiple tables

First I will tell what I want to achieve, and then I will explain how I was trying to do it.
I have two tables, one stores type of vessels with a description and their own Id. Then I have another table in wich the vessel type has been stored as text. My goal is to select each one (both records in both tables) with a checkbox in both and store in the table 2 the Id from the table 1.
I will introduce data, to help.
Table 1
1|Balandra|Some description
2|Bergantin|Some description
3|Whatever |Whatever.....
Table2
Balandra
Bergantin
Whatever
Then, I have created a php page that shows both tables with the checkbox I mentioned above. Checkboxes store the Table1 Id and Table2 vesseltypename.
<table class="table table-striped table-bordered table-list">
<thead>
<tr>
<th><em class="fa fa-cog"></em></th>
<th class="hidden-xs">idtiponavio</th>
<th>Tipo de Navío</th>
<th>Descripción</th>
<th>Agrupar</th>
</tr>
</thead>
<tbody>
<?php foreach ($naviosdyncoop as $key => $navio) { ?>
<tr>
<td align="center">
<a href=<?php echo '../vista/modificar.php?id=' .
$navio['idtiponavio']; ?> class="btn btn-default"><em class="fa fa-
pencil"></em></a>
<a href=<?php echo '../datos/borrar.php?id=' .
$navio['idtiponavio']; ?> class="btn btn-default"><em class="fa fa-
trash"></em></a>
</td>
<td class="hidden-xs"><?php echo
$navio['idtiponavio']; ?></td>
<td><?php echo $navio['tiponavio']; ?></td>
<td><?php echo $navio['descripcion']; ?></td>
<td><input type="checkbox" name="agruparid" value=<?
php echo $navio['idtiponavio']; ?> /></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
<div class="col-md-6">
<div class="panel-body paneltodo">
<table class="table table-striped table-bordered table-list">
<thead>
<tr>
<th><em class="fa fa-cog"></em></th>
<th>Tipo de Navío</th>
<th>Agrupar</th>
</tr>
</thead>
<tbody>
<?php foreach ($naviosforsea as $key => $navio) { ?>
<tr>
<td align="center">
<em class="fa fa-arrow-circle-o-left"></em>
</td>
<td><?php echo $navio['typevessel']; ?></td>
<td><input type="checkbox" name="agruparvessel" value=<?php echo $navio['typevessel']; ?> /></td>
</tr>
<?php } ?>
</tbody>
</table>
So, I want to check both table records and store the table1 Id in the table2 idtypevessel field.
I thought that a php file could store both items and call the update function with those parameters, like this:
<?php
require './modelo.php';
$idnavio = $_GET['agruparid'];
$vessel = $_GET['agruparvessel'];
Any suggestions, because I think I have to do a button to submit this parameters, but it must be working on both tables, and I don't know how to access both foreach loop at the same time.
Thanks in advance.
E. Salas
review bellow reference code to submit multi selected checkbox values
for multi selected checkbox submission you must use [] operator after name attribute in html
index.php
<form action="/checkbox.php" method="post">
<strong>Cars:</strong><br>
<?php
$cars = array("Volvo", "BMW", "Toyota");
$colors = array("Red", "Green", "Black");
foreach($cars as $single){
?>
<input type="checkbox" name="cars[]" value="<?php echo $single; ?>">
<?php
}
<br>
<strong>colors:</strong><br>
foreach($colors as $single){
?>
<input type="checkbox" name="colors[]" value="<?php echo $single; ?>">
<?php
}
?>
<br>
<input type="submit" value="Submit!">
</form>
checkbox.php
<?php
echo "<pre>";
var_dump($_POST);
exit;
In your case:
<form action="/checkbox.php" method="post">
<div class="col-md-6">
<div class="panel-body">
<table class="table table-striped table-bordered table-list">
<thead>
<tr>
<th><em class="fa fa-cog"></em></th>
<th class="hidden-xs">idtiponavio</th>
<th>Tipo de Navío</th>
<th>Descripción</th>
<th>Agrupar</th>
</tr>
</thead>
<tbody>
<?php foreach ($naviosdyncoop as $key => $navio) { ?>
<tr>
<td align="center">
<em class="fa fa-pencil"></em>
<em class="fa fa-trash"></em>
</td>
<td class="hidden-xs"><?php echo $navio['idtiponavio']; ?></td>
<td><?php echo $navio['tiponavio']; ?></td>
<td><?php echo $navio['descripcion']; ?></td>
<td><input type="checkbox" name="agruparid[]" value=<?php echo $navio['idtiponavio']; ?> /></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
<div class="col-md-6">
<div class="panel-body">
<table class="table table-striped table-bordered table-list">
<thead>
<tr>
<th><em class="fa fa-cog"></em></th>
<th>Tipo de Navío</th>
<th>Agrupar</th>
</tr>
</thead>
<tbody>
<?php foreach ($naviosforsea as $key => $navio) { ?>
<tr>
<td align="center">
<em class="fa fa-arrow-circle-o-left"></em>
</td>
<td><?php echo $navio['typevessel']; ?></td>
<td><input type="checkbox" name="agruparvessel[]" value=<?php echo $navio['typevessel']; ?> /></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
<input type="submit" value="Submit!">
</form>
Finally I solved this with Javascript. One of my partners helped me, I post this to help people in my situation.
I created a script, here is the code:
<script type="text/javascript">
function cogeNavioDyncoopnet(){
var checkedValueNavioD = null;
var inputElements = document.getElementsByClassName('checknaviod');
for(var i=0; inputElements[i]; ++i){
if(inputElements[i].checked){
checkedValueNavioD = inputElements[i].value;
break;
}
}//return checkedValueNavioD;
var input_nav_dyn = document.getElementById("nav_dyn");
input_nav_dyn.value = checkedValueNavioD;
}
function cogeNavioForSea(){
var checkedValueNavioFs = null;
var inputElements = document.getElementsByClassName('checknaviofs');
for(var i=0; inputElements[i]; ++i){
if(inputElements[i].checked){
checkedValueNavioFs = inputElements[i].value;
break;
}
}//return checkedValueNavioFs;
var input_nav_fs = document.getElementById("nav_fs");
input_nav_fs.value = checkedValueNavioFs;
}
</script>
Then I created a Form below, that collects values and sends them to my .php control file.
<div class="hidden-xs">
<form class="hidden-xs" method="POST"
action="../datos/actualizarnavios.php">
<input id="nav_dyn" type="text" name="idnaviodyncoop">
<input id="nav_fs" type="text" name="navioforsea" >
<input id="botonasignar" type="submit" name="enviardatosdynfs">
</form>
</div>
I hope this helps. Thanks for the feedback, as always.

How to select id from the datatables using checkbox

Heres my code: HTML with Server side to get data from the database. How do I get id from the datatables using checkbox option?
<div class="panel-body">
<div class="table-responsive">
<table class="table table-striped table-bordered table-hover" id="dataTables-example">
<thead>
<tr>
<h4>
<th></th> <!-- For checkbox column-->
<th>ID Number</th>
<th>Lastname</th>
<th>Firstname</th>
<th>Middlename</th>
<th>Gender</th>
<th>Course</th>
<th>Department</th>
<th>Address</th>
<th>Contact</th>
<th>Birthdate</th>
<th>View</th>
<th>Events</th>
</h4>
</tr>
</thead>
<tbody>
<?php
include 'pgconnect.php';
$mydata = mysqli_query($conn,"SELECT * FROM student") or die(mysql_error());
while($record = mysqli_fetch_assoc($mydata))
{
$id=$record['id'];
?>
<tr>
<td><input type="checkbox"></td>
<td class="id"><?php echo $record['id'] ?></td>
<td class="lname"><?php echo $record['last_name'] ?></td>
<td class="fname"><?php echo $record['first_name'] ?></td>
<td class="mname"><?php echo $record['mid_name'] ?></td>
<td class="sex"><?php echo $record['gender'] ?></td>
<td class="course"><?php echo $record['course'] ?></td>
<td class="dept"><?php echo $record['department'] ?></td>
<td class="add"><?php echo $record['home_address'] ?></td>
<td class="contact"><?php echo $record['contact'] ?></td>
<td class="bdate"><?php echo $record['birthdate'] ?></td>
<td> View Records </td>
<td> <a title='Click here to update record.'href='#edit' data-toggle='modal'><i class="fa fa-pencil fa-fw"></i></a>
<a title='Click here to add record.' href='#'><i class="fa fa-plus fa-fw"></i></a>
<a title='Click here to delete record.' href='delete.php?del=<?php echo $record['id'] ?>'><i class="fa fa-trash-o fa-fw"></i></a></td>
</tr>
<?php
}
?>
</tbody>
</table>
Here's my script:
$(document).ready(function(){
$('#dataTables-example').dataTable();
});
I tried to get the ID but still it didn't works. Hope you can help me.
In your work, you get the ID in table. your should grab it from the checkbox if you use jquery, as in;
<script>
$(document).ready(function(){
document.getElementByID("ckmyid").innerHTML;
});
</script>
Note:add Selector ID in your checkbox, first, say, "ckmyid" so the code will be like this:
<input id="ckmyid" type="checkbox" value="<?php echo $record['id'] ?>">
Based on your structure, you can grab it directly from the link in your view records link if you want to place the ID there,as in;
<td> View Records </td>

PHP - product comparison table displaying only one column

I am trying to compare products and display results in a table. I have a form page and a TABLE page, the problem is that when i select in the FORM page from the two drop-downs the same product(value) it will only display in the table one column and returns an multidimensional array containing one array not two arrays identical. When i select two different values from the from and submit, both selections are displayed. How can I avoid having one empty column if the user select the same product /ID ?
I was thinking maybe load the second drop down and subtracting from the list the value of the first drop down selection? I am not sure how to do it Or maybe there is an easier way like a form validation. Can someone show me how? Thanks
FORM.html
<?php
$products_dropdown = "select * from product_dp" ;
$statement = $dbh->prepare($products_dropdown);
$statement->execute();
$result_dp = $statement->fetchAll();?>
<form action="table.html" method="post">
<fieldset class="custimg">
<legend>Product Comparison</legend>
<div class="row">
<div class="large-block-grid-4"><br /></div>
<div class="large-block-grid-4">
<select name="id1" id="id1" class="small button secondary dropdown radius">`
<?php foreach ($result_dp as $row){
echo "<option value='".$row['id']."'>".$row['sku']."</option>";}?>
</select>
</div>
<div class="large-block-grid-4">
<select name="id2" id="id2" class="small button secondary dropdown radius">
<?php foreach ($result_dp as $row){
echo "<option value='".$row['id']."'>".$row['sku']."</option>"; }?>
</select>
<input type="submit" value="Compare" class="tiny button secondary" />
</div>
</div>
Table.html
$id1 = $_POST[id1];
$id2 = $_POST[id2];
$statement = $dbh->prepare("select * from products_specs where id IN ($id1,$id2)");
$statement->execute(array(':id1' => $id1, ':id2'=> $id2));
$statement->setFetchMode(PDO::FETCH_ASSOC);
$subrows = $statement->fetchAll();
print_r($subrows);
$yes = '<img src="images/icons/yes.png" width="16" height="16" />';
$no = '<img src="images/icons/no.png" width="16" height="16" />';?>
<table >
<thead>
<tr>
<th width="100"></th>
<th width="275"><?php echo $subrows[0][image]; ?></th>
<th width="275"><?php echo $subrows[1][image]; ?></th>
</tr>
<tr text-align="center">
<th width="100"></th>
<th class="centered-cell1"><font color="#990000"><?php echo "ID"." ".$subrows[0][id]; ?></th>
<th class="centered-cell1"><font color="#990000"><?php echo "ID"." ".$subrows[1][id]; ?></th>
</tr>
</thead>
<tbody>
<tr>
<td > Capacity </td>
<td align="center"><?php echo $subrows[0][capacity]; ?> </td>
<td align="center"><?php echo $subrows[1][capacity]; ?> </td>
</tr>
<tr>
<td > Memory </td>
<td align="center"><?php echo $subrows[0][mem]; ?> </td>
<td align="center"><?php echo $subrows[1][mem]; ?> </td>
</tr>
ect....
Very simple solution. After executing query use code:
if (!isset($subrows[1]))
$subrows[1] = $subrows[0];
Let users compare a product with itself if they want it.

sort data by field heading php mysql

I have a database
that have two tables.
The sql query fetching data correctly.
I want to sort fields data by typing table heading in text box and click on go that in table footer.
Many Thanks
<table border="0" cellpadding="0" cellspacing="0" width="50%">
<thead>
<tr>
<th class="capt" colspan="6">Available Projects</th>
</tr>
<tr>
<th>Select</th>
<th>Project</th>
<th>Crawler</th>
<th>Description</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
include(dirname(__file__)."/includes/dbConn.php");
$result = mysql_query("SELECT *, ( SELECT name FROM projects WHERE projects.id = crawlers.pid ) AS pname FROM `crawlers`", $appConn);
while($row = mysql_fetch_array($result))
{
?>
<tr id="crawler_<?php echo $row['id']; ?>">
<td>
<input value="" id="check" type="checkbox">
<input id="crawler_id_<?php echo $row['id']; ?>" type="hidden" value="<?php $row['id']; ?>" /> <!-- crawler id -->
</td>
<td><?php echo $row['pname']; ?></td>
<td><?php echo $row['name']; ?></td>
<td>username#domain.com</td>
<td>Enabled</td>
<td><a class="edit" href="#">edit</a><a class="add" href="#">run</a><a class="delete" href="#">delete</a></td>
</tr>
<?php } ?>
</tbody>
<tfoot>
<tr>
<th colspan="6"> Sort Fields by <input value="type here" id="field" type="text"> Go </th>
</tr>
</tfoot>
</table>
I think, you are looking for ORDER BY
Have a look at the MySQL Documentation about the SELECT syntax: http://dev.mysql.com/doc/refman/5.0/en/select.html

Categories