I have a search field in the page. But if there is no matching results, it is throwing an error "Fatal error: Call to a member function show() on a non-object". I have attached the screenshot.
<?php
if ( isset($_REQUEST['usersearch']) && $_REQUEST['usersearch'] )
printf( '<span class="subtitle">' . __('Search results for “%s”') .
'</span>', esc_html( $_REQUEST['usersearch'] ) );
?>
But the error line is (228th line):-
<div class='tablenav-pages'>
<?php echo $p->show(); // Echo out the list of paging. ?>
</div>
I need to remove the error on search result. It should simply show "No items found".
function pager($items)
{
global $limit;
global $p;
global $searchTerm;
global $pageLimit;
if($items > 0) {
$p = new pagination;
$p->items($items);
$p->limit($pageLimit); // Limit entries per page
$p->target("admin.php?page=User Control&usersearch=".$_REQUEST['usersearch']."&page-limit=".$_REQUEST['page-limit']);
$p->currentPage($_GET[$p->paging]); // Gets and validates the current page
$p->calculate(); // Calculates what to show
$p->parameterName('paging');
$p->adjacents(1); //No. of page away from the current page
if(!isset($_GET['paging'])) {
$p->page = 1;
} else {
$p->page = $_GET['paging'];
}
//Query for limit paging
$limit = "LIMIT " . ($p->page - 1) * $p->limit . ", " . $p->limit;
} else {
echo "No Record Found";
}
}
Your getting the error because your attempting to show a object that is undefined...use a statement to check...this is the premise.
<div class='tablenav-pages'>
<? if(is_object($p)){echo $p->show();} ?>
</div>
<?php
if (isset($p) && is_object($p)){
$p->show();
}
?>
Related
Good day. Please help me to display units of measurement for goods. OcStore 2.3 engine.
By default, units of measurement are not displayed next to the product, they helped me to display them on the product page in this way:
In
catalog/model/catalog/product.php
Find line:
public function getProduct($product_id) {
Before the line mentioned above add the following code:
public function getProductWeightWithUnit($product_id) {
$product_info = $this->getProduct($product_id);
$query = $this->db->query("SELECT unit FROM `" . DB_PREFIX . "weight_class_description` WHERE
weight_class_id='".$product_info['weight_class_id']."'");
if ($query->num_rows) {
return number_format($product_info['weight'],2) . " " . $query->row['unit'];
} else {
return false;
}
}
Save changes and close the file.
Now, open file
catalog/controller/product/product.php
Find line:
if ($product_info['minimum']) {
Before the line mentioned above add the following code:
if ($product_info['weight']) {
$data['weight'] = $this->model_catalog_product->getProductWeightWithUnit($this->request->get['product_id']);
} else {
$data['weight'] = false;
}
Now, the backend code is ready. Based on the theme that you use, you need to edit the correct product.tpl file from your theme. For example if you use the default theme, then the file to edit is the following:
catalog/view/theme/default/template/product/product.tpl
Find the line:
<li><?php echo $text_stock; ?> <?php echo $stock; ?></li>
and add the following code after:
<li><?php echo $weight; ?></li>
Fine! Everything works on the product page. But you need it to work the same in modules (for example Featured Products).
I do all the same steps for the module
Backend:
catalog/controller/extension/module/featured.php
and front end:
catalog/view/theme/default/template/extension/module/featured.tpl
But I get the error:
Notice: Undefined index: product_id in C:\OSPanel\domains\mywebsite.com\catalog\controller\extension\module\featured.php on line 43
That's what it says:
if ($product_info['weight']) {
LINE 43 $data['weight'] = $this->model_catalog_product->getProductWeightWithUnit($this->request->get['product_id']);
} else {
$data['weight'] = false;
}
Why not seen product_id in ??
Open catalog/controller/extension/module/featured.php.
Find
if ($this->config->get('config_tax')) {
Add before
if ($product_info['weight']) {
$weight = $this->model_catalog_product->getProductWeightWithUnit($product_info['product_id']);
} else {
$weight = false;
}
Basically, here I've replaced $this->request->get['product_id'] with $product_info['product_id'], like #K.B. said in his answer, but made more accurate example.
Then, in the same file find
'tax' => $tax,
Add after
'weight' => $weight,
Now go to catalog/view/theme/default/template/extension/module/featured.tpl
Find
<?php if ($product['tax']) { ?>
<span class="price-tax"><?php echo $text_tax; ?> <?php echo $product['tax']; ?></span>
<?php } ?>
Add after
<?php if ($product['weight']) { ?>
<span><?php echo $product['weight']; ?></span>
<?php } ?>
You must use $product_info['product_id'] or just $product_id instead $this->request->get['product_id'] Because you can't get product_id's for several products using this expression, which are in this loop foreach ($products as $product_id) {
I'm trying to solve this for some time. I have PHP function that is called on link click. When I click on link it's directing me to this function but it's not giving any results.
Here is how I call function.
foreach ($bandsN as $aa) {
$str = explode(',', $aa);
$next = $str[1];
?>
<?php echo $str[0]?> </div> <?php
if (isset($_GET['other'])) {other($next);}
}
In this function it's called mysql stored procedure with parameter of php function.
function other($var)
{
echo $var;
if (!$mysqli->multi_query("CALL p($var)")) {
echo "CALL failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
do{
if($resul = $mysqli->store_result()){
$bands = array();
while($ro = $resul->fetch_row())
{
$bands[] = implode(',', $ro);
}
foreach ($bands as $aa) { ?>
<p > <?php echo $aa?> </p> </div><?php
}
$resul->free();
unset($bands);
}}
while($mysqli->more_results() && $mysqli->next_result());
}
I don't know which is the problem. This code inside function is working because I already have it in other part of app.
If somebody can see what is wrong I would be very grateful.
I am attempting to create a shopping basket using PHP, I can add items, clear the whole basket however I am stuck when trying to remove just one item from the basket. I try to send a GET variable into the URL and then decrease the quantity of the specified item based on it's ID. Although at the moment it doesn't seem to work and if I click multiple times the URL gets huge and the GET variables keep getting added to the end rather than changing the whole URL.
Here is my PHP
<?php if(isset($_SESSION["cart"])) {
foreach ($_SESSION["cart"] as $id => $value) {
$ids .= $id . ',';
$count += $value['quantity'];
$totalPrice += $value['price'];
}
$query = $database->find_item_db($ids);
foreach ($query as $single_query) {
$id = $single_query['Sweet_ID'];
echo $single_query['Description']; ?> x <?php echo $_SESSION['cart'][$id]['quantity'] . '<a href=' . $_SERVER['REQUEST_URI'] .'&idToRemove=' . $id . '&action=remove> Remove </a>' . '</br>';
} ?>
<h3>Currently <?php echo $count; ?> Items in the basket</h3>
<h4> Total Price £<?php echo $totalPrice; ?> </h4>
<?php
} else {
echo "Your cart is empty";
}
?>
<?php
session_start();
if ($_GET['action'] == "add") {
$idNumber=intval($_GET['id']);
if (isset($_SESSION['cart'][$idNumber])) {
$_SESSION['cart'][$idNumber]['quantity']++;
} else {
$sql = $database->display_single($idNumber);
$second_id = $sql[0]['Sweet_ID'];
$price = $sql[0]['Price'];
$_SESSION['cart'][$second_id]=array(
"quantity" => 1,
"price" => $price
);
}
} else if ($GET['action'] == 'remove') {
$idNumber=intval($_GET['idToRemove']);
$_SESSION['cart'][$idNumber]['quantity']--;
} else if ($_GET['action'] == 'clear') {
unset($_SESSION['cart']);
}
?>
Change this:
'<a href=' . $_SERVER['REQUEST_URI'] .'&idToRemove=' . $id . '&action=remove> Remove </a>'
into this:
' Remove '
as $_SERVER['REQUEST_URI'] already contains all the parameters that are currently present in the URL.
This will create a blank URL (pointing to the current page) with only the idToRemove and action parameters.
(And add double quotes, to keep the URL nicely contained within the href attribute)
The problem comes from your HTML code.
I think you have something like this in your PHP :
<form action="<?php echo $_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING']?>"
And as you always (re)call the same HTML form, the previous posted GET vars a posted again and again ....
I've programmed a basket class based on session you can use it , any question ask me please
class basket {
function add($pid,$qty){
if(!isset($_SESSION['basket'])){
$_SESSION['basket']=array();
$_SESSION['basket'][0]['productid']=$pid;
$_SESSION['basket'][0]['qty']=$qty;
echo 'The product was successfully added';
}
else {
if($this->isexist($pid,$qty)){
echo 'The quantity was successfully updated';
}
else{
$m=$_SESSION['basket'];
$max=count($m);
$_SESSION['basket'][$max]['productid']=$pid;
$_SESSION['basket'][$max]['qty']=$qty;
echo 'The product was successfully added';
}
}
}
function isexist($pid,$qty) {
$m=$_SESSION['basket'];
$max=count($m);
for($i=0;$i<$max;$i++){
if($pid==$_SESSION['basket'][$i]['productid']){
$_SESSION['basket'][$i]['qty']=$qty;
return true;break;}
}
return false;}
function delete($pid){
$m=$_SESSION['basket'];
$max=count($m);
for($i=0;$i<$max;$i++){
if($pid==$_SESSION['basket'][$i]['productid']){
unset($_SESSION['basket'][$i]);
$_SESSION['basket']=array_values($_SESSION['basket']);
$_SESSION['basket'.'num']-=1;echo 'The product was successfully delete';
break;}
}
}
function modify($pid,$qty){
$m=$_SESSION['basket'];
$max=count($m);
if($qty>0){
for($i=0;$i<$max;$i++){
if($pid==$_SESSION['basket'][$i]['productid']){
$_SESSION['basket'][$i]['qty']=$qty;break;}
}
}
else $this->delete($pid);
}
function show_basket() {
$max=count($_SESSION['basket']);
for($i=0;$i<$max;$i++){
echo 'id=>'.$_SESSION['basket'][$i]['productid'].'qty=>'.$_SESSION['basket'][$i]['qty'];
}
}
}
In WordPress i'm currently using Yoast's SEO Plugin to display breadcrumbs for my pages and posts, which is working fine when visiting a specific page.
Here is the function i'm using to display the breadcrumbs inside of my WordPress templates:
<?php if ( function_exists('yoast_breadcrumb') ) {
yoast_breadcrumb('<p id="breadcrumbs">','</p>');
} ?>
For example when browsing to Team Members which is a child of About Us I get:
Home > About Us > Team Members
However i'd like to be able to display the same breadcrumbs (for the individual pages and posts) inside the search results loop.
So far what displays when searching for Members is:
Your Search Results:
Team Members
Home > Search > Members
Members Area
Home > Search > Members
But I don't want breadcrumbs for the Search Results page, I want them for the individual pages and posts that are displayed as a result of searching for a keyword.
For example Imagine I searched again for Members I would like displayed the below:
Your Search Results:
Team Members
Home > About Us > Team Members
Members Area
Home > Members Area
I'm not fussed if this is or isn't integrated with the SEO plugin, however thus far it's the best solution I found to display breadcrumbs in WordPress!
Also incase abody requires it, here is my search.php file: http://pastebin.com/0qjb2954
Try this. That's my own working snippet that shows breadcrumbs inside search loop.
/*Begin Loop */
<?php
echo '<div class="b-search_result_list__item_breadcrumbs breadcrumbs">';
$current_type = get_post_type();
if ($current_type == 'page') {
$parents = get_post_ancestors(get_the_ID());
if($parents){
for($i=count($parents)-1;$i>=0;$i--){
echo '<span typeof="v:Breadcrumb">';
echo '<a rel="v:url" property="v:title" title="'.get_the_title($parents[$i]).'" href="'.get_permalink($parents[$i]).'">'.get_the_title($parents[$i]).'</a>';
echo '</span>';
}
}else{
echo '<span typeof="v:Breadcrumb">';
echo '<a rel="v:url" property="v:title" title="'.get_bloginfo('name').'" href="'.get_bloginfo('url').'">'.get_bloginfo('name').'</a>';
echo '</span>';
}
echo '<span typeof="v:Breadcrumb">';
echo '<span property="v:title">'.get_the_title().'</span>';
echo '</span>';
}else{
$current_obj = get_post_type_object($current_type);
echo '<span typeof="v:Breadcrumb">';
echo '<a rel="v:url" property="v:title" title="'.get_bloginfo('name').'" href="'.get_bloginfo('url').'">'.get_bloginfo('name').'</a>';
echo '</span>';
echo '<span typeof="v:Breadcrumb">';
echo '<a rel="v:url" property="v:title" title="'.$current_obj->labels->name.'" href="'.get_post_type_archive_link( $current_type ).'">'.$current_obj->labels->name.'</a>';
echo '</span>';
$current_taxonomies = get_object_taxonomies($current_type);
if($current_taxonomies){
$current_terms = get_the_terms(get_the_ID(), $current_taxonomies[0]);
if($current_terms){
$current_term = array_shift($current_terms);
echo '<span typeof="v:Breadcrumb">';
echo '<a rel="v:url" property="v:title" title="'.$current_term->name.'" href="'.get_term_link($current_term).'">'.$current_term->name.'</a>';
echo '</span>';
/*
var_dump($current_obj->labels->name); // Archive name
var_dump(get_post_type_archive_link( $current_type )); // Archive link
var_dump($current_term->name); // Term name
var_dump(get_term_link($current_term)); // Term link
var_dump(get_permalink()); // Post link
*/
}
}
echo '<span typeof="v:Breadcrumb">';
echo '<span property="v:title">'.get_the_title().'</span>';
echo '</span>';
}
echo '</div>';
?>
/*End Loop*/
try adding this line of code above the yoast breadcrumb function in your search.php file:
WPSEO_Breadcrumbs::$instance = NULL;
This would be line 22 I believe, and also make sure to use the Yoast breadcrumb function from your question, not the new breadcrumb() function that's there now.
Please let me know if this works!
Full explanation:
The Yoast plugin breadcrumbs functionality is build on the page load, based on the current page as the child. To make it load the right child and parents, you'd need to reset it before you run the function. There is no built-in reset function, however setting the static $instance to NULL should cause the plugin to re-generate its data based on the current global post object which is set while you're looping.
Building upon Yavor's answer I found a way. Been banging my head about it for hours. You can place the backup and restore otuside of the loop though. Here it is:
global $wp_query;
//backup
$old_singular_value = $wp_query->is_singular;
//change
$wp_query->is_singular = true;
//reset
WPSEO_Breadcrumbs::$instance = NULL;
//breadcrumbs
if (function_exists('yoast_breadcrumb')){
yoast_breadcrumb('<p id="breadcrumbs">','</p>');
}
//restore
$wp_query->is_singular = $old_singular_value;
It fakes the query to make it singular so the newly-refreshed breadcrumbs thinks that this is not the search page but a single post or page or whatever you are displaying as your search results.
Using a plugin to generate breadcrumbs is not really necessary. Here's a simple PHP function you can add to your functions.php file:
function breadcrumbs() {
global $post;
echo "<ul id='breadcrumbs'>";
if (!is_home()) {
echo '<li>Home</li>';
if (is_category() || is_single()) {
echo "<li>" . the_category(' </li><li> ');
if (is_single()) {
echo "</li><li>" . the_title() . "</li>";
}
} elseif (is_page()) {
if($post->post_parent){
foreach ( get_post_ancestors( $post->ID ) as $ancestor ) {
echo '<li>' . get_the_title($ancestor) . '</li>' . get_the_title();
}
} else {
echo "<li>" . get_the_title() . "</li>";
}
}
} elseif (is_tag()) {
single_tag_title();
} elseif (is_day()) {
echo "<li>Archive for " . the_time('F jS, Y') . "</li>";
} elseif (is_month()) {
echo "<li>Archive for " . the_time('F, Y') . "</li>";
} elseif (is_year()) {
echo "<li>Archive for " . the_time('Y') . "</li>";
} elseif (is_author()) {
echo "<li>Author Archive</li>";
} elseif (isset($_GET['paged']) && !empty($_GET['paged'])) {
echo "<li>Blog Archives</li>";
} elseif (is_search()) {
echo "<li>Search Results for" . the_search_query() . "</li>";
}
echo "</ul>";
}
along with some CSS to style it, customize as you desire
#breadcrumbs {
list-style:none;
margin:5px 0;
overflow:hidden;
}
#breadcrumbs li{
float:left;
}
#breadcrumbs li+li:before {
content: '| ';
padding:0 4px;
}
You can then implement those breadcrumbs on any page you like, including your searchpage.php file or whichever file you use to display search results with this call
<?php breadcrumbs(); ?>
The search pages have a conditional function that can be used. You could always apply that to loading the breadcrumbs. Here is an example:
if ( ! is_search() ) {
if ( function_exists('yoast_breadcrumb') ) {
yoast_breadcrumb('<p id="breadcrumbs">','</p>');
}
}
It depends where you are loading the breadcrumbs as well, but this should typically work unless your theme is very unique.
I am using opencart and successfully added minimum order price for all transactions.
This is the code I used:
<?php if ($this->cart->getSubtotal() >= 10) { ?>
<div id="payment"><?php echo $payment; ?></div>
<?php } else { ?>
<div class="warning">Minimum 10 Euro to checkout</div>
<?php } ?>
Now I want to exclude one category out of it so that $9 product from that category can be bought.
Update 1:
Thank you so much for the help shadyyx
I tried shadyyx method but I am getting this error:
unexpected T_BOOLEAN_OR in this line
<?php if ($this->cart->getSubtotal() >= 10 || $this->cart->productsAreInCategory(1)) { ?>
Update 2: I tried this but it gave a pop up saying just error and ok button
<?php if (($this->cart->getSubtotal() >= 10) || $this->cart->productsAreInCategory(1)) { ?>
I tried this
<?php if (($this->cart->getSubtotal() >= 10) || ($this->cart->productsAreInCategory(1))) { ?>
it did not give any error and does same work (min amount for all orders regardless of category id)
I would go this way:
Extend the system/library/cart.php and add a method:
public function productsAreInCategory($category_id) {
$product_ids = array();
foreach($this->getProducts() as $product) {
$product_ids[] = $product['product_id'];
}
$categories = $this->db->query('SELECT category_id FROM ' . DB_PREFIX . 'product_to_category WHERE product_id IN (' . implode(',', $product_ids) . ')')->rows;
$category_ids = array();
foreach($categories as $category) {
$category_ids[] = $category['category_id'];
}
if(in_array($category_id, $category_ids) {
return true;
}
return false;
}
This method should accept a $category_id parameter to test against and should load categories for all products in cart. After first match a true is returned, if no match, a false is returned. You can now use this method this way:
<?php if (($this->cart->getSubtotal() >= 10) || $this->cart->productsAreInCategory(1)) { ?>
<div id="payment"><?php echo $payment; ?></div>
<?php } else { ?>
<div class="warning">Minimum 10 Euro to checkout</div>
<?php } ?>
Just replace the category ID in $this->cart->productsAreInCategory(1) with the correct one.