2017-07-19 3 views
-3

Hallo Semikolon dort, ich habe diesen Code:PHP Ausgabe

// 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(); 
?> 

und die Ergebnisse werden auf diese Weise dargestellt:

000002 Luis Anel Jimenez ACTIVO

000008 Alexis Gaitan ACTIVO

000010 LUIS JIMENEZ ACTIVO

Ich brauche Ergebnisse wie:

000002; Luis Anel Jimenez; IDENTIFICADOR_EPAGO; 88191; ESTADO: ACTIVO

ich diesen Code versucht:

$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"; 
} 

aber im 000002 ;; Luis Anel Jimenez bekommen; IDENTIFICADOR_EPAGO; 88191; ESTADO : ACTIVO;

eine Möglichkeit, dieses zusätzliche Semikolon zu entfernen; nach der ersten Variablen und in der letzten Variablen ACTIVO; danke

+1

* „und die Ergebnisse werden auf diese Weise gezeigt“ * - und „was“ Art und Weise * sollte * sie sein? Du hast nicht gesagt, was das Problem war. –

+1

* "und speichern Sie es dann in einer .TXT-Datei" * - Wo ist dieses Bit zu speichern? –

Antwort

1

Schnell und vielleicht etwas schmutzig ;-)

$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

Ich bekomme das 000002 ;;;;; Luis Anel Jimenez; IDENT erste Variable ;; doppeltes Semikolon – alexistkd

+1

Dann musst du beweisen, dass du die selbe Zeile hast wie ich. Wenn es das gleiche ist, dann überprüfen Sie $ row ["id"] und $ row ["nombre"] (Werte), dass es keine gibt; drin. Weil dieses kleine Schnipsel nicht erzeugen ;; es ist unmöglich, wenn keiner der 3 Werte von der Linie ... implodieren() ... a; drin. – rebru

+0

überprüfen Sie meinen Haupt-Thread Ich habe gerade mit dem Code bearbeitet, den Sie mir gaben – alexistkd