Hey, wondered if someone could help me with how to go about doing the following:

I have some code which generates bits of text randomly from a database. But I was wondering if there's a way for a user to download an individual file, which corresponds to the one they're viewing.

So say this is my code:

<?php

if($_GET['act'] == "generate_quotes") {

$con = mysql_connect("*", "*", "*");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("quote", $con);

$result = mysql_query( " SELECT * FROM `quote`  ORDER BY RAND() LIMIT 0,1 " );



while($row = mysql_fetch_array($result))
  {

  echo '<span class="pink">' . $row['q_quote'] . "</span>";

  }
echo "</table>";

mysql_close($con);

} else { 

echo "<img src=\"vpbgt.png\">";

}
?>

And an example of things in the bd are:

test 1   [id = 1]

test 2   [id = 2]

test 3   [id = 3]

If the user clicks the link to generate a piece of text, they can download what's being shown. So if they saw 'test 1' they could click a download link and download 'test1.txt'. But if they were to hit generate again it'd maybe show 'test3' so they'd click the link to download and it'd download 'test3.txt' etc... I'm sure you know the kind of thing I'm after.

I've assigned id's to each field in the bd, which I'm sure I'll need, but can someone elaborate on where to go from here? or some examples / tutorials would be great.

Thanks for any advice

how about writing all that stuff directly to a file?

echo '<span class="pink">' . $row['q_quote'] . "</span>";
fwrite($file,$row['q_quote']);

Now you just have to provide a link to your output-file so that the user can download the stuff.

how about writing all that stuff directly to a file?

echo '<span class="pink">' . $row['q_quote'] . "</span>";
fwrite($file,$row['q_quote']);

Now you just have to provide a link to your output-file so that the user can download the stuff.

Thanks for the reply, but I'm sorry the examples I were used just for testing purposes, I just used .txt because it was just what I thought of off the top of my head. Sorry, should have made that clear really. The actual file being downloaded will be a PDF file which is a more in depth version of the text. So it needs to be an actual file being downloaded. Not just written to a file and downloaded.

Any thoughts on this?

Sorry again for the confusion.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.