Showing posts with label sourcecode. Show all posts
Showing posts with label sourcecode. Show all posts

Sunday, September 29, 2013

Fibonacci Numbers with PHP

hi

how to calculate fibonacci numbers with php? you can do it this way.


CODE

<?php

fibonacci(1000);

function fibonacci($numbersToShow){
  $arr[0] = 0;
  $arr[1] = 1;
   
  echo "0: ". $arr[0] . "<br>";
  echo "1: ". $arr[1] . "<br>";
   
  for($n = 1; $n < $numbersToShow; $n++) {
      $arr[$n + 1] = $arr[$n] + $arr[$n - 1];
      echo ($n + 1) . ": " . $arr[$n + 1] . "<br>";
  }
}

?>

Saturday, September 28, 2013

Excel .xls to .json or array with PHP

ever wanted to read directly from a Excel .xls file and work with it in PHP? here is a solution, it's maybe, no, definitely not the best solution but you can use it as a starting point for your personal and better script.

this script uses the php-excel-reader from google code.



the download to my example index.php can be found here: DOWNLOAD, the needed files from the php-excel-reader and a example XLS are also in this archive.

my php script is a very basic attempt to use the php reader, in the documentation of the project you can find many other efficient ways to use it.

have fun and success
.tappa

DOWNLOAD


<?php
/*
###########################################
.xls to [] by Sebastian ".tappa" L. 
needed 1 cup coffee, 4 fags, 20 minutes DNB
and the php-excel-reader lib from 
https://code.google.com/p/php-excel-reader/
licence: MIT-Licence // 28-September-2013
###########################################
*/

require_once 'excel_reader2.php';

$data = new Spreadsheet_Excel_Reader("mappe.xls");

$counter = 1;
$i = 1;
$j = 1;
$arr = array();

while (!$data->val($i,$j) == NULL){
do {
echo $data->val($i,$j)." | ";
$arr[$i][$j] = $data->val($i, $j);
$j++;
} while (!$data->val($i,$j) == NULL);
$i++; $j = 1; echo "<br>";
}
echo "The Array<br>";
print_r($arr).PHP_EOL;
echo "<br>The json<br>";
$json = json_encode($arr);
print_r($json);

?>