Jump to content
Sign in to follow this  

Recommended Posts

Hi i created a php code but it is maybe wrong....

The Subcategory name it is displaying 2 times..

Php Code:

$catssql = "SELECT * FROM cats1";
$result2 = mysql_query($catssql);

echo '<table width="800" class="productstable" align="center">';
while($cats1row = mysql_fetch_assoc($result2))
{
echo '<tr><td><b>'. $cats1row['cat_nm'].'</b></td></tr>';
$ctid = $cats1row['id'];
$catsoncat = "SELECT * FROM categories,cats1 WHERE categories.catid = $ctid";
$result3 = mysql_query($catsoncat);
while($catsoncatrow = mysql_fetch_assoc($result3))
{
echo '<tr><td>'.$catsoncatrow['cat_name'].'</td></tr>';
}
}
echo '</table>';

If anyone knows how to fix it help please :)

Share this post


Link to post
Share on other sites

check your sql to make sure that its reading properly the only thing that i can see is maybe put a } before the second while statement. HTH

  • Like 2

Share this post


Link to post
Share on other sites

No...i have it because if i close this tag before the second while it will not display categories and subcategories at the same box

  • Like 1

Share this post


Link to post
Share on other sites

Dear You code is nice, Please check again, or can use a Counter as follows to protect duplication

 

$catssql = "SELECT * FROM cats1";

$result2 = mysql_query($catssql);

$i= 0;

echo '<table width="800" class="productstable" align="center">';

while($cats1row = mysql_fetch_assoc($result2))

{

echo '<tr><td><b>'. $cats1row['cat_nm'].'</b></td></tr>';

$ctid = $cats1row['id'];

$catsoncat = "SELECT * FROM categories,cats1 WHERE categories.catid = $ctid";

$result3 = mysql_query($catsoncat);

while($catsoncatrow = mysql_fetch_assoc($result3))

{

echo '<tr><td>'.$catsoncatrow['cat_name'].'</td></tr>';

}

$i++;

}

 

echo '</table>';

 

or see the post to know more

http://www.blog.pennybackshop.com/fetching-mysql-data-in-proper-way.html

 

 

Info  CP Moderator Message: Link Coded - Tech425

Share this post


Link to post
Share on other sites

check ur 1st sql

$catssql = "SELECT * FROM cats1";

 

u have to add where clause to fetch the main cats only =)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×