How to automatically number each row in a table in laravel? - php

Please I want to automatically number each row when data is displayed from the database. Something like this
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">First</th>
<th scope="col">Last</th>
<th scope="col">Handle</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>Mark</td>
<td>Otto</td>
<td>#mdo</td>
</tr>
<tr>
<th scope="row">2</th>
<td>Jacob</td>
<td>Thornton</td>
<td>#fat</td>
</tr>
<tr>
<th scope="row">3</th>
<td>Larry</td>
<td>the Bird</td>
<td>#twitter</td>
</tr>
</tbody>
</table>
so each row is numbered in ascending order from 1 to n depending on the data available in the database. How do I properly do that. Any Help please am new in laravel

If you are using blade, the laravel's automatically generated $loop variable might help. Like this:
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">First</th>
<th scope="col">Last</th>
<th scope="col">Handle</th>
</tr>
</thead>
<tbody>
#foreach($items as $item)
<tr>
<th scope="row">{{ $loop->iteration }}</th>
<td>{{ $item->first_name }}</td>
<td>{{ $item->last_name }}</td>
<td>#{{ $item->username }}</td>
</tr>
#endforeach
</tbody>
</table>
You can find documentation here: https://laravel.com/docs/8.x/blade#the-loop-variable

Related

Does not display data from database at blade.php file

i have this code in my blade.php file
<table class="table table-success table-striped">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">Username</th>
<th scope="col">Password</th>
<th scope="col">Email</th>
</tr>
<tbody>
#foreach($users as $user)
<tr>
<th scope="row">{{$user->ID}}</th>
<td>{{$user->username}}</td>
<td>{{$user->password}}</td>
<td>{{$user->email}}</td>
</tr>
#endforeach
</tbody>
</table>
and when i call my blade.php file to view a html page, i see only the emails from data.
This is the view when i run a dd($user) command.
This is a Html page
Looks like you got names wrong. Try this:
<table class="table table-success table-striped">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">Username</th>
<th scope="col">Password</th>
<th scope="col">Email</th>
</tr>
<tbody>
<?php
foreach($users as $user){
echo "<tr>
<th scope='row'>".$user['ID']."</th>
<td>".$user['user_login']."</td>
<td>".$user['user_pass']."</td>
<td>".$user['user_email']."</td>
</tr>";}
?>
</tbody>
</table>

HTML in TinyMCE (Laravel) show errors

I installed TinyMCE editor on my Laravel application. I want to write HTML in my TinyMCE, like or per example.
Here is the code I put in TinyMCE :
<table class="table table-dark">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">First</th>
<th scope="col">Last</th>
<th scope="col">Handle</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>Mark</td>
<td>Otto</td>
<td>#mdo</td>
</tr>
<tr>
<th scope="row">2</th>
<td>Jacob</td>
<td>Thornton</td>
<td>#fat</td>
</tr>
<tr>
<th scope="row">3</th>
<td>Larry</td>
<td>the Bird</td>
<td>#twitter</td>
</tr>
</tbody>
</table>
So I dit it and I print the result like that :
<p>{!!html_entity_decode($vpn->intro)!!}</p>
Here is the result :
Can you help me to solve the problem please ?
<table> does not allowed to be printed inside <p>, Use <div> instead.
<div>
{!!html_entity_decode($vpn->intro)!!}
</div>
Here you can find out why.

My webpage list using Laravel not showing data from database

Here is the web page:
Update data and ID is showing while other data such as customer name, phone, etc are not showing.
CustomersController.php
public function index()
{
$customers = Customer::all();
return view('customers.index', ['customers' => $customers]);
}
index.blade.php
#extends('layouts.app')
#section('content')
<h1>Customers</h1>
#if(count($customers)>0)
<table class="table">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">Name</th>
<th scope="col">Email</th>
<th scope="col">Phone</th>
<th scope="col">Address</th>
<th scope="col">Updated on</th>
</tr>
</thead>
#foreach($customers as $customer)
<tbody>
<tr>
<th scope="row">{{$customer->id}}</th>
<td>{{$customer->name}}</td>
<td>{{$customer->email}}</td>
<td>{{$customer->phone}}</td>
<td>{{$customer->add}}</td>
<td>{{$customer->updated_at}}</td>
</tr>
#endforeach
#else
#endif
#endsection
You didn't close <table> and <tbody>, and you placed <tbody> inside the loop (it should only appear once)
Try this:
#extends('layouts.app')
#section('content')
<h1>Customers</h1>
#if(count($customers)>0)
<table class="table">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">Name</th>
<th scope="col">Email</th>
<th scope="col">Phone</th>
<th scope="col">Address</th>
<th scope="col">Updated on</th>
</tr>
</thead>
<tbody>
#foreach($customers as $customer)
<tr>
<td>{{$customer->id}}</td>
<td>{{$customer->name}}</td>
<td>{{$customer->email}}</td>
<td>{{$customer->phone}}</td>
<td>{{$customer->add}}</td>
<td>{{$customer->updated_at}}</td>
</tr>
#endforeach
</tbody>
</table>
#else
#endif
#endsection
You didn't close your table tag and <tbody> tag and <tbody> opening tag should be start before foreach loop.

PHP: insert tag in DomDocument

I have the following html code :
<table>
<tbody>
<tr>
<th >operatore</th>
<td>3</td>
<td>4</td>
<td>5</td>
<tr/>
<tr>
<th >operatore</th>
<td>6</td>
<td>7</td>
<td>8</td>
<tr/>
</tbody>
</table>
I would like to insert the following tags after the TABLE tag:
<thead>
<tr>
<th >Nome IT</th>
<th >a</th>
<th >b</th>
<th >c</th>
</tr>
</thead>
so I'd like to get:
<table>
<thead>
<tr>
<th >Nome IT</th>
<th >a</th>
<th >b</th>
<th >c</th>
</tr>
</thead>
<tbody>
<tr>
.....
I created the following code for THEAD and succeeded:
$thead = $dom_ods->createElement("thead", "");
$ods2_tmp->insertBefore($thead, $ods2_tmp->firstChild);
$dom_ods->saveHTML();
while I can't for the others, how can I proceed?

The first row is not printing properly when generating PDF

I have following html code in my document for generating PDF, but resulted PDF is not generating properly.
<table border="1" class="table table-striped">
<thead>
<th>Sr No.</th>
<th>Particulars </th>
<th>Qty </th>
<th>rate </th>
<th>Amount </th>
</thead>
<tbody>
<br><br>
#foreach($data as $row)
<tr>
<td>{{ $row['temp_invoice_id'] }}</td>
<td>{{ $row['tyre_brand']." ".$row['tyre_model'] }}</td>
<td>{{ $row['quantity'] }}</td>
<td>{{ $row['rate'] }}</td>
<td>{{ $row['total'] }}</td>
</tr>
#endforeach
</tbody>
</table>
But when the PDF is generated it shows something like this I don't know why it's not generating proper PDF can anyone have solution over this problem, or we have to apply separate css for print media?
Inside thead use tr tag it is working fine.
<thead>
<th>Sr No.</th>
<th>Particulars </th>
<th>Qty </th>
<th>rate </th>
<th>Amount </th>
</thead>
use like this,
<thead>
<tr>
<th>Sr No.</th>
<th>Particulars </th>
<th>Qty </th>
<th>rate </th>
<th>Amount </th>
</tr>
</thead>
Remove the <br><br> snippet in your <tbody> they are not valid, so the browser might ignore them but maybe the PDF generator (I don't know which one you use) might not.
Also place the <th>s inside a row (<tr>)

Categories