Search This Blog

Friday, August 12, 2011

best php export to csv without space or comma

$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// DB Connection here
$db = mysql_select_db("test",$con);

$result = mysql_query("SELECT * FROM test1");
csvToExcelDownloadFromResult($result);
function csvToExcelDownloadFromResult($result, $showColumnHeaders = true, $asFilename = 'data.csv') {
setExcelContentType();
setDownloadAsHeader($asFilename);
return csvFileFromResult('php://output', $result, $showColumnHeaders);
}

function csvFileFromResult($filename, $result, $showColumnHeaders = true) {
$fp = fopen($filename, 'w');
$rc = csvFromResult($fp, $result, $showColumnHeaders);
fclose($fp);
return $rc;
}
function setExcelContentType() {
if(headers_sent())
return false;

header('Content-type: application/vnd.ms-excel');
return true;
}

function setDownloadAsHeader($filename) {
if(headers_sent())
return false;

header('Content-disposition: attachment; filename=' . $filename);
return true;
}
function csvFromResult($stream, $result, $showColumnHeaders = true) {
if($showColumnHeaders) {
$columnHeaders = array();
$nfields = mysql_num_fields($result);
for($i = 0; $i < $nfields; $i++) { $field = mysql_fetch_field($result, $i); $columnHeaders[] = $field->name;
}
fputcsv($stream, $columnHeaders);
}

$nrows = 0;
while($row = mysql_fetch_row($result)) {
fputcsv($stream, $row);
$nrows++;
}

return $nrows;
}

Monday, July 25, 2011

Using php to display visitor / user information such as their IP address

$ip = $_SERVER['REMOTE_ADDR'];
$hostaddress = gethostbyaddr($ip);
$browser = $_SERVER['HTTP_USER_AGENT'];
$referred = $_SERVER['HTTP_REFERER']; // a quirky spelling mistake that stuck in php

print "Display IP address:
\n";
print "$ip

\n";
print "More detailed host address:
\n";
print "$hostaddress

\n";
print "Display browser info:
\n";
print "$browser

\n";
print "Where you came from (if you clicked on a link to get here:
\n";
if ($referred == "") {
print "Page was directly requested";
}
else {
print "$referred";
}

magento how to use distinct or get unique records from collection

magento how to use distinct or get unique records from collection

$collection = Mage::getModel('catalog/product')->getCollection()
->distinct(true)
->addAttributeToSelect('color')
->load();

Sunday, July 17, 2011

Magento send mails like contact us form or forms or how to send mails in magento

if(!empty($post)) { $translate = Mage::getSingleton('core/translate');
$translate->setTranslateInline(false); try { $name = $_POST['name'];
$email = $_POST['email']; $to_admin =
Mage::getStoreConfig('trans_email/ident_general/email'); $name_admin =
Mage::getStoreConfig('trans_email/ident_general/name'); $subject =
'Contact Us'; /*$vars = array('Name'=>$name, 'Email Id' =>$email ); */

//const EMAIL_TEMPLATE_XML_PATH = 'customer/testemail/email_template';
//$templateId = Mage::getStoreConfig(EMAIL_TEMPLATE_XML_PATH);
$templateId=1; $sender = array('name' => $name,'email' => $email);

$translate=Mage::getModel('core/email_template')
->setTemplateSubject($subject) ->sendTransactional($templateId, $sender,
$to_admin, $name_admin, array('data' => $vars));
$translate->setTranslateInline(true);

Mage::getSingleton('core/session')->addSuccess(Mage::helper('customer')->__('Data
Sent Successfully')); $url = Mage::getUrl().'thanks';
Mage::app()->getFrontController()->getResponse()->setRedirect($url); }
catch (Exception $e) {
Mage::getSingleton('core/session')->addError(Mage::helper('customer')->__('Unable
to send email.')); $url = Mage::getUrl().'contactus';
Mage::app()->getFrontController()->getResponse()->setRedirect($url);
return; } }

Friday, July 15, 2011

admin store email magento general contacts email or Magento: Store Mail Addresses

//General contact
Mage::getStoreConfig('trans_email/ident_gerneral/name');
Mage::getStoreConfig('trans_email/ident_gerneral/email');

//Sales Representative
Mage::getStoreConfig('trans_email/ident_sales/name');
Mage::getStoreConfig('trans_email/ident_sales/email');

//Customer Support
Mage::getStoreConfig('trans_email/ident_support/name');
Mage::getStoreConfig('trans_email/ident_support/email');

//Custom email1
Mage::getStoreConfig('trans_email/ident_custom1/name');
Mage::getStoreConfig('trans_email/ident_custom1/email');

//Custom email2
Mage::getStoreConfig('trans_email/ident_custom2/name');
Mage::getStoreConfig('trans_email/ident_custom2/email');

Tuesday, July 12, 2011

Magento get path id or getpathids function to get parent category untill or upto root category

Magento get path id or getpathids function to get parent category untill or upto root category

getPathIds ()
Get array categories ids which are part of category path Result array contain id of current category because it is part of the path

Returns:
array

Definition at line 584 of file Category.php.

00585 {
00586 $ids = $this->getData('path_ids');
00587 if (is_null($ids)) {
00588 $ids = explode('/', $this->getPath());
00589 $this->setData('path_ids', $ids);
00590 }
00591 return $ids;
00592 }

magento reference doc or documents or docs

magento reference doc or documents or docs

http://freegento.com/doc/