簡體   English   中英

無法正確獲取代碼在PHP

[英]Cannot get the codes right in php

我已經使用php中的循環設計了一個棋盤。 棋盤很好,但是由於循環的緣故,我無法將棋子放置在正確的位置,在棋盤的每個正方形中都重復着相同的圖片。 誰能建議我該改變什么。 請參考下面的代碼。

<html>
<head>
<style>
th{
   width:80px;
   height:80px;
}
table{
   border: 5px solid orange;
   border-collapse:collapse;
}
td{
   width:80px;
   height:80px;
}
tr{
   width:80px;
   height:80px; 
}
h1{
   color:#6633FF;
}
</style>

<script type="text/javascript">

</script>
</head>
<body>
<?php
   echo"<h1 align='center'>SAJID Chess Board</h1>";

   echo"<table border='1' align='center'>";

   //Nested For loop starts
   for($i=1; $i<5; $i++)//this is the iteration construct starts from here
{
   echo"<tr>";
   for ($j=7; $j>=0; $j--) // For loop for 1st row

   {
   if(($j%2)==0){
   echo"<td bgcolor='grey'>";
   echo"<img src='king.gif'/>"; //here is the problem, the image of king throughout the chess board                 
   }
  else{
  echo"<td bgcolor='white'>";
  }

  echo"</td>";
  }//for loop ends
  echo "</tr>";

  echo "<tr>";

  for($j=0; $j<8; $j++){ // For loop for 2nd row

  if(($j%2)==0){
  echo"<td bgcolor='grey'>";
  }
  else{
  echo"<td bgcolor='white'>";
  }

  echo"</td>";
  }//for loop ends

  echo "</tr>";

  }//nested for loop ends

echo "</table>";

?>
</body>
</html>

您需要重新設計循環,以獲得正確的坐標,

這是我為你寫的一個例子

 <html>
    <head>
        <style>
            th{
                width:80px;
                height:80px;
            }
            table{
                border: 5px solid orange;
                border-collapse:collapse;
            }
            td{
                width:80px;
                height:80px;
            }
            tr{
                width:80px;
                height:80px; 
            }
            h1{
                color:#6633FF;
            }
        </style>

        <script type="text/javascript">

        </script>
    </head>
    <body>
        <?php

          // Format $pictures[Row][Column];
          $pictures = array();



          //Row 1
          $pictures[1][1] = "Castle";
          $pictures[1][2] = "Horse";
          $pictures[1][3] = "Bishop";
          $pictures[1][4] = "King";

          //Row 2
           $pictures[2][1] = "Pawn";
           $pictures[2][2] = "Pawn";
           $pictures[2][3] = "Pawn";
           $pictures[2][4] = "Pawn";
           $pictures[2][5] = "Pawn";
           $pictures[2][6] = "Pawn";
           $pictures[2][7] = "Pawn";
           $pictures[2][8] = "Pawn";




            echo"<h1 align='center'>SAJID Chess Board</h1>";
            echo"<table border='1' align='center'>";


            for($i = 1; $i <= 8; $i++)
            {     
                echo "<tr>";
                for($j = 1; $j <=8; $j++)
                {
                    if( ($i+$j)%2 )
                    {
                        echo"<td bgcolor='grey'>";


                    }
                    else
                    {
                        echo"<td bgcolor='white'>";
                    }


                    if(isset($pictures[$i][$j]))
                        echo $pictures[$i][$j];

                    echo "</td>";
                }
                echo "</tr>";
            }


            echo "</table>";

        ?>
    </body>
</html>

無法從<div>使用 PHP 簡單 DOM 標記</div><div id="text_translate"><p>我正在嘗試使用圖像的 div 元素獲取圖像源信息。 但是,當我調試結果時,圖像的來源不同,這是我的 DOM 樹:</p><p> <a href="https://i.stack.imgur.com/0bwQD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0bwQD.png" alt="在此處輸入圖像描述"></a></p><p> 這是 url 供參考: <a href="https://www.ebay.co.uk/itm/Front-strut-spacers-30mm-for-Ford-FOCUS-2-3-C-MAX-KUGA-GRAND-C-MAX-Lift-Kit/192539735653?hash=item2cd4433665:g:4LMAAOSwHPNa-TOF" rel="nofollow noreferrer">https://www.ebay.co.uk/itm/Front-strut-spacers-30mm-for-Ford-FOCUS-2-3-C-MAX-KUGA-GRAND-C- MAX-Lift-Kit/192539735653?hash=item2cd4433665:g:4LMAAOSwHPNa-TOF</a></p><p> 這是我的 php 代碼:</p><pre> $img = $html-&gt;find("#viEnlargeImgLayer_img_ctr", 0); print($img); if($img;= null){ $item['img'] = $img-&gt;getAttribute('src'); }else{ $item['img'] = ''; }</pre><p> 這是我 output img標簽時的結果: <a href="https://i.stack.imgur.com/q1Ne0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/q1Ne0.png" alt="在此處輸入圖像描述"></a></p><p> 如您所見,網站中的圖像源和我的結果存在差異。 有誰知道什么會導致這種情況? 我的目標是從第一個屏幕截圖中獲取此鏈接: <a href="https://i.ebayimg.com/images/g/M1cAAOSw9Ypa-TOF/s-l1600.jpg" rel="nofollow noreferrer">https://i.ebayimg.com/images/g/M1cAAOSw9Ypa-TOF/s-l1600.jpg</a></p></div>

[英]Cannot get the right image source from a <div> tag using PHP Simple DOM

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 用PHP代碼初始化超全局變量是對的嗎? PHP 5.4包含/要求-無法正確獲取文件路徑 使用javascript循環php無法獲得正確的結果順序 我的代碼無法在PHP中正確提取XPath 對於 PHP 中的這個問題,如何將我的代碼重定向到正確的循環中? Shopify API-使用PHP在商店中獲取折扣碼 無法正確獲取XML結構 無法從<div>使用 PHP 簡單 DOM 標記</div><div id="text_translate"><p>我正在嘗試使用圖像的 div 元素獲取圖像源信息。 但是,當我調試結果時,圖像的來源不同,這是我的 DOM 樹:</p><p> <a href="https://i.stack.imgur.com/0bwQD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0bwQD.png" alt="在此處輸入圖像描述"></a></p><p> 這是 url 供參考: <a href="https://www.ebay.co.uk/itm/Front-strut-spacers-30mm-for-Ford-FOCUS-2-3-C-MAX-KUGA-GRAND-C-MAX-Lift-Kit/192539735653?hash=item2cd4433665:g:4LMAAOSwHPNa-TOF" rel="nofollow noreferrer">https://www.ebay.co.uk/itm/Front-strut-spacers-30mm-for-Ford-FOCUS-2-3-C-MAX-KUGA-GRAND-C- MAX-Lift-Kit/192539735653?hash=item2cd4433665:g:4LMAAOSwHPNa-TOF</a></p><p> 這是我的 php 代碼:</p><pre> $img = $html-&gt;find("#viEnlargeImgLayer_img_ctr", 0); print($img); if($img;= null){ $item['img'] = $img-&gt;getAttribute('src'); }else{ $item['img'] = ''; }</pre><p> 這是我 output img標簽時的結果: <a href="https://i.stack.imgur.com/q1Ne0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/q1Ne0.png" alt="在此處輸入圖像描述"></a></p><p> 如您所見,網站中的圖像源和我的結果存在差異。 有誰知道什么會導致這種情況? 我的目標是從第一個屏幕截圖中獲取此鏈接: <a href="https://i.ebayimg.com/images/g/M1cAAOSw9Ypa-TOF/s-l1600.jpg" rel="nofollow noreferrer">https://i.ebayimg.com/images/g/M1cAAOSw9Ypa-TOF/s-l1600.jpg</a></p></div> PHP:如何獲取請求頁面並獲取正文和http錯誤代碼 無法通過XPath獲得正確的價值
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM