Results 1 to 2 of 2

Thread: need php help

  1. #1
    Meyer is offline Sugar Community Member
    Join Date
    Oct 2006
    Location
    south africa
    Posts
    646

    Default need php help

    hi there

    i am trying to export data from my sugar db to csv. i got this code working, but do not know how to
    do the following.

    Put a timestamp in front of the file name
    save the csv in spesific folder
    in my phone mobile it cuts of the leading zero.

    Please help




    code begins

    <?php
    $host = '209.203.46.123';
    $user = 'paul';
    $pass = '********';
    $db = '******';
    $table = 'Inter_Active_campaign_targets';
    $file = 'Leads';

    $link = mysql_connect($host, $user, $pass) or die("Can not connect." . mysql_error());
    mysql_select_db($db) or die("Can not connect.");

    $result = mysql_query("SHOW COLUMNS FROM ".$table."");
    $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 ".$table."");
    while ($rowr = mysql_fetch_row($values)) {
    for ($j=0;$j<$i;$j++) {
    $csv_output .= $rowr[$j].", ";
    }
    $csv_output .= "\n";
    }

    $filename = $file."_".date("Y-m-d_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;
    ?>

  2. #2
    datasponge is offline Sugar Community Member
    Join Date
    Mar 2008
    Location
    San Jose, CA, USA
    Posts
    553

    Default Re: need php help

    I can't tell from your code if the problem is in the php or the mobile's interpretation of the values.

    One thing I do notice is that you are not writing a text qualifier to enclose your output field. That will likely prevent your mobile from stripping the leading 0 because it forces it to treat the field as literal text rather than trying to evaluate it.

    So instead of the lines:
    PHP Code:
    while ($row mysql_fetch_assoc($result)) {
    $csv_output .= $row['Field'].", "
    Use:
    PHP Code:
    while ($row mysql_fetch_assoc($result)) {
    $csv_output .= '"'.$row['Field'].'"'.", "
    Phil

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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