0

I have some PHP code to get info from a database.

$category='New Boats';
$titles=array();
$result=db_query('SELECT vid FROM {taxonomy_vocabulary} WHERE name = :type',array(':type' => 'Boat Types'));
$row = $result->fetchAssoc();//8
$vid=$row['vid'];
$tid=db_query('SELECT tid,name FROM {taxonomy_term_data} WHERE vid=:vid',array(':vid' => $vid));
while($ttd_row= $tid->fetchAssoc()){
  if($ttd_row['name']==$category){
    $tid=$ttd_row['tid'];
    $result=db_query('SELECT entity_id FROM {field_data_field_boat_type} WHERE field_boat_type_tid=:tid',array(':tid'=> $tid));
    $eRows=$result->fetchAssoc();
    $eid=$eRows['entity_id'];
    $result=db_query('SELECT title FROM {node} WHERE nid=:nid',array(':nid'=>$eid));
    $eRows=$result->fetchAssoc();
    $titles[$eid]=$eRows['title'];
  }
print_r($titles);//<------- This outputs the contents of the array.
}

The above code prints out the contents of the $titles array.

However, this produces a blank page:

$category='New Boats';
$titles=array();
$result=db_query('SELECT vid FROM {taxonomy_vocabulary} WHERE name = :type',array(':type' => 'Boat Types'));
$row = $result->fetchAssoc();//8
$vid=$row['vid'];
$tid=db_query('SELECT tid,name FROM {taxonomy_term_data} WHERE vid=:vid',array(':vid' => $vid));
while($ttd_row= $tid->fetchAssoc()){
  if($ttd_row['name']==$category){
    $tid=$ttd_row['tid'];
    $result=db_query('SELECT entity_id FROM {field_data_field_boat_type} WHERE field_boat_type_tid=:tid',array(':tid'=> $tid));
    $eRows=$result->fetchAssoc();
    $eid=$eRows['entity_id'];
    $result=db_query('SELECT title FROM {node} WHERE nid=:nid',array(':nid'=>$eid));
    $eRows=$result->fetchAssoc();
    $titles[$eid]=$eRows['title'];
  }
}
print_r($titles);//<------------ No output, just a blank page.

Can anyone see what I'm doing wrong?

1
Contributor
1
Reply
16
Views
4 Years
Discussion Span
Last Post by davecoventry
0

I'm asking here.

Why should I go to another forum and ask the same question?

Anyway, I've solved it.

$category='New Boats';
$titles=array();
$result=db_query('SELECT vid FROM {taxonomy_vocabulary} WHERE name = :type',array(':type' => 'Boat Types'));
$row = $result->fetchAssoc();//8
$vid=$row['vid'];
$ttd=db_query('SELECT tid,name FROM {taxonomy_term_data} WHERE vid=:vid',array(':vid' => $vid));//<----- varaible name changed from $tid to $ttd
while($ttd_row= $ttd->fetchAssoc()){//<--- $tid replaced by $ttd
  if($ttd_row['name']==$category){
    $tid=$ttd_row['tid'];//<----- There was clearly a conflict with this instance of $tid
    $result=db_query('SELECT entity_id FROM {field_data_field_boat_type} WHERE field_boat_type_tid=:tid',array(':tid'=> $tid));
    $eRows=$result->fetchAssoc();
    $eid=$eRows['entity_id'];
    $result=db_query('SELECT title FROM {node} WHERE nid=:nid',array(':nid'=>$eid));
    $eRows=$result->fetchAssoc();
    $titles[$eid]=$eRows['title'];
  }
}
print_r($titles);

Edited by davecoventry: Punctuation

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.