Jump to content

Adding correct coding in arrays


heeha

Recommended Posts

Hey, I am new to programming including html or any kind. 

 

I was trying to get dns info via php 

 

Lets say we have a website example.com, and we output the info for it as 

 

$site= (dns_get_record( "example.com", DNS_ALL); 

 

print_r( $site); 

 

which output as 

Array ( [0] => Array ( [host] => example.com [class] => IN [ttl] => 3500 [type] => A [ip] => xxx.xxx.xx.xxx ) [1] => Array ( [host] => example.com [class] => IN [ttl] => 172002 [type] => NS [target] => ns1.example.com ) [2] => Array ( [host] => example.com [class] => IN [ttl] => 17002 [type] => NS [target] => ns2.example.com ) and so on )

 

but I dont want this info to be output like this.

 

I want it to appear as 

 

echo :The name of host is" . $anyvariable['host']. "."; 

 

and which output it as 

 

The name of host is example.com.  and same for the others values. or appear something like this on webpage not in array forms. 

Link to comment
Share on other sites

Thanks, I get it a little bit. 

 

But the problem now I face is that, there are 5 array, there it shows the same out as 5 fives instead of showing just once. 

 

foreach( $site as $anyvariable) {

echo "the name of the host is" .$anyvariable['host']. "." ;

echo "<br>";

}

 

and the output comes out as 

 

the name of the host is example.com 

the name of the host is example.com 

upto 5 times. 

 

How do i restricted it the the limit to 1 or the  output the "echo" only the number of times i want to output it. 

Link to comment
Share on other sites

It is showing that line 5 times because  dns_get_record( "example.com", DNS_ALL);  returns a multidimensional array of each type of dns record for that domain.

 

In your case $site is a multidimensional array, which contains 5 sub-arrays. In each of these sub-arrays there is a "host" key. Your foreach loop is looping through each of these sub-arrays and outputting the "host" key. This is why line "the name of the host is example.com" is outputted five times. If you only want to host to be outputted once,  you need add logic to your foreach loop example

$putput_host = false;
foreach( $site as $anyvariable)
{
    // check if the host has been outputted
    if(!$output_host)
    {
        echo "the name of the host is" .$anyvariable['host']. "." ;
        echo "<br>";
        // set $output_host to true, we have outputted the host
        $putput_host = true;
    }
}

If you are doing is outputting the host, then have dns_get_record return only one dns record, such as DNA_A

$site = dns_get_record( "example.com", DNS_A);
echo $site['host'] . ' ip address is ' . $site['ip'] . '<br />';
  • Like 1
Link to comment
Share on other sites

 

It is showing that line 5 times because  dns_get_record( "example.com", DNS_ALL);  returns a multidimensional array of each type of dns record for that domain.

 

In your case $site is a multidimensional array, which contains 5 sub-arrays. In each of these sub-arrays there is a "host" key. Your foreach loop is looping through each of these sub-arrays and outputting the "host" key. This is why line "the name of the host is example.com" is outputted five times. If you only want to host to be outputted once,  you need add logic to your foreach loop example

$putput_host = false;
foreach( $site as $anyvariable)
{
    // check if the host has been outputted
    if(!$output_host)
    {
        echo "the name of the host is" .$anyvariable['host']. "." ;
        echo "<br>";
        // set $output_host to true, we have outputted the host
        $putput_host = true;
    }
}

If you are doing is outputting the host, then have dns_get_record return only one dns record, such as DNA_A

$site = dns_get_record( "example.com", DNS_A);
echo $site['host'] . ' ip address is ' . $site['ip'] . '<br />';

 

Thanks, I get it completely but I guess foreach loop is still needed to output the variable. 

 

$site= dns_get_record( "example.com", DNS_A); 

echo $site['host'] . 'ip address is ' .$site['ip']. '<br />'; 

 

doesnt output any values but adding foreach loops does as it's still inside the array. 

 

$site= dns_get_record( "example.com", DNS_A); 

 

foreach( $site as $anyvariable) {

echo $anyvariable['host'] . 'ip address is ' .$anyvariable['ip']. '<br />';

}

 

Tried several times after trying foreach loop, helped me get the values. 

Link to comment
Share on other sites

Untested but s/b pretty close.


$site= (dns_get_record( "example.com", DNS_ALL);
ShowArray($site);
//***********************
function ShowArray($arg)
{
foreach ($arg as $idx=>$val)
{
if (is_array($var))
{
echo "<br>Array is<br>";
ShowArray($var);
}
else
echo "$idx is $val<br>";
}
}

Link to comment
Share on other sites

 

It is showing that line 5 times because  dns_get_record( "example.com", DNS_ALL);  returns a multidimensional array of each type of dns record for that domain.

 

In your case $site is a multidimensional array, which contains 5 sub-arrays. In each of these sub-arrays there is a "host" key. Your foreach loop is looping through each of these sub-arrays and outputting the "host" key. This is why line "the name of the host is example.com" is outputted five times. If you only want to host to be outputted once,  you need add logic to your foreach loop example

$putput_host = false;
foreach( $site as $anyvariable)
{
    // check if the host has been outputted
    if(!$output_host)
    {
        echo "the name of the host is" .$anyvariable['host']. "." ;
        echo "<br>";
        // set $output_host to true, we have outputted the host
        $putput_host = true;
    }
}

If you are doing is outputting the host, then have dns_get_record return only one dns record, such as DNA_A

$site = dns_get_record( "example.com", DNS_A);
echo $site['host'] . ' ip address is ' . $site['ip'] . '<br />';

Code 1.  Doesnt work. I have tried. 

 

regarding code 2. It does work but the problem i face now is that 

$site = dns_get_record( "example.com", DNS_NS);
print_r($site);
//outputs : Array ( [0] => Array ( [host] => example.com [class] => IN [ttl] => 5 [type] => NS [target] => my-servers.net ) [1] => Array ( [host] => example.com [class] => IN [ttl] => 5 [type] => NS [target] => your-servers.net ) [1] => Array ( [host] => example.com [class] => IN [ttl] => 5 [type] => NS [target] => this-servers.net ) )


foreach($site as $anyvariable) {

echo $site['host'] . ' ip address is ' . $site['ip'] . '<br />';
echo $site['host'] . ' ip address is ' . $site['ip'] . '<br />';

 //outputs the first value "ok" but doesnt out the rest of the name servers different ans output the value same for both. 

How do i output in way that each gets displayed with all the values for ns1 and ns2 and so on? Each target value is different. 

@ginerjm : tried code as is, doesnt output anything. 

Link to comment
Share on other sites

Update, finally solved. I guess i saw it somewhere something wrong. It works 

 

lets say there are 4 name server for a site and 2 for another. 

 

how do i display using if or elseif statement that if only website has two NS it will only display 2 and if a site has 4 server it display 4only. As i m using table to display the data, I place 4 table rows so when a website has for ns it works fine but when other website has 2 NS, the remaining two (table data) doesnt work properly. 

 

Thanks. 

Link to comment
Share on other sites

Update : Solved. It automatically parses each information and shows all the dns correctly. 

Works only with the following code, and rest of the code told by many other didn't get any oputput at all. DNS_ALL does work but it has problem in output due as too much complication in handling multidimensional arrays. Foreach loop is also needed. 

$site = dns_get_record( "example.com", DNS_A);
echo $site['host'] . ' ip address is ' . $site['ip'] . '<br />';
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.