I have a table in MySQL database. It consists of many columns. The last column is BLOB column and it is storing pdf files. I want to call particular columns of the table. The BLOB column is the last one. I want the <td> elements to say Download and on click of the anchor tag I want the pdf to get downloaded. Somebody please help. The values of the Copy Column is going to be pdf files.
$result= mysql_query("SELECT Name, Type, No, Copy from table1 Join table2 where username='{$_SESSION['username']}' AND table1.UserID=table2.UserID") or die(mysql_error());
$fields_num = mysql_num_fields($result);
echo "<table class='table table-hover'>";
echo "<th>Name</th>";
echo "<th>Type</th>";
echo "<th>Number</th>";
echo "<th>Copy</th>";
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
echo "<tr><td>".$row['Name']."</td><td>".$row['Type']."</td><td>".$row['No']."</td><td>"."<a href=''>Download</td></tr>";
}
}
echo "</table>";
Usually I save my files in a folder and just adding the path into a column in my database and to download I use the following PHP code.
I call it downloadPop_Up.php:
<?php
$data = $_REQUEST['data'];
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
//header("Content-disposition: attachment; filename=\"" . basename($data) . "\"");
header("Content-disposition: attachment; filename=\"" . basename($data) . "\"");
readfile($data);
?>
Related
I want to make a html table using text file data. Since I am new to web designing I dont know which approach would be best. It would be great if anyone would give me pointers on what to do.
So first off you will have to start with loading the txt file. Since you wrote in the comments about php, i guess you are using PHP as backend language.
So create a file called table.php.
In this file you can load the text file from your server / whatever and also create the html code and echo it. This will return the table line by line.
Another good solution would be to create an object out of your text file and then use JSON as transfer state for this object to your JavaScript frontend -> this is my prefered solution.
It should look similar to this (file load taken from this):
<?php
$html = '<table>'; //html variable will contain our table code
$fh = fopen('filename.txt','r'); //filename / path here
while ($line = fgets($fh)) {
$html .= '<tr>';
$html .= '<td>' + $line + '</td>';
$html .= '</tr>';
}
fclose($fh);
$html .= '</table>';
echo $html;
?>
I had created a table in mysql database. I am using php One column was for images, but it only stored images name. I wanted to print the whole table so did the coding but got got only the name of the images in the table.
Please explain how can I display images on the webpage that are stored in my PC. I don't want to save images in the database.
I used this code to print the table :
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test", $con);
$query="SELECT * FROM images LIMIT 0,5";
$result = #mysql_query($query);
echo "<table border='1'>
<tr>
<th>S.No.</th>
<th>Image</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['image_id'] . "</td>";
echo "<td>" . $row['filename'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
Hope you got some answer....
All your code is doing is sending the filename to the screen as a string. You need to the use image tags.
You may find this page useful, http://www.htmlcodetutorial.com/images/_IMG.html
I wan to always display the different pic. So there's a problem with looping the different pic. How do i do that?
index.php:
<?php
$result = mysql_query("SELECT * FROM table2", $connection );
echo '<ul>';
while($row = mysql_fetch_array($result)) {
if ($row)
echo "<li>";
echo "<img src='code.php?id=3'>";
echo " ";
echo "</p>";
echo "</li>";
echo "<br/>" ;
}
echo '</ul>';
mysql_close($connection);
?>
You need to pull the id value from the database. If you have a column called, maybe id you would want to put:
while($row = mysql_fetch_array($result)){
if ($row){
echo "<li>";
echo "<img src='code.php?id=".$row['id']."'/>"; // see what I did there?
echo " ";
echo "</p>"; // take this out
echo "</li>";
echo "<br/>"; // take this out
}
}
P.S. - don't write new code using the deprecated mysql extension. Use PDO or mysqli at least. And don't put <br />s between your <li>s, or close unopened <p>s. And, generally speaking, don't store your images in your database - just store the path to the image and put the images themselves in a folder on the server where they belong.
And please format your code - it is hard to read with no indentation or separation of files (your if statement was not properly enclosing what should have been conditioned statements). And mysql_real_escape_string is not as cool as you think it is.
Hope this helps.
I am a beginner programmer and I have just started using MySQL - I am using a program called PHP MyAdmin to Create a table in a database. I want to enter in values and then have those values appear in a table. I have made a table called table 1 in a database called sweetshop. The two rows I put in the PHP MyAdmin program are called SweetID and SweetName.
I hope someone will be able to help. Sorry if I don't come across clear - I haven't been coding long!
The code I'm using for this particular section is:
<?php
$con = mysqli_connect("localhost","root","","sweetshop");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to Connect to MySQL: ".mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM table1");
echo "<table>
<tr>
<th>Sweet ID</th>
<th>Name</th>
</tr>";
while ($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo " <td>" . $row['SweetID'] . "</td>";
echo " <td>" . $row['SweetName'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
I assume you mean that the columns you have are named SweetID and SweetName
If so, change your select statement from
$result = mysqli_query($con,"SELECT * FROM table1");
to
$result = mysqli_query($con,"SELECT SweetID, SweetName FROM sweetshop.table1");
and you should be good (db name in your select isn't required but is a great habit to develop now).
Try running that exact select through phpMyadmin to see what your result set looks like as well, if you get results there then you should also get them from php
It would also be helpful for you to post the full structure of the table just in case
I want to convert my sql data to csv files while clicking on a button. The code fragments I found for sql to CSV conversion were in PHP, and I'm trying to convert it to CakePHP since I'm working in CakePHP.
Here is the PHP code I'm tring to convert:
$result = mysql_query("SHOW COLUMNS FROM ".$table."");
$i = 0;
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
$csv_output .= $row['Field']."; ";
$i++;
}
}
$csv_output .= "\n";
$values = mysql_query("SELECT * FROM ".$table."");
while ($rowr = mysql_fetch_row($values)) {
for ($j=0;$j<$i;$j++) {
$csv_output .= $rowr[$j]."; ";
}
$csv_output .= "\n";
}
$filename = $file."_".date("Y-m-d_H-i",time());
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");
print $csv_output;
SOLUTION
Function in the Controller:
function exporttocsv()
{
$this->set('headers',$this->Result->find('all',array('fields'=>'Result.label')));
$this->set('values',$this->Result->find('all',array('fields'=>'Result.value')));
}
exporttocsv.ctp file:
<?php
foreach($headers as $header):
$csv_output .=$header['Result']['label'].", ";
endforeach;
$csv_output .="\n";
if(!empty($values)){
foreach($values as $value):
$csv_output .=$value['Result']['value'].", ";
endforeach;
$csv_output .="\n";
}
else{
echo "There is no data to export.";
}
$filename = "export_".date("Y-m-d_H-i",time());
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header("Content-disposition: filename=".$filename.".csv");
print $csv_output;
exit;
?>
First of all, you don't do queries and output in the same file in Cake. You query the data as usual in the Controller, $this->set() the result to the view, and in the view you do something like this:
foreach ($results as $result) {
echo join(', ', $result['COLUMNS']);
echo "\n";
}
Outputs something like this:
value, varchar(25), NO, , ,
submitter, int(11), NO, , ,
...
Since Cake automatically wraps a layout around your view, you'll have to set the layout to something different, like 'ajax' (which is simply an empty layout).
deceze is correct about outputting the results from the view file. You'll just need to set some headers so that it appears as a file download on the client side. You can simply put these 2 calls in the top of your view:
header("Content-type:application/vnd.ms-excel");
header("Content-disposition:attachment;filename=\"{$filename}\"" );
If you plan on doing csv downloads in more than one place in your application, I'd recommend this helper:
http://bakery.cakephp.org/articles/view/csv-helper-php5
I use it and it works well.