Archyvas

Įrašo tag'ai: ‘php’

My impressions about Drupal CMS (or should I say CMF?)

Vasaris 2nd, 2010 Ernestas Kardzys 4 komentarai

The more I read about Drupal Content Management System (Framework?) – the more I like it :) It’s very flexible, provides good API and well documented :)

But why it’s not object oriented?..

Kategorijos:Programavimas, Studijos KTU Raktažodžiai:, ,

PHP. Working with CSV files

Gegužė 9th, 2009 Ernestas Kardzys 6 komentarai

I wanted to create an index for my blog, so I needed to download stock prices. I download them from “Nasdaq OMX Vilnius” web site.

Maybe this might be useful for more people, so, here it is :)

Good friend of mine Paulius helped me with this CSV-based stock index counter :) If you need awesome web site for a very attractive price – don’t hesitate to contact him :)

Ok, so my index counter :) I’m not very good at PHP, so the code is not perfect. But it works :)

<?php
define(FILE, “file.csv”);

/* Gets the content of the CSV file */
function downloadCSV($file) {
$ch = curl_init();

$fp = fopen (dirname(__FILE__) . FILE, ‘w+’);
curl_setopt($ch, CURLOPT_URL, $file);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);

curl_exec($ch);

curl_close($ch);
fclose($fp);

return $fp;
}

/* Closes the CSV file */
function closeCSV($file) {
return fclose($file);
}

/* Gets data from the CSV data file  */
function getData($file, $number) {
return $file[$number];
}

/* Analyzes CSV file for market data */
function analyzeCSVFile($handle, $line_length, $delimiter) {
return fgetcsv($handle, $line_length, $delimiter);
}

/* Gets the path to the CSV file */
function getCSVPath() {
return “http://www.php.rules/folder/awesome_file_with_stock_prices.csv”;
}

/* Calculates the capilization of the stock */
function getCurrentCapitalization($name, $price) {
$capitalization = 0;

switch ($name) {
case “STOCK_PRICE1″:
$capitalization = str_replace(“,”, “.”, $price) * NUMBER_OF_STOCK_PRICE1_SHARES;
break;
case “STOCK_PRICE2″:
$capitalization = str_replace(“,”, “.”, $price) * NUMBER_OF_STOCK_PRICE2_SHARES;
break;
default:
break;
}

return $capitalization;
}

/* Calculates the index of the nowaday */
function calculateIndex($current_capitalization) {
$starting_capitalization_value = STARTING_CAPITALIZATION_OF_STOCKS; /* Starting capitalization value */
return ($current_capitalization / $starting_capitalization_value) * 100;
}

downloadCSV(getCSVPath()) or die();

$handle = fopen(FILE, ‘r’) or die(‘Error opening for reading!’);

$total_capitalization = 0;

while(($data = analyzeCSVFile($handle, 1000, “;”)) != FALSE) {
$total_capitalization += getCurrentCapitalization(getData($data, 0), getData($data, 9));
}

fclose($handle);

printf(“<b>Index: %.2f</b><br><br>\n”, calculateIndex($total_capitalization));
?>

My friend Paulius also said, that making functions getCSVPath() is not very good, because it has only “return” which probably will slow the hole process, so keep in mind that :)

Kategorijos:Programavimas Raktažodžiai:, , ,

phpMyAdmin klaida ant MySQL

Rugpjūtis 15th, 2008 Ernestas Kardzys 2 komentarai

Sukėliau phpMyAdmin’ą, sukūriau konfiguraciją, pabandžiau paleisti šį daikčiuką ir gavau bjaurią klaidą:

#1045 – Access denied for user ‘root’@'localhost’ (using password: NO)

Pakapsčiau internete ir išsprendžiau problemą – konfiguracijoje šio to trūko.

<?php
/*
* Generated configuration file
* Generated by: phpMyAdmin 2.11.8.1 setup script by Michal Čihař <michal@cihar.com>
* Version: $Id: setup.php 11423 2008-07-24 17:26:05Z lem9 $
* Date: Fri, 15 Aug 2008 09:37:15 GMT
*/

/* Servers configuration */
$i = 0;

/* Server localhost (config:root) [1] */
$i++;
$cfg['Servers'][$i]['host'] = ‘localhost’;
$cfg['PmaAbsoluteUri'] = ‘localhost/phpmyadmin/’;
$cfg['Servers'][$i]['password'] = ‘root’;

$cfg['Servers'][$i]['bookmarktable'] = ‘pma_bookmark’;
$cfg['Servers'][$i]['relation'] = ‘pma_relation’;
$cfg['Servers'][$i]['table_info'] = ‘pma_table_info’;
$cfg['Servers'][$i]['table_coords'] = ‘pma_table_coords’;
$cfg['Servers'][$i]['pdf_pages'] = ‘pma_pdf_pages’;
$cfg['Servers'][$i]['column_info'] = ‘pma_column_info’;
$cfg['Servers'][$i]['designer_coords'] = ‘pma_designer_coords’;
$cfg['Servers'][$i]['history'] = ‘pma_history’;
$cfg['Servers'][$i]['host'] = ‘localhost’;
$cfg['Servers'][$i]['extension'] = ‘mysqli’;
$cfg['Servers'][$i]['connect_type'] = ‘tcp’;
$cfg['Servers'][$i]['compress'] = false;
$cfg['Servers'][$i]['controluser'] = ‘root’;
$cfg['Servers'][$i]['controlpass'] = ‘root’;
$cfg['Servers'][$i]['auth_type'] = ‘config’;
$cfg['Servers'][$i]['user'] = ‘root’;
$cfg['Servers'][$i]['pmadb'] = ‘phpmyadmin’;

/* End of servers configuration */

?>
Trijų juodai pažymėtų eilučių trūko :) Pridėjau jas – viskas susitvarkė :)

PHP/MySQL neveikia arba nemoku

Sausis 11th, 2008 Ernestas Kardzys 6 komentarai

Klausimas programuotojams. PHP ir MySQL hibridas pas mane ne visai veikti nori.

Turim tokį kodą:

class MySQL {
// Variables
private $connector = null;
// Kodas….
// Executes a MySQL query
function execQuery($query) {
return mysql_query($query) or die(ERROR_QUERY_EXEC . ‘\”‘. $query . ‘\” ‘ . mysql_error());
}

} // Klasės
class UserBlog extends MySQL {

// Kodas ….

// Checks, if specified blog exists on the server
// Returns: true – blog exists,
//              false – otherwise
function checkIfBlogExist($blogid) {
//$result = $this->execQuery(“SELECT id FROM users WHERE username=’$blogid’”);
$result = mysql_query(“SELECT id FROM `users` WHERE username=’$blogid’”);
if (mysql_num_rows($result) == 0)
return false;
return true;
}

} // Klasės

Pas mane yra toks klausimas: kodėl kodas su execQuery() niekaip nenori veikti? PHP interpretatorius keikiasi, kad “Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/progmaker/public_html/blog/classes/userblog.php on line 66″. Galbūt žinote, kodėl taip yra?

Kategorijos:Programavimas Raktažodžiai:, , , ,

Kokių programų aš pasigendu Linux’e?

Gruodis 17th, 2007 Ernestas Kardzys 3 komentarai

Ta taigi. Linux’as – gera sistema, veikia sparčiai ir t.t. ir pan. Bet šios žinutės tikslas nėra aptarti Linux privalumų.

Man trūksta kai kurių Windows’inių programų. O jei tiksliau:  Adobe Dreamweaver (buvusi  Macromedia Dreamweaver) – vietoj jos naudoju KDevelop ir/arba vim. Kita programa: Adobe PhotoShop (naudoju GIMP arba Krita, Karbon14).

Jeigu žinote gerą PHP redaktorių Linux’e, būtinai parašykite.

MDB2

Gruodis 11th, 2007 Ernestas Kardzys Nėra komentarų

Atsakymas (galbūt) į ankstesnę žinutę.

MDB2 daikčiukas. Galbūt kam nors susidurti teko?

Papildyta: Arturas man dar vieną daikčiuką švystelėjo, kuris pavadintas CakePHP Su tortais čia tikrai nieko bendro neturi :D

Kategorijos:Programavimas Raktažodžiai:, , , ,

MySQL’o funkcijos PHP’e

Gruodis 11th, 2007 Ernestas Kardzys 2 komentarai

Galbūt žinote gerą MySQL framework‘ą PHP programavimo kalbai? Nes atsibodo su mysql_query(), mysql_fetch_array() ir pan. sugyventi.

Norėčiau kad jame būtų funkcijos, kurios supaprastintų gyvenimą su MySQL. Pvz. pateikiu užklausą SELECT * FROM news, tada su kokiomis nors funkcijomis galėčiau pasirinkti ką nors tokio: getFirstResult(); getNextResult() ir t.t.

P.S. Jei pats tokį daiktą nutversiu, informuosiu :)

Kategorijos:Programavimas Raktažodžiai:, , , , , ,