使用PHP变量将HTML写入外部文件

时间:2017-02-22 13:06:04

标签: php

如何使用fwrite将变量作为字符串写入外部文件 - 在变量周围使用单引号'(var)':

 $sql_data_element =  "UPDATE artister SET data_two="$data_two" WHERE brugernavn= ' . $brugernavn . '";

编写data_two =“$ data_two”会出错 - 需要正确的转义才能工作......

将完整代码写入外部文件:

$file = fopen($dir . '/' . $file_to_write,"w");

$template_content = '  
<article class="text" style="display:block">
<div class="wrap_om_artisten_top clearfix"> <!-- CLEAR FLOATS I wrap_profile_image OG data_elements_upload_section -->

    <!-- PROFIL FOTO -->    
    <div class="wrap_profile_image"> 
        <figure class="profile_image">
            <!-- PROFIL FOTO HER -->
        </figure>

        <div class="wrap_form_image">
            <form enctype="multipart/form-data" method="post" class="form_img_profile"> 
                <input type="file" name="img_profile_upload" id="img_profile_upload" style="display:none;" accept="image/*"  />  
                <label for="img_profile_upload" class="choose_profile_img button">Vælg billede</label> 
                                                                            <!-- LABEL: OMGÅ BROWSER DEFAULT-TEXT: "Ingen fil valgt." -->       
                <input id="submit" type="submit" name="submit_image_profile" value="Tilføj foto" class="submit_img_upload button">                  
            </form>
        </div>  
    </div>

    <!-- INOF -->
    <div class="data_elements_upload_section"> 

        <!-- INDSÆT DATA ELEMENT I MYSQL -->
        <?php
            $conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
            if ($conn->connect_error) {
                die("Connection failed: " . $conn->connect_error);
            } else {
                //echo("ja conn ok <br />");
            }

            // HVIS DER ER INDTASTET NOGET ("isset") I FORM INPUT...    
            $data_one = isset($_POST["data_one"]) ? $_POST["data_one"] : "";    
            $data_two = isset($_POST["data_two"]) ? $_POST["data_two"] : "";    
            $data_three = isset($_POST["data_three"]) ? $_POST["data_three"] : "";  


            // ERRORS
            function show_error_data($error_text) {
                echo    \'<div class="error_overlay"> \n\';
                echo        \'<div class="error_text_wrap"> \n\';
                echo            \'<p class="error_text">$error_text</p>\'; // HUSK FORSKEL PÅ ENKELT - OG DOBBELT
                echo            \'<button class="error_close">Prøv igen</button> \n\';
                echo        \'</div> \n\';
                echo    \'</div>\';
            }

            // HVIS FORM SUBMIT ER TRYKKKET...
            if (isset($_POST["submit_data_one"])) {     
                // BESTEM HVOR OG HVAD DER SKLA INDSÆTTES
                $sql_data_element =  "UPDATE artister SET data_one='\$data_one' WHERE brugernavn= \'' . $brugernavn . '\'";
                //INDSÆT INDTASTEDE DATA I MYSQL
                $conn -> query($sql_data_element);
            }
            else if (isset($_POST["submit_data_two"])) {        
                if (empty($_POST["data_one"])) { // HVIS data_one ER TOM..
                    show_error_data("Udyld Info 1 først!");
                } else {    
                    $sql_data_element =  "UPDATE artister SET data_two="$data_two" WHERE brugernavn= ' . $brugernavn . '";
                    $conn -> query($sql_data_element);
                }
            } // END OF submit_data_two
            else if (isset($_POST["submit_data_three"])) {  
                if ( (empty($_POST["data_one"])) || (empty($_POST["data_two"])) ) {
                    show_error_data("Udyld Info 1 og 2 først!");
                } else {    
                    $sql_data_element =  "UPDATE artister SET data_three="$data_three" WHERE brugernavn= ' . $brugernavn . '";
                    $conn -> query($sql_data_element);
                }
            } // END OF submit_data_three           
        ?>

        <aside class="wrap_data_elements"> 
    <!--        <button type="button" class="wrap_close_text">
                <img src="/images/slideren/icons/icon_close_fullscreen.png" class="close_text" 
                    alt="Skjul informationer om kunsteneren og vend tilbage til visning af kunsteksemksempler" />
            </button>-->



            <div class="data_element">
                <h1>' . $brugernavn . '</h1>
            </div>          

            <div class="data_element">
                <?php 
                    $query = "SELECT data_one FROM artister WHERE brugernavn = ' . $brugernavn . '"; 
                    $result = mysqli_query($conn,$query) or die (mysqli_error($conn) );

                    while($row = mysqli_fetch_array($result)) {
                        $data_one = $row["data_one"];
                        echo $data_one;
                    }
                ?>
            </div>
            <div class="data_element">
                <?php 
                    $query = "SELECT data_two FROM artister WHERE brugernavn = ' . $brugernavn . '";
                    $result = mysqli_query($conn,$query) or die (mysqli_error($conn) );

                    while($row = mysqli_fetch_array($result)) {
                        $data_two = $row["data_two"];
                        echo $data_two;
                    }
                ?>
            </div>  
            <div class="data_element">
                <?php 
                    $query = "SELECT data_three FROM artister WHERE brugernavn = ' . $brugernavn . '"; 
                    $result = mysqli_query($conn,$query) or die (mysqli_error($conn) );

                    while($row = mysqli_fetch_array($result)) {
                        $data_three = $row["data_three"];
                        echo $data_three;
                    }
                ?>
            </div>  

            <!--<ul class="wrap_se_mere">     
                <li class="overskrift_se_mere">DENNE KUNSTNER HAR MERE</li> 
                    <ul class="second_se_mere">   
                        <li class="se_mere">MUSIK</li>
                        <li class="se_mere">kunst</li>
                        <li class="se_mere">FILM</li>
                    </ul>
                </li>
            </ul>   -->      
        </aside>

        <div class="wrap_form_data">
            <button class="show_form_data button">Opret info</button>

            <!-- OPRET DATA ELEMENT 1 -->
            <form action="<?=$_SERVER["PHP_SELF"]?>" method="post" class="form_data_element">
                <input type="text" name="data_one" placeholder="Data 1" class="data_input"/>
                <input type="submit" name="submit_data_one" class="submit_data button" value="Opret data 1" />      
            </form> 
            <!-- OPRET DATA ELEMENT 2 -->
            <form action="<?=$_SERVER["PHP_SELF"]?>" method="post" class="form_data_element">
                <input type="text" name="data_two" placeholder="Data 2" class="data_input"/>
                <input type="submit" name="submit_data_two" class="submit_data button" value="Opret data 2" />      
            </form> 
            <!-- OPRET DATA ELEMENT 3 -->
            <form action="<?=$_SERVER["PHP_SELF"]?>" method="post" class="form_data_element">
                <input type="text" name="data_three" placeholder="Data 3" class="data_input"/>
                <input type="submit" name="submit_data_three" class="submit_data button" value="Opret data 3" />        
            </form> 
            <button class="cancel_data button"></button>
        </div>
    </div>  <!--    end of data_elements_upload_section -->

</div> <!-- END OF wrap_om_artisten_top -->



    <!-- VIS BESKRIVELSE -->
    <div class="wrap_om_artisten_bottom_broedtekst">    
        <?php
            $conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
            $query = "SELECT beskrivelse FROM artister WHERE brugernavn = \'' . $brugernavn . '\' ";
            $result = mysqli_query($conn,$query) or die (mysqli_error() );

            while($row = mysqli_fetch_array($result)) {
                $beskrivelse = $row["beskrivelse"];
                echo $beskrivelse;
            }
        ?>
    </div> <!-- END OF wrap_om_artisten_bottom_broedtekst -->   


    <!-- OPRET BESKRIVELSE -->
    <form action="<?=$_SERVER["PHP_SELF"]?>" method="post" class="form_beskrivelse">
        <label>Beskrivelse:</label>
        <textarea id="editor1"  name="beskrivelse" placeholder="Beskrivelse her"        
            value="<?php echo isset($_POST["beskrivelse"]) ? $_POST["beskrivelse"] : ""; // UPLOAD TEXT TIL MYSQL ?>">
            <?php echo $beskrivelse; // VIS OG EDIT EKSISTERENDE MYSQL-TEXT ?>
        </textarea>
        <script>
            // VIS ckeDITOREN
            CKEDITOR.replace( "editor1" );
        </script>
        <br />

        <input type="submit" name="submit_beskrivelse" value="Opret beskrivelse" />     
    </form>


    <!-- ART UPLOADS -->
    <div class="wrap_uploads" >
    <?php
        include ("links_art_uploads.php");
    ?>
    </div>

    <div class="upload_section">    
        <form id="Upload" action="" enctype="multipart/form-data" method="post">
            <p>
                <label>Upload et billede:</label>
                <!-- TYPE file FINDER IMG LOCATION - id + CCS-STYLE + button MED JS ER HER FOR AT OMG? BROWSER-DEFAULT TEXTEN: "Ingen fil valgt." -->
                <input type="file" name="img_art_upload" id="img_art_upload" style="display:none;" 
                accept="image/*"  onchange="showMyImage(this)"/>  <!-- TIL PREVIEW - SE script function showMyImage OG body <input type="file"... -->
                <button type="button" class="input_style button" onClick="document.getElementById("img_art_upload").click();">Vælg billede</button>
            </p>

            <p>
                <label>Skift navn på billede</label>
                <input type="text" name="img_nyt_navn" maxlength="26" /> 
            </p>

            <p>
                <label>Press to...</label>
                <input id="submit" type="submit" name="submit_art_upload" class="button" value="Upload">

            </p>    
        </form>
    </div>  



<!--    <div class="preview_wrap"style="position: absolute; bottom: 0; border: 2px solid red;">
        <img id="preview" class="preview_size"/> <!-- TIL PREVIEW - SE script og <input type="file"...  -->
    </div>-->

<!-- vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv -->

<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script>
$(document).ready(function(){   

    // PROFIL FOTO - HENT OG VIS FRA ROOT 
    $("figure.profile_image").load("links_to_uploads_template.php");

    // PROFIL FOTO - TOGGLE BUTTONS
    $("label.choose_profile_img").click(function() {
        $(this).hide(); 
        $("input.submit_img_upload").fadeIn(400);
        $("body").css({"background" : "black" });
    }); 

    // TIL UPLOAD AF SATA
    $("form.form_data_element").hide();

    $("button.show_form_data").click(function() {
        $(this).hide();
        $("form.form_data_element:nth-of-type(1)").fadeIn(400); 
        $("form.form_data_element:nth-of-type(2)").fadeIn(400); 
        $("form.form_data_element:nth-of-type(3)").fadeIn(400); 
    });

    $("form.form_data_element").click(function() {
        $("form.form_data_element").not(this).hide();
        $(this).css({"float" : "left"}); // KUN NØDVENDIG PGA FOMM 3 SOM FLOATER RIGHT - HVILKET PALCERER button.cancel_data YDERST TIL VENSTRE 
        $(this).css({"width" : "calc(100% - 30px)"}); // BREDDE FORM MINUS BREDDEN AF button.cancel_data 30PX
        $(this).find("input.data_input").css({"float" : "left", "width" : "calc(100% - 120px)"}); // BREDDE MINUS input.submit_data
        $(this).find("input.submit_data").fadeIn(400);
        $("button.cancel_data").fadeIn(400);
    });

    $("button.cancel_data").click(function() {
        $("button.show_form_data").fadeIn(400);
        $(this).hide();
        $("form.form_data_element").removeAttr("style").hide();
        $("input.data_input").removeAttr("style");
        $("input.submit_data").removeAttr("style");
    }); 

var half_height = $(window).height() / 2;


$("button.next_image").click(function() {
    var viewport_height = $(window).height(); // OGSÅ DEFINERET I custom_fullscreen.js
    var next_image = $(this).next(".window_element").outerHeight(true); // HØJDE + MARGIN + PADDING 
    $("html, body").animate({
//      scrollTop: $(".top_img").offset().top - ( $(window).height() - $(".top_img").outerHeight(true) ) / 2
        scrollTop: $(".top_img").offset().top - ( viewport_height - next_image ) / 2
    }, 1000, "swing");
});

//var position = $(".window_element").offset(); /*it gets the position of the div
//                                     "fixed" relative to the document*/
//$(".window_element").html(position.top); 


}); // END OF DOC READY
</script>   

</article> <!-- END OF article class="text" -->
'; // END OG HTML 

fwrite($file, $template_content);

fclose($file);

编写外部文件后,HTML必须如下:

$sql_data_element =  "UPDATE artister SET data_one='$data_one' WHERE username='new user' ";

这是关于什么的:在向MySql提交用户名,密码后,“index.php”会从$template_content写入一个新文件并重定向到此文件。基于新文件的书面内容,用户有更多选择。

2 个答案:

答案 0 :(得分:1)

注意您的引用字符并使用heredoc以提高可读性:

<?php

$username = 'James Bond';
$template_content = <<<CODE
\$sql_data_element = "UPDATE artister SET data_one='\$data_one' WHERE username='$username'";
CODE;

var_dump($template_content);

上面的输出显然是:

string(91) "$sql_data_element = "UPDATE artister SET data_one='$data_one' WHERE username='James Bond'";"

答案 1 :(得分:0)

$template_content = '$sql_data_element =  "UPDATE artister SET data_one="\'$data_one\'" WHERE username= \'' . $username . '\'";';

将输出

$sql_data_element =  "UPDATE artister SET data_one='$data_one' WHERE username='new user' ";

按照您的要求

您确定不想输出

$sql_data_element = "UPDATE artister SET data_one='some value' WHERE username='new user' ";