Bosna i Hercegovina


switch Lista naslova:

#1 20.02.2011-01:04
zxz Offline
Administrator
Registrovan/a od: 03.02.2009-16:22
Komentari: 9,418


Subject: Koristan PHP kod*
Brojac posjeta
DownloadIzvorni kod (PHP):
  1. <?php
  2.  
  3. /**
  4. * Create an empty text file called counterlog.txt and
  5. * upload to the same directory as the page you want to
  6. * count hits for.
  7. *
  8. * Add this line of code on your page:
  9. * <?php include "text_file_hit_counter.php"; ?>
  10. */
  11.  
  12. // Open the file for reading
  13. $fp = fopen("counterlog.txt", "r");
  14.  
  15. // Get the existing count
  16. $count = fread($fp, 1024);
  17.  
  18. // Close the file
  19. fclose($fp);
  20.  
  21. // Add 1 to the existing count
  22. $count = $count + 1;
  23.  
  24. // Display the number of hits
  25. // If you don't want to display it, comment out this line
  26. echo  $count . " posjeta od 17.2.2011 ";
  27.  
  28. // Reopen the file and erase the contents
  29. $fp = fopen("counterlog.txt", "w");
  30.  
  31. // Write the new count to the file
  32. fwrite($fp, $count);
  33.  
  34. // Close the file
  35. fclose($fp);
  36.  
  37. ?>

Pozdrav
Ovaj komentar je mijenjan 1 puta. zadnja izmjena 03.03.2013-15:45 od strane zxz. ↑  ↓

#2 12.03.2011-13:09
zxz Offline
Administrator
Registrovan/a od: 03.02.2009-16:22
Komentari: 9,418


Subject: Slanje emaila
Slanje emaila
DownloadIzvorni kod (PHP):
  1. function genSendEmail($to, $subject, $moja_poruka, $from_address="admin@programeri.com", $from_name="Moke") {  
  2.      $message = "<html><head>  
  3.       <title>".$subject."</title>  
  4.     </head>  
  5.     <body>  
  6.       ".$moja_poruka."  
  7.     </body>  
  8.     </html>  
  9.     ";  
  10.  
  11.      // Da bi poslali HTML email, the Content-type mora biti podesen  
  12.      $headers  = 'MIME-Version: 1.0' . "\r\n";  
  13.      $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";  
  14.  
  15.      // Dodatni headers  
  16.      $headers .= 'To: '.$to.' <'.$to.'>' . "\r\n";  
  17.      $headers .= 'From: '.$from_name.' <'.$from_address.'>' . "\r\n";  
  18.  
  19.      // -- Posalji postu  
  20.      ini_set(sendmail_from,$from_address);  // INI configuracija, prisili FROM adresu  
  21.      $mail_sent = mail($to, $subject, $message, $headers);  
  22.  
  23.      ini_restore(sendmail_from);  
  24.      return $mail_sent;  
  25. }  

Pozdrav
Ovaj komentar je mijenjan 1 puta. zadnja izmjena 24.10.2011-22:09 od strane zxz. ↑  ↓

#3 12.03.2011-13:12
zxz Offline
Administrator
Registrovan/a od: 03.02.2009-16:22
Komentari: 9,418


Subject: Redirect
Procedura vrsi redirekciju.
DownloadIzvorni kod (PHP):
  1. <?php  
  2. function genRedirect( $url, $msg='' ) {  
  3.      // Izbrisi sve sto nam nije potrebno  
  4.      $url = preg_split("/[\r\n]/", $url);  
  5.      $url = $url[0];  
  6.  
  7.      if (trim( $msg )) {  
  8.           $url     .= (strpos( $url, '?' ))? '&genmsg=' . urlencode( $msg ) : '?genmsg=' . urlencode( $msg );  
  9.      }  
  10.  
  11.      if (headers_sent()) {  
  12.           echo "<script>document.location.href='$url';</script>\n";  
  13.      } else {  
  14.           @ob_end_clean(); // izbrisi output buffer  
  15.           header( 'HTTP/1.1 301 Moved Permanently' );  
  16.           header( "Location: ". $url );  
  17.      }  
  18.      exit();  
  19. }  
  20. ?>  

Pozdrav
Ovaj komentar je mijenjan 1 puta. zadnja izmjena 12.03.2011-13:12 od strane zxz. ↑  ↓

#4 04.03.2012-01:08
zxz Offline
Administrator
Registrovan/a od: 03.02.2009-16:22
Komentari: 9,418


Subject: Upload fajla
Ovo kopirajte u fal i dajte mu ime upload.html
DownloadIzvorni kod (HTML):
  1.  <body>
  2.  
  3.  <form action="upload_file.php" method="post"
  4. enctype="multipart/form-data">
  5.  <label for="file">Filename:</label>
  6.  <input type="file" name="file" id="file" />
  7.  <br />
  8.  <input type="submit" name="submit" value="Submit" />
  9.  </form>
  10.  
  11.  </body>
  12.  </html>
Ovaj kod kopirajte i zapisite u isti dir kao i predhodni i dajte mu ime upload_file.php
DownloadIzvorni kod (PHP):
  1. <?php
  2.  if ($_FILES["file"]["error"] > 0)
  3.    {
  4.    echo "Error: " . $_FILES["file"]["error"] . "<br />";
  5.    }
  6.  else
  7.    {
  8.    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
  9.    echo "Type: " . $_FILES["file"]["type"] . "<br />";
  10.    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
  11.    echo "Stored in: " . $_FILES["file"]["tmp_name"];
  12.    }
  13.  ?>

Pozdrav
↑  ↓

#5 05.03.2012-20:24
zxz Offline
Administrator
Registrovan/a od: 03.02.2009-16:22
Komentari: 9,418


Subject: Kako otvoriti txt file
DownloadIzvorni kod (HTML):
  1.  <body>
  2.  
  3.  <?php
  4. $file=fopen("welcome.txt","r");
  5. ?>
  6.  
  7.  </body>
  8.  </html>

Opcije:
ModesDescription
rRead only. Starts at the beginning of the file
r+Read/Write. Starts at the beginning of the file
wWrite only. Opens and clears the contents of file; or creates a new file if it doesn't exist
w+Read/Write. Opens and clears the contents of file; or creates a new file if it doesn't exist
aAppend. Opens and writes to the end of the file or creates a new file if it doesn't exist
a+Read/Append. Preserves file content by writing to the end of the file
xWrite only. Creates a new file. Returns FALSE and an error if file already exists
x+Read/Write. Creates a new file. Returns FALSE and an error if file already exists
Sljedeći primjer generira poruku ako fopen () funkcija nije u mogućnosti da biste otvorili navedenu datoteku:
DownloadIzvorni kod (HTML):
  1.  <body>
  2.  
  3.  <?php
  4. $file=fopen("welcome.txt","r") or exit("Unable to open file!");
  5. ?>
  6.  
  7.  </body>
  8.  </html>
Fclose () funkcija koristi se da biste zatvorili otvorenu datoteku:
Primjer:
DownloadIzvorni kod (PHP):
  1. <?php
  2.  $file = fopen("test.txt","r");
  3.  
  4.  //some code to be executed
  5.  
  6.  fclose($file);
  7.  ?>

Pozdrav
↑  ↓

#6 09.11.2013-21:03
zxz Offline
Administrator
Registrovan/a od: 03.02.2009-16:22
Komentari: 9,418


Subject: Primjer switch
DownloadIzvorni kod (PHP):
  1. <?php
  2.  $favcolor="red";
  3.  switch ($favcolor)
  4.  {
  5.  case "red":
  6.    echo "Boja je  red!";
  7.    break;
  8.  case "blue":
  9.    echo "Boja je  blue!";
  10.    break;
  11.  case "green":
  12.    echo "Boja je green!";
  13.    break;
  14.  default:
  15.    echo "Moji  favoriti boja su red, blue, or green!";
  16.  }
  17.  ?>

Pozdrav
↑  ↓

#7 03.11.2016-09:33
zxz Offline
Administrator
Registrovan/a od: 03.02.2009-16:22
Komentari: 9,418


Subject: Foreach primjer
Primjer iscitavanja iz Array:
DownloadIzvorni kod (HTML):
  1. <!DOCTYPE html>
  2.  
  3. <?php
  4. $imena = array("Enes"=>"35", "Marko"=>"40", "Ivan"=>"43","mujo"=>"60");
  5.  
  6. foreach($imena as $ime => $godine)
  7. {
  8.     echo $ime . ":" . $godine . "god.";
  9.     echo "<br>";
  10. }
  11. ?>
  12. </body>
  13. </html>

Ovdje se moze upotrijebiti i funkcija sort, rsort, arsort itd...
DownloadIzvorni kod (HTML):
  1. <!DOCTYPE html>
  2. <?php
  3. $vocke = array ("jabuka", "banana", "ananas",  "kruška", "šljiva","smokva");
  4. sort($vocke);
  5. foreach ( $vocke as $vocka )
  6. {
  7. echo  "$vocka<br />";
  8. }
  9. ?>
  10. </body>
  11. </html>

Pozdrav
↑  ↓

#8 05.11.2016-17:10
zxz Offline
Administrator
Registrovan/a od: 03.02.2009-16:22
Komentari: 9,418


Subject: Globalne variable
Primjer globalnih variabli.

DownloadIzvorni kod (HTML):
  1. <META NAME="generator" CONTENT="toniarts easyhtml v.2.4">
  2. <TITLE>Title of your page</TITLE>
  3. </HEAD>
  4. <?php
  5. $a = 120;
  6. $b = 180;
  7.  
  8. function globalvar() {
  9.    $GLOBALS['c'] = $GLOBALS['a']+$GLOBALS['b'];
  10. }
  11.  
  12. globalvar();
  13. echo $c;
  14. ?>
  15. </BODY>
  16. </HTML>

Pozdrav
↑  ↓

#9 06.11.2016-11:28
zxz Offline
Administrator
Registrovan/a od: 03.02.2009-16:22
Komentari: 9,418


Subject: Sort primjer
Sortiranje po abecedno redu od a doZ.
DownloadIzvorni kod (HTML):
  1. <!DOCTYPE html>
  2.  
  3. <?php
  4. $imena = array("Ivo", "Bono", "Jakub","Enes","Azra","Vinko","Zoran","Mujo");
  5. sort($imena);
  6.  
  7. $brimena = count($imena);
  8. for($x = 0; $x < $brimena; $x++) {
  9.    echo $imena[$x];
  10.    echo "<br>";
  11. }
  12. ?>
  13. </body>
  14. </html>
Ako upotrijebimo komandu rsort umjesto sort dobit cemo sortiranje od z do a.
Isto vazi i za brojeve.

Postoje jos i komande:
asort
ksort
krsort
arsort
DownloadIzvorni kod (HTML):
  1. <!DOCTYPE html>
  2.  
  3. <?php
  4. $imena = array("Ivo"=>"32", "Bono"=>"24", "Jakub"=>"56","Enes"=>"18","Azra"=>"19","Vinko"=>"30","Zoran"=>"65","Mujo"=>"35");
  5. asort($imena);
  6.  
  7. $brimena = count($imena);
  8. foreach($imena as $imeo=>$starost)
  9. {
  10.     echo  $imeo . " :" . $starost . " godina";
  11.     echo "<br>";
  12. }
  13. ?>
  14. </body>
  15. </html>

Pozdrav
↑  ↓

Stranice (1): 1


All times are GMT +01:00. Current time: 24.09.2017-09:28.