Jump to content

Repeat query


nerd99

Recommended Posts

Hi,

 

I am very new to this. Your help would be greatly appreciated. This has also been posted on the phpexcel discussion page.

 

I would like to know how to export data from a MySql table into an excel spreadsheet. I have the knowledge to get the information from the database to the spreadsheet but how do I have the code repeat until all data is echoed onto the page?

 

Here is what I have already. This produces only a single row of results from the database...just need to know how to get it all onto the spreadsheet!

 

Using php logic, how would I get around this?

 

<?php

/** Error reporting */
error_reporting(E_ALL);

/** PHPExcel */
require_once '../Classes/PHPExcel.php';


// Create new PHPExcel object
$objPHPExcel = new PHPExcel();

require_once('../includes/connection.php');

$result = mysqli_query($connection,"SELECT * FROM students ORDER BY school ASC")
or die(mysqli_error($connection));

while($row = mysqli_fetch_array($result)) {

//get all rows you want from the tutors table

$school = $row['school'];

}

// Set properties
$objPHPExcel->getProperties()->setCreator("Somebody")
						 ->setLastModifiedBy("Somebody")
						 ->setTitle($school."Student List")
						 ->setSubject($school."Student List")
						 ->setDescription("Student List at ".$school)
						 ->setKeywords("Student List")
						 ->setCategory("Student List");


// Add some data

$result = mysqli_query($connection,"SELECT * FROM students ORDER BY school ASC")
or die(mysqli_error($connection));

while($row = mysqli_fetch_array($result)) {

//get all rows you want from the tutors table

$id = $row['id'];
$full_name = $row['full_name'];
$school = $row['school'];
$year = $row['school_year'];
$class = $row['class'];
$contact = $row['contact'];
$phone_h = $row['phone_h'];
$phone_m = $row['phone_m'];
$tutor_1 = $row['tutor_1'];

$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', $full_name)
            ->setCellValue('B1', $contact)
            ->setCellValue('C1', $year.$class)
            ->setCellValue('D1', $tutor_1);

}

// Rename sheet
$objPHPExcel->getActiveSheet()->setTitle('Simple');


// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);


// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="01simple.xls"');
header('Cache-Control: max-age=0');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
?>

Link to comment
Share on other sites

It is really only this part of the code that I am working with...thanks.

// Add some data

$result = mysqli_query($connection,"SELECT * FROM students ORDER BY school ASC")
or die(mysqli_error($connection));

while($row = mysqli_fetch_array($result)) {

//get all rows you want from the tutors table

$id = $row['id'];
$full_name = $row['full_name'];
$school = $row['school'];
$year = $row['school_year'];
$class = $row['class'];
$contact = $row['contact'];
$phone_h = $row['phone_h'];
$phone_m = $row['phone_m'];
$tutor_1 = $row['tutor_1'];

$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', $full_name)
            ->setCellValue('B1', $contact)
            ->setCellValue('C1', $year.$class)
            ->setCellValue('D1', $tutor_1);

}

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.