Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

Višestruki Update (Array, Forma TXT fajl)

[es] :: PHP :: Višestruki Update (Array, Forma TXT fajl)

[ Pregleda: 1592 | Odgovora: 3 ] > FB > Twit

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

Kusur
Grafički dizajner
Beograd

Član broj: 2485
Poruke: 208
*.dynamic.sbb.rs.



Profil

icon Višestruki Update (Array, Forma TXT fajl)07.08.2009. u 17:27 - pre 178 meseci
Pozdrav svima,

U MySql bazi sa linkovima potrebno je da kod određenog broja linkova promenim polje "provera".

Trenutno svi "linkovi" (redovi u bazi) u ovom polju imaju upisanu jedinicu "1" a meni je potrebno da samo kod nekih "linkova" (redova u bazi) upišem nulu.

- U Ekselu imam spisak linkova kod kojih treba upisati nulu a mogu i da napravim TXT fajl.
- Želeo bih da to uradim u PHP-u
- Znam da to treba uraditi pomoću "ARRAY-ja" i preko FORME ili sa "čitanjem" iz TXT fajla ali ne znam kako to da izvedem.

Da li neko može da mi pomogne?

Unapred hvala.




Kusur
 
Odgovor na temu

BobanPeric
RedLotus Europe
PHP Lead Developer

Član broj: 224308
Poruke: 14
93.87.161.*



Profil

icon Re: Višestruki Update (Array, Forma TXT fajl)07.08.2009. u 18:04 - pre 178 meseci
Cao,

ja sam skoro radio nešto slično sa .CSV fajlom, nisam siguran ali mislim da Eksel može da eksportuje i takav fajl.
Ako moraš da koristiš .TXT fajl onda samo zameniš funkciju 'fgetcsv' sa odgovarajućom za .TXT fajl valjda 'fgets'.
Moj script briše red ako je email adresa jednaka, ti promeni sql upit da setuje polje 'provera' na 0 ako je link jednak.

$link = $data[0];
Nešto tipa : UPDATE imeTabele SET provera = 0 WHERE link =$link

To je odprilike

Code:
<?php
/**    
*    Script for deleting records from the database which contain bounced emails
*    The .csv file contains the bounced emails
**/


$start_time = time();

//Your settings to change here:
//*****************************

//Database related settings
//-------------------------------
$db_server = "your_database_server"; //database server name 
$db_username = "your_database_username"; 
$db_password = "your_database_password";

$db_database_name = "your_database_name"; //database name 
$db_database_table = "your_table_name"; //table name 
$db_database_fieldname = "your_field_name"; //column name that contains the client email 
//--------------------------------

//Your .csv file name setting
//---------------------------
$filename = "dpreport.csv";
//---------------------------

//*****************************




//The script itself:

//Connecting to the database
$db_connection = mysql_connect($db_server,$db_username,$db_password)
        or die("Unable to connect to the database!");

//Linking to the specified database name
$db = mysql_select_db($db_database_name,$db_connection)
        or die("Unable to link with the specified database name!");

//Open the file for reading
$handle = fopen($filename, "r");

//Number of deleted rows
$rows = 0;

//For each line of the .csv file drop the record(s) if the email match

//The longest possible email in the database is 50 characters
while (($data = fgetcsv($handle, 50, ",")) !== FALSE) 
{
    
    //The first column in the .csv file (= the email) 
    $email = $data[0];
    
    //If the line in the .csv file is blank we jump to the next iteration
    if ($email == NULL)
    {
        continue;
    }
    
    $result = mysql_query("DELETE from $db_database_table WHERE $db_database_fieldname = '$email' ");
    
    //Increment the number of rows deleted
    if ($result) 
    {
        $rows += mysql_affected_rows();
        
    }
    else
    {
        die($rows . ' rows already deleted but stopped by error. Invalid query: ' . mysql_error());
    }
    
}

$end_time = time();
//Duration of the execution in seconds
$duration = $end_time - $start_time;

//Print that the script has finished and how many rows were deleted
echo "The script has terminated the execution in ". $duration . " seconds." . "<br />";
echo "Number of records deleted: " . $rows ;

//Close the file from reading
fclose($handle);

//Close the connection to the database
mysql_close($db_connection);

?>


[izmenio Nemanja: Kôd stavljajte u CODE tagove!]

[Ovu poruku je menjao Nemanja Avramović dana 07.08.2009. u 19:44 GMT+1]
 
Odgovor na temu

Nikola Poša
Backend (PHP) developer
Beograd

Član broj: 173839
Poruke: 1616
93.87.146.*



+33 Profil

icon Re: Višestruki Update (Array, Forma TXT fajl)08.08.2009. u 10:17 - pre 178 meseci
Ako sam dobro shvatio šta želiš, to onda ne bi trebalo da bude komplikovano, i moglo bi da se reši ovako:
Code:
$spisak = file('putanja/do/tvog/fajla.txt');

//Ovde sad ide neka konekcija sa bazom

$where = '';
foreach ($spisak as $key=>$s) {
   $where .= "'" . mysql_real_escape_string($s) . "'";
   if ($key != count($spisak) - 1) {
      $where .= ', ';
   }
}

$sql = 'UPDATE linkovi SET provera = 0 WHERE naziv IN (' . $where . ');

mysql_query($sql);

Ovaj primer bi važio u slučaju da su u tvom fajlu nazivi linkova jedni ispod drugog (odvojeni znakom za prelazak u novi red).
 
Odgovor na temu

Kusur
Grafički dizajner
Beograd

Član broj: 2485
Poruke: 208
*.dynamic.sbb.rs.



Profil

icon Re: Višestruki Update (Array, Forma TXT fajl)08.08.2009. u 15:59 - pre 178 meseci
Hvala Vam pomogli ste mi puno.

Zavšio sam "update" vrlo brzo i "bezbolno".
Kusur
 
Odgovor na temu

[es] :: PHP :: Višestruki Update (Array, Forma TXT fajl)

[ Pregleda: 1592 | Odgovora: 3 ] > FB > Twit

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.