How to insert commas into text fields - php

<? echo $rows['general']; ?><? echo $rows['comment']; ?>
'general' is a text field but 'comment' is a checkmark item. It works fine if I type in the text field or if I select the checkmark; but if I fill the text form and also check the checkmark then I would like them to be separated with a comma and space.

$separation = "";
if(!empty($rows['general']) && !empty($rows['comment'])) {
$separation = ", ";
}
echo $rows['general'] . $separation . $rows['comment'];

if thous are the only 2 field:
echo implode(',', array_filter($rows));
otherwise you can use trim to remove the , if not needed:
echo trim("{$rows['general']},{$rows['comment']}", ', ');

Something like this?
if (($rows['general'] != "") && ($rows['comment'] != "")) {
echo $rows['general'] + ", "+ $rows['comment'];
} else {
echo $rows['general'] + $rows['comment'];
}

Related

Replace first part of string with db string

Could u help me with code ?
I want to add t that script an option like :
when I type ex. data/pag , then script replaces data with key from mysql db and echo it like: replaced/pag . I mean it only replaces text before first and single "/".
Code :
$wyszukaj=$_GET['wyszukaj'];
$min_wartosc = 1;
if(strlen($wyszukaj)>=$min_wartosc)
{
$wyszukaj =htmlspecialchars($wyszukaj);
$zapytanie=$baza->prepare("SELECT * FROM wyszukiwarka WHERE nazwa LIKE :wyszukaj");
$zapytanie->bindValue('wyszukaj', $_GET['wyszukaj'],PDO::PARAM_STR);
$zapytanie->execute(array($wyszukaj));
if($zapytanie->rowCount()>0)
{
while($wynik=$zapytanie->fetch())
{
echo $wynik['nazwa'] . "<br>";
header('location:http://'.$wynik['login']);
}
}
else
{
echo "brak <b> " . $wyszukaj . " </b> w bazie danych ...";
}
}
else
{
echo "";
}

Protect Email with PHP - output is two words

This is my coding:
<?php
function protected_email($phpemail,$text='',$echo=true)
{
$pieces = explode("#", $phpemail);
$return = '
<script type="text/javascript">
var a = "<a href=\'mailto:";
var b = "' . $pieces[0] . '";
var c = "' . $pieces[1] .'";
var d = "\' class=\'email\'>";
var e = "</a>";';
if($text != ''){
$return .= 'document.write(a+b+"#"+c+d+\''.$text.'\'+e);';
}else{
$return .= 'document.write(a+b+"#"+c+d+b+"#"+c+e);';
}
$return .= '
</script>
<noscript>Please enable JavaScript to view emails</noscript>';
if($echo == true)
{
echo $return;
}else{
return $return;
}
}
?>
<?php echo protected_email('change#email.com', Their Title); ?>
I want to display the person's title instead of their email address, but I cannot get both words to show in their title, unless I remove the space between. How do I get it to dsiplay both words?
Display this:
Their Title
Rather display than:
change#email.com
I'm sure it is something really simple, but I am at a mental block right now.

Split text from the database in two columns

I have a bootstrap based website with 2 columns(col-lg-6 and col-lg-6).
I have the ability to add text from an admin panel (articles).
How can I make it so it splits in the second div (col-lg-6) after a specific number of characters or after I insert a character or something that will represent the breakage?
I prefer a PHP solution, but Javascript/Jquery would do too
edit *
if(isset($_GET['chapter']) && !empty($_GET['chapter'])){
$chapterid = (int)$_GET['chapter'];
$chaptertitle = mysql_query("SELECT bookname FROM books WHERE id=$chapterid");
$chaptertitle = mysql_fetch_array($chaptertitle);
$chaptertitle = $chaptertitle[0];
$chapter = mysql_query("SELECT bookContent FROM books WHERE id=$chapterid");
$chapter = mysql_fetch_array($chapter);
$chapter = $chapter[0];
$bookcontent = mysql_query("SELECT * FROM books WHERE bookid=$id");
$bookcontainer = '';
while($row = mysql_fetch_array($bookcontent)){
$bookcontainer .= '<h2>'.$row['bookname'].'</h2>' . $row['bookContent'];
}
and this is the place where the code is printed
<div class="row">
<div class="col-lg-6" id="paragraphs">
<div style="width:100%;">
<?php
//if($errors == false){
echo $chapter;
//}
//else{
//echo 'erorr madarfaker';
//}
?>
</div>
</div>
<div class="col-lg-6"></div>
</div>
If anyone is interested in this: I made 2 fields in the admin panel and the variable that was being sent to the database was "field 1 + ' | ' + field 2" and then I used explode to display the content on 2 sides, separator being " | "
Choose an arbitrary character for your split, like "|". Based on your posted code, use this as your echo statement:
echo str_replace($chapter, "|", "</div></div><div class='col-lg-6' id='paragraphs'><div style='width:100%;'>");
I am not sure if this solves your problem, but you can try :
<?php
// An array with your database data :
$arr = array('val1', 'val2', 'val3', 'val4');
$col1 = "";
$col2 = "";
$flag=false;
for($i=0; $i<sizeof($arr); $i++){
//Break condition :
$flag = ($arr[$i] == "val3" || $flag != true ) ? true : false;
if(!$flag){
$col1 .= $arr[$i];
}
else{
$col2 .= $arr[$i];
}
}
$html = "";
if($col1 != ""){
$html .= "<div class='col-lg-6'>".$col1."</div>";
}
if($col2 != ""){
$html .= "<div class='col-lg-6'>".$col2."</div>";
}
// The two div with your content (if the break rule is found) :
echo $html;
?>
If anyone is interested in this: I made 2 fields in the admin panel and the variable that was being sent to the database was "field 1 + ' | ' + field 2" and then I used explode to display the content on 2 sides, separator being " | "

combine multiple strings into one textarea

Can someone tell me how can I make this code under one textarea. I'm fairly new to php so any bit of advice would be helpful thanks.
if ($test != "")
print("<p>" . format_comment($test) . "</p>\n");
if ($test1 != "")
print("<p>" . format_comment($test1) . "</p>\n");
Cheers.
This is very easy to achieve, try it like this:
// Print the top
print("<p><hr>");
// Print $text if set
if ($test != "")
print(format_comment($test));
// Print $text1 if set
if ($test1 != "")
print(format_comment($test1));
// Print the bottom
print("<hr></p><br />");
Altough, I recommend you to use echo instead of print, because echo is a more common used function. Using echo you can do it like this:
echo "<p><hr>";
if ($test != "")
echo format_comment($test);
if ($test1 != "")
echo format_comment($test1);
echo "<hr></p><br />";
I might understood your question wrong, if you just want to put the content from the variables into a text area, you can do it as follows:
$content = "";
if ($test != "")
$content .= "<p><hr>" . format_comment($test) . "<hr></p><br />";
if ($test1 != "")
$content .= "<p><hr>" . format_comment($test1) . "<hr></p><br />";
echo "<textarea>" . $content . "</textarea>";
EDIT: It looks like you've used \n to get a new line, you should use <br /> instead in HTML. Simply replace all the \nparts with <br /> to fix the problem.
Hope this helps!
You may try by concatenating both content
<?php
require "include/bittorrent.php";
dbconn();
stdhead("Tags");
begin_main_frame();
begin_frame("Tags");
$test = $_POST["test"];
$test1 = $_POST["test1"];
$content="<p><hr>";
if ($test != "")
$content .= format_comment($test);
if ($test1 != "")
$content .= format_comment($test1);
$content=$content. "<hr></p><br />";
?>
<textarea id="test" name="test"><?php echo $content; ?></textarea>
<?php
end_frame();
end_main_frame();
stdfoot();
?>

Form submission adds a <br> in front of the beginning text in the form

Ok, I am using a PHP editing form (previous person built) that submits data into an MSSQL database this works fine however when the information is submitted into the database and later retrieved the the form page (for editing) there is a <br> at the beginning of the text. If the fields start with a <ul> that <br> is not inserted. I have been trying to figure out how to stop this. One thing I tried ends up removing all <br> in the text which makes the formatting disappear, How can I keep it from inserting the <br> at the top but keeping the <br> everywhere else? here is the code I have: ( I am giving you everything up to the first field where I have the issue appear)
function fromhtml ($x) {
$x = preg_replace("/<p>/i","\n\n",$x);
//$x = preg_replace("/<br>/i","\n",$x);
$x = preg_replace("/<li>/i","\n<li>",$x);
return $x;
}
$PHP_SELF = $_SERVER['PHP_SELF'];
$course_id = #$_GET["course"];
if ($course_id == "") {
$sqlquery = "SELECT id, title, goals, outline, reference, deliverymode, updated2 = CONVERT(VARCHAR(19), updated, 120)
FROM courses WHERE division_id = '$division_id' ORDER by id";
$result = mssql_query($sqlquery);
$number = mssql_num_rows($result);
print "<table border=1 id=\"content_table\"><tr><th>ID</th><th>Title</th><th>Status</th><th>Last modified</th></tr>\n";
$i = 0;
while ($number > $i) {
$course_id = mssql_result($result,$i,"id");
$reference = mssql_result($result,$i,"reference");
$updated = mssql_result($result,$i,"updated2");
print "<tr><td>";
if ($reference == "") {
print "<a href=$PHP_SELF?division=$division_id&course=$course_id>$course_id</a>";
} else {
print "$course_id";
}
print "</td><td>";
print mssql_result($result,$i,"title");
print "</td><td>";
if ($reference == "") {
if ( (mssql_result($result,$i,"goals")=="") and (mssql_result($result,$i,"outline")=="") ) {
print "<b>No syllabus, or incomplete</b></td>";
}
} else {
print "Based on $reference";
}
print "</td><td>$updated</td>\n";
print "</tr>\n";
$i++;
}
print "</table>";
exit;
}
$sqlquery = "SELECT * FROM courses WHERE id = '$course_id'";
$result = mssql_query($sqlquery);
$number = mssql_num_rows($result);
if ($number == 0) {
print "<html><body>";
print "No course with the ID \"$course_id\" exists in the course database.";
print "</body></html>";
exit;
}
$i = 0;
$description = fromhtml(mssql_result($result,$i,"description"));
print "<html><head><title>DACC Course Syllabus - $course_id</title>";
print "<script language=\"JavaScript\" type=\"text/javascript\" src=\"/rte/richtext2.js\"></script>";
?>
<script language="JavaScript" type="text/javascript">
<!--
initRTE("/rte/images/", "/rte/", "");
//-->
function submitForm() {
updateRTEs();
document.edit-course.submit();
return false;
}
</script>
<?php
print "</head><body>\n";
//Begin Form
print "<form name=\"edit-course\" id=\"edit-form\" method=\"post\" action=\"write.php?course=$course_id\" onSubmit=\"return submitForm()\">\n";
print "<fieldset>";
print "<label for=\"description\">Course Description</label>";
$description=addslashes(preg_replace('`[\r\n]`','',$description));
?>
<script language="JavaScript" type="text/javascript">
<!--
writeRichText('description', '<?php print $description; ?>', 200, 300, true, false);
//-->
</script>
<?php
print "</fieldset>";
It that BR is at very very beginning of string then use this
$x = preg_replace("/^<br(\/|)>/i","\n",$x);
instead of that commented line
preg_replace has a fourth parameter, $limit. Setting this to 1 would limit the preg_replace to the first occurence. Alternatively you could use the regular expression ^<br>. This would only replace <br> when it is at the beginning of the string.
Edit: Sorry, I misunderstood your problem. Use $variable=preg_replace("/^<br>/i", "", $variable); to strip the first <br>.

Categories