Page 1 of 1

list comparison

Posted: Mon Jul 24, 2006 9:27 pm
by SidewinderX
ok, well ive managed to build 2 little scripts, 1 that lists all the themes for a cms that are located in a certian directory, and another script that lists all the themes for a cms that have been added to a the database. however, what i want to do, is have a list that shows all the themes in the directory that have not been added to the database yet. i just watched a tv show and been pondering how to do this but i have yet to figure it out, could someone lend me a hand?


Lists all the themes in the themes folder:

Code: Select all

$dir = "../themes/";
if (is_dir($dir)) {
   if ($dh = opendir($dir)) {
       while (($file = readdir($dh)) !== false) {
			if(file_exists("../themes/$file/theme.php")){
           $themelist .= "$file ";
			}
       }
       closedir($dh);
   }
}
$themelist = explode(" ", $themelist);
sort($themelist);

for ($i=0; $i < sizeof($themelist); $i++) {
if($themelist[$i]!=""){

echo $themelist[$i] . "<br>";

}
}
Lists all the themes that have been added to the database:

Code: Select all

$q = "SELECT * FROM nuke_theme_preview";
$result = mysql_query($q) or die ('Something is wrong with query: ' . $q . '<br>'. mysql_error());
while ($row = mysql_fetch_assoc($result))
{
echo $row['t_title'] . "<br>";
}
What i want done is something that will list all the themes in the directory that are not in the database.

Thank You.

Posted: Mon Jul 24, 2006 9:34 pm
by feyd
take a look at the IN() construct in MySQL.

Posted: Mon Jul 24, 2006 10:35 pm
by SidewinderX
ok i looked at this http://dev.mysql.com/doc/refman/5.0/en/ ... tions.html

but im a litle confused, do i want to create a subquery with the $themelist array or something?

Posted: Tue Jul 25, 2006 1:17 pm
by SidewinderX
ive also attempted to do this using array_unique() seems i got a bit further, but still stuck....

Posted: Tue Jul 25, 2006 5:04 pm
by SidewinderX
well ive built this

Code: Select all

<?php
include("config.php");
// Create array here
$themelist = array("");

$dir = "../demo/themes/";
if (is_dir($dir)) {
   if ($dh = opendir($dir)) {
       while (($file = readdir($dh)) !== false) {
            if(file_exists("../demo/themes/$file/theme.php")){

                 array_push($themelist, $file); // Add value to array

           }
       }
       closedir($dh);
   }
}

$q = "SELECT * FROM nuke_theme_preview";
$result = mysql_query($q) or die ('Something is wrong with query: ' . $q . '<br>'. mysql_error());
while ($row = mysql_fetch_assoc($result))
{
     array_push($themelist, $row['t_title']); // Add value to array
}



print_r ($themelist);

?>
which outputs
Array ( [0] => [1] => 3D-Fantasy [2] => Anagram [3] => DeepBlue [4] => ExtraLite [5] => Kaput [6] => Karate [7] => Milo [8] => NukeNews [9] => Odyssey [10] => Sand_Journey [11] => Slash [12] => SlashOcean [13] => Sunset [14] => Traditional [15] => 3D-Fantasy [16] => Anagram [17] => DeepBlue [18] => ExtraLite [19] => Kaput )
but if you notice there are diplicate entries, and i dont either of the duplicate's. In other words i want the array to look like this:
Array ( [0] => [1] => Milo [2] => NukeNews [3] => Odyssey [4] => Sand_Journey [5] => Slash [6] => SlashOcean [7] => Sunset [8] => Traditional )
using array_unique () will out put this:
Array ( [0] => [1] => 3D-Fantasy [2] => Anagram [3] => DeepBlue [4] => ExtraLite [5] => Kaput [6] => Karate [7] => Milo [8] => NukeNews [9] => Odyssey [10] => Sand_Journey [11] => Slash [12] => SlashOcean [13] => Sunset [14] => Traditional )
which is close, but still not what i want.
at php.net the first user contributed note is a function that removes duplicate entries in regard to a specific element. i dont want to remove the duplicates with regard to a specific element, i just want to remove all duplicates period.

Maybe i could get a little more help this time.
Thank You.

Posted: Tue Jul 25, 2006 6:20 pm
by Weirdan
Untested

Code: Select all

<?php
include("config.php");
// Create array here
$themelist = array();

$dir = "../demo/themes/";
if (is_dir($dir)) {
   if ($dh = opendir($dir)) {
       while (($file = readdir($dh)) !== false) {
            if(file_exists("../demo/themes/$file/theme.php")){

                 array_push($themelist, $file); // Add value to array

           }
       }
       closedir($dh);
   }
}
$dbThemeList = array();
$q = "SELECT * FROM nuke_theme_preview";
$result = mysql_query($q) or die ('Something is wrong with query: ' . $q . '<br>'. mysql_error());
while ($row = mysql_fetch_assoc($result))
{
     array_push($dbThemeList, $row['t_title']); // Add value to array
}
$newThemes = array_filter($themelist, create_function('$elt', 'return !in_array($elt,' . var_export($dbThemeList, true) . ');'));

var_dump($newThemes);

?>

Posted: Tue Jul 25, 2006 6:35 pm
by SidewinderX
the output is

Code: Select all

array(9) { [5]=>  string(6) "Karate" [6]=>  string(4) "Milo" [7]=>  string(8) "NukeNews" [8]=>  string(7) "Odyssey" [9]=>  string(12) "Sand_Journey" [10]=>  string(5) "Slash" [11]=>  string(10) "SlashOcean" [12]=>  string(6) "Sunset" [13]=>  string(11) "Traditional" }
still not EXACTLEY what i want, but from here i think a little tweaking will do the trick. thanks for the reply!

EDIT:

Finished Code

Code: Select all

<?php
include("config.php");
// Create array here
$themelist = array();

$dir = "../demo/themes/";
if (is_dir($dir)) {
   if ($dh = opendir($dir)) {
       while (($file = readdir($dh)) !== false) {
            if(file_exists("../demo/themes/$file/theme.php")){

                 array_push($themelist, $file); // Add value to array

           }
       }
       closedir($dh);
   }
}
$dbThemeList = array();
$q = "SELECT * FROM nuke_theme_preview";
$result = mysql_query($q) or die ('Something is wrong with query: ' . $q . '<br>'. mysql_error());
while ($row = mysql_fetch_assoc($result))
{
     array_push($dbThemeList, $row['t_title']); // Add value to array
}
$newThemes = array_filter($themelist, create_function('$elt', 'return !in_array($elt,' . var_export($dbThemeList, true) . ');'));


for ($i=0; $i < sizeof($themelist); $i++) {
if($newThemes[$i]!=""){

echo $newThemes[$i];
echo "<br>";

}
};

?>
thank you for all your replys