Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7047941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:49:55+00:00 2026-05-28T02:49:55+00:00

I have a code: <table> <?php if(isset($_SESSION[‘cart’])) foreach($_SESSION[‘cart’] as $key => $item){ ?> <tr>

  • 0

I have a code:

<table>
                <?php
                if(isset($_SESSION['cart']))
                foreach($_SESSION['cart'] as $key => $item){
                ?>
                    <tr>
                        <td><?php echo $item['num'] ?></td>
                        <td><?php echo $item['name'] ?></td>
                        <td>
                            <img class="cart_remove" src="images/x.png" alt="" /><span class="item_index"><?php echo $key ?></span>
                        </td>
                    </tr>
                <?php } ?>
            </table>

It prints session variables like:

1 name1
2 name2
3 name3
4 name4

But I need the latest added values print first. Like:

4 name4
3 name3
2 name2
1 name1

So, how can i print session variables in descending order? And also, is it possible to print, for example, latest 3 session variables?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T02:49:56+00:00Added an answer on May 28, 2026 at 2:49 am

    Reverse the elements order:

    krsort($_SESSION['cart']);
    

    If you want to print the last 3 entries in $_SESSION['cart'] specifically – you can do something like this:

    $i = 0;
    foreach ($_SESSION['cart'] as $key => $value)
    {
        // do your other stuff here
        if (++$i === 3) break;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to following code: http://www.nomorepasting.com/getpaste.php?pasteid=22987 If PHPSESSID is not already in the table
I have the following PHP code doing a very simple select into a table.
I have code: <display:table name=sessionScope.allUserslist id=userList export=false pagesize=1> <display:column title=Select style=width: 90px;> <input type=checkbox
I have this code <?php session_start(); if (isset($_GET[cmd])) $cmd = $_GET[cmd]; else die(You should
I have the following code: <form action= method=POST> <?php $count = isset($_POST['count']) ? $_POST['count']
This is my php code <?php if(isset($_GET['name'])) { $var = $_GET['name']; $newvar =str_replace (
I have some PHP code that generates dynamic tables of data on the fly.
here is my mysql and php code layout: I have 3 tables tableA stores
I have a problem where I have code like <table id=table> <tr> <td>...</td> <tr>
I have the following test-code: CREATE TABLE #Foo (Foo int) INSERT INTO #Foo SELECT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.