Jump to content

[SOLVED] Join table issues


krv

Recommended Posts

Hello.

 

I am having issues displaying mysql results.

I have a 2 tables, 1 is categories 2 is sub categories.

 

Categories

+----+--------------+

| id | title        |

+----+--------------+

 

Sub Categories

+----+-----+----------------------+

| id | cat | title                |

+----+-----+----------------------+

 

I am trying to show them like:

Categories.title

- sub_categories.title where sub_cat.cat = categories.id

 

Ive tried a cross join in mysql command line but cant figure it out in php

 

    $sql_map = "SELECT

ht_cat.id as hid,

ht_cat.title as htitle,

ht_sub_cat.id as sid,

ht_sub_cat.cat as scat,

ht_sub_cat.title as stitle

FROM ht_cat,ht_sub_cat ORDER BY ht_cat.title ASC

";

    $map_result = mysql_query($sql_map) or die(mysql_error());

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

        foreach ($row[hid] as $row[htitle] ->$row[scat]) {

            echo "$row[htitle] ><br /> - $row[stitle]<br />";

        }

    }

 

what am I doing wrong?

Link to comment
https://forums.phpfreaks.com/topic/142124-solved-join-table-issues/
Share on other sites

If you post your actual table field name, I can change the code to work for you if you don't see what we're doing.

 

$query = "SELECT Sub_Categories.cat, Sub_Categories.title, Categories.title FROM Sub_Categories JOIN Categories on (Sub_Categories.cat = Categories.id)";

 

$result = mysql_query ($query) or DIE (mysql_error());

 

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

  echo "$row['htitle'] - $row['stitle']<br />";

}

 

Hello again.

Its not selecting title from categories.

+-----+----------------------+

| cat | title                |

+-----+----------------------+

|  4 | Competition          |

|  4 | Metro Italia        |

|  4 | Geared/ Multipurpose |

|  4 | Carbon              |

|  4 | Neo-Classical        |

|  4 | Performance          |

|  4 | Fixed/ SS            |

|  4 | Steel                |

|  3 | Cruiser              |

|  3 | Comfort              |

|  3 | Commuter            |

|  1 | Freeride            |

|  1 | All-Mountain        |

|  1 | Dirt Jump            |

|  1 | XC Sport            |

+-----+----------------------+

cat should be the categorires.title for instance 1 would be MTB

any suggestions.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.