2

How to display data group by $vendorid, When I order 5 products from different vendors.

Ex : Random added products to cart.

| $vendorid | $product_name |
-----------------------------
|     1     |    TEST31     |
|     4     |    TEST21     |
|     3     |    TEST20     |
|     3     |    TEST17     |
|     4     |    TEST11     |

But I want to display like this :

| $vendorid | $product_name |
-----------------------------
|     1     |    TEST31     |
|     3     |    TEST20     |
|     3     |    TEST17     |
|     4     |    TEST21     |
|     4     |    TEST11     |

Code :

foreach($_SESSION["products"] as $product){

    $product_name = $product["p_name"];
    $vendorid = $product["p_member_id"];        
    $cart_box = "<li>$vendorid $product_name</li>";
}
echo $cart_box;

This gives me an output in the same result that I've added to cart like this :

| $vendorid | $product_name |
-----------------------------
|     1     |    TEST31     |
|     4     |    TEST21     |
|     3     |    TEST20     |
|     3     |    TEST17     |
|     4     |    TEST11     |
8
  • 2
    Apply sort function. Commented Jan 9, 2016 at 9:11
  • assuming _SESSION["products"] comes from a datasource, just use an order by ( at the source ) Commented Jan 9, 2016 at 9:12
  • What they are saying is that you need to use ORDER BY in the mysql_query string at the end. add this to the end of the string ORDER BY p_member_id; Commented Jan 9, 2016 at 9:14
  • Add ODER BY p_member_id ASC at last.... Commented Jan 9, 2016 at 9:15
  • 1
    I somehow doubt that ORDER BY is the solution here. Given that _SESSION is used, it's more likely that somehwere code like $_SESSION['products'][] = $newitem; is executed (without hitting the database at this stage). Commented Jan 9, 2016 at 9:16

3 Answers 3

5
<?php
// sample data
$_SESSION["products"] = [];
$_SESSION["products"][] = ['vendorid'=>1, 'product_name'=>'TEST31' ];
$_SESSION["products"][] = ['vendorid'=>4, 'product_name'=>'TEST21' ];
$_SESSION["products"][] = ['vendorid'=>3, 'product_name'=>'TEST20' ];
$_SESSION["products"][] = ['vendorid'=>3, 'product_name'=>'TEST17' ];
$_SESSION["products"][] = ['vendorid'=>4, 'product_name'=>'TEST11' ];

// actual code
usort($_SESSION["products"], function($a, $b) {
    return $a['vendorid'] - $b['vendorid'];
});

foreach( $_SESSION["products"] as $product ) {
    echo '<li>',
        htmlspecialchars($product['vendorid']), ' ', htmlspecialchars($product['product_name']),
    "</li>\r\n";
}

prints

<li>1 TEST31</li>
<li>3 TEST17</li>
<li>3 TEST20</li>
<li>4 TEST11</li>
<li>4 TEST21</li>

It doesn't really matter where and when you sort the $_SESSION["products"] array.
But if you do it every time a new item is appended you don't have to do it every time you display the cart and if (and when) php gets some "sorted-insert-into-array" functon you can replace the code by that and save some computation time....

see also: http://docs.php.net/manual/en/function.usort.php

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much, I'll try your code on the next Monday.
Hi, I've try your code and it's displayed this error Parse error: syntax error, unexpected '[' on first line.
Then you are using php < 5.4 and have to use array() instead of [ ] for array literals, see 3v4l.org/V3quT and php.net/manual/en/migration54.new-features.php
1

try the below function it will work

function aasort (&$array, $key) {
$sorter=array();
$ret=array();
reset($array);
foreach ($array as $ii => $va) {
    $sorter[$ii]=$va[$key];
}
asort($sorter);
foreach ($sorter as $ii => $va) {
    $ret[$ii]=$array[$ii];
}
$array=$ret;

}

aasort($_SESSION["products"],"p_member_id");

1 Comment

Thank you so much, But I'll try your code on the next Monday.
0

Use ORDER BY in query to ascending in the query.

For example:

ORDER BY ID ASC;

3 Comments

what is the issue? reply me with your query
Problem solved, But thank you so much for your reply.
it's my pleasure. i think you are new in database designing! best of luck

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.