php - Select unexisting values -


i have database images wich can tagged.

to tag them put value (wich represents color) in database table called tagged

tagged

  imgid  (example 5)   tagid  (example 3) 

i have table called tags (the colors)

tags

  tagid   (example 3)   tag     (example blue) 

in list tags 20 tags.

what have page images 1 page imageid 5

if open page want list of tag's tags

if tagid in tagged must like

tagid selected

else just

tagid

how can this.

i have database working tag images cant 'see' if tag used.

what have:

$sql = "select *         tags";         $result = mysql_query("$sql");      while($row = mysql_fetch_array($result))       {              echo "<input type=\"checkbox\" name=\"color[]\" value=\"".$row['tagid']."\">";          echo $row['tagid'];         echo "<br>";       } 

i gues need join or best method?

ps know should use mysqli se mysql testing adjust later mysqli

you can use left join group by 1 query against database , complete result;

select tag, count(imgid) selected tags  left join tagged   on tags.tagid = tagged.tagid  , tagged.imgid = 5 group tag; 

an sqlfiddle test with.

a short explanation;

i use left join select tags , possibly matching entries in tagged imgid=5. if there no match, value of imgid null, if there match, it's 5. feature of left join.

i count number of imgid's per tag (the count , group that). null not count, count becomes 0, while 5 counts , gives count 1 (remember, restricted hits on single imgid, 5).


Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -