PHP输出到分号
问题描述:
您好我有这样的代码: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之后; 谢谢
答
快速,也许有点脏;-)
$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";
}
*“结果都以这种方式显示” * - 与“做什么”的方式*应*他们呢?你没有说出什么问题。 –
*“然后将其保存为.TXT文件”* - 该位要保存的位置? –