显示MySQL结果有两列(编辑现有代码)

时间:2015-01-07 21:24:18

标签: php html mysql

我被要求修改现有网站,它仍然使用PHP5.3和旧版本的PHPmyDirectory,代码有点乱。

我正在尝试修改它,只显示两列中的城市列表。我试图将它作为一个表格,因为它看起来最简单,但我也可以将结果拉到并排的div中,因为列出的城市从未超过26个(所以上半部分或前13部分,其余的在div二。)。

这是现有的原始代码(我知道它不是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新手,我的理解非常参差不齐,但我已经尝试了一些我在这里找到的不同解决方案,并且无法让它们发挥作用。我确定我错过了一些明显的东西。

感谢您的任何指示!

3 个答案:

答案 0 :(得分: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>";
    }

}

答案 1 :(得分: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 ....
}

答案 2 :(得分:0)

基于Kim的代码,我能够使用一些修订版。我也取消了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!