Adding php variable into a href link into a table - php

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.

Related

How to make string/variables from query results into a link in php

best regards.
I know perhaps some's considering my question is quite stupido. But I've been trying looking for to this simple scripting. As my post title above, how to make the query results into a url. I have this scripts:
$result = mysqli_query($con,'SELECT......');
echo "<table border='1'>
<tr>
<th>Title</th>
<th>Author</th>
<th>Publisher</th>
<th>Category</th>
<th>Link</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . $row['author_name'] . "</td>";
echo "<td>" . $row['publisher_name'] . "</td>";
echo "<td>" . $row['cat_name'] . "</td>";
echo "<td>" . $row['url_flipbook'] . "</td>";
echo "</tr>";
}
echo "</table>";
I want to make the result from $row ->'url_flipbook' into a url. The $row -> 'url_flipbook' will produce a html pages that users can click on it, it's located to a folder in my localhost.
The database field:
| url_flipbook |
-----------------
/myspace/click-it/info_1.html
I want that query results became a link for the output.
I've tried:
<a href=''echo $row['url_flipbook'];'?>''>FLIPBOOK</a>"</td>";
echo "<td>" . <a href='$row['url_flipbook']>FLIPBOOK</a>"</td>";
Nothing works...if you could help me to solve this problems. Thank you so much...
best regards,
Kris
Just concat the string to the href:
echo "<td>FLIPBOOK</td>";
or alternatively:
echo '<td>FLIPBOOK</td>';
did u try
echo "<td>" . "<a href='" . $row['url_flipbook'] . "' >FLIPBOOK</a></td>";
try this
echo "<td><a href='".$row['url_flipbook']."'>FLIPBOOK</a></td> ";

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>";

attach hyperlink to dynamic table cell

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.

mySQL blob images printout?

I know this has been asked before, and I know you can do it via making a seprate page for each image. But thats not ideal for what I want.
I want to do that age old thing of displaying multiple images from a db on the same page:
echo "<table>";
echo "<tr class ='tablehead'><td>Name</td><td>Location</td><td>Review</td><td>Image</td><td>Thumb</td></tr>";
while ($row = mysql_fetch_array($query))
{
echo "<tr>";
echo "<td>" . $row['user_fname'] . "</td>";
echo "<td>" . $row['user_location'] . "</td>";
echo "<td>" . $row['user_review'] . "</td>";
echo "<td>" . $row['user_image'] . "</td>";
echo "<td>" . $row['user_thumb'] . "</td>";
echo "</tr>";
}
echo "</table>";
user_image and user_thumb are blob images, is there someway of showing them all on that page, perhaps setting them to a php variable and then converting to javascript or something along those lines? Rather than:
header('Content-type: image/jpg');
echo $thumb;
In a seperate file?
You have basically two problems here:
As $thumb contains the binary data of the image, the browser will not understand it unless you tell the browser what data it is (e.g. image/jpg).
You need to tell the browser where the data is.
Let's say you want to create an image displaying the thumb in that page:
<td><img src="..." alt="thumb"></td>
The src attribute tells the browser where it can find the data of the image. So it is used to solve problem 2. It expects an Uniform Resource Locator (URI).
So how to get the $thumb into an URI? There are multiple ways to do that, including the one linked in a comment.
However, if the image is not very large and you don't need to have it cached specifically (e.g. the HTML should be cached, but not the thumb image), you can make use of a data: URI Scheme­Wikipedia:
$thumbSrc = 'data:image/jpg;base64,'.base64_encode($thumb);
You then can output that variable as the src attribute's value:
<td><img src="<?php echo $thumbSrc; ?>" alt="thumb"></td>
Hope this is helpful.
Complete answer:
echo "<table>";
echo "<tr class ='tablehead'><td>Name</td><td>Location</td><td>Review</td><td>Image</td><td>Thumb</td></tr>";
while ($row = mysql_fetch_array($query))
{
echo "<tr>";
echo "<td>" . $row['user_fname'] . "</td>";
echo "<td>" . $row['user_location'] . "</td>";
echo "<td>" . $row['user_review'] . "</td>";
echo '<td><img src="data:image/jpg;base64,', base64_encode($row['user_thumb']), '" alt='thumb'></td>';
echo '<td><img src="data:image/jpg;base64,', base64_encode($row['user_image']), '" alt='image'></td>';
echo "</tr>";
}
echo "</table>";
You may use Data URI Scheme. But note that not all browsers support this type of URI.
echo "<table>";
echo "<tr class ='tablehead'><td>Name</td><td>Location</td><td>Review</td><td>Image</td><td>Thumb</td></tr>";
while ($row = mysql_fetch_array($query))
{
echo "<tr>";
echo "<td>" . $row['user_fname'] . "</td>";
echo "<td>" . $row['user_location'] . "</td>";
echo "<td>" . $row['user_review'] . "</td>";
echo "<td>" . $row['user_image'] . "</td>";
echo "<td><img src='data:image/jpeg;base64," . base64_encode($row['user_thumb']) . "' alt='' /></td>";
echo "</tr>";
}
echo "</table>";

how to format a url to be live

I have been working on trying to teach myself programming and have come stuck on a simple problem ,
the line I am working with is
echo "<td>" . $row['website'] . "</td>";
only in the database {mysql} it is in plain text under the column 'website' , I have been trying to work out how to make the row website clickable for the whole table,
i have tried <href ="echo "<td>" . $row['website'] . "</td>"";
I have tried searching the web for an answer , only i dont seem to be able to phrase the question for the right results.
thank you .
I also tried
<?
$result = mysql_query("SELECT * FROM leader");
echo "<table border='1'> <tr> <th>id</th> <th>Club</th> <th>Website</th> <th>Club Badge</th> </tr>";
while($row = mysql_fetch_array($result)) {
echo "<tr>"."<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['club'] . "</td>";
echo "<td>" . $row['website' ] . "</td>";
echo "<td><a class=\"mylink\" href=\"" . $row['website'] . "\">" . $row['website'] . "</a></td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
but still got Parse error: syntax error, unexpected '<' in /home/a6332763/public_html/res.php on line 29
UPDATE......... Have now got link to show in right box only its adding the sites url in-front of the links url, here is the code , minus mysqul connection .
<?php
$result = mysql_query("SELECT * FROM leader");
echo "<table border='1'>
<tr>
<th>id</th>
<th>Club</th>
<th>Website</th>
<th>Club Badge Url</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>"."<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['club'] . "</td>";
echo "<td><a class=\"mylink\" href=\"" . $row['website'] . "\">" . $row['website'] . "</a></td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
<html>
<body>
Link text
Click on this link to run your first PHP script.
</body>
</html>
Anchor tags (<a>) cannot contain table rows or cells. To make the entire row clickable, you must bind an onclick handler with JavaScript or wrap the contents of each individual <td> element with its own <a> tag.
If what you are looking for is a way to make a link out the area of the row where your links is do the following:
In the PHP file:
echo "<td><a class=\"mylink\" href=\"" . $row['website'] . "\">" . $row['website'] . "</a></td>";
In the CSS file:
.mylink{display:block;}
That should do it.
Fill the appropriate onclick attribute with JavaScript code to open the new location.
If you just want the a regular link inside a table cell what you want is
<td><?= $row['website'] ?></td>
If you were actually trying to make the entire table row clickable, you need to use a javascript redirect set on the table row:
<tr onClick="window.location='<?= $row['website'] ?>'">
<td><?= $row['website'] ?></td>
</tr>

Categories