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
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 />";

}

 

Link to comment
Share on other sites

I would think you would have a few options:

 

Use AS as you did in your first post.

Use the ID of the field result instead of the name ($row[0], $row[1], $row[2]...etc)

 

Echo the $row array to see how it's being returned.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.