-1

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>" <a href="uploads/$row['DocFile']">$row['DocFile']</a> "</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

user3316499
  • 1
  • 1
  • 8

5 Answers5

0

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> <a href=\"uploads/" . $row['DocFile'] . "\"> " . $row['DocFile'] . " </a> </td>";
  echo "</tr>";
  }
aksu
  • 5,221
  • 5
  • 24
  • 39
0

This is how you can do

echo '<td><a href="uploads/'.$name.'">'.$name.'</a></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.

Abhik Chakraborty
  • 44,654
  • 6
  • 52
  • 63
0

you have syntax error in this line in while loop:

echo "<td>" <a href="uploads/$name">$name</a> "</td>";

should be :

echo "<td> <a href=\"uploads/$name\">$name</a> </td>";
Awlad Liton
  • 9,366
  • 2
  • 27
  • 53
0

In this line you have an error:

echo "<td>" <a href="uploads/$row['DocFile']">$row['DocFile']</a> "</td>";

You need to scape the " character:

echo "<td> <a href=\"uploads/" . $row['DocFile'] . "\"> " . $row['DocFile'] . " </a> </td>";

With your syntax you are creating an error because you are ending the string after the td tag.

Donnie Rock
  • 552
  • 1
  • 14
  • 27
  • I've edited it, but are you understanding your error? If you put an unscaped " in a string you are closing the string line. – Donnie Rock Feb 22 '14 at 14:21
  • 2
    Yes I sort of understand it now but that gives the error "syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/www/familease.org.uk/testing.php on line 20" – user3316499 Feb 22 '14 at 14:23
  • http://stackoverflow.com/questions/1318028/php-different-quotes In this post difference between single ' and " in string usement is explained. – Donnie Rock Feb 22 '14 at 14:29
0

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.

Shanu
  • 95
  • 11