2014-12-02 148 views
1

需要幫助 有5個服務器從陣列中創建一個表

$server1 = 「1234」; 
$server2 = 「」; 
$server3 = 「5463」; 
$server4 = 「」; 
$server5 = 「1827」; 


$server = array($server1, $server2, $server3, $server4, $server5); 

然後捲曲初始化

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
curl_setopt($ch, CURLOPT_URL, "https://api.domain.com/$server1"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
$result = curl_exec($ch); 

$json = json_decode($result, true); 
$ip = $json[droplet][networks][v4][0][ip_address]; 
$status = $json[droplet][status]; 
$currentapp = $json[droplet][image][name]; 
$hostname = $json[droplet][name]; 
$memory = $json[droplet][memory]; 
$cpu = $json[droplet][vcpus]; 
$disk = $json[droplet][disk]; 

,然後將結果以呼應表格爲:

<tr>      
<td style="text-align:center"><?php echo $ip; ?></td> 
<td style="text-align:center"><?php echo $status; ?></td> 
<td style="text-align:center"><?php echo $currentapp; ?></td> 
<td style="text-align:center"><?php echo $hostname; ?></td> 
<td style="text-align:center"><?php echo $memory; ?></td> 
<td style="text-align:center"><?php echo $cpu; ?></td> 
<td style="text-align:center"><?php echo $disk; ?></td> 
</tr> 

現在請在這裏幫我 如何在不同的行中顯示不同服務器的數據。 如果服務器2和4是空的,那麼只有服務器1 3和5應該顯示在3行中。

+0

發表您的嘗試。 – RxV 2014-12-02 04:43:32

回答

0

的一種方式?然後用戶foreach進行迭代。

$serverArray = array(); 
$serverArray[] = '1234'; 
$serverArray[] = ''; 
$serverArray[] = '5463'; 
$serverArray[] = ''; 
$serverArray[] = '1827'; 

foreach($serverArray as $serverValue) 
{ 
    if($serverValue <> '') 
    { 
     $serverURL = 'https://api.domain.com/' .$serverValue; 

     // cURL code here 

     // Your table display code here 
    } 
} 
0

首先將所有服務器值放入數組中。就像這樣:

$server1 = 「1234」; 
$server2 = 「」; 
$server3 = 「5463」; 
$server4 = 「」; 
$server5 = 「1827」; 

$serverArray = array(
    $server1, 
    $server2, 
    $server3, 
    $server4, 
    $server5, 
); 


$infoArray = NULL; // To store all the information 
$counter = 0; 
foreach($serverArray as $server){ 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
    curl_setopt($ch, CURLOPT_URL, "https://api.domain.com/$server1"); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    $result = curl_exec($ch); 

    $json = json_decode($result, true); 
    $ip = $json[droplet][networks][v4][0][ip_address]; 
    $status = $json[droplet][status]; 
    $currentapp = $json[droplet][image][name]; 
    $hostname = $json[droplet][name]; 
    $memory = $json[droplet][memory]; 
    $cpu = $json[droplet][vcpus]; 
    $disk = $json[droplet][disk]; 

    $infoArray[$counter]['ip'] = $ip; 
    $infoArray[$counter]['status'] = $status; 
    $infoArray[$counter]['currentapp'] = $currentapp; 
    $infoArray[$counter]['hostname'] = $hostname; 
    $infoArray[$counter]['memory'] = $memory; 
    $infoArray[$counter]['cpu'] = $cpu; 
    $infoArray[$counter]['disk'] = $disk; 
    $counter++; 
} 

對於視圖文件

<?php 
    $output = ''; 
    foreach($infoArray as $info){ 
    $output += "<tr>"; 
    $output += "<td style='text-align:center'>{$info['ip']}</td>"; 
    $output += "<td style='text-align:center'>{$info['status']}</td>"; 
    $output += "<td style='text-align:center'>{$info['currentapp']}</td>"; 
    $output += "<td style='text-align:center'>{$info['hostname']}</td>"; 
    $output += "<td style='text-align:center'>{$info['memory']}</td>"; 
    $output += "<td style='text-align:center'>{$info['cpu']}</td>"; 
    $output += "<td style='text-align:center'>{$info['disk']}</td>"; 
    $output += "</tr>"; 
    } 
    echo $output; 

實現它要考慮服務器作爲陣列的>

+0

結果即使在更改後也會變爲「0」 curl_setopt($ ch,CURLOPT_URL,「https://api.domain.com/$server1」); 至 curl_setopt($ ch,CURLOPT_URL,「https://api.domain.com/$server」); – Rishav 2014-12-02 05:24:21

0

Finanlly這是工作

<html> 

<?php 

$serverArray = array(); 
$serverArray[] = '2720973'; 
$serverArray[] = '277543'; 
$serverArray[] = '2720153'; 
$serverArray[] = ''; 
$serverArray[] = ''; 

foreach($serverArray as $serverValue) 
{ 
    if($serverValue <> '') 
    { 

// initialize curl 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
curl_setopt($ch, CURLOPT_URL, "https://api.domain.com/$serverValue"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
$result = curl_exec($ch); 

//decodes json and prints id 
$json = json_decode($result, true); 
$ip = $json[droplet][networks][v4][0][ip_address]; 
$status = $json[droplet][status]; 
$currentapp = $json[droplet][image][name]; 
$hostname = $json[droplet][name]; 
$memory = $json[droplet][memory]; 
$cpu = $json[droplet][vcpus]; 
$disk = $json[droplet][disk]; 
    ?> 

<table> 
<tr>     
<td style="text-align:center"><?php echo $ip; ?></td> 
<td style="text-align:center"><?php echo $status; ?></td> 
<td style="text-align:center"><?php echo $currentapp; ?></td> 
<td style="text-align:center"><?php echo $hostname; ?></td> 
<td style="text-align:center"><?php echo $memory; ?></td> 
<td style="text-align:center"><?php echo $cpu; ?></td> 
<td style="text-align:center"><?php echo $disk; ?></td> 


<?php } 
    } ?>