开发者

Variable out of scope when entering a while loop in php

开发者 https://www.devze.com 2023-01-16 03:33 出处:网络
I have a problem when trying to populate an array in php.It seems that once I enter a while loop with a mysql_fetch_assoc method I cannot populate my array.I\'ve included the code below.

I have a problem when trying to populate an array in php. It seems that once I enter a while loop with a mysql_fetch_assoc method I cannot populate my array. I've included the code below.

$params = $_REQUEST['params'];

$arr["status"]="ok";
$projects=array();
$files=array();
$titles=array();

$query = 'SELECT p.id as pid, f.fname as name, f.title FROM proj p INNER JOIN pic f ON f.projid=p.id WHERE p.catid=\'' . $params['category'] . '\' ORDER BY p.ordr, f.ordr';

require("../php/connect.php");

//select all projects from chosen category and pics from selected projects
$proj_result = mysql_query($query) or die ("Select failed");

//populate from rows
while($row = mysql_fetch_assoc($proj_result)){  
    $projects[]=$row["pid"];
    $开发者_开发知识库files[]=$row["name"];
    $titles[]=$row["title"];
}

$arr["projects"]=$projects;
$arr["files"]=$files;
$arr["titles"]=$titles;

echo json_encode($arr);

The result: {"status":"ok","projects":[],"files":[],"titles":[]}

Thank You.


A while loop doesn't create a new scope, as you can see here: http://codepad.org/H1U3wXZD
About the code itself, here's a few suggestions:

0) I would consider having a database abstraction layer (PDO would be good enough).

1) Learn how to use JOIN's. It looks like you could fetch all the necessary information with a single query, something like:

    SELECT p.id, p.proj, c.id, c.fname, c.title 
      FROM proj p 
INNER JOIN pic c ON c.projid=p.id 
     WHERE catid='<your category>' 
  ORDER BY p.ordr, c.ordr

2) You should separate the code that gets data from the db from the code that constructs the HTML (?). Perhaps you could put that in another method. Something like:

if ($cmd == 'catSelect') {
    $data = getData($params['category']);

    foreach ($data as $value) {
        // process data here
    }
}

3) I take it you are using the generated JSON to send it via AJAX to a client. In that case, I would totally cut the fat (eg: generated markup) and send only essentials (picture id, title, fname and whatever else is essential) and generate the code on the client side. This will make your page load faster and save you and your visitors bandwidth.


my jquery/ajax client side script was not sending in category properly and therefore was no selecting any rows.

The above code will work.


Within the loop try something like this :

while($row = mysql_fetch_assoc($proj_result)){  
    $projects[]=$row["pid"];
    $files[]=$row["name"];
    $titles[]=$row["title"];

    echo $row["pid"]." -- ".$row["name"]." -- ".$row["title"]."\n";
}

Do you get anything? Once you have tried it we will take it from there. My guess is that you aren't getting any data from MySQL.

0

精彩评论

暂无评论...
验证码 换一张
取 消