Jump to content

detecting new lines?


mATOK

Recommended Posts

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?
Link to comment
https://forums.phpfreaks.com/topic/18914-detecting-new-lines/
Share on other sites

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

Here is my code :)

[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");
?>


[/code]
Link to comment
https://forums.phpfreaks.com/topic/18914-detecting-new-lines/#findComment-81738
Share on other sites

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 [code]if (2 == $what) {
        str_replace("\n","<br />",$class_def);[/code]

which in my html does not give me new lines as I type, but when I choose to edit I see the breaks.
Link to comment
https://forums.phpfreaks.com/topic/18914-detecting-new-lines/#findComment-81743
Share on other sites

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


[code]<?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");
?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/18914-detecting-new-lines/#findComment-81746
Share on other sites

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
Link to comment
https://forums.phpfreaks.com/topic/18914-detecting-new-lines/#findComment-81747
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.