2012-02-23 21 views
-1

好吧,所以我有一個腳本,從不同的客戶端的PHP頁面生成PDF報告。目前,這一次只能處理一個客戶端,但我想添加一個while語句,以便在從下拉菜單中選擇「所有」客戶端時能夠應對。雖然循環選擇所有聯繫人

while應該從mydatabase.mytable中選擇*個聯繫人,並通過每個聯繫人的正常代碼運行。這是我不知道該怎麼做的一點。

我知道它是這樣的,但不能完全記住語法...

<?php 
$client_id=$_POST["client_id"]; 
$date_start=$_POST["date_start"]; 
$date_end=$_POST["date_end"]; 

if ($client_id == 'ALL') { 

} 
else 
{ 
    $command="php myfile.php $client_id $date_start $date_end > myfile.html"; 
    exec($command, $output, $status); 
    if ($status!=0) {print_r($output); die("wget failed with status $status"); } 

    $command="wkhtmltopdf-i386 --margin-left 5mm --margin-right 5mm myfile.html myfile.pdf"; 
    exec($command, $output, $status); 
    if ($status!=0) die("htmltopdf failed"); 
} 
?> 

任何建議,將不勝感激

非常感謝

+1

請粘貼您爲1位客戶使用的代碼。 – 2012-02-23 01:05:19

+0

已更新,道歉 – 2012-02-23 01:15:13

回答

0

簡單if控制結構就足夠了。

if ('All' == $client_id) { 
    // e.g. - generateReport(-1) 
    // Your function would look for -1 so it knows to generate for all 
} 
else { 
    // e.g. - generateReport($client_id); 
} 
0

如果選擇從您的數據庫中的所有聯繫人,那麼你應該能夠找回某種類型的數組,或許是這樣的:

array(
    ['John Smith'] => array('company' => 'xyz'), 
    ['Anna Citizen'] => array('company' => 'abc), 
) 

產生上述陣列,可以檢查「ALL」聯繫人:

if ($_POST['dropdown'] == "ALL"){ 
    //generate for ALL 
}else{ 
    //generate for 1 contact 
} 

然後你可以使用foreach遍歷數組的迭代的所有聯繫人:

foreach ($contacts as $contact => $attributes){ 
    var_dump($contact); //John Smith 
    var_dump($attributes); // array('company' => 'xyz); 
    var_dump($attributes['company']); //xyz 

    //Do whatever to generate reports. 
}