SQL Query to select featured products after a particular number of rows

+4 Harshit Punn · November 25, 2014
I am working on offers website and i am developing it on PHP. I want to show featured products after every 9 normal products but it i am new to SQL need help. These products data are being extracted from database.http://puu.sh/d4YHq/92684ecb37.png

Post a Reply

Replies

Oldest  Newest  Rating
+2 Ron Butcher · November 29, 2014
Try this:

<?php
//  Run both queries
$query = "SELECT * FROM products";
$result1 = mysqli_query($databseconnection,$query);

$query2 = "SELECT * FROM products";
$result2 = mysqli_query($databseconnection,$query2);

//  Initialize counter
$i=1;

//  Run through first set of results
while($row = mysqli_fetch_array($result1)){
//  Set product name and features then echo
$productname = $row['productname'];
$productfeatures = $row['features'];
echo $productname."<br />".$productfeatures;

//  After every ninth loop, echo the feature
if($i%9 == 0){
//  Call the next available row of features
$feature = mysqli_fetch_array($result2)
//  Set and display feature
$featurename = $feature['name'];
$featureproduct = $feature['feature'];
echo $featurename."<br />".$featureproduct;
}
//  Increase counter
$i++;
}
+1 Bucky Roberts · November 25, 2014
This is easier done with PHP. Can you post your source code?
+1 Harshit Punn · December 3, 2014
Thank you Ron it worked :)
0 Harshit Punn · November 25, 2014
<?php

$query = "SELECT * FROM products";

$result = mysqli_query($databseconnection,$query);

$i=1;

while($row = mysqli_fetch_array($result)){

$productname = $row['productname'];

$productfeatures = $row['features'];

if($i%9){

$query2 = "SELECT * FROM products WHERE feature=1";

$result = mysqli_query($databseconnection,$query2);

while($feature = mysqli_fetch_array($result)){

$featurename = $feature['name'];

$featureproduct = $feature['feature'];

echo $featurename."<br />".$featureproduct;


}


}
else{

echo $productname."<br />".$productfeatures;

}
}

?>
  • 1

SQL & Databases

106,970 followers
About

Everything SQL and Databases related in here!

Links
Moderators
Bucky Roberts Administrator