PHP - extraer datos de la base de datos y comprobar que no existan en un fichero de texto

 
Vista:
Imágen de perfil de voreno

extraer datos de la base de datos y comprobar que no existan en un fichero de texto

Publicado por voreno (69 intervenciones) el 29/10/2014 11:40:10
Hola, con este código lo que intento hacer es extraer de la bd unas referencias y buscarlas en un fichero csv, las referencias que no existan en el fichero de texto deberían hacer que cumpla el else del "if($leer_linea == $ref) {" y si que se cumple pero el problema es que se cumple siempre, existan o no las referencias...

¿Alguna idea sobre como lo podría hacer? gracias anticipadas.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
$section = file_get_contents('csv-temp/productos.csv');
$section=str_replace(";", " ", $section);
$section=str_replace(",", " ", $section);
$File = "csv-temp/productos.txt";
$File = str_replace(',','#',$File);
 $Handle = fopen($File, 'w');
 $Data = $section;
   fwrite($Handle, $Data);
 fclose($Handle);
 
if ($borrar_productos == "1") {
	echo "<br/>Buscando productos que ya no existen...<br/>";
$registros_encontrados = $link->query("SELECT * FROM ps_product") or die (mysql_error());
 
while ($registro = mysqli_fetch_array($registros_encontrados)){
 
if(!$fp = fopen("csv-temp/productos.txt","r"))
{
echo "No pudo abrirse el archivo";
}
else
{
while(!feof($fp))
{
	$ref = $registro['reference'];
$leer_linea = fgets($fp);
if($leer_linea == $ref) {
$cl = TRUE;
}
else {
$buscaid = $link->query("SELECT id_product FROM ps_product WHERE reference='$ref'") or die (mysql_error()); $id_product = mysqli_fetch_row($buscaid); $id = $id_product[0];
    $link->query("delete from ps_product where reference = $ref");
	$link->query("delete from ps_product_shop where id_product= $id");
	$link->query("delete from ps_category_product where id_product= $id");
	$link->query("delete from ps_product_lang where id_product= $id and id_lang=1");
	$link->query("delete from ps_product_lang where id_product= $id and id_lang=2");
	$link->query("delete from ps_product_lang where id_product= $id and id_lang=3");
	$link->query("delete from ps_specific_price where id_product= $id");
	$link->query("delete from ps_stock_available where id_product= $id");
	$link->query("delete from ps_image where id_product= $id");
	$link->query("delete from ps_image_shop where id_image= $id");
	$link->query("delete from ps_image_lang where id_image= $id and id_lang=1");
	$link->query("delete from ps_image_lang where id_image= $id and id_lang=2");
	$link->query("delete from ps_image_lang where id_image= $id and id_lang=3");
$cuentaid = strlen($id);
if ($cuentaid == 1 or $cuentaid == 2 or $cuentaid == 3 or $cuentaid == 4 or $cuentaid == 5) {
$caracter1 = $id[0];
}
if ($cuentaid == 2 or $cuentaid == 3 or $cuentaid == 4 or $cuentaid == 5) {
$caracter2 = $id[1];
}
if ($cuentaid == 3 or $cuentaid == 4 or $cuentaid == 5) {
$caracter3 = $id[2];
}
if ($cuentaid == 4 or $cuentaid == 5) {
$caracter4 = $id[3];
}
if ($cuentaid == 5) {
$caracter5 = $id[4];
}
 
if ($cuentaid == 5) {
$dir = "../img/p/".$caracter1."/".$caracter2."/".$caracter3."/".$caracter4."/".$caracter5."/";
}
if ($cuentaid == 4) {
$dir = "../img/p/".$caracter1."/".$caracter2."/".$caracter3."/".$caracter4."/";
}
if ($cuentaid == 3) {
$dir = "../img/p/".$caracter1."/".$caracter2."/".$caracter3."/";
}
if ($cuentaid == 2) {
$dir = "../img/p/".$caracter1."/".$caracter2."/";
}
if ($cuentaid == 1) {
$dir = "../img/p/".$caracter1."/";
}
$ficheroseliminados= 0;
$handle = opendir($dir);
while ($file = readdir($handle)) {
 if (is_file($dir.$file)) {
  if ( unlink($dir.$file) ){
   $ficheroseliminados++;
  }
 }
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder