attach hyperlink to dynamic table cell - php

I have a table in generated using PHP using data collected from MYSQL. How do I append a hyperlink to a row?
Here is the code for the dynamic table I am using:
mydata = mysql_query($sql,$con);
echo "<table id='name',table border=0>
<tr>
<th>Users</th>
<th>Status<th>
</tr>";
while($record = mysql_fetch_array($mydata)){
echo "<tr>";
echo "<td>" . $record['user_id'] . "</td>";
if (strtolower(trim($record['activity']))!=strtolower('LOGIN')){
echo "<td>" . $default1 . "</td>";
}else{
echo "<td>" . $default . "</td>";
}
echo "</tr>";
}
echo "</table>";
;
I have tried appending a href="..." style="display:block;"> but cannot get it to work.

Try echoing out a hyperlink.
echo "<a href='http://www.google.com/'>Google</a>";
If you want other functionality, such as clicking on a table row to work as a hyperlink, you would need to implement that in javascript as a table cell/row is not a hyperlink.

Related

From php table retrieve data from selected checkbox

I have below php table, it has row and column. Each row has checkbox, I am trying to retrieve email data from row that checkbox is selected. I can do this with Jquery, but since I can't save in text file. I am trying to do same thing with PHP. I try couple things, but none worked.
<?php
echo "<table border='1' bordercolor='red' id='event_table'>
<tr>
<th> <input type='checkbox' id='chk_all' /> </th>
<th>Id</td>
<th>Email</th>
<th>Name</th>
<th>Car</th>
<th>Phone</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td> <input type='checkbox' id='chk[]'/> </td>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['car'] . "</td>";
echo "<td>" . $row['phone'] , "</td>";
echo "</tr>";
}
echo "</table>";
?>
You can do this using Hidden fields and some indentity implementation on name= (and id= as well). Here are some inputs from my end but you can modify the code as per your requirements.
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td> <input type='checkbox' name='chk_".$row['id']."' id='SHOULD_BE_UNIQUE_ON_THIS_PAGE'/> </td>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['email'] . "<input type='hidden' name='chk_".$id."_email' value='".$row['email']."' id='SHOULD_BE_UNIQUE_ON_THIS_PAGE' /></td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['car'] . "</td>";
echo "<td>" . $row['phone'] . "</td>";
echo "</tr>";
}
echo "</table>";
Here chk_{ID} is your main identifier while filtering data. When you submit the form, you should get to know whether checkbox is checked or not and if it is checked then you will come to know which hidden input field has your data. Hidden fields will not appear in HTML page.
As PHP is a Hypertext Preprocessor language, you can't detect any client action on your page. So here is the best solution: you must detect changes you need with JavaScript/JQuery, and then send the result to a php file via Ajax and handle the result there (In your case save it in a file);
I hope it would be helpful for you. Let me know if you have any questions

Retrieve data from database to textbox wordpress

I need to fetch data from PHP MyAdmin database to WordPress website page. Since I am new to WordPress I am not too sure which method I have to follow. I tried to edit the source code of the page by adding the new PHP code inside PHP tags. But it did not work. And I found a file named 'functions.php' in the local directory where my project located. Is that the file I need to use this code? I've already written the code and I just need to know where to use it.
<?php
$results = $wpdb->get_results( "SELECT * FROM $table_name"); // Query to fetch data from database table and storing in $results
if(!empty($results)) // Checking if $results have some values or not
{
echo "<table width='100%' border='0'>"; // Adding <table> and <tbody> tag outside foreach loop so that it wont create again and again
echo "<tbody>";
foreach($results as $row){
$userip = $row->user_ip; //putting the user_ip field value in variable to use it later in update query
echo "<tr>"; // Adding rows of table inside foreach loop
echo "<th>ID</th>" . "<td>" . $row->id . "</td>";
echo "</tr>";
echo "<td colspan='2'><hr size='1'></td>";
echo "<tr>";
echo "<th>User IP</th>" . "<td>" . $row->user_ip . "</td>"; //fetching data from user_ip field
echo "</tr>";
echo "<td colspan='2'><hr size='1'></td>";
echo "<tr>";
echo "<th>Post ID</th>" . "<td>" . $row->post_id . "</td>";
echo "</tr>";
echo "<td colspan='2'><hr size='1'></td>";
echo "<tr>";
echo "<th>Time</th>" . "<td>" . $row->time . "</td>";
echo "</tr>";
echo "<td colspan='2'><hr size='1'></td>";
}
echo "</tbody>";
echo "</table>";
}
?>

How to cancel a booking for a specific row using php and mysql

I have a table with each row having a cancel link. I would like to cancel a booking for the specific rows. May I know how to start?
The codes of the table is as below.
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0) {
echo "<table border = 1>
<tr>
<th>Custid</th>
<th>Venue</th>
<th>Firt Name</th>
<th>Date</th>
<th>Time</th>
<th>Menu Type </th>
<th>No.of table </th>
<th>Total price </th>
<th>Amount Paid </th>
<th>Make Payment </th>
<th>Cancel Booking</th>
</tr>";
while($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $row['Customer_id']. " </td>";
echo "<td>" . $row['Venue_name']. " </td>";
echo "<td>" . $row['First_name']. "</td>";
echo "<td>" . $row['Date']. "</td>";
echo "<td>" . $row['Time']. "</td>";
echo "<td>" . $row['Menu_type']. "</td>";
echo "<td>" . $row['no_of_table']. "</td>";
echo "<td>" . $row['total_price']. "</td>";
echo "<td>" . $row['total_amt_paid']. "</td>";
echo"<td><a href='Payment.php'>Payment</a></td>";
echo"<td><a href='Cancel.php'>Cancel</a></td>";
echo "</tr>";
}
echo "</table>";
}
You need to pass the Cancel.php script some knowledge of what it should delete, so change
echo "<td><a href='Cancel.php'>Cancel</a></td>";
to
echo '<td>Payment</td>';
And then in the Cancel.php script you should receive that value in the $_GET array
<?php
$Customer_id = $_GET['Customer_id'];
If you want to cancel a specific booking you'll need something specific about each booking, like a booking id for example. If we assume you already have that in your table then you can pass this unique value (specific to the row) to your cancel.php and payment.php pages.
To do this we will use a get request and a query string. This will mean your original line changes from this:
echo "<td><a href='Cancel.php'>Cancel</a></td>";
to this one below. We can add the query string unique to each row like this: (assuming it might be called booking_id)
echo "<td><a href='Cancel.php?booking_id=".$row['booking_id']."'>Cancel</a></td>";
You can now retrieve this value on the Cancel.php page like this:
$booking_id = $_GET['booking_id'];
It's worth reading up about SQL injection and consider adding some cleaning method for your string to avoid this. If you are a little rusty on GET and POST then you can check this explanation out.

Adding php variable into a href link into a table

I am working on uploading a file along with a description and table and then displaying it in a table format. My problem is that I'm not sure how to link my the path for the uploaded file into the table so the user can click on the link in the table and it will download.
Code:
This is what I'm attempting to use>
<?php
include 'connect.php';
$result = mysqli_query($con,"SELECT DocDate, Description, DocFile FROM Documents");
echo "<table border='0' width='100%'>
<col width='50'>
<col width='100'>
<tr>
<th>Date</th>
<th>Description</th>
<th>File</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['DocDate'] . "</td>";
echo "<td>" . $row['Description'] . "</td>";
echo "<td>" $row['DocFile'] "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
If you feel it to be usefull I'm happy to add the code where I upload the file to my server.
EdiT Sorry I put in the wrong variable thing into my table, I don't think it changes it too much
Is this what you're looking for?
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['DocDate'] . "</td>";
echo "<td>" . $row['Description'] . "</td>";
echo "<td> " . $row['DocFile'] . " </td>";
echo "</tr>";
}
This is how you can do
echo '<td>'.$name.'</td>';
I prefer to use ' ' and within it have the HTML since HTML will contain a lot of "" so no need to use escape them and then separate PHP and HTML with concatenation.
you have syntax error in this line in while loop:
echo "<td>" $name "</td>";
should be :
echo "<td> $name </td>";
In this line you have an error:
echo "<td>" $row['DocFile'] "</td>";
You need to scape the " character:
echo "<td> " . $row['DocFile'] . " </td>";
With your syntax you are creating an error because you are ending the string after the td tag.
If you are using a web url which uses a php variable in the url and want to open a new tab when you click on the hyper link, use this
echo "<td><a target='_blank' href=\"http://view.php?Id=".$row['Id']."\">". $row['Id'] ."</a></td>";
This is helpful when you have to use a php variable in the hyperlink and is being used in a table. Clicking on the Id will open the page related to that Id in a new tab in this case.

Displaying MySQL data in PHP Table Not working

Currently I'm creating just a simple website that I'm fooling around with. Users can add movies to watch, and then can view them later on. What my current problem is, is this.
Sorry for the large image. As you can see its displaying the first result correctly, but the second result gets all skrewy and displays at the top of the screen. My code for displaying the data is:
$result = mysql_query("SELECT * FROM `movies`");
echo "
<table id=\"allTable\" align=\"center\" border=\"0\" cellpadding=\"4\" cellspacing=\"0\" width=\"100%\">
<tr>
<th>ID</th>
<th>Movie</th>
<th>Genre</th></tr>";
while($row = mysql_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['genre'] . "</td";
echo "</tr><br />";
echo "</table>";
}
Any help would be greatly appreciated!
EDIT Fixed the problem right after I created this. Removed from while loop and put it under. Fixed.
echo "</table>"; should be moved outside of your while($row = mysql_fetch_array($result))
while($row = mysql_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['genre'] . "</td";
echo "</tr><br />";
}
echo "</table>";

Categories