Page 1 of 1

Array access/comparison problem...

Posted: Fri Jul 13, 2007 3:26 pm
by nyghtrunner
Hey all,

I have what I am hoping is a pretty easy question. I'm kind of a newb at PHP, but have been coding in general for a short while now... Well, that's neither here nor there.

Anyways, I'm witing a php file that I need to have do 2 things (at least at this point). I have either of them working, but not the integration for them. Basically, I am writing a page to query a database, and return the "approved" valuse in a specific column of a table. I am able to have it do this, and spit out the correct values. I also need to have it loop through a directory on the server to get the paths and names of image files. I am able to get this to work as well. The problem really comes in when I try to compare the 2.

Here's the code I'm using:

Code: Select all

<?php

//header("Content-type: text/xml");

$dir = $_SERVER['DOCUMENT_ROOT']."/". "images/";
$handle=opendir($dir); 

$counter = 0;

$link= @mysql_connect('localhost', 'root', 'password');
if(!$link) {
	echo "<?xml version=\"1.0\"?>\n";
	echo "<quotes>\n";
	echo "<item>".mysql_error()."</item>\n";
	echo "</quotes>\n";
	exit();
}

mysql_select_db('tableName',$link);
if (!@mysql_select_db('tableName')) {
	echo "<?xml version=\"1.0\"?>\n";
	echo "<quotes>\n";
	echo "<item>".mysql_error()."</item>\n";
	echo "</quotes>\n";
	exit();
}

$query = "SELECT * FROM art WHERE approve = 1 ORDER BY id DESC";
$results = mysql_query($query);

while ($file = readdir($handle)) 
{
//check file type for images
$the_type = strrchr($file, ".");
$is_picture = eregi("jpg|gif|bmp|png",$the_type); //adjust for other image types

if ($file != "." and $file != ".." and $is_picture) 
{ 
$mypics[$counter] = $_SERVER['DOCUMENT_ROOT']."/". "images/fan/art/".$file;
$size[$counter] = getimagesize($mypics[$counter]);
$counter++;
} 
}
closedir($handle);

while($line = mysql_fetch_assoc($results)) {
print $line["img"]; //I need to compare this with the $mypics array in the for loop, see if they are equal, and write the info to my XML doc.
}


print "<?xml version='1.0' encoding='UTF-8'?>\n";

print "<data>\n";

for ($i=0; $i < ($counter); $i++) 
{ 
print "<node>\n";
print "<image_name>".$mypics[$i]."</image_name>\n"; //This is the array i need to compare.
print "<image_height>".$size[$i][0]."</image_height>\n";
print "<image_width>".$size[$i][1]."</image_width>\n";
print "</node>\n";
}
}

print "</data>\n";

?>
So i'm up a stump, because I'm not sure what syntax I can use to combine the 2 loops... anyone got any ideas? Does this even make sense?

Stpehen

Posted: Sat Jul 14, 2007 2:18 pm
by s.dot
While looping through, add the data you need to compare into containers (arrays).

Code: Select all

while(loop1)
{
   $array1[] = $value;
}

while(loop2)
{
   $array2[] = $value;
}

//compare arrays

Posted: Mon Jul 16, 2007 10:40 am
by nyghtrunner
Thanks for the reply. Perhaps you are not understanding my question...

I am already passing everything into arrays, or at least, from my understanding of PHP structure, I am...

The arrays are created and populated here:

Code: Select all

$mypics[$counter] = $_SERVER['DOCUMENT_ROOT']."/". "images/".$file; //this resides within a conditional, within a while loop.
and here:

Code: Select all

while($line = mysql_fetch_assoc($results)) {
$line["img"]; 
}
So the arrays should be there. Not sure if you know actionscript, but here is kind of what I am looking to do in terms of comparison, using the syntax of PHP, and the structure of AS2, yet it is not working at all...

Code: Select all

while($line = mysql_fetch_assoc($results)) {
	$compare1[] = $line["img"];
	for ($i=0; $i < ($counter); $i++) {
		$tempHouse = $compare1[$i];
		for ($j=0;$j<($counter);$j++) {
			if ($_SERVER['DOCUMENT_ROOT']."/". "images/fan/art/".$tempHouse = $mypics[$i]) {
				print "<node>\n";
				print "<image_name>".$mypics[$i]."</image_name>\n";
				print "<image_height>".$size[$i][0]."</image_height>\n";
				print "<image_width>".$size[$i][1]."</image_width>\n";
				print "</node>\n";
			}
		}
	}
}
Any ideas?