1

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>
7
  • ...and the question is? Commented Mar 10, 2015 at 18:03
  • how would i display the echo function on a new line Commented Mar 10, 2015 at 18:05
  • What.. echo $row[0]['City'].'<br>';? Commented Mar 10, 2015 at 18:06
  • sidenote: you dont open that p tag, or your opening it in the wrong place Commented Mar 10, 2015 at 18:08
  • You have an answer/solution below. Been there for a while, actually. Commented Mar 10, 2015 at 18:25

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

10 Comments

It's a simple as that Sam ;-)
You know me, I am such a simple guy @Fred-ii- ;-)
Hmmmm, interesting @Fred-ii-, I would have put both items in a single set of quotes.
Ah.... yes you're right. Good point. Oh well, the OP's got enough to go on now ;-) Just force of habit.
It's worth thousands Ralph! ;-)
|
0

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.

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.