Jump to content
php.lv forumi

Modify user data.


raivis

Recommended Posts

pienjemsim man ir fails "data.txt" ar mainiigajiem { list($name,$age,$email) = explode("|", $file[$line]); }

///////file:data.txt/////////

Janis|18|[email protected]|

Peteris|21|[email protected]|

Juris|51|[email protected]|

Olga|18|[email protected]|

Janis|11|[email protected]|

///////end file//////////////

 

///////file:users.php///////

<?php
$file = file("data.txt");
$totalLines = sizeof($file);
for($line = 0; $line < $totalLines; $line++){
if("//" != substr($file[$line], 0, 2)){
  @list($name,$age,$email) = explode("|", $file[$line]);
  $name = ucfirst($name);
     $string = $name.",".$age.",".$email."<br>";
     echo $string;
}
}
?>

///////end file//////////////

 

Kaads izskatiitos kods, ja programmai liktu:

1. no faila "data.txt" izdzeest taas rindinjas, kuraas $name=="Janis";

2. ja faila "data.txt" $name=="Janis" { $name veertiibu rename/paarsaukt par "JanisOzols"; }

3. izskaitiit, cik reizes failaa "data.txt" $name=="Janis";

4. no faila "data.txt" print/apluukot taas rindinjas $name,$age,$email veertiibu, kuraa $name=="Pereris";

Edited by raivis
Link to comment
Share on other sites

  • Replies 35
  • Created
  • Last Reply

Top Posters In This Topic

$fileData = file("data.txt");
$personList = array();
foraech ($fileData as $row){
list($name, $age, $email) = explode('|', $row);
$personList[] = array('name' => $name, 'age' => $age, 'email' => $email);
}

// 1. punkts
foreach ($personList as $key => $person){
if ($person['name'] != 'Janis'){
 $neviensJanis[] = $person;
}
}

// 2. punkts
foreach ($personList as $key => $person){
if ($person['name'] == 'Janis'){
 $personList[$key] = array('name' => 'JanisOzols', 'age' => $person['age'], 'email' => $person['email']);
}
}

// 3. punkts
$januskaits = 0;
foreach ($personList as $key => $person){
if ($person['name'] == 'Janis'){
 $januskaits++;
}
}

// 4. punkts
echo '<h1>Personas dati</h1>';
foreach ($personList as $key => $person){
if ($person['name'] == 'Pereris'){
 echo '<h2>' . $person['name'] . '</h2>
 <p>Vecums: ' . $person['age'] . '</p>
 <p>E-pasts: ' . $person['email'] . '</p>';
}
}

 

visiem punktiem ir kopīgs sākums, katrs no tiem ir darbināms atsevišķi.

Link to comment
Share on other sites

heh, Kaklz pastedzies! Kamēr lasīju postu nebija atbildes nevienas, uzspiežu pogu atbildēt un maģiski parādās atbilde :)

1. punktu var optimizēt (netiks veidots lieks masīvs, mazāk atmiņu ēdīs):

foreach ($personList as $key => $person){
 if ($person['name'] == 'Janis') {
   unset($personList[$key]);
 }
}

 

1. un 2. punktam vajag vēl koda nobeigumu:

$f = fopen('data.txt', 'w');
 foreach($personList as $row) {
   fwrite($f, implode('|', $row));
 }
 fclose($f);

Edited by bubu
Link to comment
Share on other sites

:huh: Paldies par atbildi, bet tai seko vēl viens jautājums..

 

KUR KĻŪDA!? :(

 

///////file:data.txt/////////

Janis|18|[email protected]|

Peteris|21|[email protected]|

Juris|51|[email protected]|

Olga|18|[email protected]|

Janis|11|[email protected]|

///////end file//////////////

 

:blink: Startējot skriptu, no faila data.txt izdzēšas ne tikai rindiņas, kurās $name='Janis', bet beidzamais '|' un atkāpes.

Atkārtoti startējot skriptu, failā data.txt rodas tīrais gļuks.

Kādēļ? Ja man vajadzīgs, lai izdzēstos tikai rindiņa + tās atkāpe, kuras $name='Janis'.

$fileData = file("data.txt");
$personList = array();
foreach ($fileData as $row){
list($name, $age, $email) = explode('|', $row);
$personList[] = array('name' => $name, 'age' => $age, 'email' => $email);
}

// 1. punkts
// No faila "data.txt" izdzēst tās rindiņas, kurās $name=="Janis";
foreach ($personList as $key => $person){
if ($person['name'] == 'Janis') {
  unset($personList[$key]);
}
}

$f = fopen('data.txt', 'w');
foreach($personList as $row) {
  fwrite($f, implode('|', $row));
}
fclose($f);

 

$fileData = file("data.txt");
$personList = array();
foreach ($fileData as $row){
list($name, $age, $email) = explode('|', $row);
$personList[] = array('name' => $name, 'age' => $age, 'email' => $email);
}

// 2. punkts
// Ja faila "data.txt" $name=="Janis" { $name vērtību rename/pārsaukt par "JanisOzols"; }
foreach ($personList as $key => $person){
if ($person['name'] == 'Olga'){
$personList[$key] = array('name' => 'JanisOzols', 'age' => $person['age'], 'email' => $person['email']);
}
}

$f = fopen('data.txt', 'w');
foreach($personList as $row) {
  fwrite($f, implode('|', $row));
}
fclose($f);

Paldies vēlreiz!

Link to comment
Share on other sites

Kas ir atkāpe?

Un priekškam tev beigās to | vajag?

Un ja jau tik ļoti vajag, tad paņem un pieliec.

Kļūdas jau nav, ir tikai negribēšana saprast, ko tev priekšā rāda:

 

$f = fopen('data.txt', 'w');
foreach($personList as $row) {
 fwrite($f, implode('|', $row).'|');
}
fclose($f);

Link to comment
Share on other sites

Un, ja man būtu jānoteic, vai $name = $username, tad laikam izskatītos kas tml. .. ?

$username = "Pēteris";

$fileData = file("data.txt");
$personList = array();
foreach ($fileData as $row){
list($name, $age, $email) = explode('|', $row);
$personList[] = array('name' => $name, 'age' => $age, 'email' => $email);
}

foreach ($personList as $key => $person){
if ($person['name'] == $username){
 echo $username." <b>nav</b> reģistrējies";
} else {
 echo $username." <b>ir</b> reģistrējies";
}
}

Link to comment
Share on other sites

Pieņemsim data.txt failā glabājās dati par 100 reģistrētajiem lietotājiem -

» kā lai alfabēta secībā nolasu lietotāja datus [$name,$age$email] tos sagrupējot pa 5?

if $lpp="0" {

print alfabēta secībā 1. sagrupēto 5 lietotāju datus [5 rindiņas];

} if $lpp="1" {

print alfabēta secībā 2. sagrupēto 5 lietotāju datus [5 rindiņas];

}

utt.

 

///////file:data.txt/////////

Janis|18|[email protected]|

Peteris|21|[email protected]|

Juris|51|[email protected]|

Olga|18|[email protected]|

Janītis|11|[email protected]|

Pēteris|86|[email protected]|

Baiba|18|[email protected]|

utt.

///////end file//////////////

Edited by raivis
Link to comment
Share on other sites

Man nepieciešams rāmī izvadīt sagrupētus(iekrāsotus) data.txt $name, $email vērtības !?..

<?php
$file = file("data.txt");
$totalLines = sizeof($file);
for($line = 0; $line < $totalLines; $line++){
@list($name,$age,$email) = explode("|", $file[$line]);

 echo "'".$name."' => '".$email."', ";

}

$users = array
(
//  $name => $email #izvada tikai peedeejaas rindinjas mainiigo veertiibu!?
   'Jānis Bērziņš' => '[email protected]',
   'Kārlis Asnītis' => '[email protected]',
   'Guntars Osiņš' => '[email protected]',
   'Laura Vāvere' => '[email protected]',
   'Anna Kocene' => '[email protected]'
);

echo '<hr>';
echo '<table border="1">';
echo '<tr style="background-color: #696969; color: #ffffff">';
echo '<th>Vārds</th><th>E-pasts</th></tr>';

$counter = 0;
foreach($users AS $name => $email)
{
   $bgcol = ($counter % 2) ? '#dcdcdc' : '#ffffff';

   echo '<tr style="background-color: ' . $bgcol . '">';
   echo '<td>' . $name . '</td><td>' . $email . '</td></tr>';

   $counter++;
}

echo '</table>';

?>

..ar šo tas neizdodas!

Link to comment
Share on other sites

<?php
 // ielasi visu failu masīvā no 0..n-1
 // sakārto to pēc alfabēta
 // pārliecinies, ka $lpp ir robežās no 0 līdz (n-1)/5
 for ($i = 5*$lpp; $i<5*($lpp+1); i++) {
   // drukā $i-to ierakstu
 }
?>

Varēji jau arī lūdzu pabeigt savu domu līdz galam, to ne tik vien ieskicējot :(

  • nolasīt faila "data.txt" mainīgo $name, $email vērtības;
  • sagrupēt tās alfabēta secībā pēc $name vērtības;

..

Link to comment
Share on other sites

Varēji jau arī lūdzu pabeigt savu domu līdz galam, to ne tik vien ieskicējot :(

Kāpēc? Es tev pašam ļauju domāt. Ideju ļoti skaidru uzrakstīju. Kā tad tu domā iemācīties php, ja tikai prasīsi, lai visu tev priekšā uzraksta? Sākumu tev iedevu, tad tālāk padomā, kā katru no tām komentāra rindiņām, realizēt php kodā.

Edited by bubu
Link to comment
Share on other sites

Man nepieciešams rāmī izvadīt sagrupētus(iekrāsotus) data.txt $name, $email vērtības !?..

<?php
$file = file("data.txt");
$totalLines = sizeof($file);
for($line = 0; $line < $totalLines; $line++){
@list($name,$age,$email) = explode("|", $file[$line]);

 echo "'".$name."' => '".$email."', ";

}

$users = array
(
//  $name => $email #izvada tikai peedeejaas rindinjas mainiigo veertiibu!?
   'Jānis Bērziņš' => '[email protected]',
   'Kārlis Asnītis' => '[email protected]',
   'Guntars Osiņš' => '[email protected]',
   'Laura Vāvere' => '[email protected]',
   'Anna Kocene' => '[email protected]'
);

echo '<hr>';
echo '<table border="1">';
echo '<tr style="background-color: #696969; color: #ffffff">';
echo '<th>Vārds</th><th>E-pasts</th></tr>';

$counter = 0;
foreach($users AS $name => $email)
{
   $bgcol = ($counter % 2) ? '#dcdcdc' : '#ffffff';

   echo '<tr style="background-color: ' . $bgcol . '">';
   echo '<td>' . $name . '</td><td>' . $email . '</td></tr>';

   $counter++;
}

echo '</table>';

?>

..ar šo tas neizdodas!

bet sādi neizdodās..

Link to comment
Share on other sites

Es vispār nesaprotu ko tu tajā kodā dari!

Vispirms tu nolasi visu failu, tad izvadi viņu ārā pa rindiņai.

Tālāk tu izveido masīvu no statiskiem datiem.

Un tad izvadi to masīvu pa rindiņai tā, lai katra otrā rindiņa ir citā krāsā.

Tā jābūt? Vai arī ko citu tam kodam būtu jādara?

Link to comment
Share on other sites

Es vispār nesaprotu ko tu tajā kodā dari!

Vispirms tu nolasi visu failu, tad izvadi viņu ārā pa rindiņai.

Tālāk tu izveido masīvu no statiskiem datiem.

Un tad izvadi to masīvu pa rindiņai tā, lai katra otrā rindiņa ir citā krāsā.

Tā jābūt? Vai arī ko citu tam kodam būtu jādara?

NJā, to pašu vien vajadzētu izdarīt kodam...

  • To, ko izdarīju ar statiskiem datiem, man vajadzētu izdarīt ar faila data.txt $name, $email vērtībām (katrā faila data.txt jaunā rindiņā ir savādāka šo mainīgo vērtība)!

Man vajadzētu katra otrā rindiņa $name, $email izvadīt citā krāsā..

Link to comment
Share on other sites


×
×
  • Create New...