2013-03-16 40 views
0

使用php創建一個字符串,使用通過表單使用post的submited數據。如何爲div提供唯一的id,因爲它們是用php書寫的頁面

這是PHP:

if ($carrier== "DL") { 


       $front = '<tr><td><div onclick="selectText(this)">EZE*#$*IET'; 
       $center = '/O$'.$FOP.'/RB'.$differencePos.'X0.00P'.$fee.'AT0.00/OIECP'.$orIssue.'111062789#MCRV*/MA'.$MCOamt; 
       $end = '</div></td></tr>'; 


           echo $front,$guest1Ticket,'X*A',$guest1Con,$center,'#N1.1',$end; 

      if($guest2Ticket) echo $front,$guest2Ticket,'X*A',$guest2Con,$center,'#N2.1',$end; 

      if($guest3Ticket) echo $front,$guest3Ticket,'X*A',$guest3Con,$center,'#N3.1',$end; 

      if($guest4Ticket) echo $front,$guest4Ticket,'X*A',$guest4Con,$center,'#N4.1',$end; 

      if($guest5Ticket) echo $front,$guest5Ticket,'X*A',$guest5Con,$center,'#N5.1',$end; 

      if($guest6Ticket) echo $front,$guest6Ticket,'X*A',$guest6Con,$center,'#N6.1',$end; 

      if($guest7Ticket) echo $front,$guest7Ticket,'X*A',$guest7Con,$center,'#N7.1',$end; 

      if($guest8Ticket) echo $front,$guest8Ticket,'X*A',$guest8Con,$center,'#N8.1',$end; 

      if($guest9Ticket) echo $front,$guest9Ticket,'X*A',$guest9Con,$center,'#N9.1',$end; 
     } //if ($airline == "DL") 

這將創建一個看起來像這樣的字符串: EZE*#$*IET8388623729002X*A/O$CA/RB71.02X0.00P50.00AT0.00/OIECP01AUG111062789#MCRV*/MA21.02#N1.1

EZE*#$*IET8388623729003X*A/O$CA/RB71.02X0.00P50.00AT0.00/OIECP01AUG111062789#MCRV*/MA21.02#N2.1

EZE*#$*IET8388623729004X*A/O$CA/RB71.02X0.00P50.00AT0.00/OIECP01AUG111062789#MCRV*/MA21.02#N3.1

....

每條線之間的唯一區別是IET 份和N 1.1部件上的字符串本身的端部。

我使用$front$end子字符串來包裝div和table元素中的實際字符串用於佈局。

什麼是最好的方式給每個div得到echo'd一個唯一的ID,所以我的onclick="selectText(this)"將工作?

我知道我可以打破$front分成幾個子,只是這樣做

if($guest2Ticket) echo $front1,'id="div2"',$front2,$guest2Ticket,'X*A',$guest2Con,$center,'#N2.1',$end;

,但有沒有更好的辦法?

回答

1

這是我會這麼做的方式。通過從變量字符串改變$front的功能,你可以使用一個靜態計數器,每一次提高它調用該函數

function front() { 
    static $i=1; 
    return '<tr><td><div id="div'.$i.'" onclick="selectText(this)">EZE*#$*IET'; 
    $i++; 
} 

它會是這個樣子

if ($carrier== "DL") { 


      function front() { 
        static $i=1; 
        return '<tr><td><div id="div'.$i.'" onclick="selectText(this)">EZE*#$*IET'; 
        $i++; 
      } 
      $center = '/O$'.$FOP.'/RB'.$differencePos.'X0.00P'.$fee.'AT0.00/OIECP'.$orIssue.'111062789#MCRV*/MA'.$MCOamt; 
      $end = '</div></td></tr>'; 


          echo front(),$guest1Ticket,'X*A',$guest1Con,$center,'#N1.1',$end; 

     if($guest2Ticket) echo front(),$guest2Ticket,'X*A',$guest2Con,$center,'#N2.1',$end; 

     if($guest3Ticket) echo front(),$guest3Ticket,'X*A',$guest3Con,$center,'#N3.1',$end; 

     if($guest4Ticket) echo front(),$guest4Ticket,'X*A',$guest4Con,$center,'#N4.1',$end; 

     if($guest5Ticket) echo front(),$guest5Ticket,'X*A',$guest5Con,$center,'#N5.1',$end; 

     if($guest6Ticket) echo front(),$guest6Ticket,'X*A',$guest6Con,$center,'#N6.1',$end; 

     if($guest7Ticket) echo front(),$guest7Ticket,'X*A',$guest7Con,$center,'#N7.1',$end; 

     if($guest8Ticket) echo front(),$guest8Ticket,'X*A',$guest8Con,$center,'#N8.1',$end; 

     if($guest9Ticket) echo front(),$guest9Ticket,'X*A',$guest9Con,$center,'#N9.1',$end; 
} //if ($airline == "DL") 
+0

添加空格前的onclick – 2013-03-16 13:49:52

+0

@AndersLindén感謝。 – Sean 2013-03-16 13:53:25

+0

顯然,我的代碼比上面顯示的還多。 (我實際上有一堆這樣的「字符串製造商」)這種方法更適合我的特殊用途。非常感謝肖恩! – DelightedD0D 2013-03-17 06:03:24

1

我會做到這一點與jQuery,不發射id。

(不要忘記先包含jquery.js)。

<script> 
    $(function() 
    { 
    $('div').click(function() 
    { 
     selectText(this); 
    }); 
    }); 
</script> 
+0

您還可以使用var txt = $(this).text()獲取div內的文本;警報(TXT);而不是selectText(this); – 2013-03-16 13:56:21

+0

或者爲什麼不讓selectText有一個字符串作爲參數傳遞,那麼你selectText($(this).text()); – 2013-03-16 13:57:45

相關問題