PHP Table - Different Hyperlink on each row - php

What I am trying to do is get it so there is a different hyper link address for each row echoed. Code is below:
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>Product Name</th> <th>Product Description</th> <th>Product Price</th> <th>Product Image</th> <th>View Product Details</th></tr>";
while($row = mysql_fetch_array( $result )) {
echo "<tr>";
echo '<td>' . $row['Product_Name'] . '</td>';
echo '<td>' . $row['Product_Description'] . '</td>';
echo '<td>' . $row['Product_Price'] . '</td>';
echo '<td>Picture Enlarge</td>';
echo '<td>View Details</td>';
echo "</tr>";
}
echo "</table>";

You can put php variables inline in your echo'ed anchors just as you are doing with the other variables.
Assuming that you're using the id field in your database you can do this:
echo '<td>View Details</td>';
If you echo a php variable (e.g. $row['id']) then it will echo out to HTML (not necessarily text). So as this one is contained in an anchor tag (in the HTML) it echos to the anchor tag and builds the id part. :)

<table border='1' cellpadding='10'>
<tr>
<th>Product Name</th>
<th>Product Description</th>
<th>Product Price</th>
<th>Product Image</th>
<th>View Product Details</th>
</tr>
while($row = mysql_fetch_array( $result )) {
<tr>
<td> <?php echo $row['Product_Name']; ?></td>
<td><?php echo $row['Product_Description']; ?></td>
<td><?php $row['Product_Price']; ?></td>
<td>Picture Enlarge</td>
<td>View Details</td>
</tr>
}
</table>
I would probably go for something like that. This assumes that your table returns a unique id, from something like an auto incremented column.
This doesn't include any escaping so could be vulnerable to exploitation. I would also look into templating. It may help you with your presentation, makes it a little easier to read the mixed html and php.

"<td align=\"center\" valign=\"top\"><<") . "</td>\n"

Related

Conditional formatting php table

I need help with a simple task, but I am stuck...
I have a database where I pull different data and I am visualizing those data on a web page.
Here is my query for getting the data:
$result = mysqli_query($mysqli, "SELECT *, DATEDIFF(nextcalibration, CURDATE()) AS days FROM tools AS dp ");
Here is my table:
<table id="table_id" class="table table-striped">
<tr>
<th>Nr</th> <th>Status</th> <th>name</th> <th>Serial</th> <th>Used At</th> <th>Owner</th> <th>Calibrated</th> <th>nextcalibration</th> <th>vendor</th> <th>days</th> <th>actions</th>
</tr>
<?php
while($user_data = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>".$user_data['toolnr']."</td>";
echo "<td>".$user_data['status']."</td>";
echo "<td>".$user_data['toolname']."</td>";
echo "<td>".$user_data['serial']."</td>";
echo "<td>".$user_data['usedat']."</td>";
echo "<td>".$user_data['owner']."</td>";
echo "<td>".$user_data['calibrated']."</td>";
echo "<td>".$user_data['nextcalibration']."</td>";
echo "<td>".$user_data['vendors']."</td>";
echo "<td>".$user_data['days']. "</td>";
echo "<td><a href='edit.php?id=$user_data[id]'><img src='img/edit.png' ></a> | <a href='delete.php?id=$user_data[id]' onclick='return checkDelete()'><img src='img/delete.png'></a></td></tr>";
}
?>
</table>
I would like to make conditional formatting only for the last column - "days".
The idea is if the value of "days" <= 30 to become red color text.
I tried with various JS, but honestly, it did not work.
<table id="table_id" class="table table-striped">
<tr>
<th>Nr</th> <th>Status</th> <th>name</th> <th>Serial</th> <th>Used At</th> <th>Owner</th> <th>Calibrated</th> <th>nextcalibration</th> <th>vendor</th> <th>days</th> <th>actions</th>
</tr>
<?php
while($user_data = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>".$user_data['toolnr']."</td>";
echo "<td>".$user_data['status']."</td>";
echo "<td>".$user_data['toolname']."</td>";
echo "<td>".$user_data['serial']."</td>";
echo "<td>".$user_data['usedat']."</td>";
echo "<td>".$user_data['owner']."</td>";
echo "<td>".$user_data['calibrated']."</td>";
echo "<td>".$user_data['nextcalibration']."</td>";
echo "<td>".$user_data['vendors']."</td>";
echo "<td>";
$days=$user_data['days'];
if($days <= 30){
echo "<span style='color:red;'>$days</span>";
}else{
echo $days;
}
echo "</td>";
echo "<td><a href='edit.php?id=$user_data[id]'><img src='img/edit.png' ></a> | <a href='delete.php?id=$user_data[id]' onclick='return checkDelete()'><img src='img/delete.png'></a></td></tr>";
}
?>
</table>
Would something simple like this - without JS- work?
<table id="table_id" class="table table-striped">
<tr>
<th>Nr</th> <th>Status</th> <th>name</th> <th>Serial</th> <th>Used At</th> <th>Owner</th> <th>Calibrated</th> <th>nextcalibration</th> <th>vendor</th> <th>days</th> <th>actions</th>
</tr>
<?php
while($user_data = mysqli_fetch_array($result))
{
$style_days=($user_data['days']<=30)?'style="color:red"':'';
echo "<tr>";
echo "<td>".$user_data['toolnr']."</td>";
echo "<td>".$user_data['status']."</td>";
echo "<td>".$user_data['toolname']."</td>";
echo "<td>".$user_data['serial']."</td>";
echo "<td>".$user_data['usedat']."</td>";
echo "<td>".$user_data['owner']."</td>";
echo "<td>".$user_data['calibrated']."</td>";
echo "<td>".$user_data['nextcalibration']."</td>";
echo "<td>".$user_data['vendors']."</td>";
echo "<td ".$style_days.">".$user_data['days']. "</td>";
echo "<td><a href='edit.php?id=$user_data[id]'><img src='img/edit.png' ></a> | <a href='delete.php?id=$user_data[id]' onclick='return checkDelete()'><img src='img/delete.png'></a></td></tr>";
}
?>
</table>
Of course, apply the necessary guarding for $user_data['days'] (existence, numeric..) :)

Im trying to retrieve data from mysql database into a html table

Almost everything is working fine except on the first column, the data is preceded by "1".
Here's my output :
The first column,ie,strength is preceded with 1. The data in my database is just a single digit number.
<?php
include 'index.php';
$sql = "SELECT t.strength, t.timeslot, t.cust_phno , c.fname, c.lname FROM tables t,customer c WHERE t.cust_phno = c.cust_phno";
$result = mysqli_query($con,$sql);
?>
<div id="view">
<table style="width:90%">
<thead>
<tr>
<th> Table Strength </th>
<th> Time Slot </th>
<th> Customer Phone Number</th>
<th> Customer Name </th>
</tr>
</thead>
<tbody>
<?php
while( $array = mysqli_fetch_assoc($result)) {
echo
print "<tr> <td>";
echo $array["strength"];
print "</td> <td>";
echo $array["timeslot"];
print "</td> <td>";
echo $array["cust_phno"];
print "</td> <td>";
echo $array["fname"];
print "&nbsp";
echo $array["lname"];
print "</td> </tr>";
}
?>
</tbody>
</table>
The 1 is the result of echoing the return from print... the following lines need to be slightly different..
while( $array = mysqli_fetch_assoc($result)) {
echo
print "<tr> <td>";
You don't need the echo...
while( $array = mysqli_fetch_assoc($result)) {
print "<tr> <td>";
<?php
while( $array = mysqli_fetch_assoc($result)) {
print "<tr> <td>";
echo $array["strength"];
print "</td> <td>";
echo $array["timeslot"];
print "</td> <td>";
echo $array["cust_phno"];
print "</td> <td>";
echo $array["fname"];
print "&nbsp";
echo $array["lname"];
print "</td> </tr>";
}
?>
Remove the echo from your code.
It is the simple and easy process, that I will show you as an example and source code.
At first connect database using mysqli in PHP. Using this code : $con=mysqli_connect(‘localhost’,’root’,’password’,’Database Name’);
Now select all data using mysqli from MySQL table in PHP. If you using MySQL query for select record or fetch the record in PHP project, so it’s simple use for you. Fetch result in your table page using while loop.
<?php
include "db_connect.php";
?>
<table class="table">
<thead class="thead-inverse">
<tr>
<th>#</th>
<th>Name</th>
<th>Email</th>
<th>Phone</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<?php
$i=1;
$mysqli_qry=mysqli_query($con,"SELECT * from `Table Name` ORDER BY `id` DESC");
while($row=mysqli_fetch_array($mysqli_qry))
{
?>
<tr>
<th scope="row"><?php echo $i; ?></th>
<td><?php echo $row['1']; ?></td>
<td><?php echo $row['3']; ?></td>
<td><?php echo $row['2']; ?></td>
<td><?php echo $row['4']; ?></td>
</tr>
<?php $i++; } ?>
</tbody>
</table>

How to show the clicked table row id in the address bar

I have a PHP based website for jobs. I echo all the jobs from database in a table below. When i click on a job in table row it shows the job details in an iframe beside the table.
This is the code of the table and iframe index.php
<table class="table1">
<tr>
<td colspan="3">
<?php
include "job.header.php";
?>
</td>
</tr>
<tr>
<td width="30%">
<div>
<?php
require "module/job.call.php";
?>
</div>
</td>
<td width="60%">
<iframe name="content" src="../module/job.unclicked.php">
</iframe>
</td>
<td width="20%"> </td>
</tr>
</table>
This is the code that i call jobs from the database job.call.php
<?php
$result = mysqli_query($conn,"SELECT * FROM job where approved='1' ORDER BY `CreatedTime` DESC");
echo "<table id='maintable' class='table-fill' border='0' cellpadding='0' cellspacing='0'>
<tr>
<th position='fixed' overflow='hidden' width='10%'>Job Title</th>
<th position='fixed' width='5%'>Company Name</th>
<th width='5%'>Closing Date</th>
</tr>";
while($row = mysqli_fetch_array($result) ) {
if (strlen($row['positiontitle']) > 20)
$row['positiontitle'] = substr($row['positiontitle'], 0, 60) . "...";
echo "<tr ref='job.details.php?id=".$row['id']."' target='content' class='positiontitle-link'>";
echo "<td><font style='text-shadow: none;'>" . $row['positiontitle'] . "</font></a></td>";
echo "<td>" . $row['companyname'] . "</td>";
echo "<td>" . $row['closingdate'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($conn);
?>
I want that when i click on the job in the table row then the address bar should take the id of the job. because if someone wants to share the job then they should just send that address for specific job.
Now it show like this
I want that it should look like this

How to get_results from the database and display sorted in a table

I am trying to display information from a table I created in my Wordpress plugin (code below) and I can display in the table without any issues but I am having trouble figuring out how to display in some sort of sorted order, I.E. sorted by row ID.
<?php
echo "<table class='widefat'>"; // start a table in HTML
echo "<thead>
<tr>
<th>Select</th>
<th>Id</th>
<th>Serial Number</th>
<th>UserID</th>
<th>Owner</th>
<th>Name</th>
<th>AreaCode</th>
<th>Number</th>
<th></th>
</tr>
</thead>
<tfoot>
<tr>
<th>Select</th>
<th>Id</th>
<th>Serial Number</th>
<th>UserID</th>
<th>Owner</th>
<th>Name</th>
<th>Area Code</th>
<th>Number</th>
<th></th>
</tr>
</tfoot>
<tbody>";
$result = $wpdb ->get_results( "SELECT * FROM wp_new_table ");
foreach($result as $row){
echo "<tr><td><input name='ID_selected' type='radio' value='" . $row->id. "' /></td><td>" . $row->id. "</td><td>" . $row->sn. "</td><td>" . $row->userid. " </td><td>" . get_user_display_name($row->userid) ."</td><td>" . $row->name. "</td><td>" . $row->areacode. "</td><td>" . $row->phone. "</td><td>
<a href='" . $_SERVER['REQUEST_URI'] . "&dbviewedit=" . $row->id . "'>View/Edit</a></td></tr>";
}
echo "</tbody></table>";
?>
you have to adjust the SQL request, since the result is already not ordered
SELECT columnname FROM tablename ORDER BY columnname ASC|DESC
I hope this is what you're looking for

viewing database using php in different way

I am doing simple shopping site and I only know how to view the products from the database in table such as this code below.
<table width ="793" align ="center" border ="5" >
<tr bgcolor ="red">
<th width="90">product No </th>
<th>User Name </th>
<th width="184">product name</th>
<th>product price</th>
<th>Edit product</th>
<th>Delete product</th>
</tr>
<?php
mysql_connect("localhost","root","root");
mysql_select_db("db");
$query = "SELECT * FROM products";
$run = mysql_query($query);
while($row = mysql_fetch_array($run))
{
echo "<tr>";
echo '<td align="center"><font color="white"><b>' . $row['id'] . '</b></td>';
echo '<td align="center"><font color="white"><b>' . $row['product_name'] . '</b></td>';
echo '<td align="center"><font color="white"><b>' . $row['product_price'] . '</b></td>';
echo '<td align="center"><font color=" #e52d00"> <b>Edit</b></td>';
echo '<td align="center"><font color=" #e52d00"><b>Delete</b></td>';
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>
and I want to know how to view data in way such as amazon or ebay. I mean inside table.
I hope you know what I mean.
Please use CSS to format the resultset..

Categories