When I import XLS files, I would like to avoid the import of empty rows. I tried with several codes without success. Can anyone help me?
This is the code:
require('xls/php-excel-reader/excel_reader2.php');
require('xls/SpreadsheetReader.php');
require_once '_inc/_db.php';
$r = rand();
$mimes = ['application/vnd.ms-excel', 'text/xls', 'text/xlsx', 'application/vnd.oasis.opendocument.spreadsheet'];
if (in_array($_FILES["file"]["type"], $mimes)) {
$uploadFilePath = 'storage/' . basename($_FILES['file']['name']);
move_uploaded_file($_FILES['file']['tmp_name'], $uploadFilePath);
rename($uploadFilePath, "storage/" . $r . ".xls");
$uploadFilePath = "storage/" . $r . ".xls";
$Reader = new SpreadsheetReader($uploadFilePath);
$totalSheet = count($Reader->sheets());
$tot = 0;
/* For Loop for all sheets */
for ($i = 0; $i < $totalSheet; $i++) {
$Reader->ChangeSheet($i);
$tots = $totalSheet;
foreach ($Reader as $Row) {
$tot++;
$nrcontenitore = isset($Row[0]) ? $Row[0] : '';
$nrcontenitore = str_replace(".", "", $nrcontenitore);
$nrcontenitore = str_replace("-", "", $nrcontenitore);
$nrcontenitore = str_replace("_", "", $nrcontenitore);
$nrcontenitore = str_replace("/", "", $nrcontenitore);
$nrcontenitore = str_replace("\'", "", $nrcontenitore);
$nrcontenitore = preg_replace('/\s+/', '', $nrcontenitore);
$nrcontenitore = isset($Row[0]) ? $Row[0] : '';
$data = isset($Row[1]) ? $Row[1] : '';
$doc .= formatcontainernr(strtoupper($nrcontenitore)) . " | " . date("d/m/Y", strtotime($data)) . "\n";
$docarr .= formatcontainernr(strtoupper($nrcontenitore)) . " | " . date("d/m/Y", strtotime($data)) . "#\n";
}
}
}
I would like to import a XLS file with no empty rows.
try to add a checking if the row is empty
foreach ($reader as $row) {
if($row == null){
continue;
}
}
Related
I need to give color to a specific cell in pdf which is generated from my MySQL database, How to set the color to the specific value?
I have no clue about giving color to the specific value, I there any way help me out?
my pdf generate page is:
$fdate = date('Y-m-d H:i:s', strtotime($fromdate));
$tdate = date('Y-m-d H:i:s', strtotime( $todate));
/*$channelsCol = Channels::find()
->select(['channel_name'])
->where('DeviceRefID != :id',['id' => 0 ])
->orderBy('channel_no')
->all();*/
$channelscol = \Yii::$app->db
->createCommand("select channel_name from channels where deviceRefID !=0")
->queryAll();
$pdfpages = $this->getNoOfPdfs(count($channelscol));
$noOfPages = count($pdfpages[0]);
$collist=[];
$selectlist=[];
for($i=0; $i < $noOfPages;$i++)
{
$collist[$i] = 'Select SQL_BUFFER_RESULT "LogDateTime ",';
$selectlist[$i] = 'select LogDateTime,';
}
$count =0;
$counter=1;
for($i=0;$i<$noOfPages;$i++)
{
for($j=$pdfpages[0][$i];$j<$pdfpages[1][$i];$j++)
{
$collist[$i] = $collist[$i] . '"'.$channelscol[$j-1]['channel_name'] .'",';
$selectlist[$i] = $selectlist[$i] . 'Channel'.$j.'Value,';
$count = $count + 1;
}
}
for($i=0;$i <$noOfPages ;$i++)
{
$collist[$i] = rtrim($collist[$i],",");
$selectlist[$i] = rtrim($selectlist[$i],",");
}
//$path ="/var/www/devicesadmin/yii-app/web/devicelog/devicelog" . date("Y-m-d-H-i-s").".csv";
$logfilename =[];
$logfailure="";
for($i=0;$i < $noOfPages; $i++)
{
$logfilename[$i] = $pdfpages[2][$i] . date("Y-m-d-H-i-s") . mt_rand() . ".csv";
//$path ='D:/Raffi/ProjectsPhp/DevicesAdminProject/yii-app/web/devicelog//' . $logfilename[$i];
$path = "/var/www/devicesadmin/yii-app/web/devicelog/" . $logfilename[$i];
$export =' INTO OUTFILE "' . $path . '" FIELDS TERMINATED BY "," ENCLOSED BY \'"\' LINES TERMINATED BY \'\\n\' FOR UPDATE';
$whereclause = "where LogDatetime between '" . $fdate . "' and '" .$tdate ."'";
$finalsql = $collist[$i] . ' Union all ' .$selectlist[$i] .' From devicelog '. $whereclause . $export;
try {
Yii::$app->db->createCommand($finalsql)->execute();
//file_put_contents("D:\my.txt", $this->getHTML($path, $header));
//echo html($this->getHTML($path, $header));
//exit();
$pdf=new \PDF('P','mm','Legal');
$pdf->AliasNbPages();
//$pdf=new \PDF('L','mm','Legal',130);
$pdf->AddPage();
$pdf->SetFont('helvetica','',10);
$pdf->WriteHTML($this->getHTML($path));
$logfilename[$i] = "dl" . $pdfpages[2][$i] . date("Y-m-d-H-i-s").".pdf";
//$filename = "D:/Raffi/ProjectsPhp/DevicesAdminProject\yii-app\web\devicelog" . $logfilename[$i];
$filename = "/var/www/devicesadmin/yii-app/web/devicelog/" . $logfilename[$i];
$pdf->Output('F',$filename);
unset($pdf);
} catch(Exception $e) {
}
}
If a pdf is generated, I need to give red color to the values which are above 50. But I can't get the values and give color.
Hello I am trying to push in an array using array_push but I am getting the value of the first index then after that all I am getting a null response, I am not getting where I have done a mistake.I am getting the values properly but in array_push there is some mistake which is in for loop.
Here is my code :
function actioncouponcsv_download() {
$this->layout = false;
foreach (Yii::app()->log->routes as $route) {
if ($route instanceof CWebLogRoute || $route instanceof CFileLogRoute || $route instanceof YiiDebugToolbarRoute) {
$route->enabled = false;
}
}
$dateRange = json_decode($_POST['dateRange'], true);
$start = $dateRange['start'];
$end = $dateRange['end'];
$validity = $_POST['validity'];
$limit = isset($_REQUEST['limit']) && trim($_REQUEST['limit']) ? $_REQUEST['limit'] : 0;
$studio_id = Yii::app()->user->studio_id;
if (isset($_GET['type']) && intval($_GET['type']) ==2) {
$couponobj = new CouponSubscription();
$getcouponobj = $couponobj->getcoupon_data($studio_id,$start,$end,$validity);
$k = 0;
$title_addon = "\t" . "Discount Cycle" . "\t" . "Extend free trail";
$data_addon = "\t" . $getcouponobj[$k]['discount_multiple_cycle'] . "\t" . $getcouponobj[$k]['extend_free_trail'];
} else {
$title_addon = "";
$data_addon = "";
$couponobj = new Coupon();
$getcouponobj = $couponobj->getcoupon_data($studio_id,$limit,1);
}
//$Coupon = Coupon::model()->find('studio_id=:studio_id', array(':studio_id' => $studio_id));
$dataCsv = '';
if ($getcouponobj) {
$headings = "Sl no" . "\t" . "Coupon" . "\t" . "Coupon Type" . "\t" . "Used by a single user" . "\t" . "Valid" . "\t" . "Used" . "\t" . "User" .$title_addon. "\t" . "Used Date". "\t" . "Content Category". "\t" . "Content"."\n";
$i = 1;
$dataCSV[] = Array();
$j = 0;
for ($k = 0; $k < count($getcouponobj); $k++) {
$userList = '-';
if ($getcouponobj[$k]['used_by'] != '0') {
if ($getcouponobj[$k]['coupon_type'] == 1) {
$userList = '';
$userIdList = explode(",", $getcouponobj[$k]['used_by']);
foreach ($userIdList as $userIdListKey => $userIdListVal) {
if ($userIdListKey == 0) {
$userList .= Yii::app()->webCommon->getuseremail($userIdListVal);
} else {
$userList .= " | " . Yii::app()->webCommon->getuseremail($userIdListVal);
}
}
} else {
$userList = Yii::app()->webCommon->getuseremail($getcouponobj[$k]['used_by']);
}
}
if($getcouponobj[$k]['is_all']!=1){
if($getcouponobj[$k]['content_category']==1){
$cont_cat = "Digital";
$content_str = Coupon::model()->getContentInfo($getcouponobj[$k]['specific_content']);
$cont_str = $content_str;
}else if($getcouponobj[$k]['content_category']==2){
$cont_cat = "Physical";
$content_str = Coupon::model()->getContentInfoPhysical($getcouponobj[$k]['specific_content']);
$cont_str = $content_str;
}else{
$cont_cat = "All";
$cont_str = "All";
}
}else{
$cont_cat = "All";
$cont_str = "All";
}
#echo $getcouponobj[$k]['coupon_code'];
array_push($dataCSV[$j],$i);
array_push($dataCSV[$j],$getcouponobj[$k]['coupon_code']);
array_push($dataCSV[$j],(($getcouponobj[$k]['coupon_type'] == 1) ? 'Multi-use' : 'Once-use'));
array_push($dataCSV[$j],(($getcouponobj[$k]['user_can_use'] == 1) ? 'Multiple times' : 'Once'));
array_push($dataCSV[$j],(($getcouponobj[$k]['used_by'] == 0) ? 'Yes' : 'No'));
array_push($dataCSV[$j],(($getcouponobj[$k]['used_by'] == 0) ? '-' : 'Yes'));
array_push($dataCSV[$j],$userList);
array_push($dataCSV[$j],$getcouponobj[$k]['discount_multiple_cycle']);
array_push($dataCSV[$j],$getcouponobj[$k]['extend_free_trail']);
array_push($dataCSV[$j],(($getcouponobj[$k]['cused_date'] == 0) ? '-' : $getcouponobj[$k]['cused_date']));
array_push($dataCSV[$j],$cont_cat);
array_push($dataCSV[$j],$cont_str);
$j++;
//$dataCsv .= $i . "\t" . $getcouponobj[$k]['coupon_code'] . "\t" . (($getcouponobj[$k]['coupon_type'] == 1) ? 'Multi-use' : 'Once-use') . "\t" . (($getcouponobj[$k]['user_can_use'] == 1) ? 'Multiple times' : 'Once') . "\t" . (($getcouponobj[$k]['used_by'] == 0) ? 'Yes' : 'No') . "\t" . (($getcouponobj[$k]['used_by'] == 0) ? '-' : 'Yes') . "\t" . $userList .$data_addon. "\t" . (($getcouponobj[$k]['cused_date'] == 0) ? '-' : $getcouponobj[$k]['cused_date'])."\t" .$cont_cat ."\t".$cont_str."\n";
$i = $i+1;
}
}
print_r(json_encode($dataCSV));
}
PS: I am getting the values. Any help will be highly appreciated.
Well, first thing i see wrong is the way you declare your array:
$dataCSV[] = Array();
$array[] = Means that you are adding a new value to an existing array. To declare your array you should use
$dataCSV = array();
Also, this code:
array_push($dataCSV[$j],$i);
means that you are adding a new value to your $dataCSV[$j] array, but this is never declared as an array, so first thing would be to do
$dataCSV[$j] = new array();
Your code is really long and complicated, those are only examples of issues i see in there.
I am using XMLReader to import huge xml file by elements to MySQL database. Xml contains 1 547 772 tags (element) named 'RECORD'.
XML example
<?xml version="1.0" encoding="utf-8"?>
<RECORD><NAME>ДОШКІЛЬНИЙ НАВЧАЛЬНИЙ ЗАКЛАД №1 ЗАГАЛЬНОГО РОЗВИТКУ УЖГОРОДСЬКОЇ МІСЬКОЇ РАДИ ЗАКАРПАТСЬКОЇ ОБЛАСТІ</NAME><SHORT_NAME>ДНЗ №1</SHORT_NAME><EDRPOU>34888585</EDRPOU><ADDRESS>88000, Закарпатська обл., місто Ужгород, ВУЛИЦЯ М.ВОВЧКА, будинок 47, "А"</ADDRESS><BOSS>НАКОНЕЧНА ОЛЕНА АНАТОЛІЇВНА</BOSS><KVED>85.10 Дошкільна освіта</KVED><STAN>зареєстровано</STAN><FOUNDERS><FOUNDER>УПРАВЛІННЯ ОСВІТИ УЖГОРОДСЬКОЇ МІСЬКОЇ РАДИ, розмір внеску до статутного фонду - 0.00 грн.</FOUNDER>...</FOUNDERS></RECORD>...
For MySQL connection use
function connectBase(){
include __DIR__ . '/../../settings/sql.set.php';
$mysql = mysqli_connect($_sqlhost, $_sqluser, $_sqlpass, $_sqldb);
mysqli_query($mysql, "Set charset utf8");
mysqli_query($mysql, "Set character_set_client = utf8");
mysqli_query($mysql, "Set character_set_connection = utf8");
mysqli_query($mysql, "Set character_set_results = utf8");
mysqli_query($mysql, "Set collation_connection = utf8_general_ci");
return $mysql;
}
Main function for parsing
function XMLReaderToDB($setting = false)
{
$mysql = connectBase();
$dir = __DIR__ . '/../../tmp/';
$xmlURL = $dir . $setting['file'];
$xml = new XMLReader();
$xml->open($xmlURL);
$start_time = time();
$start = $setting['start'];
$limit = $setting['limit'];
$stop = $start + $limit;
$i = 0;
$count = 0;
$result = 1;
while($xml->read())
{
if ($xml->nodeType == XMLReader::ELEMENT && $xml->name == $setting['tag']) {
$item[] = "('items', '" . mysqli_real_escape_string($mysql, $xml->readOuterXML()) . "')";
}
if ($xml->nodeType == XMLReader::END_ELEMENT && $xml->name == $setting['tag']) {
$i++;
$count++;
if ($count >= 500) {
insertXMLtoDB($mysql, $item);
$item = array();
$count = 0;
}
}
if($i == $stop){
break;
}
}
$xml->close();
insertXMLtoDB($mysql, $item);
$mysql->close();
$end_time = time();
$time_elapsed_secs = $end_time - $start_time;
echo '<br/>Items: ' . $i . '<br/>';
echo 'Start: ' . date('H:i:s', $start_time) . '<br/>';
echo 'End: ' . date('H:i:s', $end_time) . '<br/>';
echo $time_elapsed_secs . ' sec. (' . ($time_elapsed_secs /60) . ' min.)';
die;
}
And for MySQL insert
function insertXMLtoDB($mysql, $data = false){
mysqli_query($mysql,"INSERT INTO _parse_tmp (parse_key, parse_value) VALUES " . implode(", ", $data));
$data = array();
// echo 'Success';
}
But, MySQL requests don't stop after 1 547 772 inserts and "while" continues to run. I notice if uncommenting echo 'Success'; in insertXMLtoDB function, "while" is stopping at 1 547 772 inserts and finishing correctly.
What is wrong in my functions?
I've never personally used XMLReader however, I'd assume you could try to add another condition to your statement such as while($xml->read() && $i != $stop) opposed to having it within your loop directly. Ah also just noticed after your usage of the function insertXMLtoDB you re-declare $data although you'll notice you set it to NULL every time your call the function, therefore setting it to an array is pointless.
I can't understand why the script doesn't work fine. That's why I decided to change it.
function XMLReaderToDB($setting = false)
{
$mysql = connectBase();
$dir = __DIR__ . '/../../tmp/';
$xmlURL = $dir . $setting['file'];
$xml = new XMLReader();
$xml->open($xmlURL);
$start_time = time();
$start = $setting['start'];
$limit = $setting['limit'];
$stop = $start + $limit;
$i = 0;
$count = 0;
while (($valid = $xml->read()) && $xml->name !== $setting['tag']) ;
while ($valid) {
$i++;
$count++;
$item[] = "('" . $setting['type'] . "', '" . mysqli_real_escape_string($mysql, $xml->readOuterXML()) . "')";
if ($count == 500) {
insertXMLtoDB($mysql, $item);
$item = array();
$count = 0;
}
$valid = $xml->next($setting['tag']);
}
$xml->close();
insertXMLtoDB($mysql, $item);
$mysql->close();
$end_time = time();
$time_elapsed_secs = $end_time - $start_time;
echo '<br/>Items: ' . $i . '<br/>';
echo 'Start: ' . date('H:i:s', $start_time) . '<br/>';
echo 'End: ' . date('H:i:s', $end_time) . '<br/>';
echo $time_elapsed_secs . ' sec. (' . ($time_elapsed_secs /60) . ' min.)';
return;
}
I have this function and statements running fine in my page:
$name = "JÜRGEN RÜDIGER HERMANN";
function nameReplace($name)
{
$name = str_replace("Ñ", "N", $name);
$name = str_replace("Ü", "U", $name);
$name = str_replace("Ç", "C", $name);
$name = str_replace("Á", "A", $name);
$name = str_replace("É", "E", $name);
return $name;
}
$name = nameReplace($name);
echo "This is the name!" . $name . "<br>";
$orename = "ÑÜÇÁÉ";
$orename = nameReplace($orename);
echo "Forename test: " . $orename . "<br>";
$astname = "ÁÉÑÜÇ";
$astname = nameReplace($astname);
echo "Lastname test: " . $astname . "<br>";
Everything about works fine, the characters are changed to acceptable characters for upload into a progress DB. But later in the page I'm looping through data and taking strings from a parsed HTML file:
$familyname = nameReplace($familyname);
The above familyname nameReplace() fails. If I echo out just family name it echoes out the string. If I echo out nameReplace($familyname) it returns blank or false.
Any ideas? Do functions have a specific scope?
Thanks,
Stephen.
Below is the code for the loop as requested:
for($i=1;$i<41;$i++)
{
$textfile = 'interpol_create' . $i. '_' . $date . '.txt';
$myfile = fopen($textfile, "r") or die("Unable to open file!");
if($myfile)
{
#read through the file line by line
while (($buffer = fgets($myfile, 4096)) !== false)
{
$linenumber++;
if( (strstr($buffer, '<span')) && (strstr($buffer, 'titre')))
{
$buffer = str_replace('<br />', '|', $buffer);
#echo $buffer . '<br>';
$result = explode('|', $buffer);
#family name variable
$familyname = $result[0];
$familyname = str_replace('<span class="titre">', '', $familyname);
$familyname = trim($familyname);
$familyname = nameReplace($familyname);
#first name variable
$forename = $result[1];
$forename = nameReplace($forename);
if(strstr($forename, "RGEN"))
{
echo "Forename is ".nameReplace((string)$forename)."<br>";
$newVar = "JÜRGEN RÜDIGER";
$newVar = nameReplace($newVar) . "<br>";
echo "New variable = " . $newVar . "<br>";
}
$forename = str_replace("</span>", "", $forename);
$forename = trim($forename);
#echo ($namecounter + 1) . ' | ';
#echo 'Family name : ' . $familyname . ' | Forename is : ' . $forename . '<br>';
$update_full_file .= $forename . " " . $familyname;
$firstnames[$namecounter] = $forename;
$lastnames[$namecounter] = $familyname;
}#end of if strstr $buffer
if( (strstr($buffer, 'Age today :')) && (strstr($buffer, '<br')))
{
$buffer = str_replace('<br />', '', $buffer);
$buffer = str_replace('Age today :', '', $buffer);
$agetoday = $buffer;
$agetoday = str_replace("<br/>", "", $agetoday);
$agetoday = trim($agetoday);
#echo 'Age Today : ' . $agetoday . '<br>';
$ages[$namecounter] = $agetoday;
$update_full_file .= ",," . $agetoday;
$nextlinenumber = $linenumber + 2;
$nationalitylinenumbers[$namecounter] = $nextlinenumber;
#echo $nationcounter . " " . $nextlinenumber . '<br>';
}
if( (strstr($buffer, 'Nationality :')) )
{
#$buffer = str_replace('<br />', '', $buffer);
#$buffer = str_replace('Age today :', '', $buffer);
#echo 'Nationality : ' . $buffer . '<br>';
#echo 'Line number : ' . $linenumber . '<br>';
}
if( in_array($linenumber, $nationalitylinenumbers) )
{
#echo $nationcounter . $buffer . '<br>';
#$nationcounter++;
}
###does the current linenumber appear in the array $nationalitylinenumbers?###
if( in_array($linenumber, $nationalitylinenumbers) )
{
$nation = $buffer;
$nation = str_replace("<br/>", "", $nation);
$nation = str_replace("</div>", "", $nation);
$nation = str_replace(" ", "", $nation);
#$nation = str_replace(",", "", $nation);
$nation = str_replace('<divclass="sep">', 'No Country', $nation);
$nation = trim($nation);
if(strstr($nation, ','))
{
#echo 'Create a duplicate entry';
$nations = explode(',', $nation);
$nation = $nations[0];
$nation = trim($nation);
$duplicateNation = $nations[1];
$duplicates[$duplicate_counter] .= $forename . " " .$familyname . ",," . $agetoday . ",,,,,". trim($duplicateNation) .",,,,,\n";
$duplicate_counter++;
}
#$nation = countryMappings($nation);
###check to see if entry has 2 countries###
#echo $nationcounter . $nation . '<br>';
$nationcounter++;
$update_full_file .= ",,,,," .$nation. ",,,,,\n";
$nationalitys[$namecounter] = $nation;
#echo $buffer.'<br>';
$namecounter++;
}
}#end of while buffer = fgets
}#end of if myfile
}
I managed to fix this!
The issue was I was translating the wrong data. I parsed in the 'Ü' character and was using:
$name = str_replace("Ü", "U", $name);
I exported the data to a text file and found that the character is actually exporting as Ãœ instead of Ü. I amended my function to be as below and it now works:
$name = str_replace("Ü", "U", $name);
Thanks again to all who responded, all the tips were very helpful.
i have problem in my website in this link :-
http://www.3-hail.com/API/news.php
i get this error message :-
error on line 2 at column 6: XML declaration allowed only at the start of the document
when i try to connect with this file news.php
<?php
require_once("config.php");
$result = mysql_query("select * from newsm WHERE news_id='1' ORDER BY id Desc Limit 30");
while ($row = #mysql_fetch_array($result)) {
$title[] = $row['caption'];
$Image[] = $row['ext'];
$postID[] = $row['id'];
$Textnews[] = $row['text'];
$containing[] = $row['text'];
$post_date[] = $row['date'];
}
function sublen($text,$of,$to){
if(strlen($text) > $to){
echo substr($text,$of,$to)." ...";
}else{
echo $text;
}
}
$count = count($title);
for ( $e = 0; $e <= $count-1; $e += 1) {
}
echo '<?xml version="1.0"?>
<rss version="2.0">
<channel>
<title>صحيفة عيون حائل</title>
<link>http://3-hail.com/</link>
<description>صحيفة عيون حائل</description>';
for ( $i = 0; $i <= $count-1; $i += 1) {
$word = array(" ","[/CENTER]","[/JUSTIFY]","[CENTER]","[JUSTIFY]","[email]","[/email]","[/url]","[url]","[B]","[/B]","[IMG]","[/IMG]");
$text = $containing[$i];
$replace = "";
$containingNews = str_replace($word,$replace,$text);
echo '<item>
<title>'.$title[$i] = iconv("windows-1256", "UTF-8", $title[$i]).'</title>
<description>'.$substring = mb_substr($Textnews[$i] = iconv("windows-1256", "UTF-8", $Textnews[$i]),0,100,"utf-8").'...</description>
<containing>'.$containingNews = htmlspecialchars(strip_tags(iconv("windows-1256", "UTF-8", $containingNews))).'</containing>
<link>http://3-hail.com/news.php?action=show&id=' . $postID[$i] . '</link>
<enclosure>http://3-hail.com/contents/newsm/' . $postID[$i] . '.' . $Image[$i] . '</enclosure>
<enclosurem>http://3-hail.com/contents/newsm/' . $postID[$i] . '.' . $Image[$i] . '</enclosurem>
<bubdate>' . date('H:i d-m-Y', $post_date[$i] ) . '</bubdate>
</item>';
}
echo'</channel>
</rss>';
?>