7 Replies - 2142 Views - Last Post: 09 December 2012 - 10:21 AM Rate Topic: -----

#1 hwoarang69  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 171
  • Joined: 23-October 12

getting value from database and printing with \n

Posted 07 December 2012 - 07:34 PM

i hava a col in database called "features" and its type "text".

some where in my website page i have a textarea. where user can list features. here is a ex:
lets say below is a text area and user is entering features. at end of line is enter

<textarea type="text" name="fea" > 
    -----------------------------------
    color is red.
    size is large.
    random features1
    random features2
    etc...
    -----------------------------------
</textarea>




now calling name='fea' and storeing value in database 'features'. here is how 'features' look like in database:
features: color is red
          size is large 
          random features1
          random features2



now i am getting the value of features from database
$item_query = mysql_query("SELECT * FROM item"); 
$row = mysql_fetch_assoc($item_query)
$features_db = $row['features'];


here is the problem is. when i echo'$features_db'; it doesnt prints the enters. so for ex it print this:
 color is red  size is large  random features1 random features2


but i want to print in a nice list like this in list tag

*color is red
*size is large
*random features1

This post has been edited by hwoarang69: 07 December 2012 - 07:42 PM


Is This A Good Question/Topic? 0
  • +

Replies To: getting value from database and printing with \n

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10772
  • View blog
  • Posts: 40,122
  • Joined: 27-December 08

Re: getting value from database and printing with \n

Posted 07 December 2012 - 09:19 PM

Moved to PHP.

I think the fact that all these features are being stored in one row is indicative of a poor database design. Having a linking table to relate:
element_id (PK, FK) | feature_name (PK) | feature_description



Where element_id would reference the id of the appropriate entity in a different table.
Was This Post Helpful? 2
  • +
  • -

#3 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3572
  • View blog
  • Posts: 10,413
  • Joined: 08-June 10

Re: getting value from database and printing with \n

Posted 08 December 2012 - 03:24 AM

View Posthwoarang69, on 08 December 2012 - 03:34 AM, said:

when i echo'$features_db'; it doesnt prints the enters.

of course not, line breaks are whithe space to HTML, which gets displayed as a space character (except for the source code, where it indeed is displayed as line break).

<br> is HTML’s line break.

other than that, what mac said, poor database design.
Was This Post Helpful? 1
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3075
  • Posts: 10,783
  • Joined: 08-August 08

Re: getting value from database and printing with \n

Posted 08 December 2012 - 10:31 AM

... and single quotes tell the interpreter to ignore what's between them so the output should be a dollar sign followed by the word source, an underscore, and finally the letters db.
Was This Post Helpful? 0
  • +
  • -

#5 jordangomm  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 24
  • Joined: 12-January 10

Re: getting value from database and printing with \n

Posted 08 December 2012 - 01:53 PM

EDIT: Removed my first solution, as pointed out by Atli, it was a bad idea. Try the solution below.

Having further read your post I've noticed you wanted to display in a list, so you could also use this:

$item_query = mysql_query("SELECT * FROM item"); 
$row = mysql_fetch_assoc($item_query)
$features_db = $row['features'];
// Seperate the lines into array items using \n as the delimiter
$features = explode("\n", $features_db);
// Start the list
echo "<ul>";
// Loop through the features in the array we created
foreach ($features as $f) {
// Output each feature as a list item
echo "<li>".$f."</li>";
}
// End the list
echo "</ul>";


This post has been edited by jordangomm: 09 December 2012 - 10:22 AM

Was This Post Helpful? 0
  • +
  • -

#6 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3730
  • View blog
  • Posts: 6,017
  • Joined: 08-June 10

Re: getting value from database and printing with \n

Posted 08 December 2012 - 02:49 PM

View Postjordangomm, on 08 December 2012 - 08:53 PM, said:

When storing your information to the database, convert new lines to HTML line breaks using:

That's a bad idea. You don't taint the data by converting it to a specific output format before putting it into the database. You should store it in it's original form (or in some neutral form at least) and make whatever changes are required for it to fit your output when said output is being generated.
Was This Post Helpful? 2
  • +
  • -

#7 Anthonidas  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 221
  • Joined: 25-April 11

Re: getting value from database and printing with \n

Posted 08 December 2012 - 04:03 PM

Data has to be stored always as entered from the user. When you select your text for output you can use the PHP function nl2br() to convert new line to <br>.

For example:
<?PHP
print nl2br("This\nis\nmy\ntext...");
?>


EDIT:
Here some more information to nl2br: http://php.net/manua...ction.nl2br.php

This post has been edited by Anthonidas: 08 December 2012 - 04:04 PM

Was This Post Helpful? 0
  • +
  • -

#8 jordangomm  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 24
  • Joined: 12-January 10

Re: getting value from database and printing with \n

Posted 09 December 2012 - 10:21 AM

View PostAtli, on 08 December 2012 - 02:49 PM, said:

View Postjordangomm, on 08 December 2012 - 08:53 PM, said:

When storing your information to the database, convert new lines to HTML line breaks using:

That's a bad idea. You don't taint the data by converting it to a specific output format before putting it into the database. You should store it in it's original form (or in some neutral form at least) and make whatever changes are required for it to fit your output when said output is being generated.


Well said, didn't think that one through, although my second solution (the edit) changes on output, and also formats it into a list as the OP wanted, so that's probably more suited.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1