database file array problem!

[php]/* I am trying to check data from database with array from a file,
and if it doesn’t exist display something. Something major wrong,
unfortunately this is out of my depth and I just can’t get it working properly.
The way it is it shows matching and non matching data!
*/

$q1 = “SELECT * FROM table LIMIT 0,10”;
$r1 = mysql_query($q1) or die(mysql_error());

$rows = null;

while($a1 = mysql_fetch_array($r1, MYSQL_ASSOC)){

//The filename to check with comes from table col1 (- delimited), the key to check is also col1

$pre = explode(’-’,$a1[‘col1’]); // get filename prefix e.g $a1[‘col1’] = “1-abc”
$filename = $pre[0]; // $filename = 1

include($filename.".php"); // so this would be “1.php”

/* file includes arrays data; e.g
$data = Array(
“abc” => array(“foo”,“bar”),
“efg” => array(“foo2”,“bar”),
“hij” => array(“foo”,“bar2”),
);
key is always unique but values can be same*/

// check key for non-matching data from col1, e.g “abc” and echo if found,
foreach($data as $key => $value){
if($key != $pre[1]){
$rows .= $a1[‘col2’].":".$a1[‘col3’]."\n";
}
}

} // end while

echo nl2br($rows);[/php]

Since you have include() within the “while” loop, your array $data is re-defined on each iteration. So, 1st record from your query is compared with one $data array, second record compared with another $data array etc. Maybe the problem is here? In any case, try to print_r($data) for debugging, this will show you what is compared with what on each step.

Yep that was the problem with the include inside the while loop.

Thanks!!

Sponsor our Newsletter | Privacy Policy | Terms of Service