社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  DATABASE

如何在单击下载按钮时将mysql数据库中的值保存到txt文件中?

Naima israa • 5 年前 • 1262 次点击  

我刚开始 MySQL PHP ,我在数据库中创建了一个“笑话”表,并在PHP服务器端显示了数据,代码如下:

$result = mysqli_query($link, 'SELECT joketext FROM joke');  


while ($row = mysqli_fetch_array($result))  
{  
  $jokes[] = $row['joketext'];  
}  

include 'jokes.html.php'; 

<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 文件。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/53848
 
1262 次点击  
文章 [ 2 ]  |  最新文章 5 年前
andreah
Reply   •   1 楼
andreah    6 年前

你可以 implode() 这个 $jokes 数组:

$all_jokes = implode("\n", $jokes);
file_put_contents("all_jokes.txt", $all_jokes);

<a href="all_jokes.txt">Download all</a>

参考文献:

https://www.w3schools.com/php/func_filesystem_file_put_contents.asp https://php.net/manual/en/function.implode.php

RamRaider
Reply   •   2 楼
RamRaider    6 年前

如果修改页面的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; ?> 

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