2013-03-26 3 views
0

코드 많이 있습니다,하지만 대부분은 관련이없는, 그래서 난 그냥 코드 조각반환하지 않는 함수에서 업데이트해야하는 변수를 반환 하시겠습니까?

$error_message = ""; 

function died($error) // if something is incorect, send to given url with error msg 
{ 
    session_start(); 
    $_SESSION['error'] = $error; 
    header("Location: http://mydomain.com/post/error.php"); 
    die(); 
} 

이 잘 작동 오류에 오류를 표시하는 오류 세션, 멀리 사용자를 보냅니다을 게시 할 예정입니다. 은 "게시물"데이터베이스에 존재하는 경우 PHP

function fetch_post($url, $error_message) { 

    $sql  = "SELECT * FROM inserted_posts WHERE name = '$name'"; 
    $result = mysqli_query($con, $sql); 
    $num_rows = mysqli_num_rows($result); 

    if ($num_rows > 0) { 
     $error_message .= $url . " already exists in the database, not added"; 
     return $error_message; 
    } 
} 

이것은 또한 않는 경우,이 오류를 변수 $ ERROR_MESSAGE을 추가, 검사를 잘 작동

while ($current <= $to) { 

    $dom = file_get_html($start_url . $current); // page + page number 
    $posts = $dom->find('div[class=post] h2 a'); 

    $i = 0; 
    while ($i < 8) { 

     if (!empty($posts[$i])) { // check if it found anything in the link 

      $post_now = 'http://www.somedomain.org' . $posts[$i]->href; // add exstension and save it 

      fetch_post($post_now, &$error_message); // send it to the function 
     } 

     $i++; 
    } 

    $current++; // add one to current page number 
} 

이것은 메인 루프이고, 나는 가지고있는 몇몇 변수를 반복하고 exsternal 웹 사이트에서 포스트를 가져 와서 fetch_posts 함수에 URL과 error_message를 보낸다.

(나는 그것을 함께 보내고 나는 참고로 그것을한다. 오류 MSG가 포함되어있을 경우 asume이) ??? 글로벌

if (strlen($error_message > 0)) { 
    died($error_message); 
} 

을 유지하는 유일한 방법입니다 그리고 이것은 바로 루프 후 마지막 조각이다, 함수 오류에 오류 MSG를 전송하도록되어 chars하지만 어떤 문자도 감지하지 못합니까?

답변

4

당신이 원하는 :

strlen($error_message) > 0 

하지

또한
strlen($error_message > 0) 

, 패스에 의해 참조 5.3.0 이후 사용되지 않으며 5.4.0 이후 제거 된 시간 전화, 그래서보다는 전화 이 같은 함수 :

fetch_post($post_now, &$error_message); 

당신이처럼 정의 할 수 있습니다 :

function fetch_post($url, &$error_message) { 

    $sql  = "SELECT * FROM inserted_posts WHERE name = '$name'"; 
    $result = mysqli_query($con, $sql); 
    $num_rows = mysqli_num_rows($result); 

    if ($num_rows > 0) { 
     $error_message .= $url . " already exists in the database, not added"; 
     return $error_message; 
    } 
} 

당신은 루프 내에서 오류 메시지를 반환하고로이 작업을 수행하는 것이 더있을 것이지만 :

$error_messages = array(); 

// ... while loop 

if ($error = fetch_post($post_now)) 
{ 
    $error_messages[] = $error; 
} 

// ... end while 

if (!empty($error_messages)) { 
    died($error_messages); // change your function to work with an array 
} 
+0

나는 지체 .... 아주 아주 유감 –

+0

감사 위원장 편집 내 코드입니다 –

+0

문제 없어. 그것이 옳은 대답이라면 그것을 받아 들일 수있는 것처럼 표시 할 수 있습니다. – MichaelRushton

관련 문제