how to convert this?
<td><a style="font weight: 700; font-size:1.2em;" href="preview.php? app_id=<?php echo $row['app_id']; ?>name=<?php echo $row['name']; ?>">PRINT</a> <!-- Delete --></td>
into this?
echo '<td>' . $row['curdate'] . '</td>'
here's the full code
<tr class="success">
<td><?php echo $row['app_id']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['addr']; ?></td>
<td><?php echo $row['contact']; ?></td>
<td><?php echo $row['curdate']; ?></td>
<td><a style="font weight: 700; font-size:1.2em;" href="preview.php? app_id=<?php echo $row['app_id']; ?>name=<?php echo $row['name']; ?>">PRINT</a> <!-- Delete --></td>
</tr>
into this?
echo '<tr>';
echo '<td>' . $row['app_id'] . '</td>';
echo '<td>' . $row['name'] . '</td>';
echo '<td>' . $row['addr'] . '</td>';
echo '<td>' . $row['contact'] . '</td>';
echo '<td>' . $row['curdate'] . '</td>';
echo 'PRINT CODE GOES HERE</td>';
You can try something like this :
$html = '<tr class="success">
<td>'. $row['app_id'].'</td>
<td>'.$row['name'].'</td>
<td>'.$row['addr'].'</td>
<td>'.$row['contact'].'</td>
<td>'.$row['curdate'].'</td>
<td><a style="font weight: 700; font-size:1.2em;" href="preview.php?app_id='.$row['app_id'].'name='.$row['name'].'">PRINT</a> <!-- Delete --></td>
</tr>';
echo $html;
You can try this :
echo '<tr class="success">
<td>'. $row['app_id'].'</td>
<td>'.$row['name'].'</td>
<td>'.$row['addr'].'</td>
<td>'.$row['contact'].'</td>
<td>'.$row['curdate'].'</td>
<td><a style="font weight: 700; font-size:1.2em;" href="preview.php?app_id='.$row['app_id'].'&name='.$row['name'].'">PRINT</a> </td>
</tr>';
once again, so "many ways to skin a cat"! (it's a Welsh idiom!)
currently I prefer this:
echo <<<HTML
<tr class="success">
<td>{$row['app_id']}</td>
<td>{$row['name']}</td>
<td>{$row['addr']}</td>
<td>{$row['contact']}</td>
<td>{$row['curdate']}</td>
<td><a style="font weight: 700; font-size:1.2em;" href="preview.php?app_id={$row['app_id']}&name={$row['name']}">PRINT</a> <!-- Delete --></td>
</tr>
HTML;
this way you don't have to worry about: dropping in and out of php (<?php, ?>); multiple echo statements; string concatenation with '. and .'; escaping any ' or " depending on which you used for your echo.
Related
I got this piece of code that shows output from a database in a table. The last column shows the output on a button and can be clicked to go further.
I am looking for a way to style the button with css, but its not doing what i need at all.
<?php
while($row = mysqli_fetch_array($result)){
?>
<tr align="center">
<td><?php echo $row['klasse']; ?></td>
<td><?php echo $row['orde']; ?></td>
<td><?php echo $row['onderorde']; ?></td>
<td><?php echo $row['familie']; ?></td>
<td><?php echo $row['onderfamilie']; ?></td>
<td><?php echo $row['soort']; ?></td>
<td><?php echo $row['ondersoort']; ?></td>
<?php
$output = $row['ned_naam'];
echo "<td> <a href='info.php?value=". $output ."'><button>" . $output . "</button></a> </td>";
echo "</tr>";
}
?>
I tried:
<button class=\'btn\'>" . $output . "</button></a> </td>";
but that doesn't seem to work at all. What am i doing wrong?
There is no reason to use escape character \ on single quote since you wrapped your echo in double quotes
echo "<button class='btn'>" . $output . "</button>";
I'm trying to add a checkbox to the table column but can't figure out how to manage it.
Here is my html and php code :
<table border="1">
<tr>
<th>Select</th>
<th>ID</th>
<th>Title</th>
<th>Author's Last Name</th>
<th>Author's First Name</th>
<th>Format</th>
<th>Price</th>
<th>ISBN Code</th>
</tr>
<?php
while ( $row = mysqli_fetch_array($result) )
echo '<tr>';
echo '<td>' . <input type="checkbox" value ="''" name="todelete[]" /> . $row['id'] . '</td><td>' . $row['title'] . '</td><td>' . $row['author_last'] . '</td><td>' . $row['author_first'] . '</td><td>' . $row['format'] . '</td><td>' . $row['price'] . '</td><td>' . $row['isbncode'] . '</td>';
</tr>
</table>
?>
The code works fine. It shows everything in the table perfectly.
I just can't seem to type the "input type checkbox" in the echo statement. When I attempt to add the input the code stops working.
This is the way to do it :
echo '<td><input type="checkbox" value ="" name="todelete[]" /> '. $row['id'] . '</td><td>' . $row['title'] . '</td><td>' . $row['author_last'] . '</td><td>' . $row['author_first'] . '</td><td>' . $row['format'] . '</td><td>' . $row['price'] . '</td><td>' . $row['isbncode'] . '</td>';
and also, add curly braces to your while loop {}.
<?php
while ( $row = mysqli_fetch_array($result) ) {
echo '<td><input type="checkbox" value ="" name="todelete[]" /> '. $row['id'] . '</td><td>' . $row['title'] . '</td><td>' . $row['author_last'] . '</td><td>' . $row['author_first'] . '</td><td>' . $row['format'] . '</td><td>' . $row['price'] . '</td><td>' . $row['isbncode'] . '</td>';
echo '</tr>';
}
?>
</table>
You can show input type='checkbox' in php in two ways :
CASE 1 : instead close the php tag and put the html code
<?php
while ( $row = mysqli_fetch_array($result) )
{
?>
<tr>
<td><input type="checkbox" value ="''" name="todelete[]" />
<?php echo $row['id']; ?> </td>
<td><?php echo $row['title']; ?></td>
<td><?php echo $row['author_last']; ?></td>
<td><?php echo $row['author_first']; ?></td>
<td><?php echo $row['format']; ?></td>
<td><?php echo $row['price']; ?></td>
<td><?php echo $row['isbncode']; ?></td>
</tr>
</table>
<?php
}
?>
CASE 2 : Put your html code in php with the of echo
<?php
while ( $row = mysqli_fetch_array($result) ) {
echo '<td><input type="checkbox" value ="" name="todelete[]" /> '. $row['id'] . '</td><td>' . $row['title'] . '</td><td>' . $row['author_last'] . '</td><td>' . $row['author_first'] . '</td><td>' . $row['format'] . '</td><td>' . $row['price'] . '</td><td>' . $row['isbncode'] . '</td>';
echo '</tr>';
}
?>
$id=$row['id'];
$str = <<<EOD
<table>
<tr>
<td>
<input type="checkbox" value ='' name='todelete[]' /> $id
</td>
</tr>
</table>
EOD;
echo $str;
Here is more example on "Heredoc string quoting example"
http://php.net/manual/en/language.types.string.php
I am fetching data from Mysql database and populating them in a table.
However, i cannot seem to make the cell autofit to contents. I have tried width as the property of the table but i cant get it to work
Would really appreciate your help. Thanks
Here's what i have done so far
<div id="page-content-wrapper">
<div class="container-fluid">
<div class="row">
<div class="col-lg-12">
<table class="table table-bordered" style="width:100%">
<thead>
<tr>
<th><center>ID</center></th>
<th>Name</th>
<th><center>Email</center></th>
<th>Number</th>
<th>Package</th>
<th>Flexibility</th>
<th >Date</th>
<th>Departuring From</th>
<th>Departure Date</th>
<th>Destination</th>
<th>Arrival Date</th>
<th>Price</th>
<th>Consolidator</th>
</tr>
</thead>
<tbody>
<?php
$query = 'SELECT * FROM queries';
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
echo '<tr>';
echo '<td>'. $row['id'] . '</td>';
echo '<td>'. $row['name'] . '</td>';
echo '<td>'. $row['email'] . '</td>';
echo '<td>'. $row['contactnumber'] . '</td>';
echo '<td>'. $row['packagedetails'] . '</td>';
echo '<td>'. $row['flexibility'] . '</td>';
echo '<td>'. $row['datetoday'] . '</td>';
echo '<td>'. $row['departure'] . '</td>';
echo '<td>'. $row['dateofdeparture'] . '</td>';
echo '<td>'. $row['destination'] . '</td>';
echo '<td>'. $row['dateofarrival'] . '</td>';
echo '<td>'. $row['price'] . '</td>';
echo '<td>'. $row['vendor'] . '</td>';
echo '<td width=250>';
echo '<a class="btn btn-success" href="readquery.php?id='.$row['id'].'">Read</a>';
echo ' ';
echo '<a class="btn btn-success" href="updatequery.php?id='.$row['id'].'">Update</a>';
echo ' ';
echo '<a class="btn btn-danger" href="deletequery.php?id='.$row['id'].'">Delete</a>';
echo '</td>';
echo '</tr>';
}
?>
</tbody>
</table>
I never see you echo </tr> or </td>. It would be helpful to give us an output of the HTML being generated by your while loop.
The problem was not with my table. To make it work, i increased the width of my container in which my table was inside, and it worked
How will i Display a picture with the same id here in my pagination.php?
i tried doing this
<td width="20%" valign="top"><a href="product.php?id=' . $id . '">
<img style="border:#666 2px solid;" src="inventory_images/' . $id . '.jpg" width="150" height="102" border="1" /></a>
</td>
but it wont work.. any possible way to display the image using pagination?
for ($i = $start; $i < $end; $i++)
{
// make sure that PHP doesn't try to show results that don't exist
if ($i == $total_results) { break; }
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . mysql_result($result, $i, 'product_name') . '</td>';
echo '<td>' . mysql_result($result, $i, 'price') . '</td>';
echo '<td>' . mysql_result($result, $i, 'details') . '</td>';
echo '<td>' . mysql_result($result, $i, 'category') . '</td>';
echo '<td>' . mysql_result($result, $i, 'subcategory') . '</td>';
echo '<td>Click To View</td>';
echo "</tr>";
}
Probably you have to put in your code something like that :
<td width="20%" valign="top">
<a href="product.php?id=<?php echo $id; ?>">
<img style="border:#666 2px solid;" src="inventory_images/<?php echo $id; ?>.jpg" width="150" height="102" border="1" />
</a>
</td>
Check the generated source and adjust the code above. Please keep in mind that in HTML you cannot put php variables directly. You have to output them with php echo statement like that :
<html>
SOME HTML
<div class="valueFromPhp"><?php echo $value; ?></div>
</html>
Also remebmer to escape output using htmlentities() to prevent XSS attacks.
echo '
<td>
<img width=100 src="../PHP/saved_images/'.mysql_result($result, $i, "bkimg").'">
</td>
';
I have a php file(add_member.php). I'm creating a dynamic table by executing SQL query. The code of this file is as follows:
$sql = SELECT * FROM member_details WHERE lname='ABC';
$n = new data();
$res = $n -> querySend($sql);
?>
<table border="1" cellpadding="3" cellspacing="1">
<tr>
<td align="center"></td>
<td align="center">First Name</td>
<td align="center">Last Name</td>
<td align="center">Caste</td>
<td align="center">Residence address</td>
<td align="center">Education</td>
</tr>
<?php
$i=1;
while($row = mysql_fetch_array($res))
{
$member_no = $row['member_no'];
$total_member = "SELECT COUNT(*) AS total_member FROM family_member_details WHERE member_no =" .$member_no;
$total_res = $n -> querySend($total_member);
$row_total = mysql_fetch_array($total_res);
?>
<tr>
<td align="center" rowspan="<?php echo $row_total['total_member']+1;?>"><?php echo $i;?></td>
<td align="center"><?php echo $row['fname'];?></td>
<td align="center"><?php echo $row['lname'];?></td>
<td align="center"><?php echo $row['caste'];?></td>
<td align="center"><?php echo $row['residence_addr'];?></td>
<td align="center"><?php echo $row['education'];?></td>
</tr>
<?php
$family_sql = "SELECT * from family_member_details WHERE member_no = $member_no";
$family_res = $n -> querySend($family_sql);
while($row1 = mysql_fetch_array($family_res))
{
?>
<tr>
<td align="center"><?php echo $row1['name']?></td>
<td align="center"><?php echo $row1['name']?></td>
<td align="center"><?php echo $row1['name']?></td>
<td align="center"><?php echo $row1['name']?></td>
<td align="center"><?php echo $row1['name']?></td>
</tr>
<?php }
$i++;
} ?>
</table>
Now upon clicking on a button I want to create the same table into PDF file. For this purpose I decided to use TCPDF library. But for it I've to provide the HTML content to TCPDF file. Now my issue is how should I get the HTML of a dynamically generated table from PHP file and write this content to the text file? Can anyone please help me in this regard? Any help would be highly appreciated.
Instead of displaying your table directly to the browser page, simply store the text in a variable and echo it...then you can send the var to the TCPDF library.
$dyn_table = '<table border="1" cellpadding="3" cellspacing="1"><tr><td align="center">/td><th align="center">First Name</th><th align="center">Last Name</th><th align="center">Caste</th><th align="center">Residence address</th><th align="center">Education</th></tr>';
$i = 1;
while ($row = mysql_fetch_array($res)) {
$member_no = $row['member_no'];
$total_member = "SELECT COUNT(*) AS total_member FROM family_member_details WHERE member_no =" . $member_no;
$total_res = $n->querySend($total_member);
$row_total = mysql_fetch_array($total_res);
$dyn_table .= '<tr><td align="center" rowspan="' . $row_total['total_member'] + 1 . '">' . $i . '</td><td align="center">' . $row['fname'] . '</td><td align="center">' . $row['lname'] . '</td><td align="center">' . $row['caste'] . '</td><td align="center">' . $row['residence_addr'] . '</td><td align="center">' . $row['education'] . '</td></tr>';
$family_sql = "SELECT * from family_member_details WHERE member_no = $member_no";
$family_res = $n->querySend($family_sql);
while ($row1 = mysql_fetch_array($family_res)) {
$dyn_table .= '<tr><td align="center">' . $row1['name'] . '</td><td align="center">' . $row1['name'] . '</td><td align="center">' . $row1['name'] . '</td><td align="center">' . $row1['name'] . '</td><td align="center">' . $row1['name'] . '</td></tr>';
}
$i++;
}
$dyn_table .= '</table>';
echo $dyn_table;
EDIT
In order to post this html to your TCPDF library, I would use AJAX to prevent another page request/load. I prefer to use JQuery as it simplifies this process immensely. Here is one way you could do it:
<input type="button" name="TCPDF" id="submitToTCPDF" />
<script type="text/javascript">
var url = 'php/script/to/handle/post';
var data = {'table_html': '<? echo $dyn_table; ?>'};
$('#TCPDF').click(function(){
$.ajax({
type: "POST",
url: url,
data: data,
success: function($result){
// Do whatever after html is submitted
}
});
});
</script>
You can read more about Jquery's AJAX post method in this StackOverflow question.