Jump to content


Photo

detecting new lines?


  • Please log in to reply
10 replies to this topic

#1 mATOK

mATOK
  • Members
  • PipPipPip
  • Advanced Member
  • 84 posts
  • LocationToronto, Ontario

Posted 28 August 2006 - 05:02 PM

Hi there, I have a form where users input information and will be formating it into paragraphs etc.

I would like to keep this paragraph / line break formating.

Currently I store the txt in my db and when I retrieve it I get everything on one line...

How can I preserve the line breaks the user is keying in on my form?
"Not everything that counts can be counted, and not everything that can be counted counts."

W3 Web Tutorials | Check Your html | CSS layout techniques | ALA | Zen Garden | CSS Links

Navs with flavor | CSS Playground | CSS boxes

#2 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 28 August 2006 - 05:15 PM

str_replace("\n",'<Br>'$outputfromdb)
or
nr2br($outputfromdb)


Both should do the truick :)


Liam
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#3 mATOK

mATOK
  • Members
  • PipPipPip
  • Advanced Member
  • 84 posts
  • LocationToronto, Ontario

Posted 28 August 2006 - 06:12 PM

do I do this when I grab the data from the form or when I output it?

Here is my code :)

<?php
include ("includes/head.php");

$what = $_POST['what'];                 // Edit or Delete the class
$catnum = $_POST['cat-num'];
$classnum = $_POST['class-num'];
$classname = $_POST['name'];
$class_def = $_POST['class-definition'];
$brand_desc = $_POST['brand-desc'];
$brand_break = $_POST['brand-break'];
$media_spec = $_POST['media-spec'];



$giveme = $_POST['Cat'];                // Which Category do I want to see
$showme = $_GET['showme'];              // Which Class do I want to see
$classid = $_GET['classid'];            // Which Class do I want to edit
$editme = $_POST['class-num'];          // Am I trying to edit a Class

$username="test";                       // Database Username
$password="test";                       // Database Password
$database="ais coding rules";           // Database To Connect To

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

if (2 == $what) {
        $editquery="UPDATE class SET Class_Name='$classname', Class_Definition='$class_def', Brand_Desc='$brand_desc', Brand_Break='$brand_break',
                    Media_Spec='$media_spec', Catagory_Number1='$catnum' WHERE Class_Num='$classnum'";
        mysql_query($editquery) or die (mysql_error());
        echo "The record has been UPDATED. Click <a href=\"http://nmr001mrkint00.enterprisenet.org:8081/Operations/AIS/Code_Rules2.php\">here</a> to return to Coding Rules<br /><br />.";

}
elseif (3 == $what) {
        $delquery="DELETE FROM class WHERE Class_Num='$classnum'";
        mysql_query($delquery) or die (mysql_error());
        echo "The record has been DELETED. Click <a href=\"http://nmr001mrkint00.enterprisenet.org:8081/Operations/AIS/Code_Rules2.php\">here</a> to return to Coding Rules<br /><br />.";
                
}
elseif($classid) {                          // Create and populate a form for editing class nfo

        $cquery="select * FROM class WHERE Class_Num = '$classid'";
        $cqueryrez=mysql_query($cquery) or die (mysql_error());

        while ($i = mysql_fetch_array($cqueryrez, MYSQL_BOTH)) {
               /* $test =mysql_num_fields($cqueryrez);
                echo $test;
                echo $i['Brand_Desc']; */
                echo "<form name=\"class-change\" action=\"Cat_list_action2.php\" method=\"POST\" enctype=\"multipart/form-data\">";
                echo "<table border=\"0\">";
                echo "<tr><th>Category Number</th><td><input type=\"text\" name=\"cat-num\" size=\"5\" value=".$i['Catagory_Number1']."></td></tr>";

                echo "<tr><th>Class Number</th><td><input type=\"text\" name=\"class-num\" size=\"5\" value=\"".$i['Class_Num']."\"></td></tr>";
                echo "<tr><th>Class Name</th><td><input type=\"text\" name=\"name\" size=\"66\" value=\"".$i['Class_Name']."\"></td></tr>";
                echo "<tr><th>Class Definition</th><td><textarea rows=\"5\" cols=\"50\" name=\"class-definition\">".$i['Class_Definition']."</textarea></td></tr>";
                echo "<tr><th>Brand Description</th><td><textarea rows=\"5\" cols=\"50\" name=\"brand-desc\">".$i['Brand_Desc']."</textarea></td></tr>";
                echo "<tr><th>Brand Break</th><td><textarea rows=\"5\" cols=\"50\" name=\"brand-break\">".$i['Brand_Break']."</textarea></td></tr>";
                echo "<tr><th>Media Spec</th><td><textarea rows=\"5\" cols=\"50\" name=\"media-spec\">".$i['Media_Spec']."</textarea></td></tr>";
                
                echo "<tr><th>UPDATE<input type=\"radio\" name=\"what\" value=\"2\" /></th><th class=\"right\">DELETE<input type=\"radio\" name=\"what\" value=\"3\" /></th></tr>";
                echo "<tr><td colspan=\"2\">&nbsp;</td></tr>";
                echo "<tr><td><input type=\"submit\" value=\"Submit\"></td><td>&nbsp;</td></tr>";
                echo "</table>";
                echo "</form>";
        }
}
elseif($showme) {                           // Create tables that display all class information

        $query="select class_name as Class, class_num as class_num, class_definition as definition,
        brand_desc as bdesc, brand_break as bbreak, media_spec as mspec FROM class WHERE Class_Num = '$showme'";
        $catresult=mysql_query($query)or die(mysql_error());

        while($row=mysql_fetch_array($catresult, MYSQL_BOTH)) {
                echo "<table width=\"90%\" align=\"center\" border=\"1\" >";
                echo "<tr><th colspan=\"2\">".$row["Class"]."</th></tr>";
                echo "<tr><th>Class Definition</th><th>Brand Description</th>";
                echo "<tr><td width=\"50%\" valign=\"top\">".$row["definition"]."</td><td width=\"50%\" valign=\"top\">".$row["bdesc"]."</td></tr>";
                echo "<tr><th>Brand Breakout Rulings</th><th>Media Specific Rulings</th>";
                echo "<tr><td width=\"50%\" valign=\"top\">".$row["bbreak"]."</td><td width=\"50%\" valign=\"top\">".$row["mspec"]."</td></tr>";
                echo "<tr><td colspan=\"2\">&nbsp;</tr>";
                echo "<tr><th class=\"pnotes\" colspan=\"2\"><b><a href=Cat_list_action2.php?classid=".$row['class_num']."><i>EDIT THIS CLASS</i></a></th></tr>";
                echo "</table>";
                echo "<br />";
        }
}
elseif($giveme) {                           // Create tables that display all the classes for the selected category

        $q1="SELECT Catagory_Name FROM catagories WHERE Catagory_Number = '$giveme'";
        $r1=mysql_query($q1)or die(mysql_error());
        while($row1=mysql_fetch_array($r1, MYSQL_BOTH)) {
                echo "<h2>".$row1["Catagory_Name"]."</h2>";
                $disclass = $row1["Catagory_Name"];
        }
        
        $gquery="select a.catagory_name as Category, b.class_name as Class, b.class_num as class_num, b.path_to_file as path, b.class_definition as definition,
        b.brand_desc as bdesc, b.brand_break as bbreak, b.media_spec as mspec FROM catagories a join class b ON a.catagory_number = b.catagory_number1 WHERE
        Catagory_Number = '$giveme'";
        $gresult=mysql_query($gquery)or die(mysql_error());
        echo "<p>Here is a list of all the classes in ".$disclass."</p>";
        echo "<ul>";
        while($row=mysql_fetch_array($gresult, MYSQL_BOTH)) {
                echo "<li><a href=Cat_list_action2.php?showme=".$row["class_num"].">".$row["Class"]."</a></li>";
        }
        echo "</ul>";
}
// Close The SQL Connection
mysql_close();
include ("includes/foot.php");
?>



"Not everything that counts can be counted, and not everything that can be counted counts."

W3 Web Tutorials | Check Your html | CSS layout techniques | ALA | Zen Garden | CSS Links

Navs with flavor | CSS Playground | CSS boxes

#4 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 28 August 2006 - 06:14 PM

when you output it as your form will output \n and input that to your database and then when you pull info out your database it is still \n instead of <Br> you could always use it befor putting into your DB too would work all the same :)
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#5 mATOK

mATOK
  • Members
  • PipPipPip
  • Advanced Member
  • 84 posts
  • LocationToronto, Ontario

Posted 28 August 2006 - 06:23 PM

shocker-z, I apreciate your help but am not sure where to stick this bit of code.. I tried inserting it into the echo but it did not work.

I figure the best place would be in the insert so that I only do it once... can I do this in the query or do I format the variables first. Also can I format all the variables at once or must I do them individualy?

I added
if (2 == $what) {
        str_replace("\n","<br />",$class_def);

which in my html does not give me new lines as I type, but when I choose to edit I see the breaks.
"Not everything that counts can be counted, and not everything that can be counted counts."

W3 Web Tutorials | Check Your html | CSS layout techniques | ALA | Zen Garden | CSS Links

Navs with flavor | CSS Playground | CSS boxes

#6 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 28 August 2006 - 06:32 PM

I've edited your code so that all POST variables convert new lines to <Br> this was simplest way of doing it hope this works well mate


<?php
include ("includes/head.php");

$what = nl2br(nl2br($_POST['what']);                 // Edit or Delete the class
$catnum = nl2br(nl2br($_POST['cat-num']); 
$classnum = nl2br(nl2br($_POST['class-num']); 
$classname = nl2br(nl2br($_POST['name']); 
$class_def = nl2br(nl2br($_POST['class-definition']);
$brand_desc = nl2br(nl2br($_POST['brand-desc']);
$brand_break = nl2br(nl2br($_POST['brand-break']);
$media_spec = nl2br(nl2br($_POST['media-spec']);



$giveme = nl2br($_POST['Cat']);                // Which Category do I want to see
$showme = $_GET['showme']);              // Which Class do I want to see
$classid = $_GET['classid']);            // Which Class do I want to edit
$editme = nl2br($_POST['class-num']);          // Am I trying to edit a Class

$username="test";                       // Database Username
$password="test";                       // Database Password
$database="ais coding rules";           // Database To Connect To

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

if (2 == $what) {
        $editquery="UPDATE class SET Class_Name='$classname', Class_Definition='$class_def', Brand_Desc='$brand_desc', Brand_Break='$brand_break',
                    Media_Spec='$media_spec', Catagory_Number1='$catnum' WHERE Class_Num='$classnum'";
        mysql_query($editquery) or die (mysql_error());
        echo "The record has been UPDATED. Click <a href=\"http://nmr001mrkint00.enterprisenet.org:8081/Operations/AIS/Code_Rules2.php\">here</a> to return to Coding Rules<br /><br />.";

}
elseif (3 == $what) {
        $delquery="DELETE FROM class WHERE Class_Num='$classnum'";
        mysql_query($delquery) or die (mysql_error());
        echo "The record has been DELETED. Click <a href=\"http://nmr001mrkint00.enterprisenet.org:8081/Operations/AIS/Code_Rules2.php\">here</a> to return to Coding Rules<br /><br />.";
                
}
elseif($classid) {                          // Create and populate a form for editing class nfo

        $cquery="select * FROM class WHERE Class_Num = '$classid'";
        $cqueryrez=mysql_query($cquery) or die (mysql_error());

        while ($i = mysql_fetch_array($cqueryrez, MYSQL_BOTH)) {
               /* $test =mysql_num_fields($cqueryrez);
                echo $test;
                echo $i['Brand_Desc']; */
                echo "<form name=\"class-change\" action=\"Cat_list_action2.php\" method=\"POST\" enctype=\"multipart/form-data\">";
                echo "<table border=\"0\">";
                echo "<tr><th>Category Number</th><td><input type=\"text\" name=\"cat-num\" size=\"5\" value=".$i['Catagory_Number1']."></td></tr>";

                echo "<tr><th>Class Number</th><td><input type=\"text\" name=\"class-num\" size=\"5\" value=\"".$i['Class_Num']."\"></td></tr>";
                echo "<tr><th>Class Name</th><td><input type=\"text\" name=\"name\" size=\"66\" value=\"".$i['Class_Name']."\"></td></tr>";
                echo "<tr><th>Class Definition</th><td><textarea rows=\"5\" cols=\"50\" name=\"class-definition\">".$i['Class_Definition']."</textarea></td></tr>";
                echo "<tr><th>Brand Description</th><td><textarea rows=\"5\" cols=\"50\" name=\"brand-desc\">".$i['Brand_Desc']."</textarea></td></tr>";
                echo "<tr><th>Brand Break</th><td><textarea rows=\"5\" cols=\"50\" name=\"brand-break\">".$i['Brand_Break']."</textarea></td></tr>";
                echo "<tr><th>Media Spec</th><td><textarea rows=\"5\" cols=\"50\" name=\"media-spec\">".$i['Media_Spec']."</textarea></td></tr>";
                
                echo "<tr><th>UPDATE<input type=\"radio\" name=\"what\" value=\"2\" /></th><th class=\"right\">DELETE<input type=\"radio\" name=\"what\" value=\"3\" /></th></tr>";
                echo "<tr><td colspan=\"2\">&nbsp;</td></tr>";
                echo "<tr><td><input type=\"submit\" value=\"Submit\"></td><td>&nbsp;</td></tr>";
                echo "</table>";
                echo "</form>";
        }
}
elseif($showme) {                           // Create tables that display all class information

        $query="select class_name as Class, class_num as class_num, class_definition as definition,
        brand_desc as bdesc, brand_break as bbreak, media_spec as mspec FROM class WHERE Class_Num = '$showme'";
        $catresult=mysql_query($query)or die(mysql_error());

        while($row=mysql_fetch_array($catresult, MYSQL_BOTH)) {
                echo "<table width=\"90%\" align=\"center\" border=\"1\" >";
                echo "<tr><th colspan=\"2\">".$row["Class"]."</th></tr>";
                echo "<tr><th>Class Definition</th><th>Brand Description</th>";
                echo "<tr><td width=\"50%\" valign=\"top\">".$row["definition"]."</td><td width=\"50%\" valign=\"top\">".$row["bdesc"]."</td></tr>";
                echo "<tr><th>Brand Breakout Rulings</th><th>Media Specific Rulings</th>";
                echo "<tr><td width=\"50%\" valign=\"top\">".$row["bbreak"]."</td><td width=\"50%\" valign=\"top\">".$row["mspec"]."</td></tr>";
                echo "<tr><td colspan=\"2\">&nbsp;</tr>";
                echo "<tr><th class=\"pnotes\" colspan=\"2\"><b><a href=Cat_list_action2.php?classid=".$row['class_num']."><i>EDIT THIS CLASS</i></a></th></tr>";
                echo "</table>";
                echo "<br />";
        }
}
elseif($giveme) {                           // Create tables that display all the classes for the selected category

        $q1="SELECT Catagory_Name FROM catagories WHERE Catagory_Number = '$giveme'";
        $r1=mysql_query($q1)or die(mysql_error());
        while($row1=mysql_fetch_array($r1, MYSQL_BOTH)) {
                echo "<h2>".$row1["Catagory_Name"]."</h2>";
                $disclass = $row1["Catagory_Name"];
        }
        
        $gquery="select a.catagory_name as Category, b.class_name as Class, b.class_num as class_num, b.path_to_file as path, b.class_definition as definition,
        b.brand_desc as bdesc, b.brand_break as bbreak, b.media_spec as mspec FROM catagories a join class b ON a.catagory_number = b.catagory_number1 WHERE
        Catagory_Number = '$giveme'";
        $gresult=mysql_query($gquery)or die(mysql_error());
        echo "<p>Here is a list of all the classes in ".$disclass."</p>";
        echo "<ul>";
        while($row=mysql_fetch_array($gresult, MYSQL_BOTH)) {
                echo "<li><a href=Cat_list_action2.php?showme=".$row["class_num"].">".$row["Class"]."</a></li>";
        }
        echo "</ul>";
}
// Close The SQL Connection
mysql_close();
include ("includes/foot.php");
?>

www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#7 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 28 August 2006 - 06:34 PM

You want to store the information in the format that comes from the user (other than using stripslashes and mysql_real_escape_string) on the data when it is inserted in to the DB.

You want to use nl2br() on the data when it is displayed.

Ken

#8 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 28 August 2006 - 06:35 PM

You should use the nl2br function rather than doing it manually. Use the nl2br function when you get the data out of the database.

for exmaple nl2br($row["definition"])

#9 mATOK

mATOK
  • Members
  • PipPipPip
  • Advanced Member
  • 84 posts
  • LocationToronto, Ontario

Posted 28 August 2006 - 06:39 PM

doing this when extracting is better than

$class_def = nl2br(nl2br($_POST['class-definition']));

Is it faster?

Thanx everyone :D
"Not everything that counts can be counted, and not everything that can be counted counts."

W3 Web Tutorials | Check Your html | CSS layout techniques | ALA | Zen Garden | CSS Links

Navs with flavor | CSS Playground | CSS boxes

#10 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 28 August 2006 - 06:47 PM

Use 1 nl2br and not two. I would not recommend you to format your text when you put it in the database. You should format the text how you want it when you retreive it from the database.

#11 mATOK

mATOK
  • Members
  • PipPipPip
  • Advanced Member
  • 84 posts
  • LocationToronto, Ontario

Posted 28 August 2006 - 06:49 PM

Makes sense, thanx again everyone!
"Not everything that counts can be counted, and not everything that can be counted counts."

W3 Web Tutorials | Check Your html | CSS layout techniques | ALA | Zen Garden | CSS Links

Navs with flavor | CSS Playground | CSS boxes




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users