-6

I am trying to highlight nids of pages when I am using $nid = $node->nid they stop working of function l in drupal 7 but our nids is highlighted here is my code

$ch1=db_query("SELECT th.tid FROM taxonomy_term_hierarchy th 
  INNER JOIN taxonomy_term_data td ON th.tid=td.tid
  INNER JOIN field_data_field_books book ON book.field_books_tid = th.tid
  WHERE td.vid = '6' AND th.parent = $ch AND book.entity_id = $nid
  ")->FetchField();

$chap1=taxonomy_get_children($ch1);
echo "<ul>";
foreach($chap1 as $ca1){
  $pnid = db_query("SELECT n.nid FROM node n 
  INNER JOIN field_data_field_books book ON book.entity_id = n.nid
  WHERE  book.field_books_tid = $ca1->tid AND n.nid = $nid " )->FetchField();
  if($nid == $pnid){
    print "<li class='b'><b>".$ca1->name."</b></li>";
  } else {
    $link = l($ca1->name, 'node/' . $pnid);
  print "<li class='a'>". $link."</li>";
  }
}
echo "</ul>";

how can I solve this problem ?

closed as too broad by kiamlaluno Nov 28 '15 at 16:40

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    I understand neither title, question, nor problem. – Letharion Mar 12 '14 at 11:34
  • i am try to highlight current loaded page function l is using to connect with its node but when i am highlight node we add n.nid=$nid due to this function l can stop work but it is highlighted – Adeel Nawaz Mar 12 '14 at 11:36
  • Are you getting an error message? If so what is the actual error message you are getting? If not what is the problem you are experiencing? Where are you putting this code you have posted (in a function in a custom module - what funciton? In the theme template.php or a template file?) – rooby Mar 12 '14 at 11:36
  • @rooby it gives no error but problem is that function l is not working due to add n.nid = $nid in query – Adeel Nawaz Mar 12 '14 at 11:39
  • What do you mean by not working if there are no errors? Is it outputting nothing at all, or text that isn't a link, or something else? - or just a broken link to the wrong url? – rooby Mar 12 '14 at 11:47
1

I can't say for sure without debugging your code and actually knowing what about your link is not working but I will make assumptions.

I would guess the problem is that $pnid is not a node ID. If your query above returns nothing then $pnid will be FALSE and your link will break.

You need to check that $pnid is a real result but also you probably need to rewrite your sql so it works.

To check that $pnid is something do this:

foreach ($chap1 as $ca1) {
  $pnid = db_query("SELECT n.nid
                      FROM node n 
                INNER JOIN field_data_field_books book ON book.entity_id = n.nid
                     WHERE book.field_books_tid = :tid
                       AND n.nid = :nid", array(':tid' => $ca1->tid, ':nid' => $nid))->FetchField();
  if ($pnid) {
    if ($nid == $pnid) {
      print '<li class="b"><b>' . $ca1->name . '</b></li>';
    }
    else {
      $link = l($ca1->name, 'node/' . $pnid);
      print '<li class="a">' . $link . '</li>';
    }
  }
}

As an aside, the way you are writing your database queries is bad for security reasons. You shouldn't use variables directly in the query like:

$ch1=db_query("SELECT th.tid FROM taxonomy_term_hierarchy th 
  INNER JOIN taxonomy_term_data td ON th.tid=td.tid
  INNER JOIN field_data_field_books book ON book.field_books_tid = th.tid
  WHERE td.vid = '6' AND th.parent = $ch AND book.entity_id = $nid
  ")->FetchField();

If you are passing in valid variables then there must be no node that matches the tid you're passing into the query.

Instead it should be like this:

$ch1=db_query("SELECT th.tid FROM taxonomy_term_hierarchy th 
  INNER JOIN taxonomy_term_data td ON th.tid=td.tid
  INNER JOIN field_data_field_books book ON book.field_books_tid = th.tid
  WHERE td.vid = '6' AND th.parent = :ch AND book.entity_id = :nid
  ", array(':ch' => $ch, ':nid' => $nid))->FetchField();

Notice how it uses the tokens with the preceeding colon instead of putting the variable directly in the string. This allows the database query function to sanitize the variable so users cannot hack your website with SQL injection.

Another thing (specific to (db_query()) is you should wrap tables in curly braces or it can break if your code is ever used on a site where the database has table prefixes. For example:

FROM {node} n

I would also advise against using classes like 'b', because what does that represent? Someone else (or you in the future) reading your code could be quite confused.

Not the answer you're looking for? Browse other questions tagged or ask your own question.