Codeigniter Multiple File Upload function
An easy function to upload two image by a html form.
function save_images()
{
$html = '';
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|JPEG|jpeg|PNG|GIF|JPG';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload('pic1'))
{
$html .= $this->upload->display_errors();
$html .='<br />File Not Uploaded';
$upload_data['file_name']='nopic.png';
}
else
{
$upload_data = $this->upload->data();
$html .='File Uploaded !<br />';
}
if (!$this->upload->do_upload('pic2'))
{
$html .= $this->upload->display_errors();
$html .='<br />File Not Uploaded';
$upload_data2['file_name']='nopic.png';
}
else
{
$upload_data2 = $this->upload->data();
$html .='File Uploaded !<br />';
}
$dat=array(
'main_pic'=>$upload_data['file_name'],
'cover_pic'=>$upload_data2['file_name']
);
if($this->db->insert('picz', $dat))
return $html;
else
return 'Failed !';
}
<form action="" method="post" enctype="multipart/form-data"> <input type="file" name="pic1" /> <input type="file" name="pic2" /> <input type="submit" name="subpic" value="upload" /> </form>
Send json in HTTP POST request by CURL php
A simple post operation by php curl. And send some json data.
//extract data from the post
extract($_POST);
//set POST variables
$url = 'http://aajit.com/apipath';
$jsodata='{"to":"foo", "msg":"bar", "token":"blah"}';
$fields_string ="aajgw=".$jsodata;
//open connection
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, 1); // 1 for number of params (aajgw)
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
//execute post
$result = curl_exec($ch);
echo $result;
//close connection
curl_close($ch);
Pass data from controller to view in magento
It’s may be very easy for Zend framework user But, I find some magento developer fighting to pass data from controller to view.
In magento you have to create a empty model(if your mod don’t have a varient object model) which extends to varient object. Magento use magic getter magic setter so you can pass value by setAnyname() and getAnyname() thus functions will be auto defines by Zend like many other framework do.
Model file:
Packname_Modulename_Model_Viewinfo extends Varien_Object{
}
Controller function:
public function indexAction()
{
$bla = Mage::getSingleton('Modulename/viewinfo');
$bla->setMsg("Thank You for your visit!");
$this->loadLayout();
$this->renderLayout();
}
Now in your View .phtml file add:
<?php $blas = Mage::getSingleton('Modulename/viewinfo'); ?>
<?php echo $blas->getMsg(); ?>
Nobody beat us Fry us and eat us
Want MySQL FullText Search with InnDB Huh?
MySQL FullText indexing is a popular way to search data from table. FullText indexing is only available if database table type is MyISAM. But, MyISAM table type does not support Foreign keys. InnoDB table type support Foreign keys but does not support FullText indexing. So I have to use a trick to implement fulltext search for a InnoDB type table. I have created a temporary MyISAM table which is identical to my InnoDB table and deleted that table after search complete. check this out……
Provide the search term and limit for search result in function’s parameter.
<?php
function fulltxt_search($q,$lim1,$lim2){
$terms =trim(stripslashes(strip_tags($q)));
$rhtml="";
if(empty($terms)){
$rhtml.= "\n".'<p>No search term entered.</p>';
}else{
// SQL injection prevention (double quote mark is allowed here but should probably be disallowed)
$terms = strtr($terms, ',/\*&()$%^@~`?;', ' ');
$terms = trim($terms);
$terms = str_replace('#180', '', $terms);
$rhtml.= "\n".'<p>Searching for <i>'.htmlspecialchars($terms).'</i>... </p>';
$key = 'text_index';
// Beware of the re-definition of $s in this query.
$terms = html_entity_decode($terms, ENT_QUOTES);
mysql_query('CREATE TEMPORARY TABLE `tbl_mirror` (FULLTEXT INDEX (`info`)) Engine=MyISAM SELECT * FROM `values`');
if(!($res = @mysql_query('SELECT * FROM `tbl_mirror` WHERE MATCH(`info`) AGAINST ("'.$terms.'") LIMIT '.$lim1.', '.$lim2.';')or die(mysql_error()))){
mysql_query('DROP TEMPORARY TABLE tbl_mirror;')or die(mysql_error());
echo "\n".'<p>No match for: <i class="red">'.$terms.'</i></p>';
}else{
mysql_query('DROP TEMPORARY TABLE tbl_mirror;')or die(mysql_error());
if(@mysql_num_rows($res) == 0){
$rhtml.="\n".'<p>The search engine cannot find the page you are looking for.</p>';
$rhtml.="\n".'<p>This could be because it could not find any pages containing <i>'.$terms.'</i>';
$rhtml.="\n".'<p>Alternatively, it might have found too many pages, and could not decide which one you wanted.</p>';
}else{
$i = 0;
while($row = mysql_fetch_array($res)){
$s = substr(stristr(strip_tags($row['info']), $terms), 0, 120);
$i++;
$out[]=$row;
$rhtml.="\n".'<p>'.$i.'.) <a href="results.php?rez_id='.$row['f_id'].'">'.htmlentities($row['info']).' - '.$row['f_id'].'</a><br />... '.$s.'...'.$row['id'].'/'.$row['e_id'].'...</p>';
}
}
}
}
return $rhtml;
}
?>