How do I style my text inside an echo? - php

Part of the code is
while ($row = mysqli_fetch_assoc($result))
{
echo "$row[firstname] $row[lastname]";
echo "<hr>";
echo "<strong>Date Referred:</strong>     $row[cf_831]";
echo "<br/>";
echo "<strong>Date Today:</strong>       $today";
echo "<br/>";
}
How do I style this part for example?
"$row[firstname] $row[lastname]"

You can try
echo '<p class="mystyle">' . $row['firstname'] . " " . $row['lastname'] . '</p>';
Then add CSS
.mystyle {
}

Addition to Sidsec9's answer: if you are afraid of stylesheets, you can also consider:
echo '<p style="color: red; font-size: 10pt;">' . $row['firstname'] . " " . $row['lastname'] . '</p>';
You could also use styles to get rid of  , for example using margin-right or padding-right properties.

echo '<p id="name"> '. $row[firstname] $row[lastname] .'</p>';
Css you only using for name alone use id else use class and use the single css for all values.
#name
{
color: red;
}

You can wrap it in a class, and fix it with a stylesheet. I also changed the echo, you're allowed to close the PHP tag, do some HTML and then open PHP again.
<?=$variable?> is short echo, it is the same as <?php echo $variable; ?>.
while ($row = mysqli_fetch_assoc($result)){
?>
<span class="Example"><?=$row[firstname].' '.$row[lastname]?></span>
<hr>
<strong>Date Referred:</strong>     <?=$row[cf_831]?>
<br/>
<strong>Date Today:</strong>       <?=$today?>
<br/>
<?php
}
A better method would be making and html file, say example.com, and place <!-- FIRSTNAME -->,<!-- LASTNAME --> AND <!-- TODAY --> instead of the variables. Then, using php:
$totalResult = "";
$template_one = file_get_contents("example.html");
while ($row = mysqli_fetch_assoc($result)){
$item = $template_one;
$item = str_replace("<!-- FIRSTNAME -->", $row['firstname'], $item);
$item = str_replace("<!-- LASTNAME -->", $row['lastname'], $item);
$totalResult.= $item; // add to totel result
)
// $today doesnt change in the loop, do it once after the loop is done:
$totalResult = str_replace("<!-- TODAY -->", $today, $totalResult);
echo $totalResult; // Now you have the result in a pretty variable

Related

How can i make my paragraph go to the next line after a certain width

So a string is being entered from a database into a paragraph, when the paragraph has spaces the paragraph breaks perfectly but if the string has a word longer than the line it goes off the page as shown below, how can i make it go the the next line instead of off the page?
here is my php and html code:
<div id = "comment">
<?php
echo "<div style = 'border-bottom-style: solid;border-bottom-width:1px;'><p class = 'word'>" . $issue . "</p>";
echo "<p class = 'word info'>" . $author . ' ' . $date . "</p></div>";
$sql2 = "select * from Ticket_Message where Ticket_Main_id = '$MainId'";
$query2 = mysqli_query($con,$sql2);
while ($row = mysqli_fetch_array($query2)){
$message = $row['message'];
$auth = $row['auth'];
$date = $row['date'];
echo "<div style = 'border-bottom-style: solid; border-bottom-width: 1px;'>";
echo "<p class = 'word'>" . $message . "</p>";
echo "<p class = 'word info'>" . $auth . ' ' . $date . "</p>";
echo "</div>";
}
?>
</div>
and here is my css attempt to fix it :
p{max-width: 250px;}
basically what i need am trying to do is use css to prevent all paragraphs from extending off the page when the string is longer than the width of the page.
Try:
p { word-wrap: break-word; }
https://developer.mozilla.org/en-US/docs/Web/CSS/word-wrap
You don't need any php code here.
Use css property word-wrap
p {
word-wrap: break-word;
max-width: 250px;
}
Demo: http://www.w3schools.com/cssref/tryit.asp?filename=trycss3_word-wrap

Avoiding line breaks in HTML output of PHP

Here I have my print function:
<?php
// little helper function to print the results
function printTag($tags) {
foreach($tags as $t) {
echo '<span class="' . $t['tag'] . '">';
echo $t['token'] . "/" . $t['tag'];
echo '</span>';
echo " ";
}
}
$tagger = new PosTagger('lexicon.txt');
?>
And here is what I'm outputting from an HTML form:
<?php
if($_POST['submitbutton'] == "Submit") {
//Check whether the form has been submitted
$tags = $tagger->tag($_POST['texttotag']);
printTag($tags);
}
?>
My problem is, the output in the browser results in strange line breaks in the middle of some of my <span> like so:
<span class="VB">Enter/VB</span> <span class="PRP$">your/PRP$</span> <span class="NN
">text/NN
</span> <span class="TO">to/TO</span> <span class="NN">tag/NN</span> <span class="RB
">here/RB
</span>
This means my CSS definitions don't apply to the "interrupted" spans. Any idea why this is happening and how I can stop it? I've had a good look around and haven't been able to find cause/solution. Thanks.
It seems that your $t['tag'] variable includes a line-break.
You can get rid of that using trim($t['tag']) instead.
It appears that the $t['tag'] in printTag function contains line break character. You can use str_replace function to remove these characters like:
function printTag($tags) {
foreach($tags as $t) {
echo '<span class="' . $t['tag'] . '">';
$ttag = str_replace('\n','',$t['tag']);
$ttag = str_replace('\r','',$ttag);
echo $t['token'] . "/" . $ttag;
echo '</span>';
echo " ";
}
}

insert echo into the specific html element like div which has an id or class

I have this code.
<html>
<head>
<style type="text/css">
body{background:#666666;}
div{border:1px solid red;}
</style>
</head>
<body>
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("juliver", $con);
$result = mysql_query("SELECT * FROM hi");
while($row = mysql_fetch_array($result))
{
echo '<img src="'.$row['name'].'" />';
echo "<div>".$row['name']."</div>";
echo "<div>".$row['title']."</div>";
echo "<div>".$row['description']."</div>";
echo "<div>".$row['link']."</div>";
echo "<br />";
}
mysql_close($con);
?>
</body>
</html>
the above code works.
now, I want to insert this
echo '<img src="'.$row['name'].'" />';
echo "<div>".$row['name']."</div>";
echo "<div>".$row['title']."</div>";
echo "<div>".$row['description']."</div>";
echo "<div>".$row['link']."</div>";
echo "<br />";
into the specified div or other element in the html, example, i will insert this
echo '<img src="'.$row['name'].'" />';
into the html element .
I dont know how to do this, please help me. Thanks
Juliver
if yo want to place in an div like
i have same work and i do it like
<div id="content>
<?php
while($row = mysql_fetch_array($result))
{
echo '<img src="'.$row['name'].'" />';
echo "<div>".$row['name']."</div>";
echo "<div>".$row['title']."</div>";
echo "<div>".$row['description']."</div>";
echo "<div>".$row['link']."</div>";
echo "<br />";
}
?>
</div>
The only things I can think of are
including files
replacing elements within files using preg_match_all
using assigned variables
I have recently been using str_replace and setting text in the HTML portion like so
{{TEXT_TO_REPLACE}}
using file_get_contents() you can grab html data and then organise it how you like.
here is a demo
myReplacementCodeFunction(){
$text = '<img src="'.$row['name'].'" />';
$text .= "<div>".$row['name']."</div>";
$text .= "<div>".$row['title']."</div>";
$text .= "<div>".$row['description']."</div>";
$text .= "<div>".$row['link']."</div>";
$text .= "<br />";
return $text;
}
$htmlContents = file_get_contents("myhtmlfile.html");
$htmlContents = str_replace("{{TEXT_TO_REPLACE}}", myReplacementCodeFunction(), $htmlContents);
echo $htmlContents;
and now a demo html file:
<html>
<head>
<style type="text/css">
body{background:#666666;}
div{border:1px solid red;}
</style>
</head>
<body>
{{TEXT_TO_REPLACE}}
</body>
</html>
You can write the php code in another file and include it in the proper place where you want it.
AJAX is also used to display HTML content that is formed by PHP into a specified HTML tag.
Using jQuery:
$.ajax({url: "test.php"}).done(function( html ) {
$("#results").append(html);
});
Above code will execute test.php and result will be displayed in the element with id results.
There is no way that you can do it in PHP when HTML is already generated. What you can do is to use JavaScript or jQuery:
document.getElementById('//ID//').innerHTML="HTML CODE";
If you have to do it when your URI changes you can get the URI and then split it and then insert the HTML in script dynamically:
var url = document.URL;
// to get url and then use split() to check the parameter
refer to the basic.
$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john#example.com')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
there is no way to specifically target an element with php, you can either embed the php code between a div tag or use jquery which would be longer.
You can repeat it by fetching again the data
while($row = mysql_fetch_assoc($result)){
//another html element
<div>$row['name']</div>
<div>$row['title']</div>
//and so on
}
or you need to put it on the variable and call display it again on other html element
$name = $row['name'];
$title = $row['title']
//and so on
then put it on the other element, but if you want to call all the data of each id, you need to do the first code
Have you tried this?:
$string = '';
while($row = mysql_fetch_array($result))
{
//this will combine all the results into one string
$string .= '<img src="'.$row['name'].'" />
<div>'.$row['name'].'</div>
<div>'.$row['title'].'</div>
<div>'.$row['description'].'</div>
<div>'.$row['link'].'</div><br />';
//or this will add the individual result in an array
/*
$yourHtml[] = $row;
*/
}
then you echo the $tring to the place you want it to be
<div id="place_here">
<?php echo $string; ?>
<?php
//or
/*
echo '<img src="'.$yourHtml[0]['name'].'" />;//change the index, or you just foreach loop it
*/
?>
</div>
Well from your code its clear that $row['name'] is the location of the image on the file, try including the div tag like this
echo '<div>' .$row['name']. '</div>' ;
and do the same for others, let me know if it works because you said that one snippet of your code is giving the desired result so try this and if the div has some class specifier then do this
echo '<div class="whatever_it_is">' . $row['name'] . '</div'> ;
You have to put div with single quotation around it. ' ' the Div must be in the middle of single quotation . i'm gonna clear it with one example :
<?php
echo '<div id="output">'."Identify".'<br>'.'<br>';
echo 'Welcome '."$name";
echo "<br>";
echo 'Web Mail: '."$email";
echo "<br>";
echo 'Department of '."$dep";
echo "<br>";
echo "$maj";
'</div>'
?>
hope be useful.
I use this or similar code to inject PHP messages into a fixed DIV positioned in front of other elements (z-index: 9999) just for convenience at the development stage.
Each PHP message passes into my 'custom_message()' function and is further conveyed into the innard of preformatted DIV created by echoed JS.
There can be as many as it gets, all put inside that fixed DIV, one under the other.
<style>
#php_messages {
position: fixed;
left: 0;
top: 0;
z-index: 9999;
}
.php_message {
background-color: #333;
border: red solid 1px;
color: white;
font-family: "Courier New", Courier, monospace;
margin: 1em;
padding: 1em;
}
</style>
<div id="php_messages"></div>
<?php
function custom_message($output) {
echo
'
<script>
var
el = document.createElement("DIV");
el.classList.add("php_message");
el.innerHTML = \''.$output.'\';
document.getElementById("php_messages").appendChild(el);
</script>
';
}
?>

PHP and HTML Echo

I want to query a result from a table. The problem I run into is if the text is very long it will not auto-wrap to another line"best way i can think to explain it". It displays the text on one line and will go way off to the side of the page if the text is long. How can I get the text to space correctly. Thanks
here is my code:
<?php
echo "<div class=\"item_list2\">";
$get_items = "SELECT * FROM items WHERE category='Art'";
$result = mysql_query($get_items);
//loop! loops through the multiple results of the $get_items query
while($item = mysql_fetch_array($result, MYSQL_ASSOC)){
echo "<b>" . $item['item'] . "</b><br/>" . $item['email'] . "<br/>";
echo $item['price'] . "</b><br/>" . $item['category'] . "</b><br/>" . $item['extra'];
echo "<br/><a href='manage.php?delete=" . $item['id'] . "'><small>[Delete]</small></a><br/><br/>";
}
echo "</div>";
?>
Just use CSS to break it.
.container
{
word-wrap: break-word;
}
maybe you want to add..
nl2br($item['extra']);

PHP looping problem?

I'm trying to display the first row in one color and the second row in another color but my code displays the result twice in both colors for example lets say I have 5 results my code will double the results by displaying 10 results. How can I fix this problem?
Here is the php code.
while ($row = mysqli_fetch_assoc($dbc)) {
//first row
echo '<h3 class="title">' . $row['title'] .'</h3>';
echo '<div class="summary">' . substr($row['content'],0,255) . '</div>';
//second row
echo '<h3 class="title-2">' . $row['title'] .'</h3>';
echo '<div class="summary-2">' . substr($row['content'],0,255) . '</div>';
}
You need to change the class on each row:
$count = 0;
while ($row = mysqli_fetch_assoc($dbc)) {
if( $count % 2 == 0 ) {
$classMod = '';
} else {
$classMod = '-2';
}
//first row
echo '<h3 class="title' . $classMod . '">' . $row['title'] .'</h3>';
echo '<div class="summary' . $classMod . '">' . substr($row['content'],0,255) . '</div>';
$count++;
}
your code should be like this
CSS
.odd { background: #CCC }
.event { background: #666 }
PHP
$c = true;
while ($row = mysqli_fetch_assoc($dbc)) {
$style = (($c = !$c)?' odd':' even');
echo '<h3 class="title '.$style.'">' . $row['title'] .'</h3>';
echo '<div class="summary '.$style.'">' .substr($row['content'],0,255) . '</div>';
}
Here's a solution with minimal repetition:
$count = 0;
while (($row = mysqli_fetch_assoc($dbc)) && ++$count) {
printf(
'<h3 class="title%1$s">%2$s</h3>'
. '<div class="summary%1$s">%3$s</div>'
, $count % 2 ? "" : "-2"
, $row['title'] // might want to use htmlentities() here...
, substr($row['content'], 0, 255) // and here...
);
}
$i = 0;
while ($row = mysqli_fetch_assoc($dbc)) {
$color =$i % 2;
echo '<h3 class="title-' .$color . '">' . $row['title'] .'</h3>';
echo '<div class="summary">' . substr($row['content'],0,255) . '</div>';
$i++;
}
Your code just displays every result twice. Use a conditional (e.g. an integer or a boolean) to switch between rows (like: if true, then green; if false, then red).
For a boolean you could change the current value like so:
bool = !bool;
Couple of extra points:
You don't (normally) need so many classes. If you have <div class="stripe"> as your container, you can target the items with e.g. .stripe h3 in CSS.
If you target the odd and even items in CSS with .stripe h3, you can then overwrite just the odd items.
In a perfect world, you should keep presentation in the CSS. All browsers but IE7 and below support div:odd to target any odd child of a parent. This may require changing the structure of your HTML slightly. For IE7 and below, I'd add classes with JavaScript instead of PHP. When IE7 is no more then you can just remove the JS.
By the way, you could do this code too:
while ($row = mysqli_fetch_assoc($dbc)) {
echo '<h3 class="title">' . $row['title'] .'</h3>';
echo '<div class="summary">' . substr($row['content'],0,255) . '</div>';
if($row = mysqli_fetch_assoc($dbc)){
echo '<h3 class="title-2">' . $row['title'] .'</h3>';
echo '<div class="summary-2">' . substr($row['content'],0,255) . '</div>';
}
}
IMO, there is no excuse for not delegating this kind of non-critical, presentation layer decoration to client side code.
Just use a library like jQuery and access the odd and even rows like so:
<script>
$(document).ready(function()
{
//for your table rows
$("tr:even").css("background-color", "#F4F4F0");
$("tr:odd").css("background-color", "#EFF1F2");
});
</script>
You'll have us generating font tags next.

Categories