I am using the following code to generate excel from sql through php.this is a modeifed verion of the working code ..
WORKING CODE (NO ERROR)
<?php
//documentation on the spreadsheet package is at:
//http://pear.php.net/manual/en/package.fileformats.spreadsheet-excel-writer.php
chdir('phpxls');
require_once 'Writer.php';
chdir('..');
$sheet1 = array(
array('eventid','eventtitle' ,'datetime' ,'description' ,'notes' ),
array('5' ,'Education Seminar','2010-05-12 08:00:00','Increase your WPM','' ),
array('6' ,'Work Party' ,'2010-05-13 15:30:00','Boss\'s Bday' ,'bring tacos'),
array('7' ,'Conference Call' ,'2010-05-14 11:00:00','access code x4321','' ),
array('8' ,'Day Off' ,'2010-05-15' ,'Go to Home Depot' ,'' ),
);
$sheet2 = array(
array('eventid','funny_name' ),
array('32' ,'Adam Baum' ),
array('33' ,'Anne Teak' ),
array('34' ,'Ali Katt' ),
array('35' ,'Anita Bath' ),
array('36' ,'April Schauer'),
array('37' ,'Bill Board' ),
);
$workbook = new Spreadsheet_Excel_Writer();
$format_und =& $workbook->addFormat();
$format_und->setBottom(2);//thick
$format_und->setBold();
$format_und->setColor('black');
$format_und->setFontFamily('Arial');
$format_und->setSize(8);
$format_reg =& $workbook->addFormat();
$format_reg->setColor('black');
$format_reg->setFontFamily('Arial');
$format_reg->setSize(8);
$arr = array(
'Calendar'=>$sheet1,
'Names' =>$sheet2,
);
foreach($arr as $wbname=>$rows)
{
$rowcount = count($rows);
$colcount = count($rows[0]);
$worksheet =& $workbook->addWorksheet($wbname);
$worksheet->setColumn(0,0, 6.14);//setColumn(startcol,endcol,float)
$worksheet->setColumn(1,3,15.00);
$worksheet->setColumn(4,4, 8.00);
for( $j=0; $j<$rowcount; $j++ )
{
for($i=0; $i<$colcount;$i++)
{
$fmt =& $format_reg;
if ($j==0)
$fmt =& $format_und;
if (isset($rows[$j][$i]))
{
$data=$rows[$j][$i];
$worksheet->write($j, $i, $data, $fmt);
}
}
}
}
$workbook->send('test.xls');
$workbook->close();
//-----------------------------------------------------------------------------
?>
MODIFIED CODE TO RETRIEVE DATA FROM DATABASE (WHICH SHOWS ERROR)
<?php
$numrow12 = mysql_query("SELECT * FROM mastertable WHERE pdfstatus = 1 ORDER BY tstamp DESC");
mysql_select_db("brainoidultrafb", $link);
chdir('phpxls');
require_once 'Writer.php';
chdir('..');
while($row = mysql_fetch_array($numrow12)) {
$sheet1 = array(
array('StudentID','Students Email' ,'Diagnosis','TimeStamp' ,),
array($row['student_id'] ,$row['email_id'],$row['diagnosis'],$row['tstamp'],),
);
}
$workbook = new Spreadsheet_Excel_Writer();
$format_und =& $workbook->addFormat();
$format_und->setBottom(2);//thick
$format_und->setBold();
$format_und->setColor('black');
$format_und->setFontFamily('Arial');
$format_und->setSize(8);
$format_reg =& $workbook->addFormat();
$format_reg->setColor('black');
$format_reg->setFontFamily('Arial');
$format_reg->setSize(8);
$arr = array(
'Calendar'=>$sheet1,
);
foreach($arr as $wbname=>$rows)
{
$rowcount = count($rows);
$colcount = count($rows[0]);
$worksheet =& $workbook->addWorksheet($wbname);
$worksheet->setColumn(0,0, 6.14);//setColumn(startcol,endcol,float)
$worksheet->setColumn(1,3,15.00);
$worksheet->setColumn(4,4, 8.00);
for( $j=0; $j<$rowcount; $j++ )
{
for($i=0; $i<$colcount;$i++)
{
$fmt =& $format_reg;
if ($j==0)
$fmt =& $format_und;
if (isset($rows[$j][$i]))
{
$data=$rows[$j][$i];
$worksheet->write($j, $i, $data, $fmt);
}
}
}
}
$workbook->send('Submissions.xls');
$workbook->close();
//-----------------------------------------------------------------------------
?>
Its showing the following errors
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /home/content/58/9508458/html/psychiatric/subexcel.php on line 13
Warning: Cannot modify header information - headers already sent by (output started at /home/content/58/9508458/html/psychiatric/subexcel.php:13) in /home/content/58/9508458/html/psychiatric/phpxls/Writer.php on line 67
Warning: Cannot modify header information - headers already sent by (output started at /home/content/58/9508458/html/psychiatric/subexcel.php:13) in /home/content/58/9508458/html/psychiatric/phpxls/Writer.php on line 68
Warning: Cannot modify header information - headers already sent by (output started at /home/content/58/9508458/html/psychiatric/subexcel.php:13) in /home/content/58/9508458/html/psychiatric/phpxls/Writer.php on line 69
Warning: Cannot modify header information - headers already sent by (output started at /home/content/58/9508458/html/psychiatric/subexcel.php:13) in /home/content/58/9508458/html/psychiatric/phpxls/Writer.php on line 70
Warning: Cannot modify header information - headers already sent by (output started at /home/content/58/9508458/html/psychiatric/subexcel.php:13) in /home/content/58/9508458/html/psychiatric/phpxls/Writer.php on line 71
Right now, you're selecting the database after you try to query the database:
$numrow12 = mysql_query("SELECT * FROM mastertable WHERE pdfstatus = 1 ORDER BY tstamp DESC");
mysql_select_db("brainoidultrafb", $link);
That won't work. Just swap the order so you select the database, then query it:
mysql_select_db("brainoidultrafb", $link);
$numrow12 = mysql_query("SELECT * FROM mastertable WHERE pdfstatus = 1 ORDER BY tstamp DESC");
As for your second problem, you're overwriting $sheet1 on each iteration of the loop. Do this instead:
$sheet1 = array(
array('StudentID','Students Email' ,'Diagnosis','TimeStamp' ,)
);
while($row = mysql_fetch_array($numrow12)) {
$sheet1[] = array($row['student_id'], $row['email_id'], $row['diagnosis'], $row['tstamp'],);
}
For your second problem use:
$sheet1[] =
To append elements to the $sheet1 array. You're currently replacing it after every iteration of the loop.
You'll probably want to initialize $sheet1 outside of the while loop to avoid warnings as well. $sheet1 = array();
Related
Currently, I created a system that uses daypilot calendar. For example, when I clicked a date, it will show the list of activities on that selected date.
Below code is successful display the list:
<?php
require_once '../../../config/configPDO.php';
$Fac_ID = strtolower($_GET['Fac_ID']);
$Room_Desc = strtolower($_GET['Room_Desc']);
$stmt = $conn->prepare("SELECT * FROM booking LEFT JOIN room ON booking.Room_ID = room.Room_ID
WHERE room.Fac_ID = '$Fac_ID' AND room.Room_Desc = '$Room_Desc' AND Book_Status <> 'Reject'");
$stmt->execute();
$result = $stmt->fetchAll();
class Event {}
$events = array();
foreach($result as $row) {
$e = new Event();
$e->id = $row['Book_No'];
$e->text = "Meeting Name: ". $row['Meeting_Description']. " || " ."Requested by: ".$row['Requested_by']. " || " ."Location: ".$row['Fac_ID']. ", " .$row['Room_Desc'];
$e->start = $row['StartTime'];
$e->end = $row['EndTime'];
$events[] = $e;
}
header('Content-Type: application/json');
echo json_encode($events);
?>
But, when I change the code (Because want to retrieve json data), the data doesn't display at calendar.
Below is the code:
<?php
require_once '../../../config/configPDO.php';
$Fac_ID = strtolower($_GET['Fac_ID']);
$Room_Desc = strtolower($_GET['Room_Desc']);
//retrieve json
$url = "http://172.20.0.45/TGWebService/TGWebService.asmx/displayPABooking?Fac_ID=$Fac_ID&Room_Desc=$Room_Desc&Room_ID=&Book_No=&Book_Date_From=&Book_Date_To=";
$data = file_get_contents($url); //line 10
$json = json_decode($data);
$result = $json->bookingList; //line 12
class Event {}
$events = array(); //
foreach($result as $row) { //line 17
$e = new Event();
$e->id = $row->bookNo;
$e->text = "Meeting Name: ".$row->desc. " || " ."Requested by: ".$row->requestedBy." || " ."Location: ".$row->facID.", " .$row->roomName;
$e->start = $row->startTime;
$e->end = $row->endTime;
$events[] = $e;
}
header('Content-Type: application/json');
echo json_encode($events);
?>
And, This is the error that I found at console:
PHP Warning: file_get_contents(http://172.20.0.45/TGWebService/TGWebService.asmx/displayPABooking?Fac_ID=TGT&Room_Desc=LEVEL 1 &Room_ID=&Book_No=&Book_Date_From=&Book_Date_To=): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request
in C:\inetpub\wwwroot\ebooking\pages\manual_booking\backend\backend_events.php on line 10
PHP Notice: Trying to get property 'bookingList' of non-object in C:\inetpub\wwwroot\ebooking\pages\manual_booking\backend\backend_events.php on line 12
PHP Warning: Invalid argument supplied for foreach() in C:\inetpub\wwwroot\ebooking\pages\manual_booking\backend\backend_events.php on line 17
Can I know where I need to change? Thanks
This is probably because of the space in "Level 1". Try urlencoding your $_GET parameters:
$url = "http://172.20.0.45/TGWebService/TGWebService.asmx/displayPABooking?Fac_ID=" . urlencode($Fac_ID) . "&Room_Desc=" . urlencode($Room_Desc) . "&Room_ID=&Book_No=&Book_Date_From=&Book_Date_To=";
If you want to make the variable substitution more explicit, you could use strtr:
$template = "http://172.20.0.45/TGWebService/TGWebService.asmx/displayPABooking?Fac_ID={Fac_ID}&Room_Desc={Room_Desc}&Room_ID=&Book_No=&Book_Date_From=&Book_Date_To=";
$url = strtr($template, array(
'{Fac_ID}' => $Fac_ID,
'{Room_Desc}' => $Room_Desc
));
I'm try to update data in oracle table from excel file (.xlsx) use CodeIgniter. I've already uploaded the excel file but when I try to update the data I get the following error message:
Message: Array to string conversion
Filename: database/DB_driver.php
Line Number: 1524
Backtrace:
File: C:\xampp\htdocs\web_excel_ci_test\application\models\RoadmapModel.php
Line: 84
Function: update
File: C:\xampp\htdocs\web_excel_ci_test\application\controllers\Roadmap.php
Line: 209
Function: update_data
Controller:
function update(){
$this->load->library('session');
$fileName = $this->session->flashdata('fileName');
$fileName2 = $this->session->flashdata('fileName2');
include APPPATH.'third_party/PHPExcel/PHPExcel.php';
$excelreader = new PHPExcel_Reader_Excel2007();
$loadexcel = $excelreader->load('excel/'.$fileName);
$sheet = $loadexcel->getActiveSheet()->toArray(null, true, true ,true);
$data = [];
$numrow = 1;
foreach($sheet as $row){
if($numrow > 1){
array_push($data, [
'YEAR'=>$row['A'],
'PROVINCEID'=>$row['B'],
'PROVINCE'=>$row['C'],
'PLAN_A'=>$row['D'],
'ACTUAL_A'=>$row['E'],
]);
}
$numrow++;
}
$year = $this->input->post('YEAR');
$this->RoadmapModel->update_data($year, $fileName2, $data);
redirect("Roadmap");
}
Model:
function update_data($year, $fileName2, $data){
for ($i=0; $i < count($year) ; $i++) {
$this->db->where('YEAR', $year[$i]);
$this->db->update($fileName2, $data);
}
}
i think your $data is a multidimentional array, try to use
$this->db->update_batch
to process batch update. You can also look here to for more options.
I am unable to export my mysql table to XML format using PHP, I receive the following error when I run the below script. I have removed the connection bit, I have gone through already errors but was not able resolve this issue.
Catchable fatal error: Argument 1 passed to DOMNode::appendChild() must be an instance of DOMNode, null given,
Script:
<?php
$query = "SELECT id, name, plate, datetime , time , image_name FROM EarlsdonMSIN_anpr_vega";
$camArray = array();
if ($result = $mysqli->query($query)) {
/* fetch associative array */
while ($row = $result->fetch_assoc()) {
array_push($camArray, $row);
}
if(count($camArray)){
createXMLfile($camArray);
}
/* free result set */
$result->free();
}
/* close connection */
$mysqli->close();
function createXMLfile($camArray){
$filePath = '/cam.xml';
$dom = new DOMDocument('1.0', 'utf-8');
$root = $dom->createElement('cam');
for($i=0; $i<count($camArray); $i++){
$camId = $camArray[$i]['id'];
$camName = $camArray[$i]['name'];
$camplate = $camArray[$i]['plate'];
$camdatetime = $camArray[$i]['datetime'];
$camtime = $camArray[$i]['time'];
$camimagename = $camArray[$i]['image_name'];
$cam = $dom->createElement('cam');
$cam->setAttribute('id', $camId);
$name = $dom->createElement('name', camName);
$cam->appendChild($name);
$plate = $dom->createElement('plate', $camplate);
$cam->appendChild($plate);
$datetime = $dom->createElement('datetime', $camdatetime);
$cam->appendChild($datetime);
$time = $dom->createElement('time', $camtime);
$cam->appendChild($time);
$imagename = $dom->createElement('image_name', $camimagename); $cam->appendChild($image_name);
$root->appendChild($cam);
}
$dom->appendChild($root);
$dom->save($filePath);
}
I use php 5.5 and i'm receiving Json Parser error because of the Logcat message :
<br />
<b>Deprecated</b>: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in <b>/home4/enippeas/public_html/select_itin2.php</b> on line <b>2</b><br />
I unsterstand that i have to move to pdo ( note that my app is for educational purpose only) but trying to avoid receiving errors i try to use
error_reporting(0);
but i continue to get the same error. Any idea;
php file
<?php
$con=mysql_connect("localhost","enippeas_enippea","tiramola48" );
$database = "enippeas_etruck1";
$ok = mysql_select_db($database, $con);
mysql_set_charset("utf8",$con);
error_reporting(0);
$us1 = $_POST['username1'];
$sp1 = $_POST['startPoli1'];
$fp1 = $_POST['finalPoli1'];
$w1 = $_POST['weight1'];
$em1 = $_POST['eidosmetaf1'];
$dD1 = $_POST['depDate1'];
$dT1 = $_POST['depTime1'];
$sql = mysql_query( " SELECT `onoma01` , `epitheto01` , `email01` ,`username1`,`startPoli1`, `finalPoli1`, `eidosmetaf1`, `weight1` , `depDate1` , `depTime1`, `tilefono01`
FROM customer ,registration1
where
( '$sp1'='empty' or customer.startPoli1 = '$sp1') and
( '$w1'='empty' or customer.weight1 <= '$w1') and
( '$em1'='empty' or customer.eidosmetaf1 = '$em1') and
( '$fp1'='empty' or customer.finalPoli1 = '$fp1') and
( '$dD1'='empty' or customer.depDate1 = '$dD1') and
(customer.username1 = registration1.username01 )");
if($sql === FALSE)
{
die(mysql_error());
}
$results = array();
while($row = mysql_fetch_assoc($sql))
{
$results[] = array(
'onoma' => $row['onoma01'],
'epitheto' => $row['epitheto01'],
'email' => $row['email01'],
'username1' => $row['username1'],
'startPoli1' => $row['startPoli1'],
'finalPoli1' => $row['finalPoli1'],
'eidosmetaf1' => $row['eidosmetaf1'],
'weight1' => $row['weight1'],
'depDate1' => $row['depDate1'],
'depTime1' => $row['depTime1'],
'tilefono1' => $row['tilefono01']
);
}
echo json_encode(array('select_itin_results' =>$results));
mysql_close($con);
?>
Perhaps error_reporting(0); needs to go to the top of the file.
error_reporting is used to determine the level of errors which are reported. If you simply do not want errors to be displayed on the page, you are better to use:
ini_set("display_errors", 0);
This way the errors are not ignored and can still go into log files etc, they just aren't output to your page.
we are using PostGreSql database, when we run following code unit, it shows error of.
Severity: Warning
Message: Illegal string offset 'server'
Filename: postgre/postgre_driver.php
Message: Cannot modify header information - headers already sent by (output started at system/core/Exceptions.php:185)
Model Code:
public function tracks_add( $id ) {
$cnt = 0;
$date = date('Y-m-d H:i:s');
$s_title = $this->input->post('s_title');
$s_singer = $this->input->post('s_singer');
$s_url = $this->input->post('s_url');
foreach ($s_title as $s_title) {
$this->db->set( array('a_id'=> $id, 's_title' => $s_title, 's_singer' => $s_singer[$cnt], 's_url' => $s_url[$cnt], 'date'=> $date) );
$this->db->insert('soundtracks');
$cnt++;
}
}
You can find this issue here :
Postgres db driver insert issue
Change line 331 to
$v = pg_version($this->conn_id); $v = isset($v['server']) ? $v['server'] : 0; // 'server' key