2016-11-25 103 views
0

我有一些代碼,彈出該工程確定,並從數據庫中搜索打開匹配圖像彈出獲取圖像陣列顯示同一圖像

的JavaScript新窗口

<script> 
function CenterWindow(windowWidth, windowHeight, windowOuterHeight, url, wname, features) { 
    var centerLeft = parseInt((window.screen.availWidth - windowWidth)/2); 
    var centerTop = parseInt(((window.screen.availHeight - windowHeight)/2) - windowOuterHeight); 
    var misc_features; 

    if (features) { 
     misc_features = ', ' + features; 
    } 
    else { 
     misc_features = ', status=no, location=no, scrollbars=no, resizable=no'; 
    } 

    var windowFeatures = 'width=' + windowWidth + ',height=' + windowHeight + ',left=' + centerLeft + ',top=' + centerTop + misc_features; 
    var win = window.open(url, wname, windowFeatures); 
    win.focus(); 
    return win; 
} 
</script> 

HTML

<table align="center" border="0" width="1200px"> 
    <tr onMouseOver="this.className='highlight'" onMouseOut="this.className='normal'"> 
     <td class="tabletext" width="100" align="left"><?php echo $results['siteid']; ?></td> 
     <td class="tabletext" width="800" align="left"><?php echo $results['description']; ?></td> 
     <td class="tabletext" width="300" align="left"><a href="javascript:void(0)" onclick="CenterWindow(800,500,50,'../../admin/Test Photo Upload/<?php echo $results['location']; ?>','demo_win');">Open Image</a></td> 
    </tr> 
</table> 

我想改變彈出窗口,只有圖像沒有瀏覽器的類型彈出式窗口,它使用了這段代碼,但給了我特定搜索中所有鏈接的相同圖像,所以我的代碼似乎給了我表中的第一個結果?

CSS

<style type="text/css"> 
#fade{ 
    display: none; 
    position: fixed; 
    top: 0%; 
    left: 0%; 
    width: 100%; 
    height: 100%; 
    background-color: white; 
    z-index:1001; 
    -moz-opacity: 0.8; 
    opacity:.80; 
    filter: alpha(opacity=75); 
} 

#light{ 
    display: none; 
    position: absolute; 
    top: 25%; 
    left: 40%; 
    width: 300px; 
    height: 200px; 
    margin-left: -150px; 
    margin-top: -100px;     
    background: #000; 
    z-index:1002; 
    overflow:visible; 
} 
</style> 

的JavaScript

<script type="text/javascript"> 
window.document.onkeydown = function (e){ 
    if (!e){ 
     e = event; 
    } 
    if (e.keyCode == 27){ 
     lightbox_close(); 
    } 
} 

function lightbox_open(){ 
    window.scrollTo(0,0); 
    document.getElementById('light').style.display='block'; 
    document.getElementById('fade').style.display='block'; 
} 

function lightbox_close(){ 
    document.getElementById('light').style.display='none'; 
    document.getElementById('fade').style.display='none'; 
} 
</script> 

HTML

<table align="center" border="0" width="1200px"> 
    <tr onMouseOver="this.className='highlight'" onMouseOut="this.className='normal'"> 
     <td class="tabletext" width="100" align="left"><?php echo $results['siteid']; ?></td> 
     <td class="tabletext" width="800" align="left"><?php echo $results['description']; ?></td> 
     <td class="tabletext" width="300" align="left"><a href="#" onclick="lightbox_open();">Open Image</a></td> 
    </tr> 
</table> 

<div id="light"> 
    <a href="#" onclick="lightbox_close();"><img src="../../admin/Test Photo Upload/<?php echo $results['location'];?>"/></a> 
</div> 
<div id="fade" onClick="lightbox_close();"></div> 

謝謝

<?php 
    $query = $_POST['txtidforgallery']; 
    // gets value sent over search form 

    $min_length = 3; 
    // you can set minimum length of the query if you want 

    if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then 

     $query = htmlspecialchars($query); 
     // changes characters used in html to their equivalents, for example: < to &gt; 

     $query = mysql_real_escape_string($query); 
     // makes sure nobody uses SQL injection 

     $raw_results = mysql_query("SELECT * FROM photos 
      WHERE (`siteid` LIKE '%".$query."%') OR (`siteid` LIKE '%".$query."%')") or die(mysql_error()); 

     // * means that it selects all fields, you can also write: `id`, `title`, `text` 
     // articles is the name of our table 

     // '%$query%' is what we're looking for, % means anything, for example if $query is Hello 
     // it will match "hello", "Hello man", "gogohello", if you want exact match use `title`='$query' 
     // or if you want to match just full word so "gogohello" is out use '% $query %' ...OR ... '$query %' ... OR ... '% $query' 

     if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following 

     ?> 

     <table align="center" border="0" width="1200px"> 
     <tr> 
      <th class="tableheader" width="100" align="left">Site ID</th> 
      <th class="tableheader" width="800" align="left">Photo Description</th> 
      <th class="tableheader" width="300" align="left"></th>    

     </tr> 
     </table> 

      <?php 
      while($results = mysql_fetch_array($raw_results)){ 
      // $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop 
+0

那麼,真正的問題是什麼?因爲我看不到您的代碼有任何問題。無論你在'$ results ['location']'中有什麼,都會顯示出來。我在代碼中看不到多個圖像或任何循環顯示多個圖像 – EhsanT

+0

$ results ['location']是圖像數據庫,但只顯示第一個圖像,位置有第1行= IMG_1.jpg ,第2行有IMG 2。JPG等,但每個鏈接都顯示IMG 1'函數CenterWindow'的代碼工作正常,但不是'lightbox_open' – Andy

+0

當然,因爲你必須把它放在一個循環! – EhsanT

回答

0

OK,從信息提供你我能看到你有一個數量的記錄。

顯示所有記錄的簡單循環就是這樣。所以我把你的循環放在<table>裏,並且連續顯示每一條記錄。每個魚子都有它自己的打開圖像鏈接,如果你點擊他們每個人,該記錄的圖像會顯示在...

你的CSS看起來不錯,所以我沒有改變它...

的JavaScript

<script type="text/javascript"> 
window.document.onkeydown = function (e){ 
    if (!e){ 
     e = event; 
    } 
    if (e.keyCode == 27){ 
     lightbox_close(); 
    } 
} 

function lightbox_open(url){ 
    window.scrollTo(0,0); 
    document.getElementById('myImg').src = url; 
    document.getElementById('light').style.display='block'; 
    document.getElementById('fade').style.display='block'; 
} 

function lightbox_close(){ 
    document.getElementById('light').style.display='none'; 
    document.getElementById('fade').style.display='none'; 
} 
</script> 

PHP & HTML

<table align="center" border="0" width="1200px"> 
    <?php 
    while($results = mysql_fetch_array($raw_results)){ 
    ?> 
    <tr onMouseOver="this.className='highlight'" onMouseOut="this.className='normal'"> 
     <td class="tabletext" width="100" align="left"><?php echo $results['siteid']; ?></td> 
     <td class="tabletext" width="800" align="left"><?php echo $results['description']; ?></td> 
     <td class="tabletext" width="300" align="left"><a href="#" onclick="lightbox_open('../../admin/Test Photo Upload/<?php echo $results['location']; ?>');">Open Image</a></td> 
    </tr> 
    <?php } ?> 
</table> 

<div id="light"> 
    <a href="#" onclick="lightbox_close();"><img id="myImg" src="data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs=" /></a> 
</div> 
<div id="fade" onClick="lightbox_close();"></div> 

請注意,src="data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs="只是一個虛擬的1x1像素圖像,因爲<img>標記需要src屬性,並且它不能爲空。

然後,你可以改變它,以滿足您的需求...

+0

此代碼不會執行任何操作,非常感謝您 – Andy

+0

不客氣。如果這個答案可以幫助你,那麼請選擇它作爲接受的答案,並給它一個upvote :) – EhsanT

-2

HTML

<a class="fancybox" rel="group" href="/img<?php echo $user_id ?>/<?php echo $value ?>"> 
    <div class="col-md-4 col-xs-4 crest-pic border-slide no-padd row_gal"> 
     <div style="background: url('/img<?php echo $user_id ?>/<?php echo $value ?>'); "> 
     </div> 
    </div> 
</a> 
+0

這段代碼究竟幹什麼? – EhsanT

+0

如果您不使用_fancyBox Plugin_ – EhsanT