Ser E.Demir
Ser E.Demir

Reputation: 63

How to order Data from a row properly

How can i order results from a mysql row. Currently my code is as such:

require 'dbconnect.php';

// Query the database
$resultSet = $conn->query("SELECT * FROM wp_wppizza_orders");

    if($resultSet->num_rows != 0){
        // Turn the results into an Array
         while($rows = $resultSet->fetch_assoc())
         {
            $id = $rows['id'];
            $klantgegevens = $rows['customer_details'];   
            $bestelling = $rows['order_details'];   
            $statusbetaling = $rows['payment_status'];   
            $printed = $rows['printed'];
            if ($printed == 1){
               $printed = "FINISHED";
            } else {
               $printed = "NEW";
            }
            echo "<p class = 'orders'>";
            echo "<b>Klant gegevens:</b> $klantgegevens <br /><b>Bestelling:</b> $bestelling<br /> <b>Betaal Status:</b> $statusbetaling <br /> <b>Order Status:</b> $printed <br /> <a href='delete.php?del=$id'>Delete</a> <a href='update.php?del=$id'>Bon</a>";
            echo "</p>";
         }

The result is such:

Klant gegevens(customer details): Name : Swag Email : [email protected] Address : Florisvosstraat 56 Telephone : +31685035990 Comments : Hi my name is jason bourne.

Bestelling: 1x Pizza A medium [£ 7.45] £ 7.45 1x Pizza B medium [£ 7.45] £ 7.45 1x Pizza C medium [£ 7.45] £ 7.45 your items £ 22.35 free delivery total £ 22.35

Betaal Status: COMPLETED

Order Status: FINISHED

Delete Bon

As you can see the customer details is just a mess, how can i order this so it looks neat. I had something like this in mind:

Name: Name

email: email

Adress: adress

Telephone: telephone

Comment: comment

Editor's note: The issue is that the data is all stored in one variable on the database, and the database cannot be restructured. The data needs to be pulled out of one variable and restructured as above.

Upvotes: 0

Views: 57

Answers (1)

nomistic
nomistic

Reputation: 2962

You have all of your details in $klantgegevens, and that that data is all shown in one row. That will cause you problems.

It appears you have not normalized your data in your database. I suggest creating a table name something like customerDetails and then including rows such as id, name, email, etc

You query would look something like this:

$resultSet = $conn->query("SELECT * FROM wp_wppizza_orders w
                           JOIN customerDetails c
                           ON w.id = c. id");

This way when you run your query, each of these will appear in a separate row, like this:

$name = $rows['name']; 
$email = $rows['email']; 

This way when you echo out the rows, it will separate them correctly like so:

     while($rows = $resultSet->fetch_assoc()) {

            echo 'Name: '.$name.'<br/>';
            echo 'email: '.$email.'<br/>';
     }

Edit: : Since you are forced to split apart a line of text, you may need to do something with regex. Here's an example of how to create a variable for name. Try this out. You should be able to modify your code to handle something like this.

$klantgegevens = "Name : Swag Email : [email protected] Address : Florisvosstraat 56 Telephone : +31685035990 Comments : Hi my name is jason bourne";
$names = preg_split('/Name :/', $klantgegevens);
array_shift($names);
echo "<pre>";
$name = print_r($names[0]);

echo $name;

You could do something similar for the remaining rows.

Edit again: Here's a much better way. We are just grabbing the rows in between the tags "Name" and "Email". Much easier than dealing with preg_split. The wildcard below is just shown with (.*?)

$klantgegevens = "Name : Swag Email : [email protected] Address : Florisvosstraat 56 Telephone : +31685035990 Comments : Hi my name is jason bourne";
preg_match("'Name : (.*?)Email :'s", $klantgegevens, $name);
echo $name[1];

The only thing you need to do to get the other variables is to change the strings, such as change Name : to Email : and then Email : to Address :, etc.

Upvotes: 1

Related Questions