Ignore First Array Item from $wpdb->get_results()

Lets assume you have a product table in WordPress where you save product details. In front end your design is like in a division there will be information of a product and rest of the product will be list out below with summary. When you click on a product then in top division there will be information of your clicked product and rest in below. In this case you have to query selected product in top division and eliminate or ignore that product in below summary division. Lets learn the query to ignore first array item from $wpdb->get_results() WordPress query.

product-list-wp

Lets say that a single product has a title, an image and description. So in top division to show the selected product’s description the query will be:

[html]

<?php
global $wpdb;
global $wp_query;
$product_id = $wp_query->query_vars[‘product_id’];
$table_name = $wpdb->prefix . "product_lists";
$selected_product = $wpdb->get_row( "SELECT * FROM $table_name where product_id=’$product_id’ ");
?>

<div class="product_detail">
<img src="<?php echo($selected_product->product_image); ?>" alt="<?php echo($selected_product->product_title); ?>"/>
<?php echo($selected_product->product_title); ?>
<?php echo($selected_product->product_description); ?>
</div>

[/html]

Here $product_id is the selected product id and by using get_row query we have shown the selected product image title and description.

Now the rest of the product list:

Here is the query to show rest of the product list summery:

[html]

<?php
$rest_of_the_products = $wpdb->get_results(‘SELECT * FROM ‘ . $wpdb->prefix . ‘product_lists WHERE product_id!="’.$product_id.’"’);
$rest_of_the_product[] = array();
$i = 0;
foreach ( $rest_of_the_products as $rest_of_the_product ) {
$rest_of_the_product[$i][‘product_id’] = $first_product->product_id;
$rest_of_the_product[$i][‘product_title’] = $first_product->product_title;
$rest_of_the_product[$i][‘product_description’] = $first_product->product_description;
$rest_of_the_product[$i][‘product_image’] = $first_product->product_image;
$i++;
}

foreach ( $rest_of_the_product as $product )
{
?>
<div class="rest-of-the-products">

<h1><?php echo $product["product_title"] ; ?></h1>

<a href="<?php bloginfo(‘url’);?>/product-details/<?php echo $product["product_id"] ; ?>/<?php echo $p_all["product_title"] ; ?>/">View Product</a>

</div>
<?php
}
?>

[/html]

Here we have used $wpdb->get_results and we ignore the product_id which has been select on top portion.

 You can also use array_shift if you always want to ignore first element:

[html]
<?php
$rest_of_the_products = $wpdb->get_results(‘SELECT * FROM ‘ . $wpdb->prefix . ‘product_lists’);
array_shift($rest_of_the_products);
foreach ( $rest_of_the_products as $product )
{
?>
<div class="rest-of-the-products">

<h1><?php echo $product->product_title; ?></h1>

<a href="<?php bloginfo(‘url’);?>/product-details/<?php echo $product->product_id; ?>/<?php echo $product->product_title; ?>/">View Product</a></pre>
</div>
<?php
}
?>
[/html]

Author: mytutorialguru

Share This Post On

Submit a Comment

Your email address will not be published. Required fields are marked *