PHP substr breaks my table - php

I have a table like so
echo '<table>';
echo '<tr>';
echo '<td>' . $value['title'] . '</td>';
echo '<td>' . $value['date'] . '</td>';
echo '<td>' . $value['preview'] . '</td>';
echo '<td>' . substr($value['description'], 0, 10) . '</td>';
echo '<td><img src="../images/articles/' . $value['image'] . '"></td>';
echo '<td><img src="../images/articles/' . $value['detailImage'] . '"></td>';
if($value['showDetailImage'] == 1){
$showDetailImage = 'Yes';
}else{
$showDetailImage = 'No';
}
echo '<td>' . $showDetailImage . '</td>';
echo '<td><a class="btn btn-default" href="Press.php?action=edit&id=' . $value['id'] . '">Edit</a></td>';
echo '<td><a onclick="if(!confirm(\'Are you sure you want to delete this item?\')) return false;" class="btn btn-default" href="Press.php?action=delete&id=' . $value['id'] . '">Delete</a></td>';
echo '</tr>';
echo '</table>';
notice I have substr and when I have that in there, it does not display the description and breaks the rest of my table, if I remove the substr the table is fine and everything is displayed. Why is substr breaking my table.

It's probably the contents of description that contains HTML markup that's breaking the table, use htmlspecialchars...
echo '<td>' . htmlspecialchars(substr($value['description'], 0, 10)) . '</td>';

Is that because your substr concatenates half way through some HTML element?
For example, you should use:
echo htmlentities(substr($value['description'], 0, 10));
Or if you want to strip the HTML tags:
echo strip_tags(substr($value['description'], 0, 10));

Related

How to send a variable value with HREF in PHP

i have one table in locallhost ,In my table there is an item id,Now I want to send the id Related row to another page by clicking on EDIT . Thankful
The part of the program I think is difficult to write below
....
....
$response["travel"]=array();
while ($row = mysql_fetch_array($sql)) {
// Print out the contents of the entry
echo '<tr>';
echo '<td class="text-center">' . $i . '</td>';
echo '<td class="text-center">' . $row['companyname'] . '</td>';
echo '<td class="text-center">' . $row['cod'] . '</td>';
echo '<td class="text-center">' . $row['bigan'] . '</td>';
echo '<td class="text-center">' . $row['stop'] . '</td>';
echo '<td class="text-center">' . $row['date'] . '</td>';
echo '<td class="text-center">' . $row['time'] . '</td>';
echo '<td class="text-center">' . $row['price'] . '</td>';
echo '<td class="text-center">' .'EDIT' .'</td>';
$i++;
.......
.....
You don't use <?php inside strings, you use that when you've gone back into HTML mode. You should just concatenate the variable, like you do everywhere else
echo '<td class="text-center">' .'ویرایش' .'</td>';

How to highlight rows in php table?

I have table called reservations. It displays reservations made by users. I want highlight records in current date using end date.
Php code
$result2 = mysql_query("SELECT * FROM reservations WHERE hotel_id = '1' ORDER BY end");
while ($row = mysql_fetch_array($result2)) {
echo '<tr>';
echo '<td class="contacts">' . $row['fname'] . ' ' . $row['lname'] . '</td>';
echo '<td class="contacts">' . $row['start'] . '</td>';
echo '<td class="contacts">' . $row['end'] . '</td>';
echo '<td class="contacts">' . $row['qty'] . '</td>';
echo '</td>';
echo '<td class="contacts">' . $row['room_type'] . '</td>';
echo '<td class="contacts">' . '<a href=out.php?id=' . $row["res_id"] . '>' . 'Check Out' . '</a>' . '</td>';
echo '</tr>';
}
I'd do that at frontend side, but if you want to compute than in PHP, you will need to compare the date from $row and current date and add a class or a style tag to the .
Like so:
$rowHasCurrentDate = $row['date'] == date('Y-m-d');
echo '<tr' + ($rowHasCurrentDate ? ' class="highlight-row"' : "") + '>';
instead of
echo '<tr>';
You can replace the 'class="highlight-row"' with 'style="background-color:red"' if you want to do it without touching frontend side at all.
The example is kinda spaghetti-code, but you can move this logic somewhere else after you get it working.
I am assuming that your start date is current date. This is css thing, i have given you solution to you.
<html>
<head>
<style>
.currdate
{
background-color:red; //change this color to whatever you wish to change to
}
</style>
</head>
<body>
<?php
$result2 = mysql_query("SELECT * FROM reservations WHERE hotel_id = '1' ORDER BY end");
while ($row = mysql_fetch_array($result2)) {
echo '<tr>';
echo '<td class="contacts">' . $row['fname'] . ' ' . $row['lname'] . '</td>';
echo '<td class="contacts currdate">' . $row['start'] . '</td>';
echo '<td class="contacts">' . $row['end'] . '</td>';
echo '<td class="contacts">' . $row['qty'] . '</td>';
echo '</td>';
echo '<td class="contacts">' . $row['room_type'] . '</td>';
echo '<td class="contacts">' . '<a href=out.php?id=' . $row["res_id"] . '>' . 'Check Out' . '</a>' . '</td>';
echo '</tr>';
}
?>
<body>
</html>

Issue with using php variable in hyperlink

How do you use one php variable for the url in a hyperlink around another php variable? Below is my code, which is clearly not working. I want $url to be the hyperlinked text of $songTitle.
if ($result) {
$numberOfRows = $result->num_rows;
for($i=0; $i < $numberOfRows; $i++) {
$row = $result->fetch_assoc();
echo '<tr>';
echo '<td>' . $row['artistName'] . '</td>';
echo '<td>'<a href=". $row['url'] . > . $row['songTitle'] . </a> '</td>';
echo '<td>' . $row['yOR'] . '</td>';
echo '</tr>';
}
}
You are not concatenating your strings properly.
for($i=0; $i < $numberOfRows; $i++) {
$row = $result->fetch_assoc();
echo '<tr>';
echo '<td>' . $row['artistName'] . '</td>';
echo '<td><a href="' . $row['url'] . '" >' . $row['songTitle'] . '</a></td>';
echo '<td>' . $row['yOR'] . '</td>';
echo '</tr>';
}
did you copy your script from somewhere? In parts of it you seem to understand how to escape and use PHP values inside a string, but then you messed up the one line.
Anyway, this is it fixed.. you didn't have your quotes or single quotes used properly.
echo "<td><a href='". $row['url'] . "'>" . $row['songTitle'] ." </a> </td>";

How to add auto line break and maximum column width to table

How can I add different maximum width to my table columns? I'd also like to have an auto line break when I write something into the columns. Here's my code:
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>ID</th> <th>Kategorie</th> <th>Titel</th> <th>Betroffene Seiten</th> <th>Beschreibung</th><th>prioritaet</th><th>Status</th><th>Eingereicht von</th><th>Umsetzung mit Release</th><th>Dev. Kommentar</th><th>Entwickler</th><th></th> <th></th></tr>";
// loop through results of database query, displaying them in the table
while($row = mysql_fetch_array( $result )) {
// echo out the contents of each row into a table
echo "<tr>" ;
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['kategorie'] . '</td>';
echo '<td>' . $row['titel'] . '</td>';
echo '<td>' . $row['betroffen'] . '</td>';
echo '<td>' . $row['beschreibung'] . '</td>';
echo '<td>' . $row['prioritaet'] . '</td>';
echo '<td>' . $row['status'] . '</td>';
echo '<td>' . $row['eingereicht'] . '</td>';
echo '<td>' . $row['umsetzung'] . '</td>';
echo '<td>' . $row['kommentar'] . '</td>';
echo '<td>' . $row['entwickler'] . '</td>';
echo '<td>bearbeiten</td>';
echo '<td>löschen</td>';
echo "</tr>";
}
// close table>
echo "</table>";
you can set a width to your td in pixels or %. setting in one td will set it for the column.
echo '<td style="width:40px">' . $row['id'] . '</td>';
I didn't understood you intenstion by:
"when I write something into the columns."
you can use the \n to get new lines in your php code. just add it to your code where appropriate.
you can see here
you can also use <br /> in your HTML to get the same line break.
You can do as following to break line in column and keep max width,
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>ID</th> <th>Kategorie</th> <th>Titel</th> <th>Betroffene Seiten</th> <th>Beschreibung</th><th>prioritaet</th><th>Status</th><th>Eingereicht von</th><th>Umsetzung mit Release</th><th>Dev. Kommentar</th><th>Entwickler</th><th></th> <th></th></tr>";
// loop through results of database query, displaying them in the table
while($row = mysql_fetch_array( $result )) {
// echo out the contents of each row into a table
echo "<tr>" ;
echo '<td width="'.strlen($row['id'])+5.'">' . wordwrap($row['id'], 20, "<br />\n") . '</td>';
echo '<td width="'.strlen($row['id'])+5.'">' . wordwrap($row['kategorie'], 20, "<br />\n") . '</td>';
echo '<td width="'.strlen($row['id'])+5.'">' . wordwrap($row['titel'], 20, "<br />\n") . '</td>';
echo '<td width="'.strlen($row['id'])+5.'">' . wordwrap($row['betroffen'], 20, "<br />\n") . '</td>';
echo '<td width="'.strlen($row['id'])+5.'">' . wordwrap($row['beschreibung'], 20, "<br />\n") . '</td>';
echo '<td width="'.strlen($row['id'])+5.'">' . wordwrap($row['prioritaet'], 20, "<br />\n") . '</td>';
echo '<td width="'.strlen($row['id'])+5.'">' . wordwrap($row['status'], 20, "<br />\n") . '</td>';
echo '<td width="'.strlen($row['id'])+5.'">' . wordwrap($row['eingereicht'], 20, "<br />\n") . '</td>';
echo '<td width="'.strlen($row['id'])+5.'">' . wordwrap($row['umsetzung'], 20, "<br />\n") . '</td>';
echo '<td width="'.strlen($row['id'])+5.'">' . wordwrap($row['kommentar'], 20, "<br />\n") . '</td>';
echo '<td width="'.strlen($row['id'])+5.'">' . wordwrap($row['entwickler'], 20, "<br />\n") . '</td>';
echo '<td>bearbeiten</td>';
echo '<td>löschen</td>';
echo "</tr>";
}
// close table>
echo "</table>";
using strlen function set max width to td and using wordwrap break column value to new line

if this row is emty then it will showing another row

how can i do if this row is empty then it will showing another row
suppose name row is blank
echo '<td>' . $row['name'] . '</td>';
so if name row is blank then it will show echo '<td>' . $row['companyname'] . '</td>'; row
please help me how can i fix this
thanks
echo "<tr>";
echo '<td>' . $row['buildingname'] . '</td>';
echo '<td>' . $row['area'] . '</td>';
echo '<td>' . $row['city'] . '</td>';
echo '<td>' . $row['flatno'] . '</td>';
echo '<td>' . $row['name'] . '</td>';
echo '<td>' . $row['mobileno'] . '</td>';
echo '<td>' . $row['agreementdates'] . '</td>';
echo '<td>' . $row['agreementdatee'] . '</td>';
echo "</tr>";
Try
if(empty( $row['name'])){
$tbl = '<td>' . $row['companyname'] . '</td>';
}else{
$tbl = '<td>' . $row['name'] . '</td>';
}
echo $tbl;
try this i think this is working.
echo '<td>' . empty($row['name']) ? $row['companyname'] : $row['name'] . '</td>';
try this:
echo "<tr>";
if(!empty($row['buildingname'])) {
echo '<td>' . $row['buildingname'] . '</td>';
}
if(!empty($row['area'])) {
echo '<td>' . $row['area'] . '</td>';
}
if(!empty($row['city'])) {
echo '<td>' . $row['city'] . '</td>';
}
if(!empty($row['flatno'])) {
echo '<td>' . $row['flatno'] . '</td>';
}
if(!empty($row['name'])) {
echo '<td>' . $row['name'] . '</td>';
}
if(!empty($row['mobileno'])) {
echo '<td>' . $row['mobileno'] . '</td>';
}
if(!empty($row['agreementdates'])) {
echo '<td>' . $row['agreementdates'] . '</td>';
}
echo "</tr>";

Categories