Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 3 of 4 FirstFirst 1234 LastLast
Results 31 to 45 of 50
  1. #31
    New Coder
    Join Date
    Aug 2012
    Posts
    22
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Hi Guys,

    Back again and need some help.

    I am trying to export the database into a .csv file with custom headers.

    I was able to find this script but getting stuck.

    PHP Code:
    <?php

    $DB_host
    ="localhost"// Host name
    $DB_username="username"// Mysql username
    $DB_password="dbpassword"// Mysql password
    $db_name="dbname; // Database name

    #
    # Create the connection to MySQL
    #

    $con = mysql_connect("
    $DB_host","$DB_username","$DB_password");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
      
      
    #
    #  Create the Column Headings
    #

    $csv_output .= '"
    Id",';
    $csv_output .= '"
    Full Name",';
    $csv_output .= '"
    Number",';
    $csv_output .= '"
    Email Address",';
    $csv_output .= '"
    City",';
    $csv_output .= '"
    Address",';
    $csv_output .= '"
    Which animal do you want to keep?"';
    $csv_output .= "
    n";


    #
    # Get Users Details form the DB
    #

    $result = mysql_query( "
    SELECT from tablename WHERE id '$id'" );

    while ( $a_row = mysql_fetch_array( $result ) ){

    $csv_output .= '"'.str_replace('"', '""', $a_row['id']).'"
    ,';
    $csv_output .= '"'.str_replace('"', '""', $a_row['
    FullName']).'",';
    $csv_output .= '"'.str_replace('"', '""', $a_row['Number']).'"
    ,';
    $csv_output .= '"'.str_replace('"', '""', $a_row['
    EmailAddress']).'",';
    $csv_output .= '"'.str_replace('"', '""', $a_row['City']).'"
    ,';
    $csv_output .= '"'.str_replace('"', '""', $a_row['
    Address']).'",';
    $csv_output .= '"'.str_replace('"', '""', $a_row['compathome']).'"
    ;
    $csv_output .= "\n";
    }


    #
    # Close the MySql connection
    #

    mysql_close($con);

    #
    # Set the headers so the file downloads
    #

        
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        
    header("Content-Length: " strlen($csv_output));
        
    header("Content-type: text/x-csv");
        
    header("Content-Disposition: attachment; filename=resultExport.csv");
        echo 
    $csv_output;
        exit;

    ?>

    I get the error with row having:
    while ( $a_row = mysql_fetch_array( $result ) ){

    Error is: Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in.........

    And:
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Content-Length: " . strlen($csv_output));
    header("Content-type: text/x-csv");
    header("Content-Disposition: attachment; filename=resultExport.csv");

    Error is:
    Warning: Cannot modify header information - headers already sent by

    Thank you in advance for helping

    Regards,
    mohammedsali

  2. #32
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    Quote Originally Posted by mohammedsali View Post
    Error is:
    Warning: Cannot modify header information - headers already sent by

    That error is because some html has been sent to the page before the header function.

    PHP Code:
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
    Even white space will cause the error. You might have to use the backspace key to remove any spaces that might be considered white space, perhaps it's some tabs or spaces that have to be removed.

    Another solution is to use the php function ob_flush at the start and end of the page . I haven't used it in years so you might have to check the manual for exact use of ob_flush.



    ---
    Last edited by Len Whistler; 08-13-2012 at 10:26 PM.
    Leonard Whistler

  3. #33
    New Coder
    Join Date
    Aug 2012
    Posts
    22
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Hi Len Whistler,

    Thank you for the reply

    Tried to remove spaces under $csv_output example:
    $csv_output .= '"FullName",';


    Also removed spaces under:
    header("Cache-Control:must-revalidate,post-check=0,pre-check=0");


    Very new to php and not sure of ob_flush, but if you can help would really appreciate it

    But it did not help

    Thanks & Regards,
    mohammedsali

  4. #34
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,500
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    Worry about this one for now ...

    Error is: Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in.........

    Are you sure there are results? Maybe it didn't find anything.

  5. #35
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    Below is how I would remove all spaces, before the header function. Even the comments are removed. Is there any code above the php opening tag?


    PHP Code:
    <?php
    $DB_host
    ="localhost"// Host name
    $DB_username="username"// Mysql username
    $DB_password="dbpassword"// Mysql password
    $db_name="dbname; // Database name
    $con = mysql_connect("
    $DB_host","$DB_username","$DB_password");
    if (!$con){
    die('Could not connect: ' . mysql_error());
    }
    $csv_output .= '"
    Id",';
    $csv_output .= '"
    Full Name",';
    $csv_output .= '"
    Number",';
    $csv_output .= '"
    Email Address",';
    $csv_output .= '"
    City",';
    $csv_output .= '"
    Address",';
    $csv_output .= '"
    Which animal do you want to keep?"';
    $csv_output .= "
    n";
    $result = mysql_query( "
    SELECT from tablename WHERE id '$id'" );
    while ( $a_row = mysql_fetch_array( $result ) ){
    $csv_output .= '"'.str_replace('"', '""', $a_row['id']).'"
    ,';
    $csv_output .= '"'.str_replace('"', '""', $a_row['
    FullName']).'",';
    $csv_output .= '"'.str_replace('"', '""', $a_row['Number']).'"
    ,';
    $csv_output .= '"'.str_replace('"', '""', $a_row['
    EmailAddress']).'",';
    $csv_output .= '"'.str_replace('"', '""', $a_row['City']).'"
    ,';
    $csv_output .= '"'.str_replace('"', '""', $a_row['
    Address']).'",';
    $csv_output .= '"'.str_replace('"', '""', $a_row['compathome']).'"
    ;
    $csv_output .= "\n";
    }
    mysql_close($con);
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Content-Length: " strlen($csv_output));
    header("Content-type: text/x-csv");
    header("Content-Disposition: attachment; filename=resultExport.csv");
    echo 
    $csv_output;
    exit;
    ?>
    Last edited by Len Whistler; 08-13-2012 at 10:55 PM.
    Leonard Whistler

  6. #36
    New Coder
    Join Date
    Aug 2012
    Posts
    22
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Hi mlseim,

    Thank you for the reply

    There is data in the Table to be shown. I have this script which exports the data from table but the headers are not custom.

    PHP Code:

    <?php
     $host 
    'localhost'// <--  db address
     
    $user 'username'// <-- db user name
     
    $pass 'dbpassword'// <-- password
     
    $db 'dbname'// db's name
     
    $table 'tblname'// table you want to export
     
    $file 'tbl_export'// csv name.
     
    $link mysql_connect($host$user$pass) or die("Can not connect." mysql_error());
     
    mysql_select_db($db) or die("Can not connect.");
     
    $term $_POST['term'];

    $result mysql_query("select * from tblname where FullName like '%$term%'");
     
    $i 0;
     
    if (
    mysql_num_rows($result) > 0) {
    while (
    $row mysql_fetch_assoc($result)) {
    $csv_output .= $row['Field'].",";
    $i++;}
    }
    $csv_output .= "\n";
     
    $values mysql_query("select * from tblname where FullName like '%$term%'");
     
    while (
    $rowr mysql_fetch_row($values)) {
    for (
    $j=0;$j<$i;$j++) {
    $csv_output .= $rowr[$j].", ";
    }
    $csv_output .= "\n";
    }
     
    $filename $file."_".date("d-m-Y_H-i",time());
     
    header("Content-type: application/vnd.ms-excel");
    header("Content-disposition: csv" date("Y-m-d") . ".csv");
    header"Content-disposition: filename=".$filename.".csv");
     
    print 
    $csv_output;
     
    exit;
    ?>
    If there is a way to insert custom headers in this script too will help.

    Thanks & Regards,
    mohammedsali

  7. #37
    New Coder
    Join Date
    Aug 2012
    Posts
    22
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Hi Len Whistler,

    Thank you for the corrected script

    Was getting in link number 6, added "$db_name" to $con = mysql_connect that solved the problem but now get the error:

    Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE

    Thanks & Regards,
    mohammedsali

  8. #38
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    Haven't used it in years so there might be errors below. But it's something to have in mind for headers already sent errors.



    PHP Code:
    <?php
    ob_start
    ();




    rest of code 







    ob_flush
    ();
    ?>
    Last edited by Len Whistler; 08-13-2012 at 11:12 PM.
    Leonard Whistler

  9. #39
    New Coder
    Join Date
    Aug 2012
    Posts
    22
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Hi Len Whistler

    Thank you for the suggestion, tried the following.

    PHP Code:

    <?php
    $DB_host
    ="localhost"// Host name
    $DB_username="username"// Mysql username
    $DB_password="dbpassword"// Mysql password
    $db_name="dbname; // Database name
    $con = mysql_connect("
    $DB_host","$DB_username","$DB_password", "$db_name");
    if (!$con){
    die('Could not connect: ' . mysql_error());
    }
    ob_start();
    $csv_output .= '"
    Id",';
    $csv_output .= '"
    Full Name",';
    $csv_output .= '"
    Number",';
    $csv_output .= '"
    Email Address",';
    $csv_output .= '"
    City",';
    $csv_output .= '"
    Address",';
    $csv_output .= '"
    Which animal do you want to keep?"';
    $csv_output .= "
    n";
    $result = mysql_query( "
    SELECT from hr_candidates WHERE id '$id'" );
    while ( $a_row = mysql_fetch_array( $result ) ){
    $csv_output .= '"'.str_replace('"', '""', $a_row['id']).'"
    ,';
    $csv_output .= '"'.str_replace('"', '""', $a_row['
    FullName']).'",';
    $csv_output .= '"'.str_replace('"', '""', $a_row['MobileNumber']).'"
    ,';
    $csv_output .= '"'.str_replace('"', '""', $a_row['
    EmailAddress']).'",';
    $csv_output .= '"'.str_replace('"', '""', $a_row['City']).'"
    ,';
    $csv_output .= '"'.str_replace('"', '""', $a_row['
    Address']).'",';
    $csv_output .= '"'.str_replace('"', '""', $a_row['petathome']).'"
    ;
    $csv_output .= "\n";
    }
    mysql_close($con);
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Content-Length: " strlen($csv_output));
    header("Content-type: text/x-csv");
    header("Content-Disposition: attachment; filename=resultExport.csv");
    echo 
    $csv_output;
    exit;
    ob_flush();
    ?>
    But still get the same error:
    Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE in..

    Thanks,
    mohammedsali

  10. #40
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    Quote Originally Posted by mohammedsali View Post
    Hi Len Whistler

    Thank you for the suggestion, tried the following.

    But still get the same error:
    Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE in..

    Thanks,
    mohammedsali

    Removing all the extra space was a possible solution for the header already sent error. The above error should give a line number you can look at.


    ----
    Leonard Whistler

  11. #41
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,302
    Thanks
    4
    Thanked 204 Times in 201 Posts
    You're missing the closing quote on this line:

    PHP Code:
    $db_name="dbname; // Database name 
    Should be

    PHP Code:
    $db_name="dbname"// Database name 
    Hint, look at the code you posted and you'll see where things turn red when they shouldn't be.
    Dave .... HostMonster for all of your hosting needs

  12. #42
    New Coder
    Join Date
    Aug 2012
    Posts
    22
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Hi Guys,

    Thank you for your suggestions and help

    Found a script, works perfectly:

    PHP Code:

    // output headers so that the file is downloaded rather than displayed
    header('Content-Type: text/csv; charset=utf-8');
    header('Content-Disposition: attachment; filename=data.csv');

    // create a file pointer connected to the output stream
    $output fopen('php://output''w');

    // output the column headings
    fputcsv($output, array('Column 1''Column 2''Column 3'));

    // fetch the data
    mysql_connect('localhost''username''password');
    mysql_select_db('database');
    $rows mysql_query('SELECT field1,field2,field3 FROM table');

    // loop over the rows, outputting them
    while ($row mysql_fetch_assoc($rows)) fputcsv($output$row); 

  13. #43
    New Coder
    Join Date
    Aug 2012
    Posts
    22
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Hi Guys,

    I am trying to create a page to view records.

    1) Have a fixed header for the column headers so browsing down the user does not lose sight of the column headers.


    2) I could like to make it that the user can either view all the records in one page or user can set number of records to be shown on the page and go page by page or go directly to any particular page.

    I have the below code which shows all the records but lose sight of the headers when browse down.

    PHP Code:
    <?php
    $host
    ="localhost"// Host name 
    $username="username"// Mysql username 
    $password="password"// Mysql password 
    $db_name="dbname"// Database name 
    $tbl_name="tblname"// Table name  

    // Connect to server and select database.
    mysql_connect("$host""$username""$password")or die("cannot connect"); 
    mysql_select_db("$db_name")or die("cannot select DB");

    $sql="SELECT * FROM $tbl_name";
    $result=mysql_query($sql);
    ?>

    <table align="center" width="400" border="0" cellspacing="1" cellpadding="0">
    <tr>
    <td>
    <table align="center" width="400" border="1" cellspacing="0" cellpadding="3">
    <tr>
    <td align="center" colspan="4"><strong>View all records</strong> </td>
    </tr>

    <tr>
    <td align="center"><strong>Id</strong></td>
    <td align="center"><strong>Name</strong></td>
    <td align="center"><strong>Number</strong></td>
    <td align="center"><strong>Email Address</strong></td>
    <td align="center"><strong>City</strong></td>
    <td align="center"><strong>Address</strong></td>
    <td align="center"><strong>petathome?</strong></td>
    <td align="center"><strong>Edit Record</strong></td>
    </tr>

    <?php
    while($rows=mysql_fetch_array($result)){
    ?>

    <tr>

    <td><? echo $rows['Id']; ?></td>
    <td><? echo $rows['Name']; ?></td>
    <td><? echo $rows['Number']; ?></td>
    <td><? echo $rows['EmailAddress']; ?></td>
    <td><? echo $rows['City']; ?></td>
    <td><? echo $rows['Address']; ?></td>
    <td><? echo $rows['petathome']; ?></td>

    <td align="center"><a href="update.php?id=<? echo $rows['id']; ?>">edit</a></td>
    </tr>

    <?php
    }
    ?>

    </table>
    </td>
    </tr>
    </table>

    <?php
    mysql_close
    ();
    ?>
    Thank you in advance for helping

    Regards,
    mohammedsali

  14. #44
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    Quote Originally Posted by mohammedsali View Post
    1) Have a fixed header for the column headers so browsing down the user does not lose sight of the column headers.
    I believe you can use CSS to fix a div in a specific location, such as the top of the page. But I find it extremely distracting to have objects fixed in a position and as you scroll it always pops up. I see it with menus, and advertisements.

    I would have a slightly different color for each column, and let the column headers scroll out of sight.



    Quote Originally Posted by mohammedsali View Post
    2) I could like to make it that the user can either view all the records in one page or user can set number of records to be shown on the page and go page by page or go directly to any particular page.
    The term is pagination, and yes it's a good idea to limit the number of records per page. PHP handles pagination very well.




    ----
    Last edited by Len Whistler; 08-17-2012 at 09:44 PM.
    Leonard Whistler

  15. Users who have thanked Len Whistler for this post:

    mohammedsali (08-18-2012)

  16. #45
    New Coder
    Join Date
    Aug 2012
    Posts
    22
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Hi Len,

    Thank you guiding me in the right direction

    With a lot of difficulty and searching was able to find and put together this script which works. The only problem i am not able to figure out is it does not list the first row of the every page correctly.

    Example first page is to start with the id 1, but instead it starts from id 2
    Same happens to every page, example if the next page is to start with id 21, it will instead shows 22.

    Can please let me know the exact correction i will need to make?

    If possible can you help me with the correct script?

    And also was not able to any head way in fixed column headers. If you have any working script or can guide me to one would be of great help.

    I really appreciate you help once again

    Regards,
    mohammedsali

    PHP Code:

    <html>
        <head>
            <title>View Results</title>
        </head>

        <body>
        
        <?php
            $host
    ="localhost"// Host name 
            
    $username="username"// Mysql username 
            
    $password="password"// Mysql password 
            
    $db_name="dbname"// Database name 
            
    $tbl_name="tblname"// Table name 
            
            
    define('MAX_REC_PER_PAGE'25);
            
    mysql_connect("$host""$username""$password") or die("Couldn't connect to db!");
            
    mysql_select_db("$db_name") or die("Couldn't select db!");
            
    $result mysql_query("SELECT COUNT(*) FROM $tbl_name") or die("Count query error!");
            list(
    $total) = mysql_fetch_row($result);
            
    $total_pages ceil($total MAX_REC_PER_PAGE);
            
    $page intval(@$_GET["page"]);     
            if (
    == $page){
                
    $page 1;
            }        
            
    $start MAX_REC_PER_PAGE * ($page 1);
            
    $max MAX_REC_PER_PAGE;
            
    $result mysql_query("SELECT id, Name, Number, EmailAddress, City, Address, petathome FROM $tbl_name ORDER BY id ASC LIMIT $start, 
            $max"
    ) or die("$tbl_name query error!");
            
    ?>
    <table align="center" width="400" border="0" cellspacing="1" cellpadding="0">
    <tr>
    <td>
    <table align="center" width="400" border="1" cellspacing="0" cellpadding="3">
    <tr>
    <td align="center" colspan="9"><strong>View all records</strong> </td>
    </tr>

    <tr>
    <td align="center"><strong>Id</strong></td>
    <td align="center"><strong>Name</strong></td>
    <td align="center"><strong>Number</strong></td>
    <td align="center"><strong>Email Address</strong></td>
    <td align="center"><strong>City</strong></td>
    <td align="center"><strong>Address</strong></td>
    <td align="center"><strong>petathome?</strong></td>
    <td align="center"><strong>Edit Record</strong></td>
    </tr>    
            <?php
                
    while (list($id$Name$Number$EmailAddress$City$Address$petathome) = mysql_fetch_row($result)) {
                while(
    $rows=mysql_fetch_array($result)){
            
    ?>
        
    <td><? echo $rows['id']; ?></td>
    <td><? echo $rows['Name']; ?></td>
    <td><? echo $rows['Number']; ?></td>
    <td><? echo $rows['EmailAddress']; ?></td>
    <td><? echo $rows['City']; ?></td>
    <td><? echo $rows['Address']; ?></td>
    <td><? echo $rows['petathome']; ?></td>

    <td align="center"><a href="update.php?id=<? echo $rows['id']; ?>">edit</a></td>
    </tr>
            <?php
                
    }
            
    ?>
            <?php
                
    }
            
    ?>
        </table>
        <table border="0" cellpadding="3" align="center">
            <tr>
                <td>Goto Page:</td>
                <?php
                    
    for ($i 1$i <= $total_pages$i++) {
                        
    $txt $i;
                        if (
    $page != $i)
                            
    $txt "<a href=\"" $_SERVER["PHP_SELF"] . "?page=$i\">$txt</a>";
                
    ?>            
                <td align="center"><?= $txt ?></td>
                <?php
                    
    }
                
    ?>
            </tr>
        </table>
        <hr>
        </div>
        </body>
    </html>


 
Page 3 of 4 FirstFirst 1234 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •