Php 在两列中显示MySQL结果(编辑现有代码)
我被要求修改一个现有的站点,它仍然使用PHP5.3和PHPmyDirectory的旧版本,代码有点凌乱 我正试图修改它,以便只在两列中显示城市列表。我试着把它当作一张桌子来做,因为它看起来很简单,但我也可以把结果拉到并列的分区中,因为列出的城市从来没有超过26个(所以上半场或第一分区的前13个,其余的在第二分区) 这是现有的原始代码(我知道它不是mysqli,但我们很快就会重做这个站点,所以现在尝试重做一百万页代码是没有意义的):Php 在两列中显示MySQL结果(编辑现有代码),php,html,mysql,Php,Html,Mysql,我被要求修改一个现有的站点,它仍然使用PHP5.3和PHPmyDirectory的旧版本,代码有点凌乱 我正试图修改它,以便只在两列中显示城市列表。我试着把它当作一张桌子来做,因为它看起来很简单,但我也可以把结果拉到并列的分区中,因为列出的城市从来没有超过26个(所以上半场或第一分区的前13个,其余的在第二分区) 这是现有的原始代码(我知道它不是mysqli,但我们很快就会重做这个站点,所以现在尝试重做一百万页代码是没有意义的): 功能创建服务区($title){ 全球美元上市; $sql=“从
功能创建服务区($title){
全球美元上市;
$sql=“从“.T\u清单\u城市”中选择州id、城市id。其中清单id={$listing['id']}”;
$result=query($sql);
如果(!$result){
$output=“呼叫服务区!”;
}
否则{
$output=“”;
$result_array=array();
while($service=fetch_数组($result))
{
$sql2=“从.T_位置选择标题。”其中id={$service['city_id']}”;
$result2=查询($sql2);
如果(!$result2){
打破
}否则{
而($service2=fetch_数组($result2))
{
$output.=“{$service2['title']}”;
$title_数组=分解(',',$service2['title']);
$result\u array[]=$title\u array;
}
$output.=“
”;
}
}
如果($listing['custom_103']==“Yes”){
$output.=“.$title.”将额外付费旅行!”;
}否则{
$output.=“”;
}
}
返回$output;
}
这就是当前的情况:
以下是我试图做的:
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;
}
功能创建服务区($title){
全球美元上市;
$sql=“从“.T\u清单\u城市”中选择州id、城市id。其中清单id={$listing['id']}”;
$result=query($sql);
如果(!$result){
$output=“呼叫服务区!”;
}
否则{
$result_array=array();
而($service=fetch_数组($result)){
$sql2=“从.T_位置选择标题。”其中id={$service['city_id']}”;
$result2=查询($sql2);
$i=0;
如果(!$result2){
打破
}
否则{
而($service2=fetch_数组($result2)){
$output.=“{$service2['title']}”;
$title_数组=分解(',',$service2['title']);
$result\u array[]=$title\u array;
$i++;
}
回声“;
对于($j=0;$j如果我没有弄错,你应该更改你的
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=“”;
$result_array=array();
while($service=fetch_数组($result))
{
$sql2=“从.T_位置选择标题。”其中id={$service['city_id']}”;
$result2=查询($sql2);
如果(!$result2){
打破
}否则{
而($service2=fetch_数组($result2))
{
$output.=“{$service2['title']}”;
$title_数组=分解(',',$service2['title']);
$result\u array[]=$title\u array;
}
$output.=“
”;
}
}
如果($listing['custom_103']==“Yes”){
$output.=“.$title.”将额外付费旅行!”;
}否则{
$output.=“”;
}
}
与
else{
$output=“”;
$result_array=array();
$偶数=真;
while($service=fetch_数组($result))
{
$sql2=“从.T_位置选择标题。”其中id={$service['city_id']}”;
$result2=查询($sql2);
如果(!$result2){
打破
}否则{
$output.=“”;
而($service2=fetch_数组($result2))
{
如果($偶数){
$output.='''.{$service2['title']}.'';
$偶数=假;
}否则{
$output.='''.{$service2['title']}.'';
$偶数=真;
}
$output.=“{$service2['title']}”;
$title_数组=分解(',',$service2['title']);
$result\u array[]=$title\u array;
}
}
}
如果($listing['custom_103']==“Yes”){
$output.=“.$title.”将额外付费旅行!”;
}否则{
如果(!$偶数)$输出=“”;
$output.=“”;
}
}
试试这个,我当然无法测试它,因为我无法访问正在加载的数据
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>";
echo”“;
$result_array=array();
while($service=fetch_数组($result))
{
//这将循环多次。示例中Tony S.的循环次数为7次。
$sql2=“从.T_位置选择标题。”其中id={$service['city_id']}”;
$result2=查询($sql2);
$i=0;
如果(!$result2)
{
打破
}
其他的
{
而($service2=fetch_数组($result2))
{
$title_数组=分解(',',$service2['title']);
$result\u array[]=$title\u array;
$i++;
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>";
}
}
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 ....
}
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;
}