I have this PHP code with MySQL database query and I would like to display a record from MySQL database such as first_name and last_name records, I would like only to display like PHP echo $row the first 3 letters of the first name and last name output, something like this:
Joh*** Smi***
Jef*** Joh***
I saw this example online, I tried it and it works! but just need to replace the example value of '$row = "Hamingway"' to fetch the real array value from MySQL Database instead of: "Hamingway" `Ham****** `**
$row = "Hamingway";
echo (strlen($row) > 3) ? substr($row, 0, 3) . str_repeat('*', strlen($row) - 3) : str_repeat('*', strlen($row));
'John Smith' needs to convert to display output ---> 'Joh... Smi...' 'Jeff Johnson' needs to convert to display output ---> 'Jef... Joh...'
I currently presenting rows of data from a MySQL query in an HTML table with the following structure:
<tr>
<td><?php echo $row['first_name']; ?></td>
<td><?php echo $row['last_name']; ?></td>
<td><?php echo $row['last_number']; ?></td>
</tr>
My current code looks like this:
require_once 'includes/db.php';
$query = "SELECT * FROM sign_in3 ORDER BY date ASC LIMIT 5";
$results = mysqli_query($conn, $query);
$records = mysqli_num_rows($results);
if (!empty($records)) {
while ($row = mysqli_fetch_assoc($results)) { ?>
<tr style="font-size:40px;">
<td><center><?php echo $row['first_name']; ?></center></td>
<td><center><?php echo $row['last_name']; ?></center></td>
<td><center><?php echo $row['last_number']; ?></center></td>
</tr>
<?php }
} ?>
I would like to see only the first 3 letters from the first name and last name values.
You can try this below assume $result is the result of your MySQL query, fetched as an associative array. In this case you would have to adjust the keys accordingly based on your actual database structure.