如何使用PHP从服务器调出多个图像到自动创建的新网页

时间:2017-05-20 03:01:07

标签: php html

我有一个关于从HTML模板自动创建网页的问题,

  1. 如何从HTML模板自动创建新网页,并在自动创建的网页中从服务器调出多个图片?
  2. 请参阅下面的编码,谢谢。

    下面是自动创建网页的PHP代码,

    
    
    <?php 
    
    $sql = "SELECT * FROM ABC WHERE loginUser='$loginUser'";
    $result = $conn->query($sql);
    $data['multiple'] = array();
        while($row = $result->fetch_assoc()) {
            $data['multiple'][] = $row['multiPic'];
        }
    
    $tpl_file = "Service.html";
    $tpl_path = "../folder/";
    $members_path = "../folder/multiPicFolder/";
    
    $data['username'] = $username; 
    
    $placeholders = array("{multiple}", "{username}");
    
    $tpl = file_get_contents($tpl_path.$tpl_file);
    
    $new_member_file = str_replace($placeholders, $data, $tpl);
    
    $html_file_name = $data['username'].".html";
    
    $fp = fopen($members_path.$html_file_name, "w+"); 
    fwrite($fp, $new_member_file); 
    fclose($fp);
    
    $_SESSION['html_file_name'] = $html_file_name;
    
    ?>
    &#13;
    &#13;
    &#13;

    下面是自动创建的HTML模板,

    &#13;
    &#13;
        <?php
            session_start();
            include 'databasehander.php';
        ?>
    
          <!DOCTYPE HTML>
          <html>
          <body>
    
            <?php 
    
                foreach({multiple} as $eachPic) {
                  if (!empty($eachPic)) {
                      echo "<img src="../$eachPic">";
                  } 
                }
    
            ?>
    
            <?php include '../footer.php';?>
    
          </body>
    &#13;
    &#13;
    &#13;

1 个答案:

答案 0 :(得分:0)

这是我找到的解决方案,

<?php 

$tpl_file = "Service.html";
$tpl_path = "../folder/";
$members_path = "../folder/multiPicFolder/";

$data['multiple']="";  
    foreach($_SESSION['multiple'] as $ServiceCat1)  
    {  
        $data['multiple'] .= $ServiceCat1.",";  
    }  

$data['username'] = $username; 

$placeholders = array("{multiple}", "{username}");

$tpl = file_get_contents($tpl_path.$tpl_file);

$new_member_file = str_replace($placeholders, $data, $tpl);

$html_file_name = $data['username'].".html";

$fp = fopen($members_path.$html_file_name, "w+"); 
fwrite($fp, $new_member_file); 
fclose($fp);

$_SESSION['html_file_name'] = $html_file_name;

?>
<?php
        session_start();
        include 'databasehander.php';
    ?>

      <!DOCTYPE HTML>
      <html>
      <body>

        <?php 

            $getMultiplePart1 = '{multiple}';
            $findMultiplePart1 = explode(",", $getMultiplePart1);

            $multiplePart1="";  
            foreach($findMultiplePart1 as $multipleA) { 
                    $multiplePart1 .= "../".$multipleA.",";  
            }  
            
            $getMultiplePart2 = $multiplePart1;
            $findMultiplePart2 = explode(",", $getMultiplePart2);
            
            foreach($findMultiplePart2 as $multipleB) {  
                    echo "<img src='".$multipleB."'>";
            }

        ?>

        <?php include '../footer.php';?>

      </body>