making hyperlink of a value stored in table column - php

How to make the values inserted in table column($row['survey_name']) a hyperlink. this is my code which I have tried in php:
while($row=mysql_fetch_array($result1))
{
$content .= "
<tr id=\"special\" style=\"background:;\">
<td>". $row['survey_name'] . "</td>
<td>" . $row['date'] . "</td>
</tr>
";
?>
<script
$('#special').onclick(function(){window="http://urllinkto.com/x/y/z";})
</script>
<?php }//end of while
if ($content) {
// See note on the "quote switch" here
echo '
<table border= "5" cellpadding="2" cellspacing="2" width="100%">
<tr>
<td><strong>Survey Name</strong></td>
<td><strong>Date Created</strong></td>
/tr>' .
$content . '
</table> ';

Chage
<td>". $row['survey_name'] . "</td>
to:
<td><a href='URL HERE'>". $row['survey_name'] . "</a></td>

Try to declare variables first.
while($row=mysql_fetch_array($result1)){
$surveyname = $row['survey_name']
$date = $row['date']
$content .= "
<tr id=\'special\' style=\"background:;\">
<td><a href='#'>echo $surveyname</a> </td>
<td><a href='#'>echo $date</a></td>
</tr>
";
}

Related

Why is formatting broken when using bootstrap in PHP?

I want to generate Bootstrap 5 table .table-striped using data from MySQL, table: [users]
The table in plain HTML looks fine, but as soon as I generate it using PHP all styling disappears
PHP Code:
<?php
$result = mysqli_query($link,"SELECT * FROM users ORDER BY id ASC");
echo "<table class='table table-striped'>
<thead>
<tr>
<th>#</th>
<th>Username</th>
<th>Rank</th>
<th>Created at</th>
</tr>
</thead>";
while($row = mysqli_fetch_array($result))
{
echo "<tbody>";
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['rank'] . "</td>";
echo "<td>" . $row['created_at'] . "</td>";
echo "</tr>";
echo " </tbody>";
}
echo "</table>";
mysqli_close($link);
?>
and I have <div class='table-responsive'> before <?php and </div> after ?>
I would be glad if you could explain me where I am making a mistake and I would like the table to have the same styling as in pure HTML
EDIT:
rendered HTML
<div class='table-responsive'>
<table class='table table-striped'>
<thead>
<tr>
<th>#</th>
<th>Username</th>
<th>Rank</th>
<th>Created at</th>
</tr>
</thead><tbody><tr><td>39</td><td></td><td>julia</td><td>2021-12-20 00:42:14</td></tr> </tbody><tbody><tr><td>40</td><td>test</td><td>user</td><td>2021-12-20 02:35:37</td></tr> </tbody><tbody><tr><td>41</td><td>testt</td><td>user</td><td>2021-12-20 17:22:43</td></tr> </tbody></table></div>
You are building a new tbody for every row. You should move the tbody creation outside the while loop. You also only need 1 echo, rather than echoing every line. I would do:
<?php
$result = mysqli_query($link,"SELECT * FROM users ORDER BY id ASC");
$output = "<div class='table-responsive'>
<table class='table table-striped'>
<thead>
<tr>
<th style='background-color:#ad8c70'>#</th>
<th style='background-color:#ad8c70'>Username</th>
<th style='background-color:#ad8c70'>Rank</th>
<th style='background-color:#ad8c70'>Created at</th>
</tr>
</thead>
<tbdoy>";
while($row = mysqli_fetch_array($result))
{
$output .= "<tr>
<td>" . $row['id'] . "</td>
<td>" . $row['username'] . "</td>
<td>" . $row['rank'] . "</td>
<td>" . $row['created_at'] . "</td>
</tr>";
}
mysqli_close($link);
$output .= '</tbody></table>';
echo $output;
?>

Not display table SQLite and php

$table = "<p><table width=\"770px\">
<thead>
<tr>
<th><b>FECHA</b></th>
<th><b>PRODUCTO</b></th>
<th><b>CANTIDAD</b></th>
<th><b>PRECIO</b></th>
</tr>
</thead>
<tbody> " . while($res = $result->fetchArray(SQLITE3_ASSOC))
{
echo
"<tr>
<td>" . $res['fecha'] . "</td>
<td>" . $res['nombre'] . "</td>
<td>" . $res['cantidad'] . "</td>
<td>" . $res['precio_hospital'] . "<br></td>
</tr>";
} . "
</tbody>
</table>";
Hello friends, I have a php page that does not allow me to show the table correctly I do not know how to place the while, please can you help me.
works friend, but I want to save the result of this table in a php variable to print it in
$ pdf-> writeHTML ($table, true, false, true, false, '');
You can't use echo when you are already inside and echo. Try:
$output = '<p><table width="770px">
<thead>
<tr>
<th><b>FECHA</b></th>
<th><b>PRODUCTO</b></th>
<th><b>CANTIDAD</b></th>
<th><b>PRECIO</b></th>
</tr>
</thead>
<tbody>';
while($res = $result->fetchArray(SQLITE3_ASSOC)) {
$output .= '<tr><td>' . $res['fecha'] . '</td>' .
'<td>' . $res['nombre'] . '</td>' .
'<td>' . $res['cantidad'] . '</td>' .
'<td>' . $res['precio_hospital'] .
'<br></td></tr>';
}
$output .= '</tbody></table>';
Like this:
$myVar = "<p><table width=\"770px\">
<thead>
<tr>
<th><b>FECHA</b></th>
<th><b>PRODUCTO</b></th>
<th><b>CANTIDAD</b></th>
<th><b>PRECIO</b></th>
</tr>
</thead>
<tbody> ";
while($res = $result->fetchArray(SQLITE3_ASSOC)) {
$myVar .=
"<tr>
<td>" . $res['fecha'] . "</td>
<td>" . $res['nombre'] . "</td>
<td>" . $res['cantidad'] . "</td>
<td>" . $res['precio_hospital'] . "<br></td>
</tr>";
}
$myVar .= "</tbody></table></p>";
//echo $myVar; //if you want to see the result
//$pdf->writeHTML ($myVar, true, false, true, false, ''); //to pdf
You can't concatenate a control structure to a string. PHP will throw a syntax error.
Edited to use a variable

Delete Row Button for MySQL and PHP

I have an admin panel where administrators can view all the data in a table, however I would like them to have a small delete button next to each row. Each row can be defined by an ID, however I am not sure about the code behind it.
<!-- Requests -->
<div class="panel panel-<?php echo $PColor; ?>">
<table class="table table-striped table-requests">
<thead class="table-requests">
<tr>
<th>Req ID</th>
<th><?php echo "Song Name"; ?></th>
<th><?php echo "Requested By"; ?></th>
<th><?php echo "Comments (If any)"; ?></th>
<th><?php echo "Time"; ?></th>
</tr>
</thead>
<tbody>
<?php
// Select Requests
$SelectRequests = $db->query("SELECT * FROM `requests`");
// Print Output
foreach($SelectRequests as $PrintRequests)
{
echo
"
<tr>
<td><b>" . $PrintRequests['ID'] . "</b></td>
<td>" . $PrintRequests['song'] . "</td>
<td>" . $PrintRequests['name'] . "</td>
<td>" . $PrintRequests['dedicated'] . "</td>
<td>" . $PrintRequests['time'] . "</td>
";
}
?>
</tbody>
</table>
</div>
</div>
Is there any way to have the delete button to the right of each displayed row, and make it functional?
Thank you in advance!
<!-- Lets call this file index.php -->
<!-- Requests -->
<div class="panel panel-<?php echo $PColor; ?>">
<form action='deleteRecord.php' method='GET'>
<table class="table table-striped table-requests">
<thead class="table-requests">
<tr>
<th>Req ID</th>
<th><?php echo "Song Name"; ?></th>
<th><?php echo "Requested By"; ?></th>
<th><?php echo "Comments (If any)"; ?></th>
<th><?php echo "Time"; ?></th>
<!-- Add this for the table header -->
<th><?php echo "Delete"; ?></th>
</tr>
</thead>
<tbody>
<?php
// Select Requests
$SelectRequests = $db->query("SELECT * FROM `requests`");
// Print Output
foreach($SelectRequests as $PrintRequests){
echo
"<tr>
<td><b>" . $PrintRequests['ID'] . "</b></td>
<td>" . $PrintRequests['song'] . "</td>
<td>" . $PrintRequests['name'] . "</td>
<td>" . $PrintRequests['dedicated'] . "</td>
<td>" . $PrintRequests['time'] . "</td>
<td><input type='checkbox' value ='" . $PrintRequests['ID'] . "' name='removal[]'></td>
</tr>";
}
?>
</tbody>
</table>
<input type="submit" value="Submit">
</form>
</div>
deleteRecord.php
<?php
//Assume you can connect to DB
$removals = $_GET['removal'];
if(!empty($removals)){
if(count($removals) > 1){
$r = implode(', ', $removals);
$query="DELETE from requests where ID IN (". $r . ")";
} else {
$query="DELETE from requests where ID = ". $removals[0];
}
//Commit to db
} else {
echo "Nothing Selected";
//Handle an error if needed
}
//redirect
?>
try something like that :
<?php
// Select Requests
$SelectRequests = $db->query("SELECT * FROM `requests`");
// Print Output
foreach($SelectRequests as $PrintRequests)
{
echo
"
<tr>
<td><b>" . $PrintRequests['ID'] . "</b></td>
<td>" . $PrintRequests['song'] . "</td>
<td>" . $PrintRequests['name'] . "</td>
<td>" . $PrintRequests['dedicated'] . "</td>
<td>" . $PrintRequests['time'] . "</td>
<td> <a href='delete.php?pid=" . $PrintRequests['ID'] . "'> Delete</a> </td>;
";
}
?>
In delete.php
<?php
// Connexion to database..
$Query="delete from requests where id=".$_REQUEST['pid'];
if(!mysqli_query($Conection,$Query)) {echo "<i>Error !</i>";}
echo '<script language="javascript"> document.location="Your_previous_page.php";</script>';
?>

For loop for the first row, the button cant work

This is the table i created , so when i press the Edit button it should bring me to another page to edit. For the first edit button on the first row it does not work however for the rest of the edit button on the second row onward it works. Please help. Thank you !
<table border='1'>
<tr>
<th width='120'>Production ID</th>
<th width='120'>Description</th>
<th width='120'>BOM</th>
</tr>
<?php
for ($i = 0; $i < $num; $i++) {
$fetch = mysqli_fetch_row($select);
echo "<tr>
<td>" . $fetch[0] . "</td>
<td>" . $fetch[1] . "</td>
<td>" . $fetch[2] . "</td>
<td><form action='AddProductMainEdit.php?prodid' method='GET'><input type='hidden' value='".$fetch[0]."' name='prodid'><input type='submit' id='edit' value='Edit'></form></td>
</tr>";
}
?>
</table>
Try this:
<table border='1'>
<tr>
<th width='120'>Production ID</th>
<th width='120'>Description</th>
<th width='120'>BOM</th>
</tr>
<?php
while ($fetch=mysqli_fetch_row($select)) {
echo "<tr>
<td>" . $fetch[0] . "</td>
<td>" . $fetch[1] . "</td>
<td>" . $fetch[2] . "</td>
<td><form action='AddProductMainEdit.php' method='GET'><input type='hidden' value='".$fetch[0]."' name='prodid'><input type='submit' id='edit' value='Edit'></form></td>
</tr>";
}
?>
</table>
You don't need to add the variable prodid to the url, the get method add all the variables to the url.

Dealing with duplicate arrays

I have a scanner project I'm developing where a class reads in a txt file created by scanner and queries the database to display "bundles" scanner by a scanner per room. However now I have some rooms which duplicate the results. The results from the query are stop in arrays per day and manipulate. What i want to do is to display unique bundles and remove duplicates leaving the earliest entry. Below is my index page. Any tips advise would be much appreciated.
<?php
// First of all initialise the user and check for permissions
require_once "/var/www/users/user.php";
$user = new CHUser(13);
// Initialise the template
require_once "/var/www/template/template.php";
$template = new CHTemplate();
// Initialise the scanner class
require_once "/var/www/Scanners/scanners.php";
$scanner = new CHScanners();
$_GET['date'] = date("Y-m-d", $scanner->GetPreviousMonday(strtotime($_GET['date'])));
$weeklyTotal = 0;
$content = "<h1>" . $scanner->GetRoomName($_GET['room']) . " Information</h1>
<form action='room.php' method='get'>
Enter date: <input type='text' name='date' /><input type='submit' /><input type='hidden' name='room' value='" . $_GET['room'] ."' />
</form>
<table width='100%'>
<tr>
<td style='vertical-align: top;'>
<table width='100%'>
<tr>
<th colspan='2'>Monday</th>
</tr>
<tr>
<th>Bundle #</th>
<th>Qty</th>
</tr>";
$result = $scanner-
>ListRoomTotals($_GET['room'],$_GET['date']);
$total = 0;
foreach($result as $x) {
$content .= "<tr>
<td>" . $x[0] . "</td>
<td>" . $x[1] . "</td>
</tr>";
$total += $x[1];
}
$weeklyTotal += $total;
$content .= "<tr><td>Total Pairage:</td><td>".$total."
</td></tr>
<tr><td>Total Dozens:</td><td>".number_format($total/12,1)."</td></tr></table>
</td>
<td style='vertical-align: top;'>
<table width='100%'>
<tr>
<th colspan='2'>Tuesday</th>
</tr>
<tr>
<th>Bundle #</th>
<th>Qty</th>
</tr>";
$date = date("Y-m-d",(strtotime($_GET['date']) + 86400));
$result = $scanner->ListRoomTotals($_GET['room'], $date);
$total = 0;
foreach($result as $x) {
$content .= "<tr>
<td>" . $x[0] . "</td>
<td>" . $x[1] . "</td>
</tr>";
$total += $x[1];
}
$weeklyTotal += $total;
$content .= "<tr><td>Total Pairage:</td><td>" . $total . "
</td></tr>
<tr><td>Total Dozens:</td><td>" .
number_format($total/12,1) . "</td></tr></table>
</td>
<td style='vertical-align: top;'>
<table width='100%'>
<tr>
<th colspan='2'>Wednesday</th>
</tr>
<tr>
<th>Bundle #</th>
<th>Qty</th>
</tr>";
$date = date("Y-m-d",(strtotime($_GET['date']) +
(86400*2)));
$result = $scanner->ListRoomTotals($_GET['room'], $date);
$total = 0;
foreach($result as $x) {
$content .= "<tr>
<td>" . $x[0] . "</td>
<td>" . $x[1] . "</td>
</tr>";
$total += $x[1];
}
$weeklyTotal += $total;
$content .= "<tr><td>Total Pairage:</td><td>" . $total . "
</td></tr>
<tr><td>Total Dozens:</td><td>" .
number_format($total/12,1) . "</td></tr></table>
</td>
<td style='vertical-align: top;'>
<table width='100%'>
<tr>
<th colspan='2'>Thursday</th>
</tr>
<tr>
<th>Bundle #</th>
<th>Qty</th>
</tr>";
$date = date("Y-m-d",(strtotime($_GET['date']) +
(86400*3)));
$result = $scanner->ListRoomTotals($_GET['room'], $date);
$total = 0;
foreach($result as $x) {
$content .= "<tr>
<td>" . $x[0] . "</td>
<td>" . $x[1] . "</td>
</tr>";
$total += $x[1];
}
$weeklyTotal += $total;
$content .= "<tr><td>Total Pairage:</td><td>" . $total . "
</td></tr>
<tr><td>Total Dozens:</td><td>" .
number_format($total/12,1) . "</td></tr></table>
</td>
<td style='vertical-align: top;'>
<table width='100%'>
<tr>
<th colspan='2'>Friday</th>
</tr>
<tr>
<th>Bundle #</th>
<th>Qty</th>
</tr>";
$date = date("Y-m-d",(strtotime($_GET['date']) +
(86400*4)));
$result = $scanner->ListRoomTotals($_GET['room'], $date);
$total = 0;
foreach($result as $x) {
if($x[0] != "" and isset($x[0])) {
$content .= "<tr>
<td>" . $x[0] . "</td>
<td>" . $x[1] . "</td>
</tr>";
$total += $x[1];
}
}
$weeklyTotal += $total;
$content .= "<tr><td>Total Pairage:</td><td>" . $total . "
</td></tr>
<tr><td>Total Dozens:</td><td>" .
number_format($total/12,1) . "</td></tr></table>
</td>
</tr>
</table>";
$options .= "Weekly Pairs: " . $weeklyTotal . "<br>
Weekly Dozens: " . $weeklyTotal/12;
$template->SetTag("options", $options);
$template->SetTag("content", $content);
echo $template->Display();
?>
Before appending do a in_array() test.
It may be better to implement the constraint in your query than to do it in PHP. Try using group by and using min on your date to get the room with the earliest entry. Here is a relevant example on this site.
If you wish to do it inside PHP, you would have to some filter. Set up a temp array to store the final results, and loop through all your results. If the room is not in the result array, add it. If it is, compare the date and use the earliest date.

Categories