1

I am new in PHP. I have a code in which i use foreach loop to display data in table of explode function.

Here i have a question

The data in my db is

enter image description here

Here you see After Aatir there is a blank space. When i use loop to print data

<?php
ini_set('error_reporting', E_ALL);
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "pacra1";

$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "SELECT * FROM `pacra_clients` WHERE `id` = 50";

$conn->multi_query($sql);
$result = $conn->use_result();

echo $conn->error; 
$row = $result->fetch_assoc();

$liaison_one = $row['liaison_one'];

$liaison_one_chunks = explode(",", $liaison_one);

    echo '<table border="01">';
    foreach($liaison_one_chunks as $row){
        echo '<tr>';
        $row = explode(',',$row);
    foreach($row as $cell){
            echo '<td>';
            echo $cell;
            echo '</td>';
        }
        echo '</tr>';
    }
    echo '</table>';
?>

The result of my code is

enter image description here

In result you can see that there is a blank cell due to blank space in data.

Is it possible that i can skip blank space which is in my data???

5
  • 1
    $row = $result->fetch_assoc($sql); Commented Aug 25, 2015 at 5:11
  • 1
    Add an if() condition that when the string is empty(), don't create a row Commented Aug 25, 2015 at 5:12
  • 1
    ^ or foreach($row as $cell) { if(!empty($cell)) { echo "<td>{$cell}</td>"; }} Commented Aug 25, 2015 at 5:12
  • Why don't you put al these values in different fields in your MySQL table? Commented Aug 25, 2015 at 5:15
  • @Fin lets suppose in future i want to some more data like address of that person. For that i create new column every time when i update data. and its difficult to handle. That why i use Implode and Explode Commented Aug 25, 2015 at 5:18

2 Answers 2

2

Yes it is possible. Use continue.

 foreach($row as $cell){
            if ($cell == "")
                   continue;
            echo '<td>';
            echo $cell;
            echo '</td>';
        }
Sign up to request clarification or add additional context in comments.

Comments

1

Try this

 foreach($row as $cell){
        if ($cell != "" && $cell != null)
        {
        echo '<td>';
        echo $cell;
        echo '</td>';
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.