Foreach提供的参数无效。

时间:2012-02-23 13:18:09

标签: php flexigrid

您好我正在使用flexigrid,我有一个问题,我不知道为什么它继续说 Invalid argument supplied for foreach()Undefined variable: rows我查看了对我来说似乎正确的代码,我知道我可能忽略了但是你可以帮助我吗,谢谢

继承人我的代码。

<?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 个答案:

答案 0 :(得分:0)

您的$rows永远不会在下面的代码段之前定义或初始化(很可能是它的第一次使用)。在这种情况下,foreach预测变量循环的可能性极小。

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