简体   繁体   中英

Incorrect field order in exported CSV table

I have a php export file, which is working fine, export datas as I want, but in the exported CSV file table field is incorrect as you can see the below picture:

在此处输入图片说明

The php code is:

<?php
session_start();
?>
<?php
header('Content-Disposition: attachment; filename="befizetesek.csv"');
    $hostname = "localhost";
    $dbusername = "username";
    $dbname = "dbname";
    $dbpassword = "password";

$dbhandle = ($GLOBALS["___mysqli_ston"] = mysqli_connect($hostname,  $dbusername,  $dbpassword)) 
  or die("Nem sikerült kapcsolódni az adatbázishoz");

$selected = ((bool)mysqli_query($dbhandle, "USE " . $dbname)) 
  or die("Nincs kiválasztva adatbázis tábla");

$query ="SELECT
   VU.first_name AS 'Befizeto neve',
   VU.email AS 'Befizeto tagkodja',
   VI.order_item_name AS 'Befizetett idoszak',
   VO.order_total AS 'Befizetett osszeg',
   VO.created_on AS 'Befizetes datuma',
   VO.order_number AS 'Befizeto azonosito'  
FROM
   m1uzl_virtuemart_orders VO INNER JOIN
   m1uzl_virtuemart_order_items VI ON
       VO.virtuemart_order_id = VI.virtuemart_order_id INNER JOIN
   m1uzl_virtuemart_order_userinfos VU ON
       VI.virtuemart_order_id = VU.virtuemart_order_id
WHERE
    VO.order_status='C' AND
    (VO.created_on >= STR_TO_DATE ('" . $_SESSION["tol"] .  "', '%Y-%m-%d') 
AND VO.created_on <=  STR_TO_DATE ('" . $_SESSION["ig"] .  "', '%Y-%m-%d'))";

$export = mysqli_query($GLOBALS["___mysqli_ston"], $query ) or die ( "Sql error : " . ((is_object( )) ? mysqli_error( ) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)) );

$fields = (($___mysqli_tmp = mysqli_num_fields( $export )) ? $___mysqli_tmp : false);

for ( $i = 0; $i < $fields; $i++ )
{

    $header .= ((($___mysqli_tmp = mysqli_fetch_field_direct( $export ,  $i )->name) && (!is_null($___mysqli_tmp))) ? $___mysqli_tmp : false) .";" ;
}

while( $row = mysqli_fetch_row( $export ) )
{
    $line = '';
    foreach( $row as $value )
    {                                            
        if ( ( !isset( $value ) ) || ( $value == "" ) )
        {
            $value = "";
        }
        else
        {
            $value = str_replace( ';' , ';' , $value );
            $value = ';' . $value ;
        }
        $line .= $value;
    }
    $data .= trim( $line ) . "\n";
}
$data = str_replace( "\r" , "" , $data );

if ( $data == "" )
{
    $data = "\n(0) Nincs találat!\n";                        
}

header("Content-type: application/octet-stream");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data";
?>

I think it's just a banal blame, but I can't see..... Please help anybody.

Thanks for your attention. I found the solution. The problem was a reverse sequence in this line:

$value = ';' . $value ;

The good one this:

$value = $value . ';' ;

Best regards!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM