2012-02-23 4 views
0

안녕하세요 저는 flexigrid를 사용하고 있는데 문제가 있습니다. 이유는 모르겠지만 계속해서 말하고 있습니다. Invalid argument supplied for foreach()Undefined variable: rows 코드가 옳은 것 같습니다. 간과 할 수도 있지만 도움이 될 수 있습니다. , 감사합니다Foreach 잘못된 인수가 제공되었습니다.

Heres 내 코드.

<?php 
$page = isset($_POST['page']) ? $_POST['page'] : 1; 
$rp = isset($_POST['rp']) ? $_POST['rp'] : 10; 
$sortname = isset($_POST['sortname']) ? $_POST['sortname'] : 'name'; 
$sortorder = isset($_POST['sortorder']) ? $_POST['sortorder'] : 'desc'; 
$query = isset($_POST['query']) ? $_POST['query'] : false; 
$qtype = isset($_POST['qtype']) ? $_POST['qtype'] : false; 


$usingSQL = true; 
function runSQL($rsql) { 

    $db['default']['hostname'] = "localhost"; 
    $db['default']['username'] = 'root'; 
    $db['default']['password'] = ""; 
    $db['default']['database'] = "testdb"; 

    $db['live']['hostname'] = 'localhost'; 
    $db['live']['username'] = 'root'; 
    $db['live']['password'] = ''; 
    $db['live']['database'] = 'testdb'; 

    $active_group = 'default'; 

    $base_url = "http://".$_SERVER['HTTP_HOST']; 
    $base_url .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']); 

    $connect = mysql_connect($db[$active_group]['hostname'],$db[$active_group]['username'],$db[$active_group]['password']) or die ("Error: could not connect to database"); 
    $db = mysql_select_db($db[$active_group]['database']); 

    $result = mysql_query($rsql) or die ($rsql); 
    return $result; 
    mysql_close($connect); 
} 

function countRec($fname,$tname) { 
     $sql = "SELECT count($fname) FROM $tname "; 
    $result = runSQL($sql); 
    while ($row = mysql_fetch_array($result)) { 
      return $row[0]; 
    } 
} 

$sort = "ORDER BY $sortname $sortorder"; 
$start = (($page-1) * $rp); 

$limit = "LIMIT $start, $rp"; 

$where = ""; 
if ($query) $where = " WHERE $qtype LIKE '%".mysql_real_escape_string($query)."%' "; 

$sql = "SELECT * FROM city $where $sort $limit"; 
$result = runSQL($sql); 

$total = countRec("ID","city $where"); 

if(!isset($usingSQL)){ 
    include dirname(__FILE__).'/countryArray.inc.php'; 
    if($qtype && $query){ 
      $query = strtolower(trim($query)); 
      foreach($rows AS $key => $row){ 
        if(strpos(strtolower($row[$qtype]),$query) === false){ 
          unset($rows[$key]); 
        } 
      } 
    } 
    //Make PHP handle the sorting 
    $sortArray = array(); 
    foreach($rows AS $key => $row){ 
      $sortArray[$key] = $row[$sortname]; 
    } 
    $sortMethod = SORT_ASC; 
    if($sortorder == 'desc'){ 
      $sortMethod = SORT_DESC; 
    } 
    array_multisort($sortArray, $sortMethod, $rows); 
    $total = count($rows); 
    $rows = array_slice($rows,($page-1)*$rp,$rp); 
} 
header("Content-type: application/json"); 
$jsonData = array('page'=>$page,'total'=>$total,'rows'=>array()); 
foreach($rows AS $row){ **//THIS IS MY LINE 82** 
    //If cell's elements have named keys, they must match column names 
    //Only cell's with named keys and matching columns are order independent. 
    $entry = array('id'=>$row['iso'], 
      'cell'=>array(
        'ID'=>$row['ID'], 
        'Name'=>$row['Name'], 
        'CountryCode'=>$row['CountryCode'], 
        'District'=>$row['District'], 
        'Population'=>$row['Population'] 

      ), 
    ); 
    $jsonData['rows'][] = $entry; 
} 
echo json_encode($jsonData); 

미리 감사드립니다.

+1

반환 후 아무 코드도 실행되지 않습니다 (mysql_close()가 전혀 실행되지 않음). 또한 while 루프에서 리턴 할 포인트가 없거나 레코드 세트에 결과가 1 개 밖에 없을 때 while 루프를 사용하십시오. $ rows 변수가 실제로 정의되지 않았습니다. –

답변

0

귀하의 $rows은 절대로 아래의 스 니펫 (처음 사용되었을 가능성이 높음) 전에 정의되거나 초기화되지 않습니다. 그러한 경우에는 foreach이 변수가 반복 될 것으로 예상 할 확률이 낮습니다.

$query = strtolower(trim($query)); 
     foreach($rows AS $key => $row){ 
       if(strpos(strtolower($row[$qtype]),$query) === false){ 
         unset($rows[$key]); 
       } 
     } 
관련 문제