2017-07-19 50 views
-3

您好我有这样的代码:PHP输出到分号

// Create connection 
$conn = new mysqli($servername, $username, $password, $dbname); 
// Check connection 
if ($conn->connect_error) { 
    die("Connection failed: " . $conn->connect_error); 
} 

$sql = "SELECT usuarios.id, usuarios.cedula, usuarios.estado, usuarios.nombre, facturas.idcliente, facturas.total\n" 
    . "FROM usuarios\n" 
    . "LEFT JOIN facturas ON usuarios.id=facturas.idcliente"; 
$result = $conn->query($sql); 

if ($result->num_rows > 0) { 
    // output data of each row 
    while($row = $result->fetch_assoc()) { 
     echo "" . $row["id"]. " " . $row["nombre"]. " " . $row["estado"]. "<br>"; 
    } 
} else { 
    echo "0 results"; 
} 

$conn->close(); 
?> 

,结果显示了这种方式:

000002路易斯·阿奈尔吉梅内斯ACTIVO

000008亚历盖坦ACTIVO

000010 LUIS JIMENEZ ACTIVO

我需要显示如下结果:

000002;路易斯阿奈尔Jimenez的; IDENTIFICADOR_EPAGO; 88191; ESTADO:ACTIVO

我试图此代码:

$data = []; 
if ($result->num_rows > 0) { 
    // output data of each row 
    while($row = $result->fetch_assoc()) { 
     $data[] = implode(";", [$row["id"],$row["saldo"],$row["nombre"],"IDENTIFICADOR_EPAGO",$row["cedula"],"ESTADO: " . $row["estado"],"\r\n" ]); 
    } 
    if(count($data)) { 
     file_put_contents("epago.txt", $data); 
    } 
          } else { 
    echo "0 results"; 
} 

但即时得到000002 ;;路易斯阿奈尔Jimenez的; IDENTIFICADOR_EPAGO; 88191; ESTADO :ACTIVO;

任何方式删除多余的分号;在第一个变量和最后一个变量ACTIVO之后; 谢谢

+1

*“结果都以这种方式显示” * - 与“做什么”的方式*应*他们呢?你没有说出什么问题。 –

+1

*“然后将其保存为.TXT文件”* - 该位要保存的位置? –

回答

1

快速,也许有点脏;-)

$data = []; 
if ($result->num_rows > 0) { 
    // output data of each row 
    while($row = $result->fetch_assoc()) { 
     // Dont add the \r\n as array entry, because it will be removed 
     // the string is concated and you open it with a texteditor 
     // Implode did concat all array values with the sign that you 
     // provide with the first parameter. 
     // This will end up in field1;field2;field3;field4;\r\n and so 
     // on, this will end up in a trailing ; on your lines 
     $data[] = implode(";", [ $row["id"],$row["nombre"],$row["estado"] ]); 
    } 
    if(count($data)) { 
     // Its urgent, that you implode here all "lines" from the array 
     // This will prevent, that lines will end with ; because 
     // the \r\n will be removed, if you read them with a texteditor 
     file_put_contens('path/to/your/file.txt', implode("\r\n", $data)); 
    } 
} else { 
    echo "0 results"; 
} 
+0

我得到这个000002 ;; Luis Anel Jimenez; IDENT第一个变量;;双分号 – alexistkd

+1

然后你必须证明,你有和我一样的线。如果它相同,那么检查$ row [“id”]和$ row [“nombre”](值)是否存在;在里面。 因为这个小片段不会产生;;,它不可能的时候,没有一个从行的3个值... implode()...有一个;在里面。 – rebru

+0

检查我的主线我刚刚用你给我的代码编辑 – alexistkd