Root0x
Root0x

Reputation: 490

Display PHP Array On New Line

I am trying to display the output of a sql query on 3 different lines using the echo function. The php code is enclosed inside a div tag. My code is below

<div class="ClientAdress" id="ClientAdress">
<?php
$db = new SQLite3( 'Stock_Control.sqlite');
$sql = 'SELECT City,County,Street_Adress FROM Customer WHERE Customer_ID = 14';
$result = $db->query($sql);//->fetchArray(SQLITE3_ASSOC); 

    $row = array(); 

    $i = 0; 

     while($res = $result->fetchArray(SQLITE3_ASSOC)){ 

         if(!isset($res['City'])) continue; 
          $row[$i]['City'] = $res['City']; 
          $row[$i]['County'] = $res['County']; 
          $row[$i]['Street_Adress'] = $res['Street_Adress']; 

          $i++; 

      } 

        echo $row[0]['City'];
        echo $row[0]['County'];
        echo $row[0]['Street_Adress'];

?>
</p>
</div>

The current output is "BurgasBurgasplaces"


Edit:

This is what I tried:

<div class="ClientAdress" id="ClientAdress">
<?php
$db = new SQLite3( 'Stock_Control.sqlite');
$sql = 'SELECT City,County,Street_Adress FROM Customer WHERE Customer_ID = 14';
$result = $db->query($sql);//->fetchArray(SQLITE3_ASSOC); 
$row = array(); 
$i = 0; 
while($res = $result->fetchArray(SQLITE3_ASSOC))
{ 
if(!isset($res['City'])) continue; 
$row[$i]['City'] = $res['City']; 
$row[$i]['County'] = $res['County']; 
$row[$i]['Street_Adress'] = $res['Street_Adress']; 
$i++; 

} 
echo $row[0]['City'] .  "<br />\n";;
echo $row[0]['County'] .  "<br />\n";;
echo $row[0]['Street_Adress'] .  "<br />\n";;
?>
</div>

Upvotes: 1

Views: 618

Answers (2)

dojs
dojs

Reputation: 517

That code looks ugly, simplify it even more;

foreach ($row[0] as $key => $value)
{
  printf('%s => %s <br>\n', $key, $value);
  // OR
  echo "$key => $value <br> \n";
}

Automating will help you make your code cleaner, easier to understand and make you program faster.

Upvotes: 0

Jay Blanchard
Jay Blanchard

Reputation: 34416

Concatenate a line break to each one -

echo $row[0]['City'] . "<br />" . "\n";
echo $row[0]['County'] . "<br />" . "\n";
echo $row[0]['Street_Adress'] . "<br />" . "\n";

Using \n to produce clean HTML in source.

You can also use the following, having both <br /> and \n inside one set of quotes:

echo $row[0]['City'] . "<br />\n";
echo $row[0]['County'] . "<br />\n";
echo $row[0]['Street_Adress'] . "<br />\n";

Otherwise, and should you later decide to add styling to those or wrapped in table tags, will all be inside one long line, rather than having each line neatly placed one underneath the other.

Upvotes: 2

Related Questions