2014-01-08 70 views
0

我想通過使用mysql來檢索它,將這一行硬編碼圖像轉換爲動態代碼。使用php mysql動態顯示圖像

<div class="item-block-1"> 
          <span class="tag-sale"></span> 
          <div class="image-wrapper"> 
           <div class="image"> 
            <div class="overlay"> 
             <div class="position"> 
              <div> 
               <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam quis metus non erat tincidunt consectetur. Maecenas ac turpis id lorem.</p> 
               <a href="#" class="quickshop">Quick shop</a> 
              </div> 
             </div> 
            </div> 
            <a href="#"><img src="images/photos/photo-3.jpg" style="margin: -32.5px 0 0 0;" alt="" /></a> 
           </div> 
          </div> 
          <h2><a href="pandora-item.html">Polka dot light blue blouse</a></h2> 
          <p class="price">$13.99<s>$36.99</s></p> 
         </div> 

我的動態代碼如下,但只允許我檢索,而不是通過代碼循環只有一個形象..

<div class="item-block-1"> 
          <div class="image-wrapper"> 
           <div class="image"> 
            <div class="overlay"> 
             <div class="position"> 
              <div> 
               <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam quis metus non erat tincidunt consectetur. Maecenas ac turpis id lorem.</p> 
               <a href="#" class="quickshop">Quick shop</a> 
              </div> 
             </div> 
            </div> 
             <?php 

$result = mysql_query("SELECT * FROM my_image", $connection); 


while($row = mysql_fetch_array($result)) 
{  
    echo "<div><img src=\"uploadedimages/".$row['name']."\" /></div>"; 
} 
?> 
</div> 
</div> 

任何人都可以糾正我?

+0

首先,你應該認真考慮切換到mysqli_ *或PDO,mysql_ *已被棄用從PHP 5.5.0 – Kostis

+0

感謝您的意見,我仍然是一個學生嘗試一些教程..你能幫我以上? – user3145668

+0

按下'ctrl + u'並在瀏覽器中檢查呈現的源代碼以檢查img'src',或者您可以在瀏覽器中安裝並使用'firebug'工具或開發人員工具 –

回答

0

既然你彌補的建議

<?php 
/* 
* I usually put this in a class, but doing this all in-line for you. 
* I did this with PDO, which is just like mysql_, but better. 
*/ 
$db_name = ''; // put your database name here 
$db_host = ''; // your host name, usually localhost 
$db_username = ''; // the database username used to access this database 
$db_password = ''; // password associated with the username 
$dsn = 'mysql:dbname='.$db_name.';host='.$db_host; // this is for the PDO object  
$PDO = new PDO($dsn, $db_username, $db_password); 

$query = 'SELECT * FROM my_image'; 

$Stmt = $PDO->prepare($query); // prepares your SQL 
$Stmt->execute() // runs your SQL 

// This will test to see if you get any images back from your database 
if ($Stmt->rowCount() > 0) { 
    /* 
    * This means you have atleast 1 image back from your database 
    * The wile loop will iterate through your images from the database 
    * and generate the HTML below for each, only changing the name of the file 
    * on each iteration 
    */ 
    while ($row = $Stmt->fetch(PDO::FETCH_ASSOC)) { 
     echo '<div class="item-block-1">'; 
     echo  '<div class="image-wrapper">'; 
     echo   '<div class="image">'; 
     echo    '<div class="overlay">'; 
     echo     '<div class="position">'; 
     echo      '<div>'; 
     echo       '<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam quis metus non erat tincidunt consectetur. Maecenas ac turpis id lorem.</p>'; 
     echo       '<a href="#" class="quickshop">Quick shop</a>'; 
     echo      '</div>'; 
     echo     '</div>'; // class="position" div 
     echo    '</div>'; // class="overlay" div 
     echo    '<div><img src="uploadedimages/'.$row['name'].'" /></div>'; // This is the actual image 
     echo   '</div>'; // class="image" div  
     echo  '</div>'; // class="image-wrapper" div 
     echo '</div>'; // class="image-block-1" div  

    } 
} 

當然,這並不是如何正確地做到這一點試試這個代碼

<?php 
$result = mysql_query("SELECT * FROM my_image"); 
?> 
<div class="item-block-1"> 
          <div class="image-wrapper"> 
           <div class="image"> 
            <div class="overlay"> 
             <div class="position"> 
              <div> 
               <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam quis metus non erat tincidunt consectetur. Maecenas ac turpis id lorem.</p> 
               <a href="#" class="quickshop">Quick shop</a> 
              </div> 
             </div> 
            </div> 
            <?php         
            while($row = mysql_fetch_array($result)) 
             { 
            ?> 
            <div> 
             <img src="new/<?php echo $row['name']; ?>"/> 
            </div> 
            <?php 
             }  
            ?> 
            </div> 
           </div> 
          </div> 
+0

嗨,我有一個解析錯誤:語法錯誤,意外'>' – user3145668

+0

立即嘗試。他的格式都搞砸了。 '''不是代碼的一部分 – Machavity

+0

嗨,它的工作現在,但它仍然只顯示1圖像。 – user3145668

0

,但如果你是做在線狀這,就是這樣。 while循環將爲每個圖像生成SAME html,除了圖像文件路徑,其中的圖像文件路徑會根據while循環中的當前行進行更改。

+0

嗯我很高興誤解了這個問題。我會稍微編輯我的答案,以顯示爲1項目添加多個圖像。但bascially你回聲相同的HTML和循環只通過圖像,而不是整個HTML。 – kchang4