WebSite X5Help Center

 
Errol W.
Errol W.
User

Php code issue  en

Автор: Errol W.
Просмотрено 1086, Подписчики 1, Размещенный 0  

Here is the code I am using.

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);

// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}

$sql = "SELECT order_id, label, value FROM cartinvoice_addresses";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo " " . $row["label"]. " " . $row["value"] ;
if( $row["label"] = "address"){ "<br>"}
}
} else {
echo "0 results";
}
$conn->close();
?>

I added this to the original code "if( $row["label"] = "address"){ "<br>"}"  I want the output to skip a line when it finds the word "address" in the table column. When I run the process I get "This Page not working" I suspect the syntax is wrong. Help me. Without that piece of code, there is no problem.

Размещено
8 Ответы - 1 Корректно
Axel  
Axel  
User
Лучший пользователь месяца FR

Try this:

if( $row["label"] = "address"){ echo "<br>"; }

Читать больше
Размещено От Axel  
Errol W.
Errol W.
User
Автор

Hello Axel,

I am not getting the page not working after using your advice. However I am not getting the line break.

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);

// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}

$sql = "SELECT order_id, label, value FROM cartinvoice_addresses";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo " " . $row["label"]. " " . $row["value"] ;
if( $row["label"] = "address"){ echo "<br>"; }

}
} else {
echo "0 results";
}
$conn->close();
?>

Читать больше
Размещено От Errol W.
Axel  
Axel  
User
Лучший пользователь месяца FR

Try to modify your script with :

$conn = mysqli_connect("my_server", "user", "password", "dbname");

and to save it in PHP file like test.php

and try tu use your test.php  without WSX5. Directly from your web server like https://myserver.com/test.php

It's better to debug the PHP script outside of WSX5.

When it's run, you can add it to use it...

Hope this helps

Axel

Читать больше
Размещено От Axel  
Errol W.
Errol W.
User
Автор

Thanks, I'll give it a go.

Читать больше
Размещено От Errol W.
Errol W.
Errol W.
User
Автор

Oh, Yeah that much better for fixing code. Thanks

Читать больше
Размещено От Errol W.
Paul M.
Paul M.
Moderator

Hello Errol,

The comparison operator for equality in PHP is a double equals sign as opposed to a single.

Thus:

if( $row["label"] == "address")

This is why your code is failing the if statement.

Also, you might want to capitalise "address" to match what is in the MySQL database.

if( $row["label"] == "Address")

Kind regards,

Paul

Search the WebSite X5 Help Center

Читать больше
Размещено От Paul M.
Errol W.
Errol W.
User
Автор

Paul, that did the trick.

Читать больше
Размещено От Errol W.
Paul M.
Paul M.
Moderator

Great, thanks for letting us know!

Читать больше
Размещено От Paul M.