我刚开始使用MySQL
和PHP
,我在数据库中创建了一个表“ Joke”,并使用以下代码在PHP服务器端显示了数据:
$result = mysqli_query($link, 'SELECT joketext FROM joke');
while ($row = mysqli_fetch_array($result))
{
$jokes[] = $row['joketext'];
}
include 'jokes.html.php';
和HTML代码:
<p>Here are all the jokes in the database:</p>
<?php foreach ($jokes as $joke): ?>
<blockquote><p>
<?php echo htmlspecialchars($joke, ENT_QUOTES, 'UTF-8'); ?>
</p></blockquote>
<?php endforeach; ?>
这对我有用,但是现在我想在每个笑话旁边添加一个按钮“下载”,以将其下载到.txt
文件中。
答案 0 :(得分:0)
您可以<local:EntryPage Title="Home" Icon="home.png"/>
<NavigationPage Title="Maps" Icon="placeholder.png">
<x:Arguments>
<local:MapPage />
</x:Arguments>
</NavigationPage>
<NavigationPage Title="Order" Icon="list.png">
<x:Arguments>
<local:ListPage />
</x:Arguments>
</NavigationPage>
<NavigationPage Title="Settings" Icon="settings.png">
<x:Arguments>
<local:SettingsPage />`enter code here`
</x:Arguments>
</NavigationPage>
implode()
数组:
$jokes
并在您的html页面中:
$all_jokes = implode("\n", $jokes);
file_put_contents("all_jokes.txt", $all_jokes);
此处引用:
https://www.w3schools.com/php/func_filesystem_file_put_contents.asp https://php.net/manual/en/function.implode.php
答案 1 :(得分:0)
如果您修改页面的HTML / PHP部分以包括如下所示的简单超链接:
<p>Here are all the jokes in the database:</p>
<?php foreach ($jokes as $id => $joke): ?>
<blockquote>
<p>
<?php
echo htmlspecialchars($joke, ENT_QUOTES, 'UTF-8');
?>
</p>
<a class='download' href='#' title='Download the joke'>Download</a>
</blockquote>
<?php endforeach; ?>
然后从下面将javascript复制到您的页面,您应该发现自己能够根据需要下载笑话。如果您复制下面的所有代码,则应该会在浏览器中正常运行。
<html>
<head>
<title>Download a joke</title>
<script>
/*
wait for the DOM to load and then set event listeners
on the new hyperlinks
*/
document.addEventListener('DOMContentLoaded', ()=>{
/*
Query the DOM for ALL hyperlinks of class `download`
and assign an event handler for click events
*/
document.querySelectorAll('a.download').forEach( function(a){
a.addEventListener( 'click', function( event ){
/*
prevent the default action for this hyperlink
*/
event.preventDefault();
/*
The Joke is located in the previous
DOM node - P
*/
let p=this.previousElementSibling;
/*
create a BLOB object to store the joke text
*/
let blob=new Blob([ p.innerText ],{type: 'text/plain'});
/*
function to send the file - jiggery pokery
*/
const sendfile=function( blob, name ){
let url=URL.createObjectURL( blob );
let lnk=document.createElement('a');
let evt=new MouseEvent('click',{
bubbles:true,
cancelable:true,
view:window
});
p.appendChild( lnk );
lnk.href=url;
lnk.download=name;
lnk.dispatchEvent( evt );
p.removeChild( lnk );
}
sendfile.call( this, blob, 'joke.txt' );
});
});
});
</script>
</head>
<body>
<!-- EXAMPLE DATA -->
<blockquote>
<p>
This is the joke - not very funny though is it?
</p>
<a class='download' href='#' title='Download the joke'>Download</a>
</blockquote>
<blockquote>
<p>
This is another joke - or what passes for a joke!
</p>
<a class='download' href='#' title='Download the joke'>Download</a>
</blockquote>
<blockquote>
<p>
This is probably not the best joke in the World.
</p>
<a class='download' href='#' title='Download the joke'>Download</a>
</blockquote>
<blockquote>
<p>
A woman gets on a bus with her baby. The bus driver says: 'Ugh, that's the ugliest
baby I've ever seen!' The woman walks to the rear of the bus and sits down, fuming.
She says to a man next to her: 'The driver just insulted me!' The man says: 'You go
up there and tell him off. Go on, I'll hold your monkey for you.
</p>
<a class='download' href='#' title='Download the joke'>Download</a>
</blockquote>
</body>
</html>