2015-01-07 56 views
-1

我被要求修改現有的網站,它仍然使用PHP5.3和舊版本的PHPmyDirectory,並且代碼有點雜亂。在兩列顯示MySQL結果(編輯現有代碼)

我試圖修改它只顯示兩列中的城市列表。我試圖把它當作桌子來使用,因爲它看起來最簡單,但我也可以把結果放到並排的div中,因爲列表中沒有超過26個城市(所以前一半或前一個13 ,其餘部分在第二部分)。

下面是現有的原代碼(我知道它不是mysqli的,但我們會在短期內重做這個網站,所以沒有感覺想重做萬頁的代碼現在):

function create_service_area($title) { 

     global $listing; 
     $sql = "SELECT state_id, city_id FROM " .T_LISTINGS_CITIES. " WHERE listing_id = {$listing['id']} " ; 
     $result = query($sql); 
     if(!$result){ 
      $output = "<p>Call for Service Area!</p>"; 
      } 
       else { 
        $output = "<p>"; 
        $result_array = array(); 
        while ($service = fetch_array($result)) 
        { 

        $sql2 = "SELECT title FROM " .T_LOCATIONS. " WHERE id = {$service['city_id']} " ; 
        $result2 = query($sql2); 

         if(!$result2){ 
         break; 
         } else { 
            while ($service2 = fetch_array($result2)) 
            { 
             $output .= "{$service2['title']}"; 
             $title_array = explode(',', $service2['title']); 
             $result_array[] = $title_array;                
             } 
         $output .= "<br/>"; 
         } 


        } 
        if($listing['custom_103'] =="Yes") { 
         $output .= "<b>".$title." will travel for an additional fee!</b></p>"; 
        } else { 
         $output .="</p>"; 
        } 

       } 


     return $output; 

     } 

這是是什麼樣子目前:Current Site

這裏就是我試圖做的事:

function create_service_area($title) { 

global $listing; 
$sql = "SELECT state_id, city_id FROM " .T_LISTINGS_CITIES. " WHERE listing_id = {$listing['id']} " ; 
$result = query($sql); 

    if(!$result){ 

     $output = "<p>Call for Service Area!</p>"; 
     } 
    else { 
     $result_array = array(); 
     while ($service = fetch_array($result)) { 

       $sql2 = "SELECT title FROM " .T_LOCATIONS. " WHERE id = {$service['city_id']} " ; 
       $result2 = query($sql2); 
       $i=0; 

       if(!$result2) { 
        break; 
        } 
        else { 
         while ($service2 = fetch_array($result2)) { 

           $output .= "{$service2['title']}"; 
           $title_array = explode(',', $service2['title']); 
           $result_array[] = $title_array; 
           $i++;                
          } 
         echo "<table>"; 
         for ($j=0; $j<$i; $j=$j+2) { 
         echo "<tr>"; 
         echo "<td>".$title_array[$j]."</td><td>".$title_array[$j+1]."</td>"; 
         echo "</tr>"; 
         } 
        echo "</table>"; 
        } 


       } 
       if($listing['custom_103'] =="Yes") { 
         $output .= "<p><b>".$title." will travel for an additional fee!</b></p>"; 
        } 
        else { 
         $output .=""; 
        } 

       } 

return $output;     
} 

這裏就是我得到:DEV site

我是一個PHP新手,我的理解很不明顯,但我嘗試了一堆我在這裏找到的不同解決方案,並且無法讓他們工作。我確定我錯過了一些明顯的東西。

感謝您的指點!

+1

我相信1998年只是打電話回它的網站。去看看它。 – Mouser

+1

正確縮進以便您可以看到什麼是循環。老實說,你怎麼能用這個可怕的縮進工作? – developerwjk

+0

另外,繼續前進,並在後面加一個新括號'{'。保留換行符不會獲得更多性能。埃及括號不會讓你獲得任何東西,但不可讀。 – developerwjk

回答

0

大廈,我能得到它與一些修改工作。我也爲桌面取消了div,因爲它對我來說似乎不那麼凌亂,而且似乎桌子造型在某種程度上是干擾的。

function create_service_area($title) { 

    global $listing; 
    $sql = "SELECT state_id, city_id FROM " .T_LISTINGS_CITIES. " WHERE listing_id = {$listing['id']} " ; 
    $result = query($sql); 
    if(!$result){ 
    $output = "<p>Call for Service Area!</p>"; 
     } else { 
     $output = "<div>"; 
     //$result_array = array(); 
     $even_odd=true; 
     while ($service = fetch_array($result)) 
      { 

      $sql2 = "SELECT title FROM " .T_LOCATIONS. " WHERE id = {$service['city_id']} " ; 
      $result2 = query($sql2); 

       if(!$result2){ 
        break; 
       } else { 
        $output .= "{$service2['title']}"; 
        $title_array = explode(',', $service2['title']); 
        $result_array[] = $title_array; 

       while ($service2 = fetch_array($result2)) 
       { 
        if ($even_odd) { 
         $output .= '<div style="float:left;width:50%;">'."{$service2['title']}".'</div>'; 
         $even_odd=false; 
        } else { 
         $output .= '<div style="float:right;width:50%;">'."{$service2['title']}".'</div>'; 
         $even_odd=true; 
        } 

       } 

      } 

     } 
     if($listing['custom_103'] =="Yes") { 
     $output .= "<div style='clear:both;width:90%;float:none;'><p><b>".$title." will travel for an additional fee!</b></p></div>"; 
     } else { 

     } 
    } 

    return $output; 
} 

非常感謝Kim和Mouser!

1

,如果我把它糾正你應該改變你

else { 
    $output = "<p>"; 
    $result_array = array(); 
    while ($service = fetch_array($result)) 
    { 

     $sql2 = "SELECT title FROM " .T_LOCATIONS. " WHERE id = {$service['city_id']} " ; 
     $result2 = query($sql2); 

     if(!$result2){ 
      break; 
     } else { 
      while ($service2 = fetch_array($result2)) 
      { 
       $output .= "{$service2['title']}"; 
       $title_array = explode(',', $service2['title']); 
       $result_array[] = $title_array; 
      } 
      $output .= "<br/>"; 
     } 


    } 
    if($listing['custom_103'] =="Yes") { 
     $output .= "<b>".$title." will travel for an additional fee!</b></p>"; 
    } else { 
     $output .="</p>"; 
    } 

} 

else { 
    $output = "<table>"; 
    $result_array = array(); 
    $even_odd=true; 
    while ($service = fetch_array($result)) 
    { 

     $sql2 = "SELECT title FROM " .T_LOCATIONS. " WHERE id = {$service['city_id']} " ; 
     $result2 = query($sql2); 

     if(!$result2){ 
      break; 
     } else { 
      $output .= ""; 

      while ($service2 = fetch_array($result2)) 
      { 
       if ($even_odd) { 
        $output .= '<tr><td>'."{$service2['title']}".'</td>'; 
        $even_odd=false; 
       } else { 
        $output .= '<td>'."{$service2['title']}".'</td></tr>'; 
        $even_odd=true; 
       } 
       $output .= "{$service2['title']}"; 
       $title_array = explode(',', $service2['title']); 
       $result_array[] = $title_array; 
      } 

     } 


    } 
    if($listing['custom_103'] =="Yes") { 
     $output .= "<b>".$title." will travel for an additional fee!</b></p>"; 
    } else { 
     if (!$even_odd)$output .="<td></td></tr>"; 
     $output .="</table>"; 
    } 

} 
+0

這是在正確的方向,但不會創建兩列的城市名單,它只是劃分城市和州兩列:) – Mouser

+0

OH!所以,我完全錯了,我需要取消[$ j]和[j + 1]的參考嗎?你是說這是在一個td呼叫城市,在另一個td狀態? – Haikukitty

+0

檢查我的更新回答 – Alex

1

試試這個,我無法測試它,當然,因爲我已經到了數據無法訪問正在加載。

echo "<table>"; 
$result_array = array(); 
while ($service = fetch_array($result)) 
{ 
    //this will loop multiple times. 7 times for Tony S. in the example. 

    $sql2 = "SELECT title FROM " .T_LOCATIONS. " WHERE id = {$service['city_id']} " ; 
    $result2 = query($sql2); 
    $i=0; 
    if(!$result2) 
    { 
     break; 
    } 
    else 
    { 
     while ($service2 = fetch_array($result2)) 
     { 
      $title_array = explode(',', $service2['title']); 
      $result_array[] = $title_array; 

      $i++;                
     } 
    } 

} 

for ($j=0; $j < count($result_array); $j++) 
{ 
    if ($j % 2 == 0) 
    { 
     echo "<tr>"; 
    } 
    echo "<td>".$result_array[$j][0]." (".$result_array[$j][1].")</td>"; 
    if ($j % 2 == 0) 
    { 
     echo "</tr>"; 
    } 

    if ($j % 2 == 1 && $j == count($result_array)-1) 
    { 
     echo "<td></td></tr>"; 
    } 
}  
echo "</table>"; 

粘貼這行之間替換:關於金正日的代碼

if(!$result){ 

    $output = "<p>Call for Service Area!</p>"; 
    } 
else { 
    .... PASTE IN HERE .... 
} 
+0

感謝Mouser!我嘗試了一下 - 它做了一些奇怪的事情,重複了兩次結果,但不是在表格中。我想知道爲什麼我不斷得到重複...參見:http://www.bluetabby.com/aboutfacesentertainment/entertainers/caricature-artists/l/united-states/georgia/greater-atlanta-area/atlanta-ga/tony- ETA:實際上 - 我看到它在做什麼,它添加+1循環以將每個新城市添加到先前的結果,然後重複更新的結果。 – Haikukitty

+0

我刪除了原始函數底部的返回$輸出。我應該沒有,或者甚至沒有你所說的? :) – Haikukitty

+0

你可以運行更新的代碼。發現一個錯誤。 – Mouser