PHP - Forum Breaks
Just a general question, but could someone help me and tell me how to allow the user to enter a <br> without actually typing it.
Like when they're typing in the forums, I want it to automatically go on a new line when they hit the ENTER key. Or could someone lead me to a guide? I tried searching everywhere and just can't seem to find it. Similar TutorialsI'm trying to insert the contents of a textarea into a MySQL database but I am wondering what the best way is to preserve the users line breaks. I know that you can use nl2br() to convert "\n" into "<br />" when the retrieve data from the database but I am looking at a way to insert these line breaks when I enter the data. Thanks for any help. I have an XML file (differences.xml) <element> <item code="lM" name="castle" type="building"> <cost>5000</cost> </item> ...more items.... </element> I have parser.php whih parses the XML and displays all item names in a checklist for the user to select items. These selected items are method=post back to parser.php which re-parses the XML where selected item = item name and SHOULD then display all selected items' names (and more): <?php if (isset($_GET['formSubmit'])) { $option = $_POST['option']; $option = array_values($option); if (!empty($option)){ $xml = simplexml_load_file('differences.xml'); $i = 0; $count = count($option); while($i < $count) { $selected = $xml->xpath("//item[@name='".$option[$i]."']"); echo $selected[$i]['name']; $i++; } }else{ echo "No items selected, Please select:"; } }else{ $xml = simplexml_load_file('differences.xml'); $object = $xml->xpath('//item'); $count = count($object); $i = 0; echo "<form method='POST' action='parser.php'>"; while($i < $count){ $xmlname = $object[$i]['name']; echo "<input type='checkbox' name='option[".$i."]' value='$xmlname'>".$xmlname; echo "<br>"; $i++; } echo "<br><input type='submit' name='formSubmit' value='Proceed'><br>"; echo "</form>"; } ?> The problem is, the parser is only displaying the first selected item and isn't outputting the rest. To clarify the code: - The first half executes only if items have been previously selected in the form. If no items were selected, it goes to the secod half, which parses the XML file and presents the checklist form for the user to select items. THIS WORKS GREAT. - After pressing the submit button, the items are sent using POST and the page is reloaded. THIS WORKS GREAT. - After reloading, the first half executes and finds the $_POST and resets the array so that it works with the coming loop. THIS WORKS GREAT. - The loop parses the XML and returns only those item names that have been selected in the $_POST. THIS DOES NOT WORK. So my question is: what's wrong with my code? The array $options has all the proper information, but the parser is only outputting the first item. All consecutive loops result in empty space with no output. I've been at this for 2 weeks and can't find the solution. Any help is appreciated! Here's the code that I have: Code: [Select] <?php if (get_magic_quotes_gpc()) { function undoMagicQuotes($array, $topLevel=true) { $newArray = array(); foreach($array as $key => $value) { if (!$topLevel) { $key = stripslashes($key); } if (is_array($value)) { $newArray[$key] = undoMagicQuotes($value, false); } else { $newArray[$key] = stripslashes($value); } } return $newArray; } $_GET = undoMagicQuotes($_GET); $_POST = undoMagicQuotes($_POST); $_COOKIE = undoMagicQuotes($_COOKIE); $_REQUEST = undoMagicQuotes($_REQUEST); } function safe($value){ return mysql_real_escape_string($value); } $subject1 = "It's just a test."; $message1 = // $subject1 and message1 are actually submitted from a form on the page... "Here's a test. // so this section would really be $subject = safe($_POST['subject']); same w/ message // I did it this way to simplify... I'm pretty sure it would be the same either way. Thanks, Me"; // this is what is typed in the text area of the form. $subject = safe($subject1); $message = safe($message1); ?> When these variables are mailed using php mail(), they display like this: Code: [Select] Subject: It\'s just a test. Message: Here\'s a test.\r\n\r\nThanks,\r\nMe When they are inserted into a database, they are inserted correctly, as they were typed. My question is this: Is there a way to mail these and have them display correctly? Should I just insert them into the database, then select them again to mail? Thanks for any help. php5 I am using json_encode() to store an array of data as a string in a mysql data base. I then extract the sting from the data base and convert the back into an array using json_decode. This works great up until I have any line breaks in any of the data. When there is a line break, the json_decode() function chokes and returns nothing. I would really like to keep the line breaks. Has anyone come across this issue/have an suggestions/solutions? Okay, so I keep crashing my server while trying to get a simple script to run. Firstly, here's the code. <?php $dbhost = 'localhost'; $dbuser = 'root'; $dbpass = ''; $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql'); $dbname = 'businessletters'; mysql_select_db($dbname); $files = getFiles("parseme.txt"); for($i=0;$i<200;$i++) { $file = getReadFile($files[$i][0]); insertIntoDb($files[$i][1], $file); //echo "Inserted successfully - " . $files[$i][0] . "<br />"; } function getFiles($file) { $fh = fopen($file, "rb"); $data = fread($fh, filesize($file)); fclose($fh); $dataArr = explode("\n", $data); for($i=0;$i<count($dataArr);$i++) { $dataArr[$i] = explode(",", $dataArr[$i]); } return($dataArr); } function getReadFile($file) { $fh = fopen($file, "rb"); $data = fread($fh, filesize($file)); fclose($fh); return $data; } function insertIntoDb($title, $body) { $sql = "insert into letters values('', '".mysql_real_escape_string($title)."', '".mysql_real_escape_string($body)."')\n"; //mysql_query($sql) or die("Error inserting"); echo $sql; } mysql_close(); ?> And now, here's what it's supposed to do. Open and read File A - this file has file names and titles in a CSV file. File,Title is the format. Then the loop starts. Here I would like to do everything in one run, which could be done by counting the records in the CSV file, which I can do with count(), but even 200 is too much. I have 600. Anyway, it goes through the CSV file and opens every file referenced and pulls out the content. Then it creates a simple SQL statement where the content is entered into the database. And the problem is that Apache keeps crashing on me... There must be a better way for me to do this without having my server crash on me! I have a form which has a textarea in it. When the form is submitted the contents are submitted to a MySQL database and the contents or the textarea are stored in a text variable. What is the best was to preserve line breaks in the textarea? At the moment if the user enters to separate paragraphs into the textarea they will be merged into one. Thanks for any help. I have a list which is echo line by line from a database using the ID number as the identifier. However, the echo has now stopped because I have removed 3 lines. Instead of skipping from ID13 to ID17 it stops at ID13 so whatever else goes into the database it will not display it. Can anyone advise why this may have happened? ID 12 ID 13 break break break ID 17 ID 18 ID 19 Hey I am trying to write to a csv file with the data in my database. My problem I am having is that when I echo out to the browser everything is fine, but when I try writing to the file I get a lot of line breaks and that throws everything off for the csv file. Here's my code: Code: [Select] $query = "SELECT * FROM $table"; $result = mysql_query($query); $numcolumns = mysql_num_fields($result); $numrows = mysql_num_rows($result); $file = "testFile.csv"; $fh = fopen($file, 'w') or die("can't open file"); while($row = mysql_fetch_array($result)) { for($i = 0; $i <= ($numcolumns - 1); $i++) { if($i < ($numcolumns - 1)) { $string = $row[$i] . ","; echo $string; fwrite($fh, $string); } else { $string = $row[$i]; echo $string; fwrite($fh, $string); } } $string = "\n"; echo "<br />"; fwrite($fh, $string); } fclose($fh); My output to the browser looks like this for one record.. which is how it should look: (Had to edit the content a bit for confidential reasons) Code: [Select] ######,12/31/99 ,123 Drive,City,TX ,##### ,County And here is what I get in the csv file when I open it up on notepad: Code: [Select] ######,12/31/99 ,123 Drive ,City ,TX ,##### ,County Any idea where these line breaks are coming from? Is it because the data is being pulled from a database? Any help on how to fix this would be great!! Thanks. probably another overly-newbie-ish question, but I built a series of pages for inputting data. Page 1 inputs data to table 1, pulls the item id and passes it to page 2 to input to table 2. This all works fine. But now I want to add a header and footer to include the site design. And when I add my <? include("header.php"); ?>, it stops working. I tried naming the header something else, in case it was just a duplicate name somewhere, but the problem is still there. When i submit, it goes to the next page and generates this error: Warning: Cannot modify header information - headers already sent by (output started at header.php:62) in file.php on line 65 Is there a way to include a header without it creating this issue? Or do I put it lower down in the code? Here's the main idea of the code: Code: [Select] if (isset($_POST['submitted'])) { $errors = array(); $descriptor1 = trim($_POST['plant_id']); $descriptor2 = trim($_POST['existence_status']); $descriptor3 = etc. etc. if (empty($errors)) { require ('connection.php'); $query = "INSERT INTO habits (plant_id, existence_status, reproduction_methods, etc.) VALUES ('$descriptor1', '$descriptor2', etc.)"; $result = @mysql_query ($query); $plant_id=mysql_insert_id(); header ("Location: thisfile.php?var1=$plant_id"); and, if it helps any, here's the header file: Code: [Select] <div id="ugg" style="background-color:#CDE472; height:80px; padding:10px;"> <h1>Twigzy</h1> </div> <style type="text/css" media="screen"> /**************** menu coding *****************/ #menu { width: 100%; background: #eee; float: left; } #menu ul { list-style: none; margin: 0; padding: 0; width: 12em; float: left; } #menu a, #menu h2 { font: bold 11px/16px arial, helvetica, sans-serif; display: block; border-width: 1px; border-style: solid; border-color: #ccc #888 #555 #bbb; margin: 0; padding: 2px 3px; } #menu h2 { color: #fff; background: #324F17; text-transform: uppercase; } #menu a { color: #000; background: #efefef; text-decoration: none; } #menu a:hover { color: #a00; background: #fff; } #menu li {position: relative;} #menu ul ul { position: absolute; z-index: 500; } #menu ul ul ul { position: absolute; top: 0; left: 100%; } div#menu ul ul, div#menu ul li:hover ul ul, div#menu ul ul li:hover ul ul {display: none;} div#menu ul li:hover ul, div#menu ul ul li:hover ul, div#menu ul ul ul li:hover ul {display: block;} </style> <!--[if IE]> <style type="text/css" media="screen"> #menu ul li {float: left; width: 100%;} </style> <![endif]--> <!--[if lt IE 7]> <style type="text/css" media="screen"> body { behavior: url(csshover.htc); font-size: 100%; } #menu ul li a {height: 1%;} #menu a, #menu h2 { font: bold 0.7em/1.4em arial, helvetica, sans-serif; } </style> <![endif]--> <!-- start menu HTML --> <div id="menu"> <ul> <li><a href="link"">Home</a> </li> </ul> <ul> <li><h2>About</h2> <ul> <li><a href="link0.com">About site</a> </li> </ul> </li> </ul> <ul> <li><h2>Search for a plant</h2> <ul> <li><a href="link1.php">Basic Search</a></li> <li><a href="link2.php">Search by Leaf</a></li> <li><a href="link3.php">Search by Flower</a></ul> </li> </ul> <ul> <li><h2>Login</h2> <ul> <li><a href="link4.php">Login</a></li> <li><a href="asdf.html">asdfasdf</a><!-- fully working sample --> <ul> <li><a href="test.html">asdf</a> </li> </ul> </li> </ul> </li> </ul> </div> OK, about ready to go postal, as I've been wasting inordinate amounts of time trying to resolve this issue! If I create basic HTML echo-ed content, PHP works fine. But if I try to reference any database related syntax, the whole PHP web page breaks! VERY FRUSTRATING. This works: <?php // Printing results in HTML echo "<html>\n"; echo "<head>"; echo "</head>"; echo "<body bgcolor=\"#CCCCCC\">"; echo "<CENTER><H1>Current Inventory</H1>"; but this blows up and just gives me a white page with nothing!: <?php // Printing results in HTML echo "<html>\n"; echo "<head>"; echo "</head>"; echo "<body bgcolor=\"#CCCCCC\">"; echo "<CENTER><H1>Current Inventory</H1>"; $dbhostip = "192.168.0.10"; $dbport = "5432"; $dbname = "testdata"; $dbuser = "postgres"; $dbpass = "password1"; $dbconn = pg_connect($dbhostip,$dbport,$dbname,$dbuser,$dbpass); echo $dbconn; if(!$dbconn) { echo "An error has occurred (line 17)\n"; exit; } $result = pg_query($dbconn, "SELECT * FROM inventory"); if(!$result){ echo "An error has occurred (line 24)\n"; exit; } $resultCheck = pg_num_rows($result); if ($resultCheck > 0) { while ($row = pg_fetch_assoc($result)){ echo $row['cid']; echo $row['item_title']; } } ?> Any help would be appreciated. It seems that anything with $whatever causes the issue...
I am constructing an XML file with PHP. I read information from a template XML file and then add elements and save it. The line breaks from the template are preserved in the saved file, but all the new elements are on one line. Is there a way to break them up so it is easier to read the file? Here is the original template: Code: [Select] <InternationalShippingServiceOption> <ShippingService> token </ShippingService> <ShippingServiceAdditionalCost currencyID="CurrencyCodeType"> AmountType (double) </ShippingServiceAdditionalCost> <ShippingServiceCost currencyID="CurrencyCodeType"> AmountType (double) </ShippingServiceCost> <ShippingServicePriority> int </ShippingServicePriority> </InternationalShippingServiceOption> Here is the saved information (all the ShipToLocation elements on one line): Code: [Select] <InternationalShippingServiceOption> <ShippingService>StandardInternational</ShippingService> <ShippingServiceAdditionalCost currencyID="CurrencyCodeType">26.994</ShippingServiceAdditionalCost> <ShippingServiceCost currencyID="CurrencyCodeType">44.99</ShippingServiceCost> <ShippingServicePriority>2</ShippingServicePriority> <ShipToLocation>BE</ShipToLocation><ShipToLocation>FR</ShipToLocation><ShipToLocation>IE</ShipToLocation><ShipToLocation>LU</ShipToLocation><ShipToLocation>MC</ShipToLocation><ShipToLocation>NL</ShipToLocation><ShipToLocation>GB</ShipToLocation><ShipToLocation>DE</ShipToLocation><ShipToLocation>IT</ShipToLocation></InternationalShippingServiceOption> I am using the $doc->formatOutput = true; option. I have developed the following code, which queries a MySQL database and returns multiple results using several WHILE loops: Code: [Select] <?php # if submit_hash is present, then pull up the attendance from specified hash if($_GET['submit_hash']) { # select all the hashers from the specified hash $query = " SELECT h.*, ha.* FROM hashes as h, hashers as ha, hash_records as hr WHERE hr.hash_id = " . $_GET['hash_id'] . " && hr.hash_id = h.hash_id && hr.hasher_id = ha.hasher_id ORDER BY ha.hasher_name "; $result = mysql_query($query) or die('Error selecting Hash attendance.'); $x = 1; while($row = mysql_fetch_array($result)) { if($x == 1) { echo '<strong>' . $row['hash_num'] . ' ' . $row['hash_name'] . ' - ' . date("D, d M Y", strtotime($row['hash_date'])) . '</strong><em>Total Hashers: ' . mysql_num_rows($result) . '</em>'; $x++; } # see if this person was the hare if($row['hare_id'] == $row['hasher_id'] || $row['hare2_id'] == $row['hasher_id'] || $row['hare3_id'] == $row['hasher_id']) { $hare = '- hare'; } else { $hare = ''; } echo $row['hasher_name'] . ' <b>' . $hare . '</b>'; } } else if($_GET['submit_hasher']) { # if submit_hasher is present, pull up all of their hashes and aliases # select all the hashes that this person has attended $a_query = " SELECT h.*, ha.* FROM hashes as h, hashers as ha, hash_records as hr WHERE hr.hash_id = h.hash_id && hr.hasher_id = ha.hasher_id && hr.hasher_id = " . $_GET['hasher_id'] . " ORDER BY h.hash_date DESC "; $a_result = mysql_query($a_query) or die('Error selecting the person\'s Hashes . '); $x = 1; while($a_row = mysql_fetch_array($a_result)) { if($x == 1) { echo '<strong>Hash Attendance Record For ' . $a_row['hasher_name'] . '</strong> <em>(' . mysql_num_rows($a_result) . ' total hashes)</em>'; $x++; } echo '#' . $a_row['hash_num'].' ' . $a_row['hash_name'] . ' on ' . date("D, d M Y", strtotime($a_row['hash_date'])); # see if this person was a hare if($a_row['hasher_id'] == $a_row['hare_id'] || $a_row['hasher_id'] == $a_row['hare2_id'] || $a_row['hasher_id'] == $a_row['hare3_id']) { echo ' - <b>hare</b>'; } echo ''; } echo ''; } echo ' <table width="100%"> <br /> <tbody> <tr> <br /> <td> '; ?> The problem is that everything that is returned from the WHILE loops is displayed in a single line. However, I want the data displayed with a line break between each result, like this: Header Return 1 from WHILE loop Return 2 from WHILE loop Return 3 from WHLIE loop How do I need to modify the code to make it display appropriately? Hello, i am trying to build a website with German language. I save all of my php pages as utf-8 without BOM. If i hard code every page, i have no problems. However, all of my pages are the same except for content between header and footer. I made a template header, individual content files and a template footer (all php files). index.php include "header.php"; include "pagecontent.php"; include "footer.php"; when i do this, i look at my pages and everything seems fine. However, my German characters become jumbled letters if i use the back button in the web browser to view the previous page. why is this happening? can i fix it? hello guys, suppose I want to echo out the text given below exactly as what has been set against, including the line breaks. How do I do it? coz each time i did it so, there s no line breaks but the output is usually a dense lump of words with no paragraphs. Code: [Select] I took her out it was a Friday night I wore cologne to get the feeling right We started making out and she took off my pants But then I turned on the TV And that's about the time she walked away from me Nobody likes you when you're 23 I'm still more amused by TV shows What the hell is ADD? My friends say I should act my age What's my age again? What's my age again? please help P.S. The whole set of phrase give above are treated as a single variable coz I use a WHILE loops when making an echo. tthanks Hi there. I have a javascript code that packages an array as a string and sends it to a php mailer, which then separates the string into an array for mailing using a preg_split regular expression that searches for a comma followed by an html tag.
$myWrongArray = preg_split("/,(?=<)/",$myVar); // lookahead splits the string when the comma is before a tag (the opening bracket < ) Everything works great EXCEPT when the string contains a degree sign or an &, in which case the array is returned up to the degree or & and then stops. I can’t for the life of me understand it. Any ideas? So I have this textarea in an HTML form that I need to convert the contents of from having multiple line breaks into only one line break for each. Example: I want to convert the following: Code: [Select] I went to the store. I had a good time. I ran into a friend. Into this: Code: [Select] I went to the store. I had a good time. I ran into a friend. Any help would be super appreciated. Hi All, I am exporting data to excel, but run into a problem if the text contains a line break. When it gets to the line break, it cuts off the rest of the text. Here is my printer code: Code: [Select] $file = 'Notes_Export'; $csv_output = array(); $tmp = array(); $tmp[] = 'Created On'; $tmp[] = 'Created By'; $tmp[] = 'Note'; $csv_output[] = '"' . implode('","', $tmp) . '"'; $sql = "SELECT pn.created_on, CONCAT( u.firstname,' ', u.lastname) AS created_by, pn.note FROM prop_notes pn LEFT JOIN users u ON pn.created_by = u.user_id "; $sql .= "WHERE pn.deleted_by IS NULL AND pn.archive = '0' AND pn.property_id = '".$_GET['pid']."'"; $result = mysqli_query($connect, $sql); while($rowr = mysqli_fetch_row($result)) { $tmp = array(); for ($j=0; $j<3; $j++) {$tmp[] = $rowr[$j];} $csv_output[] = '"' . implode('","', $tmp) . '"'; } $filename = $file."_".date("Y-m-d_H-i",time()); header("Content-type: application/vnd.ms-excel"); header("Content-disposition: csv" . date("Y-m-d") . ".csv"); // header( "Content-disposition: filename=".$filename.".csv"); header("Content-disposition: attachment; filename=".$filename.".csv"); print implode("\n",$csv_output) . "\n"; exit; I have a simple contact form with a textarea field, where the visitor can type a message in. However, if the user enters paragraphs or any type of line breaks, then the email that is sent shows the "\r\n" characters. I've tried using the nl2br function to no avail. Here's a sample code snippet: Code: [Select] $message = $_POST["message"]; // To send HTML mail, the Content-type header must be set $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; // Additional headers $headers .= 'From: '. $_POST["email"]; mail($to, $subject, $message, $headers); Am I missing something? Besides the "\r\n" characters showing, everything else works fine. Hi My problem is that text that is in my database are showing up next to each other in a line rather than on seperate lines, normally I would have just used <br> but as it's generated from my sql I don't know how to do it. Code: [Select] <ul id="headlines"> <?php foreach ( $results['articles'] as $article ) { ?> <a href=".?action=viewArticle&articleId=<?php echo $article->id?>"><?php echo htmlspecialchars( $article->title )?></a> <?php } ?> </ul> Anyone have any idea? Thanks |