PHP循环显示错误的文本

时间:2015-02-12 13:38:34

标签: php mysql foreach html-table

我试图在我的类别下显示一个公司的表格,并使用CSS在点击时显示子类别。到目前为止,我有下面的代码,但我得到一个双文本输出,它似乎显示在表格之前和表格中的列表中!

//get local category names
$query = "SELECT DISTINCT `Merchant Category`, `company` , `id` FROM `otherfeeds`  ORDER BY `Merchant Category`";
$result=mysql_query($query) or die("Cant find merchant names and cats");
$merchant_cat_array = array();
while ($row = mysql_fetch_array($result)) 
{ 
$merchant_cat_array[] = $row['Merchant Category'];
}
mysql_close();

//start table to display data
$i=0;
$merchant_cat_was = ""; //set first check to empty
echo '<table width="100%">';
foreach($merchant_cat_array as $merchant_cat)
{

if($merchant_cat_was != $merchant_cat)
{ //if new cat start td
if($i==0) { echo "<tr><td valign='top'>0"; }
elseif($i==1 or $i==2) { echo "<td valign='top'>1or2"; }
///display new category in td
echo '<h2>'.$merchant_cat.'</h2>';
}
///get companys in category and local id
include("../../include/connectaw.php");
$query = "SELECT * FROM `otherfeeds` WHERE `Merchant Category` =    '$merchant_cat'";
$result=mysql_query($query) or die("Cant find merchants for cats    $merchant_cat");
$merchant_id_array = array();
$merchant_company_array = array();
while ($row = mysql_fetch_array($result)) 
{ 
$merchant_id_array[] = $row['id'];
$merchant_company_array[] = $row['company'];
}
mysql_close();

//loop through all companys found
$loop=0;
foreach($merchant_company_array as $company)
{
$id = $merchant_id_array[$loop];
if($id<=50) { $table = "compare_tester"; }
elseif($id>50 and $id<=100) { $table = "compare_tester2"; }
elseif($id>100 and $id<=150) { $table = "compare_tester3"; }
elseif($id>150 and $id<=200) { $table = "compare_tester4"; }
elseif($id>200 and $id<=250) { $table = "compare_tester5"; }

///display company and catnames for company
include("../../include/connect.php");
$query = "SELECT  DISTINCT `catname` FROM ".$table." WHERE company =     '$company' LIMIT 10";
$result=mysql_query($query) or die("Cant find merchants for subcats");
$c=0;
?>
<style>
.alert-<?php echo $id; ?>
{
 display:none;   
}
}
.hide-<?php echo $id; ?>:focus ~ .alert-<?php echo $id; ?> {
  display: none;
}
.show-<?php echo $id; ?>:focus ~ .alert-<?php echo $id; ?> {
  display: block;

}</style>
<?php

echo '<h3>'.$company.'</h3><span class="show-'.$id.'" tabindex="0">Browse     Categorys</span> or <a href="sql-cat.php?cat='.$company.'">Shop Now</a><BR>';
///hidden div
echo '<ul class="alert-'.$id.'">';

while ($row = mysql_fetch_array($result)) 
{

$catname = $row['catname'];
if($catname)
{
echo '<li><a href="sql-catname.php?    cat='.$company.'&catname='.$catname.'">'.$catname.'</a></li>';
}
$c++;
}
echo '</ul>';
mysql_close();
$loop++;
}

//end display
///
if($merchant_cat_was != $merchant_cat)
{

if($i==0 or $i==1) { echo "</td>"; $i++; }
elseif($i==2) { echo "</td></tr>"; $i=0; }
}
$merchant_cat_was = $merchant_cat;

//end td
} //end loop
echo "</table>"; //end table
mysql_close();

我出错的任何想法?

输出应该是这样的:

<table>
<tr>
<td>
<h2>Cat 1<h2>
<h3>Shop name</h3>
<ul><li>sub cat 1</li>
<li>sub cat 2</li></ul>
</td>
<td>
<h2>Cat 2<h2>
<h3>Shop name</h3>
<ul><li>sub cat 1</li>
<li>sub cat 2</li></ul>
</td>
<td>
<h2>Cat 3<h2>
<h3>Shop name</h3>
<ul><li>sub cat 1</li>
<li>sub cat 2</li></ul>
</td>
</tr>
</table>

我得到的是

<h2>Cat 1<h2>
<h3>Shop name</h3>
<ul><li>sub cat 1</li>
<li>sub cat 2</li></ul>

<h2>Cat 2<h2>
<h3>Shop name</h3>
<ul><li>sub cat 1</li>
<li>sub cat 2</li></ul>

<h2>Cat 3<h2>
<h3>Shop name</h3>
<ul><li>sub cat 1</li>
<li>sub cat 2</li></ul>

<table>
<tr>
<td>
<h2>Cat 1<h2>
<h3>Shop name</h3>
<ul><li>sub cat 1</li>
<li>sub cat 2</li></ul>
</td>
<td>
<h2>Cat 2<h2>
<h3>Shop name</h3>
<ul><li>sub cat 1</li>
<li>sub cat 2</li></ul>
</td>
<td>
<h2>Cat 3<h2>
<h3>Shop name</h3>
<ul><li>sub cat 1</li>
<li>sub cat 2</li></ul>
</td>
</tr>
</table>

1 个答案:

答案 0 :(得分:0)

我发现我过早地关闭了一个foreach循环,现在已经解决了这个问题。