Yii2 generate 3 column table using foreach - foreach

how do i generate only 3 columns in a page using below code
if i keep echo'(table)' inside it generates the whole table so i want only one table
allow only 3 columns in a page after 3 columns append a new row
<?php
$i = 0;
echo '<table style="float: left;width: -weekbit-fill-available"; ><tr>';
?>
<?php foreach ($product as $p) {
$i++;
// $i++;
echo "<tr><td>Name:<b>". $p->name ."</td></tr>";
echo "<tr><td>productID:<b>".$p->proid ."</td></tr>";
echo "<tr><td>Price:<b>". $p->price ."</td></tr>";
echo "<tr><td><a href='/cart/cart/add?id=$p->proid'><input type='button' value='addtocart'/></td></tr>";
if($i==3){
echo '</tr><tr>';
}
echo"</tr>
</table>";
?>
<?php }?>
<?php echo"</tr>
</table>"?>
thanks in advance

Try below
<table style="float: left; width: -weekbit-fill-available">
<tr>
<th>Name</th>
<th>ProductID</th>
<th>Price</th>
<th>Action</th>
</tr>
<?php foreach ($product as $p) : ?>
<tr>
<td><?= $p->name ?></td>
<td><?= $p->proid ?></td>
<td><?= $p->price ?></td>
<td colspan="3"><?= \yii\helpers\Html::a('addtocart',
['/cart/cart/add', 'id' => $p->proid],
[
'title' => 'Add to Cart',
'class' => 'btn btn-warning btn-sm',
]); ?></td>
</tr>
<?php endforeach; ?>
</table>

Use <tr> for each row and <td> for each column
<table style="float: left; width: -weekbit-fill-available"; >
<?php foreach ($product as $p): ?>
<tr>
<td>Name: <b><?= $p->name ?></td>
<td>productID: <b><?= $p->proid ?></td>
<td>Price: <b><?= $p->price ?></td>
<td><a href='/cart/cart/add?id=<?= $p->proid ?>'><input type='button' value='addtocart'/></td>
</tr>
<?php endforeach; ?>
</table>";
See also:
HTML tables https://www.w3schools.com/html/html_tables.asp
Yii2 GridView https://www.yiiframework.com/doc/guide/2.0/en/output-data-widgets

Related

Table dropdown with bootstrap 5 not working

I am trying to show product items when button order detail is pressed, but its not working in any way I tried, it's possible to make it work with bootstrap 5 or I have to do it with javascript ?
Useless information for "It looks like your post is mostly code; please add some more details." : In the first foreach I am displaying information from the order, then the second foreach is retrieving product id and quantity from a different table that contain order id and the third foreach is getting product where id from the second foreach in order to display product details
<table class="table border">
<thead>
<tr>
<th class="h4" style="width: 18%">Order Number</th>
<th class="h4">Date</th>
<th class="h4">Total</th>
<th class="h4">Status</th>
<th class="h4"></th>
<td></td>
</tr>
</thead>
<tbody>
<tr>
<?php
foreach ($order as $row_order) {
$order_status = intval($row_order->status);
?>
<td class="fw-bold">{{ $row_order->id }}</td>
<td class="fw-bold">{{ $row_order->added_on }}</td>
<td class="fw-bold">{{ $row_order->order_total }} $</td>
<td class="fw-bold"><?php if($order_status == 0){ ?>
Not Paid
<?php }elseif ($order_status == 1) { ?>
Paid
<?php }elseif ($order_status == 2) { ?>
Shipped
<?php }elseif ($order_status == 3) { ?>
Delivered
<?php } ?>
</td>
<td></td>
{{-- <td><a class="btn btn-primary" href="">Order Detail</a></td> --}}
<div class="btn-group">
<td>
<button class="btn btn-primary dropdown-toggle" type="button" id="dropdownMenuButton" data-bs-toggle="dropdown" aria-expanded="false">
Order Detail
</button>
</td>
#foreach (\App\Models\Order_product_size_color::where('order_id', '=', $row_order->id)->get() as $order_variant)
#foreach (\App\Models\Product_size_color::where('id', '=', $order_variant->product_size_color_id)->get() as $variant)
<div class="dropdown-menu">
<td class="border-0 p-0 dropdown-item">
<tr>
<td class="border-0"><img class="rounded" src="{{ asset('storage/'. $variant->image1->product_image) }}" alt="" style="width: 80px; height: 90px;"></td>
<td class="fw-bold border-0">{{ $variant->product->product_name }}</td>
<td class="border-0">{{ $order_variant->count }} products</td>
<td class="fw-bold border-0">Size: {{ $variant->size->size_name }}</td>
<td class="fw-bold border-0">Color: {{ $variant->color->color_name }}</td>
<td class="fw-bold border-0">${{ $variant->product->price }}</td>
</tr>
</td>
</div>
#endforeach
#endforeach
</div>
<tr>
<td class="fw-bold">Delivery Address:</td>
<td class="fw-bold">{{ $row_order->address }}</td>
</tr>
</tr>
<?php }//end foreach ?>
</tbody>
</table>

jQuery Tablesorter not sorting table on page load

I'm having issues on my WordPress page getting tablesorter to sort the data when the page initially loads. When the page initially loads it sorts the table by post date. Any way I can get it sorted by date_signed in descending order when the page loads initially? I can click the sort button on the table after the page has loaded and it sorts just fine, so its working, its just ignoring the initial sort function. I'm not a strong coder, so I appreciate the help. I'm working with existing code.
Thanks!
<?php /* Template Name: All Court Orders */ ?>
<?php get_header(); ?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php
while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'template-parts/content', 'header' ); ?>
<?php // Get all amendments
$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'court_order',
'meta_key' => 'date_signed',
'orderby' => 'meta_value_num',
'order' => 'DESC'
)); ?>
<div class="row">
<div class="col-md-12">
<div class="table-responsive" id="tblOpinions">
<table class="table table-striped tablesorter" id="myTable">
<thead>
<tr>
<th>Order Name</th>
<th>Date Signed</th>
<th>Effective Date</th>
</tr>
</thead>
<tbody>
<?php foreach ($posts as $post) { ?>
<?php
$file = get_field('file_upload');
$dateSigned = get_field('date_signed', false, false);
$dateSigned = new DateTime($dateSigned);
$dateEffective = get_field('effective_date', false, false);
$dateEffective = new DateTime($dateEffective);
$name = get_the_title();
?>
<tr>
<td><?php the_title(); ?></td>
<td><?php echo $dateSigned->format('m/d/Y'); ?></td>
<td><?php echo $dateEffective->format('m/d/Y') ?></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
</div>
<?php endwhile; // End of the loop. ?>
</main><!-- #main -->
</div><!-- #primary -->
<?php
get_footer(); ?>
<script>
$(document).ready(function()
{
$("#myTable").tablesorter( {
sortList: [[1,1]]
});
}
);
</script>

Laravel accessing joined table in view

I am joining two tables (companies and users on users.id=companies.user_id), send the data to the controller and then pass it to the view where it tells me that $id is an undefined property referring to the line in the view where it says ($comps->id).
Without a join, this message does not show up and all is fine. Do I have to access the properties differently when having a join?
Model:
public static function companyUser(){
return DB::table('companies as c')
->leftJoin('users', 'c.user_id', '=', 'users.id')
->select('users.user_name as uname, c.*')
->get();
}
Controller:
$data['companies'] = Companies::companyUser();
return View::make('admin/companies_view', $data);
View:
<?php if($companies) : ?>
<?php foreach($companies as $comps) : ?>
<tr>
<td class="center"><?php echo $comps->id; ?></td>
<td><?php echo $comps->company; ?></td>
<td class="center"><?php echo $comps->uname; ?></td>
<td class="center"><?php echo $comps->rate; ?></td>
<td class="center"><?php echo $comps->status; ?></td>
</tr>
<?php endforeach; ?>
<?php else : ?>
<tr>
<td colspan="8" class="no_results">There are no Users</td>
</tr>
<?php endif; ?>
I think the error is probably that select() expects each thing you want to get as a separate string. So if you want both the user_name and c.*, you'll need to do this:
->select('users.user_name as uname', 'c.*')
That should hopefully be enough to get it going. :)

PHP handling multiple checkboxes

Ok, I have index.html with a form as follows:
<form action="process.php" method="post">
<table>
<tr>
<td><input name="Field[]" type="checkbox" value="Accounting" />Accounting</td>
<td><input name="Field[]" type="checkbox" value="Finance" />Finance</td>
<td><input name="Field[]" type="checkbox" value="Marketing" />Marketing</td>
</tr>
</table>
</form>
And I have process.php as follows:
<table>
<tr>
<th>Field(s):</th>
<td>
<?php
if(isset($_POST['Field']))
{
for($i = 0; $i < count($_POST['Field']); $i++)
{ echo $_POST['Field'][$i] . ' '; }
}
?>
</td>
</tr>
</table>
Yet for some reason, I only get the first letter of the last checkbox that was checked printed out. Help please!
Try this one in process.php to get the values from $_POST['Field']
<table>
<tr>
<th>Field(s):</th>
<td>
<?php
if(isset($_POST['Field']))
{
foreach ($_POST['Field'] as $value) {
echo $value;
}
}
?>
</td>
</tr>
</table>

Embedding a form in a partial with a foreach statement

Objective:
list a set of records with a checkbox next to each along with a "delete" input. If the user checks one checkbox or multiple checkboxes, the submit action will get the id of each checkbox and delete corresponding record(s).
I know this functionality is provided on the backend, but I'm trying provide the functionality to the frontend users that are logged in.
Update: I can now render the widget correctly, but am still having difficulty capturing the ID of the checkbox(s) that are selected. Help here would be appreciated.
Index page:
<h1>Jobs</h1>
<?php include_partial('list', array('saved_jobss' => $saved_jobss, 'form' => $form)) ?>
//I'm not sure if I can even pass two objects??
Index action:
public function executeIndex(sfWebRequest $request)
{
$userId = sfContext::getInstance()->getUser()->getId();
$this->saved_jobss = Doctrine_Core::getTable('saved_jobs')->getSavedJobs($userId);
$this->form = new Saved_JobsForm();
}
Partial page named: _list
<table class="sortable">
<thead>
<tr>
<th>Delete</th>
<th>Company:</th>
<th>Job name:</th>
<th>Job No.</th>
<th>Saved:</th>
</tr>
</thead>
<tbody>
<?php foreach ($saved_jobss as $saved_jobs): ?>
<tr>
<td>
<?php echo $form['id']->renderRow() ?>
<td>
<a href="
<?php
$jobId = $saved_jobs->getJobId();
echo 'job/'.$saved_jobs->getJob($jobId);
?>
">
<?php
$jobId = $saved_jobs->getJobId();
echo $saved_jobs->getJobCompany($jobId);
?>
</a>
</td>
<td>
<?php
$jobId = $saved_jobs->getJobId();
echo $saved_jobs->getJobName($jobId);
?>
</td>
<td>
<?php
echo $saved_jobs->getJobId();
?>
</td>
<td><?php echo date("M-j-y", strtotime($saved_jobs->getCreatedAt())) ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<input type="submit" value="Delete" />
</form>
Action:
public function executeSubmit(sfWebRequest $request)
{
$this->forward404Unless($request->isMethod('post'));
$params = $request->getPostParameters();
$this->redirect('saved_jobs/deleteConfirmation?'.http_build_query($params));
}
Form class:
class Saved_JobsForm extends BaseSaved_JobsForm
{
public function configure()
{
$this->widgetSchema['id'] = new sfWidgetFormInputCheckbox();
$this->widgetSchema->setLabel('id', false);
}
}
I tinkered with the idea of using javascript, but was unsure if that was the way to go or not. Any help, or a point in the right direction would be most appreciated. Thanks in advance.
Solved: I just passed a link to each record with the action and record Id.

Resources