2013-02-08 3 views
0

웹 사이트를위한 콘테스트 시스템을 구축했습니다. 사용자가 로그인하여 실제 생활 이벤트 (특정 개체 판매)에 따라 투표 용지를 제출 한 다음 그 월말에 무작위로 투표 용지가 선택되고 그 투표 용지의 소유자가 승자가됩니다.사용자 별 정보로 모든 사용자에게 전자 메일을 보내는 방법.

데이터베이스에있는 모든 사용자에게 현재 시스템에있는 투표 용지를 이메일로 보낼 스크립트를 만들어야합니다.

내 현재 로그인/등록 시스템은 HTML 폼 거스 Simple PHP Registration System의 많이 편집 된 버전입니다.

나는 내가하고 싶은 의사 코드를 알고있다.

단계별로 필요한 방법은 다음과 같습니다.

EmailUsersTotalEntries를 호출하고 데이터베이스의 모든 사용자로 배열을 채우고 배열 1의 첫 번째 항목을 선택하고 사용자 1과 함께 itemsold 열의 모든 행의 합계를 찾습니다. 하나는 userid = 1 인 투표에서 select sum (itemsold)의 결과가 포함 된 전자 메일입니다. 그런 다음 사용자 2에게 돌아가서 데이터베이스의 모든 사용자에게 전자 메일을 보낼 때까지 동일한 작업을 수행합니다.

다음은 내가 작성했거나이를 수행하는 데 사용되는 로그인 시스템의 메소드 중 일부입니다. 내 유일한 문제는 사용자 1에서 시작하여 사용자 2에게 계속 진행할 수 있도록 루프를 만드는 방법을 모르는 것입니다. 그리고 데이터베이스/루프를 사용하는 사용자의 user_id에 대해 데이터베이스를 쿼리하는 방법을 모릅니다. 님이 현재 운영 중입니다.

방법은 다음과 같습니다. 이것은 기본 방법이며, 하위 방법을 호출하여 사용자를 수집 한 다음 실제 이메일을 보냅니다. TotalEntries이 여기

function GetUsers(&$user_rec) 
{ 
    if(!$this->DBLogin()) 
    { 
     $this->HandleError("Database login failed!"); 
     return false; 
    } 

    $result = mysql_query("Select * from $this->tablename",$this->connection); 

    $user_rec = mysql_fetch_assoc($result); 

    return true; 
} 

내가 사용자에 대한 TotalEntries를 얻기 위해 쓴 방법으로 사용자를 수집하는 방법이다

function EmailTotalEntries() 
{ 
    if(empty($_POST['email'])) 
    { 
     $this->HandleError("Email is empty!"); 
     return false; 
    } 
    $user_rec = array(); 
    if(false === $this->GetUsers($user_rec)) 
    { 
     return false; 
    } 
    **/* $TotalEntries = array(); */** 
    if(false === $this->GetTotalEntriesForEmail($user_rec, $TotalEntries) 
    { 
     return false; 
    } 

    //At this point, I have an array, user_rec, populated with all the data from my users table, and an array $TotalEntries that will have nothing since its trying to pull from user_rec, which usually is one user but right now is all of the users. 
    //This is where I know I should have already started the loop, so chosen the first element in user_rec, and applied the GetTotalEntriesForEmail method, then the SendUserEmail method, then gone to the top of the loop and gone to the second user_rec element and repeat. 

    if(false === $this->SendUsersEmail($user_rec, $TotalEntries)) 
    { 
     return false; 
    } 
    return true; 
} 

배열이 아닌지하는 경우는 잘 모르겠어요 이 여기에

function GetTotalEntries() 
{ 
    if(!$this->CheckLogin()) 
    { 
     $this->HandleError("Not logged in!"); 
     return false; 
    } 

    $user_rec = array(); 
    if(!$this->GetUserFromEmail($this->UserEmail(),$user_rec)) 
    { 
     return false; 
    } 
    $qry = "SELECT SUM(itemsold) AS TotalEntries FROM entries WHERE user_id = '".$user_rec['id_user']."'"; 
    $result = mysql_query($qry,$this->connection); 
    while($row = mysql_fetch_array($result)) 
         {       
    echo $row['TotalEntries']; 
         } 



} 

을 (그가 얼마나 많은 항목을보고 자신의 컨트롤 패널을 확인)에 기록되어 것은 내가이에 적용 할 필요가 생각하는 방법이다 이메일에서 일해.

function GetTotalEntriesForEmail($user_rec, &$TotalEntries) 
{ 
    if(!$this->DBLogin()) 
    { 
     $this->HandleError("Database login failed!"); 
     return false; 
    } 


    $qry = "SELECT SUM(itemsold) FROM entries WHERE user_id = '".$user_rec['id_user']."'"; //$user_rec['id_user'] should the be id of the user the loop is currently on. 
    $result = mysql_query($qry,$this->connection); 
    $TotalEntries = mysql_fetch_assoc($result); 
      return true; 

} 

Heres는 실제 이메일

function SendUsers($user_rec, $TotalBallots) 
{ 
    $email = $user_rec['email']; //should be the for the user the loop is currently on. 

    $mailer = new PHPMailer(); 

    $mailer->CharSet = 'utf-8'; 

    $mailer->AddAddress($email,$user_rec['name']); //user the loop is currently on. 

    $mailer->Subject = "Total Ballots to Date"; 

    $mailer->From = $this->GetFromAddress(); 

    $mailer->Body ="Hello ".$user_rec['name']."\r\n\r\n". //Same thing 
    "To date you have: "/* .$TotalBallots. */" ballots.\r\n" //Same thing 

    if(!$mailer->Send()) 
    { 
     return false; 
    } 
    return true; 
}  

나는 PHP에서 아주 좋은 아니에요,이 모든 것은 나를 위해 학습 경험, 그래서 도움이 크게 감사합니다. 나는 내가 충분히 명확 havent 한 경우 어쩌면 그래서 heres는 내가하고 싶은 것, 명확한 것이 다른 언어의 예를 제공하지만, 자바

for(x = 0; x <= user_rec.length; x++) 
{ 
int ballots = getTotalEntriesForUser(x); 
sendEmailToUser(ballots) 
} 

에, 명확 havent 한 경우

알려 주시기 바랍니다 가능한 한 최선을 다해 명확히하려고 노력할 것입니다.

위의 코드를 모든 사용자에게 보내는 전자 메일을 하나씩 보내는 루프와 어떻게 조합 할 수 있습니까?

답변

1

함수가 클래스의 일부입니까? 반드시 그렇게 할 필요는 없습니다. 원하는 경우 함수 또는 클래스로 바꿀 수있는 권장 사항은 다음과 같습니다. 또한 MySQLi를 살펴보고 MySQLi를 사용하고 사용하는 클래스를 활용하는 것이 좋습니다. 다시 말하지만, 모든 나의 권고.

테이블 구조를 알지 못하면서이 점을 짐작할뿐입니다.

$sql = mysql_query("SELECT u.*, 
          u.user_id AS user, 
          COALESCE(SUM(e.itemssold), 0) AS total_items 
        FROM users u 
        LEFT JOIN entries e ON e.user_id = u.user_id 
        GROUP BY u.user_id"); 

while($row = mysql_fetch_array($sql)) 
{ 
    $user = $row['user']; 
    $email = $row['user_email']; 
    $items = $row['total_items']; 

    yourEmailFunction($email, $items); 
} 

사용자 테이블에서 정보를 가져오고 일치하는 사용자 ID를 기반으로 항목 테이블을 가져옵니다. 사용자 테이블에서 사용자 ID를 사용자로 설정하므로 나중에이 두 ID를 구별하지 않아도됩니다. COALESCE 기능에 대해 알아 보려면 read here. while() 함수는 SQL 문에서 가져 오는 모든 사용자를 반복합니다.

이것은 어떤 방식 으로든 테스트되지 않았지만 기본적으로 필요한 것입니다. 사용자의 전자 메일과 전체 항목을 전달하고 전자 메일 기능을 작성하여 해당 정보를 해당 전자 메일 주소로 보냅니다.

그러나 함수가 제대로 작동하고 Java에서 제공 한 것과 같이 for 루프를 사용하려는 경우 다음은 PHP로 작성하는 방법입니다.

for($x = 0; $x <= count($user_rec); $x++) 
{ 
    $ballots = getTotalEntriesForUser($x); 
    sendEmailToUser($ballots); 
} 
+0

u 및 e는 무엇입니까? 내 데이터베이스가 설정되는 방식, 2 개의 테이블, 사용자 및 항목으로 이루어진 컨테스트 (contest.users 및 contest.entries) –

+0

내 사용자 데이터베이스에서 필요한 열은 id_user 및 전자 메일이며 항목 테이블에서 user_id (사용자의 id_user와 일치해야 함) 및 itemsold –

+0

오! 이해 했어. 쿼리가 작동하고 정확히 그것이 무엇을 볼,이 일할 수있는, 감사합니다! –

관련 문제