elkidogz Posted August 18, 2007 Share Posted August 18, 2007 <?php function comparearrays($array) { $con = mysql_connect("nope"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("nope", $con); $query2="SELECT fileName FROM pictures"; $result2 = mysql_query($query2); $result = array_diff($array, $result2); foreach ($result as $arrayElement)//rebuilds table notadded { $query="INSERT INTO tobeadded (tobeaddedfilename) VALUES ('$arrayElement')"; $doit = mysql_query($query); echo "File:".$arrayElement." was added to table tobeadded"; } echo "function comparearrays is complete."; } $files = scandir("allPICS",0); comparearrays($files); ?> basically i want to see that it gets into the foreach loop, which it isn't right now... i think it's messing up on the arrays (the bane of my existence is arrays) it runs it's part and completes. any advice? Quote Link to comment Share on other sites More sharing options...
marcus Posted August 18, 2007 Share Posted August 18, 2007 You need to set $array as an array. ($array = array() Quote Link to comment Share on other sites More sharing options...
elkidogz Posted August 18, 2007 Author Share Posted August 18, 2007 so it should be like this? <?php function comparearrays($array = array() { $con = mysql_connect("nope"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("nope", $con); $query2="SELECT fileName FROM pictures"; $result2 = mysql_query($query2); $result = array_diff($array, $result2); foreach ($result as $arrayElement)//rebuilds table notadded { $query="INSERT INTO tobeadded (tobeaddedfilename) VALUES ('$arrayElement')"; $doit = mysql_query($query); echo "File:".$arrayElement." was added to table tobeadded"; } echo "function comparearrays is complete."; } $files = scandir("allPICS",0); comparearrays($files); ?> basically i want to see that it gets into the foreach loop, which it isn't right now... i think it's messing up on the arrays (the bane of my existence is arrays) it runs it's part and completes. any advice? Quote Link to comment Share on other sites More sharing options...
marcus Posted August 18, 2007 Share Posted August 18, 2007 Try it Quote Link to comment Share on other sites More sharing options...
elkidogz Posted August 18, 2007 Author Share Posted August 18, 2007 no change in results... Quote Link to comment Share on other sites More sharing options...
elkidogz Posted August 18, 2007 Author Share Posted August 18, 2007 This function works fine. What i don't get is that the other one is basically doing the same thing, it has three arrays the first is provided, second is from a select, third is the difference of those two arrays... and then just insert it. <?php function insertarray($arrayName) { $con = mysql_connect("nope"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("nope", $con); $cleartable="TRUNCATE TABLE `notadded`"; //clears table notadded $doitnow=mysql_query($cleartable); foreach ($arrayName as $arrayElement)//rebuilds table notadded { if ($arrayElement != '.' && $arrayElement != '..')// don't want . and .. in array just file names { $query="INSERT INTO notadded (notinfilename) VALUES ('$arrayElement')"; $result = mysql_query($query); //echo "File:".$arrayElement." was added to the table notadded. <br>"; } } echo"function insertarray is complete.<br>"; }?> Quote Link to comment Share on other sites More sharing options...
marcus Posted August 18, 2007 Share Posted August 18, 2007 print_r($files); is that even an array Quote Link to comment Share on other sites More sharing options...
elkidogz Posted August 18, 2007 Author Share Posted August 18, 2007 yes, it's an array of all the files in a given folder, my last post had a function that inserts to a table off that, then i use that same list to compare again to a second table. <?php $files = scandir("allPICS",0);?> output [177] => Copy of DSCN1966.jpg [178] => Copy of DSCN1967.jpg [179] => Copy of DSCN1975.jpg [180] => Copy of DSCN1976.jpg [181] => Copy of DSCN3550.jpg [182] => Copy of DSCN3561.jpg [183] => DSCN0005.jpg [184] => DSCN0006.jpg [185] => DSCN0007.jpg [186] => DSCN0008.jpg [187] => DSCN0009.jpg [188] => DSCN0010.jpg [189] => DSCN0011.jpg [190] => DSCN0012.jpg [191] => DSCN0013.jpg [192] => DSCN0014.jpg [193] => DSCN0015.jpg [194] => DSCN0016.jpg [195] => DSCN0017.jpg [196] => DSCN0018.jpg [197] => DSCN0019.jpg [198] => DSCN0020.jpg [199] => DSCN0021.jpg [200] => DSCN0022.jpg [201] => DSCN0023.jpg [202] => DSCN0024.jpg [203] => DSCN0025.jpg [204] => DSCN0026.jpg [205] => DSCN0028.jpg [206] => DSCN0037.jpg [207] => DSCN0038.jpg [208] => DSCN0039.jpg [209] => DSCN0042.jpg [210] => DSCN0043.jpg [211] => DSCN0044.jpg [212] => DSCN0045.jpg [213] => DSCN0046.jpg [214] => DSCN0047.jpg Quote Link to comment Share on other sites More sharing options...
elkidogz Posted August 18, 2007 Author Share Posted August 18, 2007 when i do a print_r($result2); on the variable for my select it returns Resource id #4 so i must not be getting that right... hmm how to set it up Quote Link to comment Share on other sites More sharing options...
Masna Posted August 18, 2007 Share Posted August 18, 2007 This needs to be justified first: $result = array_diff($array, $result2); array_diff "Computes the difference of arrays." $array has no value, so you're matching something against nothing. The difference is everything. Why are you doing that? Quote Link to comment Share on other sites More sharing options...
elkidogz Posted August 18, 2007 Author Share Posted August 18, 2007 $array is what the function gets it's the folders directory <?php $files = scandir("allPICS",0); comparearrays($files); ?> this generates an array of file names (scroll up for an example of the values) inside the function its a variable called $array latest code revision <?php function comparearrays($array) { //$cleartables="TRUNCATE TABLE `tobeadded`"; //clears table notadded //$doitnow2=mysql_query($cleartables); //$query1="SELECT notinfilename FROM notadded"; //$result1 = mysql_query($query1); //$array=mysql_fetch_array($result1); print_r($array); $query2="SELECT fileName FROM pictures"; $result2 = mysql_query($query2); $rows = mysql_fetch_array($result2); print_r($rows); $result = array_diff($array, $rows); foreach ($result as $arrayElement)//rebuilds table notadded { $query="INSERT INTO tobeadded (tobeaddedfilename) VALUES ('$arrayElement')"; $doit = mysql_query($query); echo "File:".$arrayElement." was added to table tobeadded <br>"; } echo "function comparearrays is complete."; } $files = scandir("allPICS",0); print_r($files); //shows array $files (verify the array exists) comparearrays($files); //sends $files array to function comparearrays as $array mysql_close($con) ?> Quote Link to comment Share on other sites More sharing options...
elkidogz Posted August 18, 2007 Author Share Posted August 18, 2007 having it fetch the array is what made it work, the second array wasn't being built. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.