Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

Loop and print array results

 
Post new topic   Reply to topic    Smarty Forum Index -> Smarty Development
View previous topic :: View next topic  
Author Message
Linu82
Smarty n00b


Joined: 29 Aug 2017
Posts: 1

PostPosted: Tue Aug 29, 2017 12:28 pm    Post subject: Loop and print array results Reply with quote

Hi,

I followed the instructions at https://www.smarty.net/docsv2/en/language.function.foreach.tpl but cannot get this to work.

My array is called $getDNSrecords and contains:

Code:

array(2) {
  ["metadata"]=>
  array(4) {
    ["result"]=>
    int(1)
    ["version"]=>
    int(1)
    ["command"]=>
    string(8) "dumpzone"
    ["reason"]=>
    string(15) "Zone Serialized"
  }
  ["data"]=>
  array(1) {
    ["zone"]=>
    array(1) {
      [0]=>
      array(1) {
        ["record"]=>
        array(18) {
          [0]=>
          array(4) {
            ["raw"]=>
            string(148) "; cPanel first:11.66.0.13 (update_time):1503177319 11.66.0.13: Cpanel::ZoneFile::VERSION:1.3 hostname:central.sometest.om latest:11.66.0.13"
            ["ttl"]=>
            string(5) "86400"
            ["Line"]=>
            int(1)
            ["type"]=>
            string(4) ":RAW"
          }
          [1]=>
          array(4) {
            ["ttl"]=>
            string(5) "86400"
            ["Line"]=>
            int(2)
            ["raw"]=>
            string(37) "; Zone file for sometest.com"
            ["type"]=>
            string(4) ":RAW"
          }
          [2]=>
          array(3) {
            ["type"]=>
            string(4) "$TTL"
            ["ttl"]=>
            string(5) "14400"
            ["Line"]=>
            int(3)
          }
          [3]=>
          array(13) {
            ["minimum"]=>
            string(5) "86400"
            ["expire"]=>
            string(7) "1209600"
            ["type"]=>
            string(3) "SOA"
            ["ttl"]=>
            string(5) "86400"
            ["name"]=>
            string(22) "sometest.com."
            ["Lines"]=>
            int(6)
            ["class"]=>
            string(2) "IN"
            ["retry"]=>
            string(4) "7200"
            ["serial"]=>
            string(10) "2017081901"
            ["Line"]=>
            int(4)
            ["mname"]=>
            string(22) "ns.nameserver1.com"
            ["refresh"]=>
            string(4) "3600"
            ["rname"]=>
            string(33) "root.central.sometest.com"
          }
          [4]=>
          array(4) {
            ["Line"]=>
            int(10)
            ["ttl"]=>
            string(5) "86400"
            ["raw"]=>
            string(0) ""
            ["type"]=>
            string(4) ":RAW"
          }
          [5]=>
          array(6) {
            ["type"]=>
            string(2) "NS"
            ["nsdname"]=>
            string(22) "ns.nameserver1.com"
            ["class"]=>
            string(2) "IN"
            ["name"]=>
            string(22) "sometest.com."
            ["Line"]=>
            int(11)
            ["ttl"]=>
            string(5) "86400"
          }
          [6]=>
          array(6) {
            ["name"]=>
            string(22) "sometest.com."
            ["ttl"]=>
            string(5) "86400"
            ["Line"]=>
            int(12)
            ["class"]=>
            string(2) "IN"
            ["nsdname"]=>
            string(22) "ns.nameserver2.com"
            ["type"]=>
            string(2) "NS"
          }
          [7]=>
          array(6) {
            ["class"]=>
            string(2) "IN"
            ["nsdname"]=>
            string(22) "ns.nameserver3.com"
            ["ttl"]=>
            string(5) "86400"
            ["Line"]=>
            int(13)
            ["name"]=>
            string(22) "sometest.com."
            ["type"]=>
            string(2) "NS"
          }
          [8]=>
          array(6) {
            ["nsdname"]=>
            string(22) "ns.nameserver4.com"
            ["class"]=>
            string(2) "IN"
            ["name"]=>
            string(22) "sometest.com."
            ["ttl"]=>
            string(5) "86400"
            ["Line"]=>
            int(14)
            ["type"]=>
            string(2) "NS"
          }
          [9]=>
          array(4) {
            ["type"]=>
            string(4) ":RAW"
            ["ttl"]=>
            string(5) "86400"
            ["Line"]=>
            int(15)
            ["raw"]=>
            string(0) ""
          }
          [10]=>
          array(4) {
            ["type"]=>
            string(4) ":RAW"
            ["ttl"]=>
            string(5) "86400"
            ["Line"]=>
            int(16)
            ["raw"]=>
            string(0) ""
          }
          [11]=>
          array(6) {
            ["class"]=>
            string(2) "IN"
            ["ttl"]=>
            string(5) "14400"
            ["Line"]=>
            int(17)
            ["name"]=>
            string(22) "sometest.com."
            ["type"]=>
            string(1) "A"
            ["address"]=>
            string(14) "197.1.1.1"
          }
          [12]=>
          array(4) {
            ["ttl"]=>
            string(5) "86400"
            ["Line"]=>
            int(18)
            ["raw"]=>
            string(0) ""
            ["type"]=>
            string(4) ":RAW"
          }
          [13]=>
          array(7) {
            ["exchange"]=>
            string(21) "sometest.com"
            ["class"]=>
            string(2) "IN"
            ["ttl"]=>
            string(5) "14400"
            ["Line"]=>
            int(19)
            ["name"]=>
            string(22) "sometest.com."
            ["type"]=>
            string(2) "MX"
            ["preference"]=>
            string(1) "0"
          }
          [14]=>
          array(4) {
            ["ttl"]=>
            string(5) "86400"
            ["Line"]=>
            int(20)
            ["raw"]=>
            string(0) ""
            ["type"]=>
            string(4) ":RAW"
          }
          [15]=>
          array(6) {
            ["class"]=>
            string(2) "IN"
            ["ttl"]=>
            string(5) "14400"
            ["Line"]=>
            int(21)
            ["cname"]=>
            string(21) "sometest.com"
            ["name"]=>
            string(27) "mail.sometest.com."
            ["type"]=>
            string(5) "CNAME"
          }
          [16]=>
          array(6) {
            ["name"]=>
            string(26) "www.sometest.com."
            ["cname"]=>
            string(21) "sometest.com"
            ["ttl"]=>
            string(5) "14400"
            ["Line"]=>
            int(22)
            ["class"]=>
            string(2) "IN"
            ["type"]=>
            string(5) "CNAME"
          }
          [17]=>
          array(6) {
            ["cname"]=>
            string(21) "sometest.com"
            ["name"]=>
            string(26) "ftp.sometest.com."
            ["ttl"]=>
            string(5) "14400"
            ["Line"]=>
            int(23)
            ["class"]=>
            string(2) "IN"
            ["type"]=>
            string(5) "CNAME"
          }
        }
      }
    }
  }
}


I need to print the data array(1) into a table, but all variations I tried have failed.

eg.

Code:
{section name=dnsrecords loop=$getDNSrecords}
    <tr>
        <td>{$getDNSrecords[dnsrecords].data}</td>
            {section name=zone loop=$getDNSrecords[dnsrecords].zone}
        <td>{$getDNSrecords[dnsrecords].data[zone].record}</td>
            {/section}
    </tr>
{/section}


And help would be much appreciated.
Back to top
View user's profile Send private message
bsmither
Smarty Pro


Joined: 20 Dec 2011
Posts: 175
Location: West Coast

PostPosted: Wed Aug 30, 2017 2:13 am    Post subject: Reply with quote

Let's start by identifying exactly what you want to start with, and what you want to loop through.

I assume you are wanting to start looping through $getDNSrecords.data.zone as there are no other sibling elements to the zone element. Within zone, however, there is an array with index 0. This purports to be the first of many zones. So, let's loop through the zone array even though at this time there is only one.
Code:
{$zones=$getDNSrecords.data.zone}
{* Not strictly necessary, but let's use a variable name indicative of its contents. *}
Then, we will loop through $zones.
Code:
{foreach $zones as $aZone}
We need to assume that each $aZone has only one named element in its array, and that named element is 'record'. But, within each 'record', there exists an array with an unknown number of elements. Your example gives 18 elements.
Code:
{foreach $aZone.record as $aZoneRecord}
Within each $aZoneRecord, there is an associative array of inconsistent elements. That is, the first $aZoneRecord has an array with 'raw', 'ttl', 'Line', and 'type' as the keys to these elements. However, the fourth $aZoneRecord has different stuff in it. So printing the array by keyname will not be possible. We must simply print each element regardless of the key and the value.
Code:
{foreach $aZoneRecord as $aZoneRecordKey => $aZoneRecordVal}
    <tr>
        <td>{$aZoneRecordKey}</td>
        <td>{$aZoneRecordVal}</td>
    </tr>
{/foreach}
{/foreach}
{/foreach}
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Smarty Development All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP