Laravel table is showing unique id multiple times - php

In my laravel project am taking data from multiple tables and showing it in view.
I have uploaded the picture of current output.
I have multiple services in one location id, but I want to show only one location id and their corresponding services in right side.
Following is my code in controller
public function showClinicLocations($id)
{
$clinic = Clinic::find($id);
$locations = Location::where('clinicID', $id)->get();
$locationservices = Service::select('services.serviceName as servicename','locations.locationID as locid')
->join('location_services', 'location_services.serviceID', '=', 'services.serviceID')
->join('locations', 'locations.locationID', '=', 'location_services.locationID')
->join('clinics', 'clinics.clinicID', '=', 'locations.clinicID')
->where('clinics.clinicID','=',$id)
->get();
return view('clinic.locations')->with(['locations' => $locations ,'clinic'=>$clinic , 'services'=> $locationservices]);
}
Following is the code in view page
<table>
<thead>
<tr>
<th>Location Name</th>
<th>Services</th>
</tr>
</thead>
<tbody>
#foreach($services as $service)
<tr>
<td>{!! $service->locid !!}</td>
<td>{!! $service->servicename !!}</td>
</tr>
#endforeach
</tbody>
</table>
I want to show only one unique locid and their services in one row, why am not getting like that
And my expected output is in given image

Currently you are retrieving from database as the output you show, you can achieve what you want by just changing your blade template like this:
<tbody>
#foreach($locations as $location)
<tr>
<?php
$first = true;
foreach($services as $service){
echo('<tr>')
if($service->locid == $location->locationID ){
if($first){
echo('<td class="first">' . $location->locationID . '</td>')
$first=false;
}
else{
echo('<td class="second"></td>')
}
echo('<td>'.$service->servicename.'</td>')
echo('<td><button id="btn_'.$service->serviceID.'">Delete</button></td>')
}
echo('</tr>')
}
?>
</td>
</tr>
#endforeach
</tbody>
To erase the lines, you'll have to add some CSS:
.first{
border-top: 1px solid black;
border-right: 1px solid black;
border-left: 1px solid black;
}
.second{
border-right: 1px solid black;
border-left: 1px solid black;
}
I hope this helps you.

As of now your query result will be separated because you didn't group them.
Here's example
$locationservices = Service::select(DB::raw("GROUP_CONCAT(services.serviceName SEPERATOR ',') as servicename, locations.locationID as locid")
->join('location_services', 'location_services.serviceID', '=', 'services.serviceID')
->join('locations', 'locations.locationID', '=', 'location_services.locationID')
->join('clinics', 'clinics.clinicID', '=', 'locations.clinicID')
->where('clinics.clinicID','=',$id)
->groupBy('locations.locationID')
->get();
If group by has an error just add the servicename
->groupBy('locations.locationID', 'services.serviceName')
then the view is just the same.
You can change the seperator anything you want.
Hope it helps.

Related

mysql table values merged into one value instead of multiple

i've ran into an issue with my code which has left me stumped. in the code below, i have created a table for mysql data to be fetched into. however when running my code all of the data (which includes titles, descriptions and prices), seem to be merged into one continuous row which fills the entire page. i'm not sure if this has to do with the size of the data i'm trying to fit into a specific row, or a fault in my code. so any help would be appreciated.
styling used:
table {
border: none;
border-collapse: collapse;
width: 100%;
color: #ffffff;
font-size: 15px;
}
th {
background-color: #a6a6a6;
color: white;
}
table td {
font-size: 5px;
border-left: 1px solid #000;
border-right: 1px solid #000;
}
table td:first-child {
border-left: none;
}
table td:last-child {
border-right: none;
}
</style>
table and php:
<table>
<tr>
<th>Event Title:</th>
<th>Event Category:</th>
<th>Event Description:</th>
<th>Venue:</th>
<th>Opening Date:</th>
<th>Closing Date:</th>
<th>Price:</th>
</tr>
</table>
<?php //Beginig of php script for database events
include "Database_connection.php"; //Will make the database connection
$sql = "
SELECT e.eventTitle
, c.catDesc
, e.eventDescription
, v.venueName
, e.eventStartDate
, e.eventEndDate
, e.eventPrice
FROM NEE_venue v
JOIN NEE_events e
ON e.venueID = v.venueID
JOIN NEE_category c
ON c.catID = e.catID
ORDER
BY eventTitle ASC
";
$queryResult = $dbConn->query($sql);
if($queryResult === false) { //will detect if the connection failed, and give an error message
echo "<p>Query failed: ".$dbConn->error."</p>\n</body>\n</html>";
exit;
}
else { //if connection is succsessful, code will retrive the events
while($row = $queryResult->fetch_assoc()){
echo "<tr><td>". $row["eventTitle"] ."</td><td>". $row["catDesc"] ."</td><td>". $row["eventDescription"] ."</td><td>". $row["venueName"] ."</td><td>". $row["eventStartDate"] ."</td><td>". $row["eventEndDate"] ."</td><td>". $row["eventPrice"]
."</td></tr>";
}
echo "</table>";
}
$queryResult->close();
$dbConn->close();
?>
screenshot of the issue:
mysql issue
Get rid of the end table tag in the top section.
<table>
<tr>
<th>Event Title:</th>
<th>Event Category:</th>
<th>Event Description:</th>
<th>Venue:</th>
<th>Opening Date:</th>
<th>Closing Date:</th>
<th>Price:</th>
</tr>
</table> <--- That one.
Looks like from both the code and the image that the data gets printed outside of the table, because the end table tag gets added too early.

How to style a table that is part html and part php

I found it hard to phrase this question but I will try my best. I am creating a php and MYSQL leaderboard, it all works but now I want to style it. I don't know very much css and this might be a simple fix. I am trying to style the table so that all the data table elements sit symmetrical like a table should be, and aligned to the center of the headers. I think it is not working because I technically have 2 tables I am trying to style and they are not working in conjunction with each other. Here is the code and forgive my usernames in the table, I get lazy sometimes.
<!DOCTYPE html>
<html>
<head>
<title>Leaderboards</title>
<style type="text/css">
th {
overflow: auto;
font-size: 25px;
border: 1px solid;
padding: 5px;
margin: 2px 2px 2px 2px;
}
td {
font-size: 25px;
padding-left: 30px;
padding-top: 3px;
}
</style>
</head>
<body>
<h1>Leaderboard</h1>
<table>
<tr>
<th>Rank</th>
<th>User</th>
<th>Score</th>
</tr>
</table>
<?php
include 'HytecFunctions.php';
$conn=connectDB();
$rank = 1;
$sql = 'SELECT Name, Score FROM Names ORDER BY Score DESC';
foreach ($conn->query($sql) as $row) {
echo "<table>
<td>$rank</td>
<td>$row[Name]</td>
<td>$row[Score]</td>
</tr>
</table>";
$rank++;
}
$conn->close();
?>
</body>
</html>
The table as it shows in my browser
Because you try to create another table every loop. Try to put them all in a single table.
<table>
<tr>
<th>Rank</th>
<th>User</th>
<th>Score</th>
</tr>
<?php
include 'HytecFunctions.php';
$conn=connectDB();
$rank = 1;
$sql = 'SELECT Name, Score FROM Names ORDER BY Score DESC';
foreach ($conn->query($sql) as $row) {
echo '<tr>
<td>'.$rank.'</td>
<td>'.$row["Name"].'</td>
<td>'.$row["Score"].'</td>
</tr>';
$rank++;
}
$conn->close();
?>
</table>
Note: Don't mind much the way I input your $row variables. Yours will still work even if you use double tick (") to display rows. I just use a single tick (') to display your data as is to have a much cleaner look on your code. Refer here for the difference.

MYSQL fetching rows to different tables

I'm here with following problem. I have table with multiple records. Each row represents news of some kind, so I need to distinguish one from the another. The way to do that, is to make a table for each of these news(or a wrapper of some kind), however I encoutred a problem with fetching each result to each table(wrapper).
I can't figure out how to tell SQL to put the record separatley. Below is chunk of my code, as well as the design for the table.
.news{
width:400px;
height:auto;
border: 1px solid red;
float:left;
}
.news tr:first-child{
font-weight: bold;
height:40px;
text-align: center;
}
.news tr:last-child{
background-color: whitesmoke;
height: 200px;
padding-bottom: auto;
text-align: center;
}
.details td{
width:200px;
height: 40px;
text-align: center;
}
echo "<table class='news'>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr>
<td colspan='2'>
$row[0]
</td>
</tr>
<tr class='details'>
<td>
$row[1]
</td>
<td>
$row[2]
</td>
</tr>
<tr>
<td colspan='2'>
$row[3]
</td>
</tr>";
echo "</table>";
}
$query = " SELECT headline, date, concat (firstName,' ',lastName), body FROM "
. "school_info natural join teachers ORDER BY id_school_inf DESC LIMIT 2;";
$result = mysqli_query($conn, $query);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^This is my query. There is a limit, so i can always fetch latest (let's say last 5) news.
Any help would be much appreciated
You need to create a separate table for every type of record you have? Simple enough, if you order your records by the type:
SELECT * FROM whatever ORDER BY type_of_record
then
$previous = null;
while(... fetch from db ...) {
if ($row['type'] != $previous) {
... got new record type, start new table
}
... display row
$previous = $row['type']; // store current type for comparison later
}

Use tr:nth-child(odd) and tr:nth-child(even) selectors only on nested top tables

I want a zebra striped table.
The data is extracted from the database and every instance creates a new table.
I want the zebra-striped on the <table> level. this would mean that every other <table> element gets a different color.
I tried to add a class to my <table class="oddeven"> but this still does the changing on every tr.
Here is the code I use it on:
<?php
global $wpdb;
$sql = "SELECT * FROM $wpdb->postmeta WHERE meta_key = 'group' AND meta_value='$group'";
$results = $wpdb->get_results($sql) or die(mysql_error());
echo'<table cellpadding="0" cellspacing="0" border="0" width="100%">';
foreach( $results as $result )
{
$post_id = $result->post_id;
$company_name = get_post_meta($post_id, 'company_name', true);
$address = get_post_meta($post_id, 'address', true);
$postal_code = get_post_meta($post_id, 'postal_code', true);
$city = get_post_meta($post_id, 'city', true);
$phone = get_post_meta($post_id, 'phone', true);
echo '
<tr>
<td>
<table cellpadding="0" cellspacing="0" border="0" width="100%" class="oddeven">
<tr>
<td width="75%">
<strong>'.$company_name.'</strong>
</td>
<td rowspan="4"><img class="table_image" src="'.get_bloginfo('template_directory').'/images/arrow.png"></td>
</tr>
<tr>
<td>
'.$address.'
</td>
</tr>
<tr>
<td>
'.$postal_code.' '.$city.'
</td>
</tr>
<tr>
<td>
'.$phone.'
</td>
</tr>
</table>
</td>
</tr>';
}
echo '</table>';
?>
This is the styling:
(while typing this I realise that this is on tr:level)
.oddeven tr:nth-child(odd){
background: #ffffff;
}
.oddeven tr:nth-child(even){
background: #000000;
}
I hope I make myself clear
If you change your foreach loop to a for loop like this:
for($i = 0; $i < count($results); $i++) {
$result = $results[$i];
...
Then you can figure out if the current row is even or odd by testing if $i % 2 == 0. If that evaluates to true, then add an 'even' class; else add an 'odd' class.
If you do not want to propagate the change to child tables, you can also enforce the same color on the children :
.top tr:nth-child(odd) {
background-color: red;
}
.top tr:nth-child(odd) tr {
background-color: red;
}
.top tr:nth-child(even) {
background-color: yellow;
}
.top tr:nth-child(even) tr {
background-color: yellow;
}
I assume that's what your want since you already have stripped rows on your nested tables
Or maybe I got it wrong, you might have to explain what the <table> level is, since your have nested tables.

How to make a number list in a Table

I have a leaderboard on my website, and I want to be able to have rank numbers (1, 2, 3, 4, 5, etc) as using...
<ol>
<li>this</li>
<li>that</li>
</ol>
...doesn't work in a table
How can I do this?
One way of achieving this, given the following structure:
<table>
<thead>
<tr>
<th>Rank:</th>
<th>Name:</th>
</tr>
</thead>
<tbody>
<tr>
<td class="rank"><span></span></td>
<td>Him</td>
</tr>
<tr>
<td class="rank"><span></span></td>
<td>Her</td>
</tr>
</tbody>
</table>
Is to use CSS-counters:
table {
border: 1px solid #ccc;
empty-cells: show;
width: 40%;
counter-reset: ranking;
}
th {
border-bottom: 2px solid #ccc;
min-width: 4em;
width: 50%;
max-width: 6em;
}
tbody tr {
counter-increment: ranking;
}
td {
border-bottom: 1px solid #ccc;
}
td.rank > span:before {
content: counter(ranking);
}
And a JS Fiddle demo.
As noted elsewhere, though, these are not widely supported. And would be better-implemented either using JS/jQuery or by simply using an ol.
You could do this using CSS counters
...unfortunately they're not that widely supported yet. It's best to generate the numbers on the server side, it could be argued that they are significant content and should be in HTML anyways.
Simply add this to your CSS:
ol {
list-style-type: decimal;
margin-left:12px;
}
JSFiddle:
http://jsfiddle.net/Mutant_Tractor/zhCzQ/2/
Or you could also use list-style-type: decimal-leading-zero;
I'm afraid numbering rows in a table is a standard html feature. You'll need to resort to Javascript or server-side generation of the numbers.
Edit:
You really should do this via (in order of preference):
CSS numbering (see other answers)
Server-side (I don't do php - sorry)
client-side javascript should be a last resort:
<table id="numbered">
<tr>
<td>a</td>
<td>b</td>
</tr>
<tr>
<td>c</td>
<td>d</td>
</tr>
<tr>
<td>e</td>
<td>f</td>
</tr>
</table>
<script type="text/javascript">
var table=document.getElementById('numbered');
var tr=table.getElementsByTagName('tr');
for(var i=0; i<tr.length; i++)
{
var td=tr[i].getElementsByTagName('td');
td[0].insertBefore(document.createTextNode(i+1+'. '), td[0].firstChild);
}
</script>
As for me I am using while loop in a while loop (for php) If you wanted to get data from database.
Ex.
$query = mysql_query("SELECT * FROM table);
$getnumbers = mysql_num_rows($query);
$x=1; //initialize your number
while($x<=$getnumbers)
{
while($rows = mysql_fetch_assoc($query))
{
echo $x;echo $row["row1"];echo "<br>";
$x++ }
}

Categories