jQuery Tablesorter not sorting table on page load - tablesorter

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>

Related

Yii2 generate 3 column table using 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

How insert array values with foreach in opencart?

My case similar with this Insert array values with foreach using codeigniter (user input by dynamically adding and removing fields).
But it's CI, I trying in open cart, so I build function insert and passing value(in array) and performs for each in model.
What I want insert is product this for code Controller :
// check request and store to array
if(isset($this->request->get['product']))
{
$product = $this->request->get['product'];
$product2 = array();
foreach($product as $p)
{
$product2[] = $p;
}
}
else
{
$product = null;
}
// This for function insert
public function insert()
{
$this->language->load('item/item');
$this->document->setTitle($this->language->get('heading_title'));
$this->load->model('item/item');
if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm())
{
$this->model_item_item->insert_detail($this->request->post);
$this->session->data['success'] = $this->language->get('text_success');
$this->redirect($this->url->link('item/item', 'token=' . $this->session->data['token'], 'SSL'));
}
}
This is for Model :
public function insert_detail($product2)
{
foreach($product2 as $detail)
{
$this->db->query("INSERT INTO " . DB_PREFIX . "show_product_detail SET product_id = '". $this->db->escape($detail['product']) . "'");
}
}
This for View :
<?php echo $header; ?>
<div id="content">
<div class="breadcrumb">
<?php foreach ($breadcrumbs as $breadcrumb) { ?>
<?php echo $breadcrumb['separator']; ?><?php echo $breadcrumb['text']; ?>
<?php } ?>
</div>
<?php if ($error_warning) { ?>
<div class="warning"><?php echo $error_warning; ?></div>
<?php } ?>
<?php if ($success) { ?>
<div class="success"><?php echo $success; ?></div>
<?php } ?>
<div class="box">
<div class="heading">
<h1><img src="view/image/product.png" alt="" /> <?php echo $heading_title; ?></h1>
<div class="buttons"><a onclick="$('#form').submit();" class="button"><?php echo $button_save; ?></a><?php echo $button_cancel; ?></div>
</div>
<div class="content">
<form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data" id="form">
<table class="form">
<tr>
<td><span class="required">*</span> <?php echo $entry_head; ?></td>
<td><input type="text" name="head_text_field" value="" placeholder="Input Head Text" size="40"/>
<?php if ($error_head) { ?>
<span class="error"><?php echo $error_head; ?></span>
<?php } ?>
</td>
</tr>
<tr>
<td><span class="required">*</span> <?php echo $entry_title; ?></td>
<td><textarea name="title_text_field" placeholder="Input Title Text" style="width:230px;"/></textarea>
<?php if ($error_title) { ?>
<span class="error"><?php echo $error_title; ?></span>
<?php } ?>
</td>
</tr>
<tr>
<td><?php echo $entry_max_item; ?></td>
<td>
<select name="max" id="maxitem">
<?php
for($i=1; $i<=6; $i++)
{
if($i == 1)
echo "<option selected='selected' value=".$i.">".$i."</option>";
else
echo "<option value=".$i.">".$i."</option>";
}
?>
</select>
</td>
</tr>
<tr>
<td><?php echo $entry_product; ?></td>
<td><input type="text" id="product" name="product" value="" placeholder="Input Product" size="40"/></td>
</tr>
<tr>
<td></td>
<td>
<table>
<tr>
<td><input type="button" id="ADD" value="Add Item"></td>
<td><input type="reset" id="RESET" value="Reset"></td>
</tr>
</table>
</td>
</tr>
<tr>
</tr>
</table>
<table border="1" id="tblname" cellpadding="5" cellspacing="5">
<thead>
<tr>
<td>
Total Item
</td>
<td>
Name Item
</td>
<td>
DELETE
</td>
<tr>
</thead>
<tbody align="center">
</tbody>
</table>
</form>
</div>
</div>
</div>
This for Javascript:
<script type="text/javascript"><!--
$('input[name=\'product\']').autocomplete({
delay: 100,
source: function(request, response) {
$.ajax({
url: 'index.php?route=catalog/product/autocomplete&token=<?php echo $token; ?>&filter_name=' + encodeURIComponent(request.term),
dataType: 'json',
success: function(json) {
response($.map(json, function(item) {
return {
label: item.name,
value: item.product_id
}
}));
}
});
},
select: function(event, ui) {
$('input[name=\'product\']').val(ui.item.label);
return false;
},
focus: function(event, ui) {
return false;
}
});
//--></script>
<Script type="text/javascript">
$(document).ready(function(){
var item = 1;
var isAllowed = 3;
var isSet = 0;
$('#ADD').click(function(){
var maxitem = parseInt($("#maxitem").val(), 10); //from max item in html
var iCount = 0;
if($('#product').val()){ // check input product
if( item <= maxitem )
{
iCount = $('#tblname tbody tr').length + 1;
szTr = "<tr><td>";
szTr = szTr + iCount + "</td>";
szTr = szTr + "<td>" +$('#product').val() +"</td>";
szTr = szTr + "<td><input type='button' class='DEL' value='DELETE'></td>";
szTr = szTr + "</tr>";
$('#tblname tbody').append(szTr);
item +=1;
isSet = 1;
restFormOpts();
}
else
{
alert ("Max Limit !!!");
}
}else{alert('Enter Product !!! ');}
});
// for delete row
$('body').on('click','#RESET', function() {
item = 1;
isAllowed = 3;
isSet = 0;
$("#maxitem").attr("disabled",false);
$('.DEL').parents('tr').remove();
});
$('body').on('click', 'input.DEL', function() {
$(this).parents('tr').remove();
item -= 1;
});
function restFormOpts()
{
if(isSet === isAllowed) {
$("#ADD").attr("disabled",true);
$("#maxitem").attr("disabled",false);
}
else {
$("#ADD").attr("disabled",false);
$("#maxitem").attr("disabled",true);
}
}
});
</script>
This code works to enter into the database , but no contents?
Where i did mistake?
Here's the correct insert statement:
$this->db->query("
INSERT INTO " . DB_PREFIX . "show_product_detail
(show_product_detail)
values ('". $this->db->escape($detail['product']) . "'");
You are putting an array in product_id field.
First you need to change product_id field type int to text and
Check with following code
public function insert_detail($product2)
{
foreach($product2 as $detail)
{
$this->db->query("INSERT INTO " . DB_PREFIX . "show_product_detail SET product_id = '". $this->db->escape(json_encode($detail)) . "'");
}
}

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. :)

Got errors When try load xml zend framework 2

I have create a controller:
public function testAction() {
$hml = '<div>
<table>
<tr>
<td class="foo">
<div>
Lorem ipsum <span class="bar">
One
Two
Three
Four
</span>
</div>
</td>
</tr>
</table>
</div>';
​​​​​​​​ use Zend\Dom\Query;
$dom = new Query($html);
$results = $dom->execute('.foo .bar a');
return new ViewModel(array(
'results' => $results,
)
);
}
My View
<!-- Begin page content -->
<div id="container">
<div class="pane ui-layout-center">
<?php
print_r($results);
?>
</div>
But I when I run that controller I got the message:
Cannot query; no document registered
anybody know what is the problem?
It does not work, because you have a typo in variable names. You store HTML in the $hml variable, however pass a non existing $html variable to the constructor of the Query class.

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