0

I have a code in which i want to display product list from array. I am getting around 1000 products from an array of n number of brands.n can be 1,2,3..etc. I want to display 2 products of one brand then 2 products from second brand and then 2 from next brand and so on it should be repeated while displaying..

Homepage.php

<html>
<head>
<title>Insert title here</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<style type="text/css">
#image{
width:250px;
height:250px;
border:1px solid black;
}
</style>
</head>
<body>

<script type="text/javascript">
    function get_check_value() {
        var c_value = [];

        $('input[name="brand"]:checked').each(function () {
            c_value.push(this.value);
        });
        return c_value.join(',');

    }
    function get_disc_value(){
    var d_value=[];
        $('input[name="discount"]:checked').each(function () {
            d_value.push(this.value);
        });
        return d_value.join(',');
        }

    $(document).ready(function(){
   checkboxValidate = function (e) {
   if(e)e.preventDefault();
    //alert("hi");
        //var os = $('#originState').val();
       //var c = $('#commodity').val();
        //var ds = $('#destState').val();
        var ser = get_check_value();
        var disc=get_disc_value();
        //var queryString = "os=" + os;
        var data = "?ser=" + ser;
        var queryString = "&ser=" + ser;
       // alert(ser);
       $.ajax({
       //alert("ajax");
        type: "POST",
        url: "sortingajax.php",
        data: {ser:ser,disc:disc},
        dataType :  'html',
        success: function (b) {
           // alert(a+' ok. '+b)
            $('#results').html(b);
            console.log(b);
        }
    });

    } 
    $( "[type=checkbox]" ).change(checkboxValidate);

    checkboxValidate();
});
</script>


brand
    <input type="checkbox" name="brand" value="Sunbaby" id="check" />Sunbaby
    <br/>
    <input type="checkbox" name="brand" value="Advance Baby" id="check"/>Advance Baby
    <br/>
    store
    <br/>
    <input type="checkbox" name="discount" value="10" />10
    <br/>
    <input type="checkbox" name="discount" value="20" />20
    <br/>
    <input type="checkbox" name="discount" value="30" />30
    <br/>


<button id="btnSubmit">sort</button>
<div id="image">
<img src="http://img5a.flixcart.com/image/sunglass/4/u/y/mb-d4-09b-miami-blues-free-size-275x275-imadzkhuchryqjgp.jpeg" width="250px" height="250px"/>
</div>
<div id="results">
sdfsdfsdfsdfdsfgsdgsbsfgvf
</div>
</body>
</html>

sortingajax.php

<?php

include('connection.php');
$query=$_POST['ser'];

$query2=$_POST['disc'];


$query=explode(",",$query);
$query = array_filter($query);
$query2=explode(",",$query2);
$query2 = array_filter($query2);
$result=count($query);
$result1=count($query1);
//echo $result;
echo $query;
echo $query1;
echo $result1;
$parts = array();
$brandarray=array();
$discarray=array();
$limit = 10;
    $offset = 0;
    foreach( $query as $queryword ){
    $brandarray[] = '`BRAND` LIKE "%'.$queryword.'%"';

}
foreach( $query2 as $discword ){
    $discarray[] = '`DPERCENT` < "'.$discword.'"';

}
"/>

I want to display 2 products of one brand first and then 2 from second one and then from next brands.But according to above code it displays all products from one brand and then from next brand....Please guide me on how to change above code product list brand by brand..

user3545382
  • 3
  • 1
  • 6
  • Most people won't just debug your code, try to focus on the very problem you're facing. [sscce](http://www.sscce.org/) – Michael Kunst Apr 22 '14 at 15:00
  • @MichaelKunst-I want to display products in such a way that from 1000 array products,one from one brand second from second brand third from third brand and then repeating it.But i m not able to do that with above code.its not showing any error,i just want to get my code changed with some help – user3545382 Apr 22 '14 at 15:02

3 Answers3

0

If you insert _POST vars in mysql queries like this you will be hacked immediately see :

'`BRAND` LIKE "%'.$queryword.'%"';
Alexis Pigeon
  • 7,423
  • 11
  • 39
  • 44
  • I didnt get hacked means.please guide in detail @user2780675 – user3545382 Apr 22 '14 at 15:04
  • He means, you need to check your data for malicious code before trying to execute it against a database. Google SQL Injection to learn more. Also, here is a [Stack Overflow question](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) where someone is asking how to be safe against these kinds of attacks. The golden rule of programming is to never trust input that users are supplying. You will always want to check it to make sure it's safe before doing anything with it. – Quixrick Apr 22 '14 at 15:12
  • K.So if i use GET in place of POST,will that be fine?.or what do i need to change – user3545382 Apr 22 '14 at 15:19
  • No, certainly not; `$_POST` vs `$_GET` doesn't matter. `$_POST` is fine. It's not the method that you receive your data that causes problems. It's blindly taking user data and passing it on to your database. You'll want to clean (sanitize) it first. This is **very** important to understand in programming. Check out that article I linked to in my previous comment or Google around for "SQL Injection". MySQL has introduced some features that you can use to combat this, but you can also clean your data before you send it to the database. – Quixrick Apr 22 '14 at 15:25
0

based on a quick overview of your code, you seem to be adding the first element from each product to the array and then the second and then the third etc.. until you reach the highest count.

try instead, first limiting the max to only 2 instead of the max (so in this case, $highest_number should =2) and second, add 1 brand, then the next then the next, instead of all at once, as unless you are handling it some other way would interlace each of the brands. I dont know if thats what you want?

does this help?

Matt
  • 113
  • 4
  • 8
0

I've spent a little time looking at your code and am not entirely sure how it works. It seems like there's got to be an easier way to do this. Can you start a counter and cut off your loops once it hits the counter?

$i = 0;
$all_products = array();

while($row = mysql_fetch_array($firstsql3)) {

    if (++$i <= 2) {

        // DO YOUR STUFF HERE
        $product_name = $row['product_name']; // GET THE PRODUCT NAME FROM THE DB
        $product_color = $row['product_color'];
        $product_size = $row['product_size'];

        $individual_product_array = array(); // CREATE A NEW ARRAY FOR THIS PRODUCT
        $individual_product_array['color'] = $product_color;
        $individual_product_array['size'] = $product_size;

        $all_products[$product_name][] = $individual_product_array;

    }

}
Quixrick
  • 3,190
  • 1
  • 14
  • 17
  • I edited my code according to your code,May be your code can crack it.But still am far from it.Please check my edited post,i think i have picked ur code wrongly...Please check – user3545382 Apr 22 '14 at 15:18
  • I see that you made an edit, but you removed the portion of code that this applies to. Anyway, you won't be able to just plop my code into your code. I made up an example since I do not fully understand how your code is working. You need to take the concept of what I was trying to demonstrate. Sorry if I confused you on that. Basically, you can just limit the number of results based on a counter. Start the counter at zero. `$i = 0;`. Then, each time you loop through your query `while ($row = ...`, increment the counter and check to see if it's less than or equal to 2. `if (++$1 <= 2) {` – Quixrick Apr 22 '14 at 15:22
  • @Quixrick-Can u please help me in editing the code as ur scenario is completely correct for me.As i m new to PHP.it will be big thanks to u if u can help me in editing sir... – user3545382 Apr 22 '14 at 15:42
  • Yes, I offered already to do that. Check my previous comment for a link to collabedit. I already have your code imported over there. – Quixrick Apr 22 '14 at 15:43