2012-02-23 2 views
1

이것은 내가 달성하고자하는 것입니다. 드롭 다운 메뉴에서 모두를 선택한 경우 데이터베이스에서 모든 클라이언트를 선택합니다. 각 클라이언트에 대해 스크립트를 실행하여 html 페이지와 pdf를 만듭니다. 이것은 내가해야하는 곳이지만 각 클라이언트에 대해 html 페이지와 pdf를 생성 할 수는 없습니다.각 루프의 PHP

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

if ($client_id == 'ALL') 
{ 
    $con = mysql_connect("localhost","user","password"); 
    if (!$con) 
    { 
    die('Could not connect: ' . mysql_error()); 
    } 

    mysql_select_db("mydatabase", $con); 

    $query = "select client_id from ca_client_account"; 

    $result = mysql_query($query) or die(mysql_error()); 

    while($row = mysql_fetch_array($result)) 
    { 
    $command="php $result.php $result $date_start $date_end > $result.html"; 
    exec($command, $output, $status); 
    echo $command; 
    if ($status!=0) {print_r($output); die("wget failed with status $status"); } 

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

} 
else 
{ 
    $command="php $client.php $client_id $date_start $date_end > $client.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 $client.html $client.pdf"; 
    exec($command, $output, $status); 
    if ($status!=0) die("htmltopdf failed"); 
} 
?> 

단일 클라이언트의 경우 모든 것이 완벽합니다. 내가 모든 클라이언트 진술을 시도하고 생성 할 때 나는 그것을 작동시키지 못한다.

내가 뭘 잘못하고 있니?

많은 감사

+0

출력물은 무엇입니까? 오류가 있습니까? –

+0

$ 결과 변수가 각 client_id를 의도 한대로 출력하지 않습니다. 그것은 호기심이 많은 자원 ID를 출력합니다. –

+0

왜'exec' 파일을'include'하는 대신에 다른 파일처럼 실행합니까? 나에게 덜 번거로운 것처럼 보입니다. –

답변

2

$result 여기서 기대하는 바를 제공하지 않습니다. 에코하려는 정확한 필드를 지정해야합니다. 이 경우 $row['client_id']가 작동합니다.

+0

감사합니다. 거의 다 왔지만 정의되지 않은 오프셋이 있습니다 : 1. 나는 $ row [ 'client_id'] 대신에 $ row [1]을 사용했다. -이게 그 원인이 아니란 것일 테니. –

+0

@ TheManiac 맞아. 나는 그들이 정적 파일 인 myfile.php를 바꿀 의도는 아니었다. - 게시물을 일반화하기 위해 여기에 복사 할 때 생각없이 변경했다. –

+0

위대한 - 한 가지 더 :'$ row [ 'client_id' ]''$ row [1] '대신에 (''mysql_fetch_assoc'' ') (http://php.net/manual/en /function.mysql-fetch-assoc.php) –

2

데이터베이스 호출 작동 방식에 대한 근본적인 오해가 있습니다. $command에는 $result을 사용합니다. 이는 데이터베이스 쿼리 결과 핸들입니다. 그것은 쿼리의 값이 아니며, mysql _ *() 함수 이외의 다른 것은 사용할 수 없다. 표준 쿼리 시퀀스는 다음과 같습니다.

$sql = "SELECT ..."; 
$result = mysql_query($sql) or die(mysql_error()); 
$row = mysql_fetch_array($result); 

echo $row['some_field']; 

$ 결과에는 쿼리 결과를 나타내는 명령문 핸들이 포함됩니다. 이 명령문 핸들을 사용하여 데이터 행을 FETCH합니다. 가져온 데이터 중 $row 개 안에 검색어로 지정한 개별 데이터 필드가 있습니다. 외부 스크립트에 전달할 데이터 비트입니다.

관련 문제