0

i fetch id and name from categories table like this :

  |id|name| tag |desc|order|status|
  |1 |test| NULL|NULL|  1  |   1  |


  $i = 0;
  $allcats = Access::FETCH("SELECT * FROM " . CATS . "");
     $cats = array();
  foreach($allcats AS $row2){                                               
     $cats[$i] = array("name" => $row2['name'], "id" => $row2['id']);
  $i++;                                             
  }

i fetch category id for each news :

|id|catid|storyid|
|1 |  1  |   5   |

$groupcats = Access::FETCH("SELECT * FROM " . GROUPCATS . " WHERE  storyid = ?", $row['postid']);

Now, i need to print name of cat for storyid. i,e : i need to print test(catname) for story id 5.

How do can i print this?

3
  • That's great. What have you tried? Commented Jun 8, 2014 at 19:38
  • You need to research SQL JOINs. Commented Jun 8, 2014 at 19:39
  • @tadman: i dont have any idea!! Commented Jun 8, 2014 at 19:42

2 Answers 2

3

Easier to do with with a join rather than in php. Something like,

$joinedcats = Access::FETCH("SELECT name FROM " . CATS . 
                            " JOIN " . GROUPCATS . " ON catid = id");
foreach($joinedcats as $row) {
     echo $row['name'];
}

See https://dev.mysql.com/doc/refman/5.5/en/join.html

Sign up to request clarification or add additional context in comments.

Comments

0

You can use join to get needed rows for each story:

"SELECT * FROM " . GROUPCATS . " LEFT JOIN " . CATS . " USING(categoryid) WHERE  storyid = ?"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.