0

hello folks

I'm a greenhorn in PHP so excuse me if i'm being dumb.

I have set up a page that is pulling records out of a database table, the user viewing the page is intended the choose a number of the records on the page (using checkboxes) and download as CSV to use for referencing.The problem is that i dont know the technique to do this.

the code looks like this

<?php

include 'data.php';

try{
    $con = new PDO('mysql:host=localhost;dbname=abc', $user, $pass);
    $con->SetAttribute (PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        $sql=$con->prepare('select id, title, author, secondary_title, year, volume, number, pages from refs');
        $sql->execute();

        $sql->setFetchMode(PDO::FETCH_ASSOC);?>
<!doctype html>
<html style="width:100%;height:100%">

<head>


<title>Scholar</title>
<link href="authors.css" rel="stylesheet" type="text/css"/>

</head>
<body>

<div id = "container">
<form action = "<?Php echo $_SERVER['PHP_SELF'];?>" method = "post">
<?php

    while ($row = $sql->fetch()){
        $id = $row ['id'];
        $title = $row ['title'];
        $author = $row ['author'];
        $secondary_title = $row ['secondary_title'];
        $year = $row ['year'];
        $volume = $row ['volume'];
        $number = $row ['number'];
        $pages = $row ['pages'];
        //$ref = $row["'$volume' . '($number)' . '$pages'"];

        echo "<input type = 'checkbox' name='checkbox[]' value=''>$id <a href =''>$title \n </a> </br>  $author \n </br> $secondary_title $year $volume($number):$pages </br><p>";
        }?>
    <input type = "submit"   name="submit">
</div>
</body><?php
}

catch (PDOException $e){
echo 'ERROR:'. $e->getMessage();

}
$con = null;
?>

you can view the attached thumbnail.

Attachments results.jpg 29.19 KB results.jpg 29.19 KB
5
Contributors
11
Replies
61
Views
4 Years
Discussion Span
Last Post by Amaina
Featured Replies
  • Here is what I would do to improve your code a bit. database data output: echo '<table border="1"> <thead> <tr> <th>&nbsp;</th> <th>ID</th> <th>TITLE</th> <th>AUTHOR</th> <th>SECONDARY TITLE</th> <th>YEAR</th> <th>VOLUME</th> <th>NUMBER</th> <th>PAGES</th> </tr> </thead> <tbody>'; while($row = $sql->fetch()){ echo '<tr>'; echo '<td><input type="checkbox" name="checkbox[]" value="'.$row['id'].'" /></td>'; echo '<td>'.$row['title'].'</td>'; echo '<td>'.$row['author'].'</td>'; echo '<td>'.$row['secondar_title'].'</td>'; … Read More

0

So the user has to check some checkboxes, of which the values will then be converted to .csv and offered as a download? Or are there multiple .csv's available for download, and does the user have to select which of those he wants to download?

Edited by minitauros

0
  • The user has to check some (random) checkboxes, of which the values will then be converted to .csv and offered as a download.

  • The user can choose not to download. Store the selected values temporarily in a variable/session.

*

1

Here is what I would do to improve your code a bit.

database data output:

echo '<table border="1">
            <thead>
                <tr>
                    <th>&nbsp;</th>
                    <th>ID</th>
                    <th>TITLE</th>
                    <th>AUTHOR</th>
                    <th>SECONDARY TITLE</th>
                    <th>YEAR</th>
                    <th>VOLUME</th>
                    <th>NUMBER</th>
                    <th>PAGES</th>
                </tr>
            </thead>
            <tbody>';
            while($row = $sql->fetch()){
                echo '<tr>';
                echo '<td><input type="checkbox" name="checkbox[]" value="'.$row['id'].'" /></td>';
                echo '<td>'.$row['title'].'</td>';
                echo '<td>'.$row['author'].'</td>';
                echo '<td>'.$row['secondar_title'].'</td>';
                echo '<td>'.$row['year'].'</td>';
                echo '<td>'.$row['volume'].'</td>';
                echo '<td>'.$row['number'].'</td>';
                echo '<td>'.$row['pages'].'</td>';
                echo '</tr>';
            }
            echo '</tbody>
        </table>';

Data to csv file: (this is just a example may need to changed to your needs)

if(isset($_POST['save_csv'])){

    $id = trim($_POST['id']);

    if(!empty($id) && is_numeric($id)){

        $con = new PDO('mysql:host=localhost;dbname=abc', $user, $pass);
        $con->SetAttribute (PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        $sql=$con->prepare('select id, title, author, secondary_title, year, volume, number, pages from refs where id={$id}');
        $sql->execute();
        $sql->setFetchMode(PDO::FETCH_ASSOC);
        $data = $sql->fetch();

        $fp = fopen('php://output', 'w');
        if ($fp) {
            header('Content-Type: text/csv');
            header('Content-Disposition: attachment; filename="export.csv"');
            fputcsv($fp, $data);
        }
    }
}

Edited by gabrielcastillo: need to add some php tags

0

Nice piece of code :). @topic starter: Just don't forget to contain your database code within a try-catch block if you set PDO's errmode to PDO::ERRMODE_EXCEPTION, as in gabrielcastillo's example.

0

A little help with that code:

header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="export.csv"');

These headers are sent to the browser, invisible to the end user. A browser always receives headers from a web server, telling it what kind of stuff it is working with. In this case the headers tell the browser that a text/csv file is being given to it, and that it should be offered to the user as a download called export.csv.

0

@gabrielcastillo...Just to clarify some few issues

I take it that we have a form with a submit button named "save.csv".

on line no.10 in your second posting, why do you use {} to enclose id??

Once again forgive me for my greenhorn questions

0

Sorry to jump in, the {} are not really needed for $id. The {braces} are usually reserved for array items or properties or to separate variables when placed directly before most characters within double quoted strings.

I imagine that the submit button would be named 'save_csv' not 'save.csv'

Edited by diafol

0

@gabrielcastillo. thnx, i was looking some thing like this, the code is really helpfull. :)

0

sorry braces are not needed.. and yes the submit button would have the name of save_csv, but the code is just an example of on method of solving the problem. You would need to wrap the table in a form as well.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.