PHP and HTML Echo - php

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']);

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

How do I style my text inside an echo?

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

How to apply style in an echo'ed <td>?

this is my PHP code:
$query="SELECT name, surname, address FROM employee";
$results = mysql_query($query);
while ($row = mysql_fetch_assoc($results)) {
echo '<tr>';
foreach($row as $field) {
echo '<td class="element">' . htmlspecialchars($field) . '</td>';
}
echo '</tr>';
And this is my CSS:
.element{
color:red;
}
But fields displayed in my HTML page aren't red. Why it doesn't work?
I already tried with
echo "<td class='element'>" . htmlspecialchars($field) . "</td>";
and
echo '<td class=\"element\">' . htmlspecialchars($field) . "</td>";
but with no success.
I suggest to specify element in your css.
td.element{
color:red;
}
Also it's a good idea to give your "tr" an ID
"tr id="myTR"...
#myTR td.element{
color:red;
}
then try this.
echo '<td style="color:red">' . htmlspecialchars($field) . '</td>';
even this 1 below shold work
echo '<td class="element">' . htmlspecialchars($field) . '</td>'; // for this just dont remove your css class
you just copy and paste to replace your code. and remove that class in css
the issue of mysl_ does not affect that not to display, i am using latest php engine and they work fine, even though we need to change.

conditioning in XML parsing - PHP

Its been a while since the last time I was coding, and I am a little rusty. I am trying to parse an xml file and having a problem with the condition. Spent hours trying to figure out where is the problem, but without success...
PHP CODE:
<div id="gallery">
<?php
$cat_img="images/cat.png";
$logo_path="logos/";
$file = simplexml_load_file('stores.xml');
$old_cat="";
foreach($file->store as $store){
if($store->cat != $old_cat){
echo "<img id='cat' src='".$cat_img."'><div id='cat_text'>".$store->cat."</div><br>";
$old_cat=$store->cat;
}
echo "<div id='store'><img id='store_img' src='".$logo_path.$store->logo."' alt='logo'><br>";
echo "<store_name>".$store->name."</store_name>";
echo "<phone>".$store->phone."</phone><phone>טל: </phone></div>";
}
?>
</div>
STORES.XML
<?xml version="1.0" encoding="UTF-8"?>
<stores>
<store><logo>renuar.jpg</logo>
<name>renuar</name>
<phone>052-6059962</phone>
<cat>clothing</cat></store>
<store><logo>yoop.jpg</logo>
<name>YOOP</name>
<phone>08-6601451</phone>
<cat>clothing</cat></store>
....
</stores>
The idea is to print every store, when category changes it should print the category divider and keep printing the stores. Somehow the condition is always true and I have no idea why...
p.s
If anyone has a better idea on how to divide the categories, a different method, I would be happy to hear.
Thanks!!!
$store->cat will be SimpleXMLElement object and two object will not be same even with same category name. You need to cast it to string for comparison
Like (string) $store->cat, try this:
<?php
$cat_img = "images/cat.png";
$logo_path = "logos/";
$file = simplexml_load_file('test.xml');
$old_cat = "";
foreach ($file->store as $store) {
if ((string) $store->cat != $old_cat) {
echo "<img id='cat' src='" . $cat_img . "'><div id='cat_text'>category: " . $store->cat . "</div><br>";
$old_cat = (string) $store->cat;
}
echo "<div id='store'><img id='store_img' src='" . $logo_path . $store->logo . "' alt='logo'><br>";
echo "<store_name>" . $store->name . "</store_name>";
echo "<phone>" . $store->phone . "</phone><phone>טל: </phone></div>";
}
?>

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