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

  • SEARCH
  • Home
  • 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 8169013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:42:51+00:00 2026-06-06T20:42:51+00:00

To start off, here is the JSON response that I’ve been working on: {

  • 0

To start off, here is the JSON response that I’ve been working on:

{  "status": "ok",
 "permalinks": [
"http:\/\/localhost\/blog\/2012\/06\/",
"http:\/\/localhost\/blog\/2012\/04\/",
"http:\/\/localhost\/blog\/2012\/03\/",
"http:\/\/localhost\/blog\/2011\/10\/"
 ],
 "tree": {
"2012": {
  "06": "221",
  "04": "2",
  "03": "3"
},
"2011": {
  "10": "3"
}
}
}

PHP is being used to format it as an HTML UL inside a DIV:

function DisplayResponse(){
$baseref = 'http://localhost/' ;
$url = $baseref . 'api/get_date_index';
$r = file_get_contents( $url ) ;
if( $r !== false ){
    $r = json_decode($r,true);
    $t = $r['tree'] ;
    echo '<div class="sidebar">' ;
    echo '<h3>Response</h3>' ;
    echo '<ul class="sidebar-list">' ;
    for ( $y = 0 ; $y <= 2 ; $y++ ){
        if ( $y == 0 ){
            for ( $m = 12 ; $m >= 0 ; $m-- ) {
                $d = mktime ( 0, 0, 0, $m, 1, Date("Y") ) ;
                $du = Date("Y", $d ) ;
                $dm = Date("M", $d ) ;
                if ( $m < 11 ){
                    $mm = sprintf("%02d",$m) ;
                    if ( $t[$du][$mm] != NULL ){

                        echo '<li><a href="' . $baseref . 'blog/' . $du . '/' . $mm
                            . '/">' . $dm . ',&nbsp;' . $du . '</a></li>' ;
                    }
                } else {
                    if ( $t[$du][$m] != NULL ){

                        echo '<li><a href="' . $baseref . 'blog/' . $du . '/' . $mm
                            . '/">' . $dm . ',&nbsp;' . $du . '</a></li>' ;
                    }
                }
            }
        } else {
            for ( $m = 12 ; $m >= 0 ; $m-- ) {
                $d = mktime ( 0, 0, 0, $m, 1, ( Date("Y") - $y) ) ;
                $du = Date("Y", $d );
                $dm = Date("M", $d ) ;
                if ( $m < 11 ){
                    $mm = sprintf("%02d",$m) ;
                    if ( $t[$du][$mm] != NULL ){

                        echo '<li><a href="' . $baseref . 'blog/' . $du . '/' . $mm
                            . '/">' . $dm . ',&nbsp;' . $du . '</a></li>' ;
                    }
                } else {
                    if ( $t[$du][$m] != NULL ){

                        echo '<li><a href="' . $baseref . 'blog/' . $du . '/' . $mm
                            . '/">' . $dm . ',&nbsp;' . $du . '</a></li>' ;
                    }
                }
            }
        }
    }

    echo '</ul></div><div class="sidebar-bottom"></div>' ;

}

}

The output is the desired result of having month and year line item wonderfully displayed in descending on the section of a page, but how do I clean up this code so that is beautifully displayed in the source?

  • 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-06-06T20:42:53+00:00Added an answer on June 6, 2026 at 8:42 pm

    Some tips:

    • Divide into three parts:

      1. Fetch the array from the website
      2. Extract the interesting part out of it
      3. Do the display with that part.

        $response = get_response();
        $tree = get_tree($response);
        output_tree($tree);
        
    • Inside your current display code you have got a lot of code-duplication. Just compact any duplicate code, like suggested by putting it into a function. When you do so, you might also realize that most of the code isn’t even needed.

    • You can iterate over an array, even inside one:

      echo "<ul>\n";
      foreach ($tree as $year => $months) {
          foreach ($months as $month => $count) {
              echo "  <li>", $month, ",&nbsp;", $year, "</li>\n";
          }
      }
      echo "<ul>\n";
      

      This small example displays the whole list, I just have left creating the links out which should be okay for demonstration purposes.

    If you keep things small and apart, you will see that it’s easier to create something.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is more of a 2nd part of a question that start off here
for reference here is a jsfiddle link: http://jsfiddle.net/STmwz/4/ To start off with, there is
To start off, I have this XML file that contains recursive parent-child elements. Here
I'm creating a simple questions DB, here are some details to start: 1st off,
Lets start off with a list of strings that will be used to filter
I'll start off with a solid example: I have a function that generates hashes
First, I'll start off by saying that I do not have control over the
I am having trouble sending JSON to a WebMethod. Here is the way that
To start off, I'm not that great with database strategies, so I don't know
if i start off on a Detail page: http:\\www.mysite.com\App\Detail i have a controller action

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.