...

View Full Version : nl2br help...



40esp
05-31-2007, 03:43 AM
okays. I have my own php mysql blog, but I want it to automatically insert line breaks. I am a little inexpierienced with php but I know enough I need to use nl2br. Its getting it to work thats the problem. Below I'll have my pages raw code without nl2br.

Can anyone help me set up nl2br on that page? I want it to post to the mysql database with linebreaks.

I need The Textbox "post" To be nl2br compatible.

Thanks a Lot If You Can Help Me!



<?php require_once('../Connections/data.php'); ?>
<?php
if (!isset($_SESSION)) {
session_start();
}
$MM_authorizedUsers = "";
$MM_donotCheckaccess = "true";

// *** Restrict Access To Page: Grant or deny access to this page
function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) {
// For security, start by assuming the visitor is NOT authorized.
$isValid = False;

// When a visitor has logged into this site, the Session variable MM_Username set equal to their username.
// Therefore, we know that a user is NOT logged in if that Session variable is blank.
if (!empty($UserName)) {
// Besides being logged in, you may restrict access to only certain users based on an ID established when they login.
// Parse the strings into arrays.
$arrUsers = Explode(",", $strUsers);
$arrGroups = Explode(",", $strGroups);
if (in_array($UserName, $arrUsers)) {
$isValid = true;
}
// Or, you may restrict access to only certain users based on their username.
if (in_array($UserGroup, $arrGroups)) {
$isValid = true;
}
if (($strUsers == "") && true) {
$isValid = true;
}
}
return $isValid;
}

$MM_restrictGoTo = "unauthorized.php";
if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) {
$MM_qsChar = "?";
$MM_referrer = $_SERVER['PHP_SELF'];
if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&";
if (isset($QUERY_STRING) && strlen($QUERY_STRING) > 0)
$MM_referrer .= "?" . $QUERY_STRING;
$MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer);
header("Location: ". $MM_restrictGoTo);
exit;
}
?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$insertSQL = sprintf("INSERT INTO blog (title, post, dateposted, postedby, avatar) VALUES (%s, %s, %s, %s, %s)",
GetSQLValueString($_POST['title'], "text"),
GetSQLValueString($_POST['post'], "text"),
GetSQLValueString($_POST['dateposted'], "date"),
GetSQLValueString($_POST['postedby'], "text"),
GetSQLValueString($_POST['avatar'], "text"));

mysql_select_db($database_data, $data);
$Result1 = mysql_query($insertSQL, $data) or die(mysql_error());

$insertGoTo = "posted.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}

mysql_select_db($database_data, $data);
$query_avatars = "SELECT * FROM avatars";
$avatars = mysql_query($query_avatars, $data) or die(mysql_error());
$row_avatars = mysql_fetch_assoc($avatars);
$totalRows_avatars = mysql_num_rows($avatars);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Post A New Blog!</title>
<style type="text/css">
<!--
body {
background-color: #222b33;
}
a:link {
color: #333333;
text-decoration: none;
}
a:visited {
color: #333333;
text-decoration: none;
}
a:hover {
color: #CCCCCC;
text-decoration: underline;
}
a:active {
color: #333333;
text-decoration: none;
}
.style1 {font-size: 12px}
-->
</style>
<script type="text/javascript">
function validateForm() {
with (document.form1) {
var alertMsg = "The following\nhave been left empty:\n";
if (title.value == "") alertMsg += "\ntitle";
if (post.value == "") alertMsg += "\npost";
if (postedby.value == "") alertMsg += "\npostedby";
if (alertMsg != "The following\nhave been left empty:\n") {
alert(alertMsg);
return false;
} else {
return true;
} } }
</script>
</head>

<body>
<table width="358" height="38" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#000000" bgcolor="#FFFFFF">
<tr>
<td><div align="center"><strong>Post A Blog! </strong></div></td>
</tr>
</table>
<p>&nbsp;</p>
<table width="552" height="286" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#000000" bgcolor="#FFFFFF">
<tr>
<td><form method="post" name="form1" onsubmit="return validateForm()" action="<?php echo $editFormAction; ?>">
<p>&nbsp;</p>
<table align="center">
<tr valign="baseline">
<td nowrap align="right"><div align="center"><strong>Title:</strong></div></td>
<td>
<div align="center"><strong>
<input name="title" type="text" class="style1" value="" size="32">
</strong></div></td>
</tr>
<tr align="center" valign="middle">
<td nowrap><div align="center"><strong>Post:</strong></div></td>
<td>
<div align="center"><strong>
<textarea name="post" cols="50" rows="10" class="style1"></textarea>
</strong></div></td>
</tr>

<tr valign="baseline">
<td nowrap align="right"><div align="center"><strong>Posted By: </strong></div></td>
<td>
<div align="center"><strong>
<input name="postedby" type="text" class="style1" value="" size="32">
</strong></div></td>
</tr>
<tr valign="baseline">
<td nowrap align="right"><div align="center"><strong>Avatar:</strong></div></td>
<td><div align="center"><strong>
<select name="avatar" class="style1">
<option value="http://71.13.89.34/v4bin/PostIcons/none.gif">----Select Avatar----</option>
<?php
do {
?>
<option value="<?php echo $row_avatars['imglink']?>" ><?php echo $row_avatars['title']?></option>
<?php
} while ($row_avatars = mysql_fetch_assoc($avatars));
?>
</select>
</strong></div></td>
<tr valign="baseline">
<td nowrap align="right"><div align="center"></div></td>
<td><div align="center"><strong>
<input type="submit" value="Post New Blog">
</strong></div></td>
</tr>
</table>
<input name="dateposted" type="hidden" value="<?php
echo date("Y-m-d h:i:s", time());
?>
" size="32" />
<input type="hidden" name="MM_insert" value="form1">
</form>")
<p align="center"><strong><a href="directory.php"><em>Go Back</em></a></strong></p></td>
</tr>
</table>
<p>&nbsp;</p>
</body>
</html>
<?php
mysql_free_result($avatars);
?>

Fumigator
05-31-2007, 06:36 AM
You don't need to store the <br /> tags in your table. Just store the text as it is entered and then when you display that text elsewhere, use nl2br() to properly display <br />s.

40esp
05-31-2007, 08:48 PM
How Would I Do That?

Fumigator
05-31-2007, 08:57 PM
echo nl2br($columnFromYourTable);

40esp
05-31-2007, 10:04 PM
thanks!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum