Jump to content

Comparing Values

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
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.


Lists all the themes in the themes folder:
$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:
$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. I believe I have to use the IN ( ) MySQL construct, but i have no idea how to format the syntax. could someone give me a hand

Thank You.

#2
dirkfirst

dirkfirst

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 354 posts
You can use array to do this



// Create array here

$themelist = array("");


$dir = "../themes/"; 

if (is_dir($dir)) { 

   if ($dh = opendir($dir)) { 

       while (($file = readdir($dh)) !== false) { 

            if(file_exists("../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

} 



[SIZE="3"][B]// Make the array only have unique values

$unqThemeList = array_unique($themelist);


// Cycle through the $unqThemeList array and print

......



#3
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
was this suppose to be in the code?

[size="3"][b]

Edit: i removed that line and it kind of works, however it lists every values, both in the directory and in the database.
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 )

In the database i have these themes 3D-Fantasy, Anagram, DeepBlue, ExtraLite, and Kaput.
In the directory i have these themes 3D-Fantasy, Anagram, DeepBlue, ExtraLite, Kaput, Karat, Milo, NukeNews, Odyssey, Sand_Journey, Slash, SlashOcean, Sunset, and Traditional. Therefor i want the array to output

Array ( [0] => [1] => Karat [2] => Milo [3] => NukeNews [4] => Odyssey [5] => Sand_Journey [6], Slash[7], SlashOcean[8], Sunset[9], Traditional )

Its probabley just a minor adjustment, ill see what i can do, but in the mean time ill post it for everyone elses branes to work on too :)

#4
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
well after a few hours of deep thought i got it, thank you for pushing me in the right direction :)

Finished Code:
include("config.php");

// Create array here


echo "<select name='t_title'>";

$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 "<option value='" . $newThemes[$i] . "' name='t_title'>" . $newThemes[$i] . "</option>";

}

}

echo "</select>";


#5
Lop

Lop

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,172 posts
Nice work. I think you learn best when you figure out things this way.