First of all my php knowledge is low.
My problem is that substr is not working as I know so I need help to understand why.
I have removed style.css and I get same result (ofc with no style).
Here the code and a picture of result I get.
I try with a function and I get same result.
I miss something and I do not know what.
I removed <tr> <td>
from php and try with a html but I get same result.
All I want is to show only first 100 characters of $res['descriere']
and when I try to view it to show all characters.
I did this before and it work but now it dos not :(
Thank your for your time
<?php
$result = mysqli_query($mysqli, "SELECT * FROM products WHERE login_id=".$_SESSION['dep_id']." ORDER BY id DESC");
while($res = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>".$res['trn_date']."</td>";
echo "<td>".$res['name']."</td>";
echo "<td>".$res['locatia']."</td>";
echo "<td>".$res['canal_primire']."</td>";
echo "<td>".$res['intemeiata']."</td>";
echo "<td>".$res['domeniu_vizat']."</td>";
echo "<td>".substr($res['descriere'],0,100)."</td>";
echo "<td><a href=\"edit.php?id=$res[id]\">Editați</a> | <a href=\"delete.php?id=$res[id]\" onClick=\"return confirm('Ești sigur că vrei să ștergi?')\">Șterge</a>| <a href=\"view_id.php?id=$res[id]\">Vizualizați</a></td>";
echo "</tr>";
}
?>
From my limited understanding of your language, descriere
seems to translate to "description". Your picture shows a destroyed HTML table, which means there must be HTML (of a table) within the desctiption.
You are basically printing a substring of HTML table within your table on yor webpage. Since the inner table is not valid HTML, the browser tries to make some sense of it. That is what you're getting.
To get rid of the weird results, you need to close all open HTML tags within your code after truncating it. This can be archieved with the following function:
function closetags($html) {
preg_match_all('#<(?!meta|img|br|hr|input\b)\b([a-z]+)(?: .*)?(?<![/|/ ])>#iU', $html, $result);
$openedtags = $result[1];
preg_match_all('#</([a-z]+)>#iU', $html, $result);
$closedtags = $result[1];
$len_opened = count($openedtags);
if (count($closedtags) == $len_opened) {
return $html;
}
$openedtags = array_reverse($openedtags);
for ($i=0; $i < $len_opened; $i++) {
if (!in_array($openedtags[$i], $closedtags)) {
$html .= '</'.$openedtags[$i].'>';
} else {
unset($closedtags[array_search($openedtags[$i], $closedtags)]);
}
}
return $html;
}
Source: Close open HTML tags in a string