joe_ma Posted October 20, 2015 Share Posted October 20, 2015 Hello I have a template with a page reference field for categories. Now I'd like to list content from all the pages that have the same categories. My code looks like this: // list all addresses that have rubrik = adr_rubrik. $out = ""; if($page->fields->adr_rubrik) { $rubrik = $page->adr_rubrik; $out .= '<table id="myTable" class="tablesorter adressliste display"> <thead> <tr> <th class="inst">Institution</th> <th class="plz">PLZ</th> <th class="ort">Ort</th> <th class="inst_info">Beschreibung</th> </tr> </thead> <tbody>'; foreach($rubrik as $r) { $list = $pages->find("template=adress_daten, adr_rubrik=$r, include=all"); foreach($list as $l) { $out .= "<tr><td>"; if($l->adr_firma) $out .= "$l->adr_firma <br>"; if($l->adr_abteilung) $out .= "$l->adr_abteilung <br>"; if($l->adr_vorname) $out .= "$l->adr_vorname <br>"; if($l->adr_name) $out .= "$l->adr_name <br>"; if($l->adr_strasse) $out .= "$l->adr_strasse <br>"; if($l->adr_postfach) $out .= "$l->adr_postfach <br>"; if($l->adr_tel) $out .= "$l->adr_tel <br>"; if($l->adr_fax) $out .= "$l->adr_fax <br>"; if($l->adr_email) $out .= "$l->adr_email <br>"; if($l->adr_web) $out .= "<a target='_blank' href='$l->adr_web'>website</a> <br>"; if($l->adr_webzusatz) $out .= "<a target='_blank' href='$l->adr_webzusatz'>zusätzliche Website</a>"; $out .= "</td><td>"; $out .= $l->adr_plz; $out .= "</td>"; $out .= "<td>"; $out .= $l->adr_ort; $out .= "</td>"; $out .= "<td>"; $out .= $l->adr_beschreibung; $out .= "</td></tr>"; } } $out .= '</tbody></table>'; $content .= $out; } So far everything works fine. Except when the page reference field (adr_rubrik) has no pages selected. Then the output is a table without rows, i.e. the table head and end. What I like to have is, that nothing at all is being output if the field adr_rubrik is empty. Thanks for help. Link to comment Share on other sites More sharing options...
diogo Posted October 20, 2015 Share Posted October 20, 2015 try: if($page->fields->adr_rubrik->count) 1 Link to comment Share on other sites More sharing options...
joe_ma Posted October 21, 2015 Author Share Posted October 21, 2015 Tried that. Now nothing at all is output, even where the field is not empty. Link to comment Share on other sites More sharing options...
LostKobrakai Posted October 21, 2015 Share Posted October 21, 2015 I think diogo fell in the copy/paste trap here. You need to check the count of the field instance of the page. $page->fields->… will get you the field's properties, but not actual data. if($page->adr_rubrik->count()){ // just syntax sugar for: count($page->adr_rubrik) … } 1 Link to comment Share on other sites More sharing options...
joe_ma Posted October 21, 2015 Author Share Posted October 21, 2015 Nope, no difference. Still output of the table head and end, where field is empty. Where the field has a category selected, rows are displayed as expected. Link to comment Share on other sites More sharing options...
tpr Posted October 21, 2015 Share Posted October 21, 2015 Have you moved the count check above the whole table? Currently it is inside the body. Link to comment Share on other sites More sharing options...
joe_ma Posted October 21, 2015 Author Share Posted October 21, 2015 Hm, I don't think so. At the moment the code looks like this. if($page->adr_rubrik->count()){ $rubrik = $page->adr_rubrik; $out .= '<table id="myTable" class="tablesorter adressliste display"> <thead> <tr> <th class="inst">Institution</th> <th class="plz">PLZ</th> <th class="ort">Ort</th> <th class="inst_info">Beschreibung</th> </tr> </thead> <tbody>'; foreach($rubrik as $r) { $list = $pages->find("template=adress_daten, adr_rubrik=$r, include=all"); foreach($list as $l) { $out .= "<tr><td>"; I think the line in question IS above the table. Isn't it? Link to comment Share on other sites More sharing options...
tpr Posted October 21, 2015 Share Posted October 21, 2015 Seems so... don't know what I was looking. Try var_dump-ing values to spot any difference between empty/non-empty fields. Link to comment Share on other sites More sharing options...
joe_ma Posted October 22, 2015 Author Share Posted October 22, 2015 var_dump($rubrik) where field is not empty: object(PageArray)#352 (7) { ["hooks"]=> array(2) { ["PageArray::render"]=> string(60) "MarkupPageArray->renderPageArray() in MarkupPageArray.module" ["PageArray::renderPager"]=> string(56) "MarkupPageArray->renderPager() in MarkupPageArray.module" } ["count"]=> int(1) ["items"]=> array(1) { [0]=> string(31) "/de/adressen/rubriken/religion/" } ["total"]=> int(1) ["start"]=> int(0) ["limit"]=> int(0) ["selectors"]=> string(0) "" } var_dump($rubrik) where field is empty: object(PageArray)#352 (7) { ["hooks"]=> array(2) { ["PageArray::render"]=> string(60) "MarkupPageArray->renderPageArray() in MarkupPageArray.module" ["PageArray::renderPager"]=> string(56) "MarkupPageArray->renderPager() in MarkupPageArray.module" } ["count"]=> int(1) ["items"]=> array(1) { [0]=> string(30) "/de/adressen/rubriken/vereine/" } ["total"]=> int(1) ["start"]=> int(0) ["limit"]=> int(0) ["selectors"]=> string(0) "" } var_dump($list) where field is not empty: object(PageArray)#363 (7) { ["hooks"]=> array(2) { ["PageArray::render"]=> string(60) "MarkupPageArray->renderPageArray() in MarkupPageArray.module" ["PageArray::renderPager"]=> string(56) "MarkupPageArray->renderPager() in MarkupPageArray.module" } ["count"]=> int(3) ["items"]=> array(3) { [0]=> string(44) "/de/adressen/adressdaten/familientreff-bern/" [1]=> string(50) "/de/adressen/adressdaten/muetterzentrum-bern-west/" [2]=> string(45) "/de/adressen/adressdaten/stiftung-idee-sport/" } ["total"]=> int(3) ["start"]=> int(0) ["limit"]=> int(0) ["selectors"]=> string(38) "template=adress_daten, adr_rubrik=1273" } object(PageArray)#363 (7) { ["hooks"]=> array(2) { ["PageArray::render"]=> string(60) "MarkupPageArray->renderPageArray() in MarkupPageArray.module" ["PageArray::renderPager"]=> string(56) "MarkupPageArray->renderPager() in MarkupPageArray.module" } ["count"]=> int(3) ["items"]=> array(3) { [0]=> string(44) "/de/adressen/adressdaten/familientreff-bern/" [1]=> string(50) "/de/adressen/adressdaten/muetterzentrum-bern-west/" [2]=> string(45) "/de/adressen/adressdaten/stiftung-idee-sport/" } ["total"]=> int(3) ["start"]=> int(0) ["limit"]=> int(0) ["selectors"]=> string(38) "template=adress_daten, adr_rubrik=1273" } object(PageArray)#363 (7) { ["hooks"]=> array(2) { ["PageArray::render"]=> string(60) "MarkupPageArray->renderPageArray() in MarkupPageArray.module" ["PageArray::renderPager"]=> string(56) "MarkupPageArray->renderPager() in MarkupPageArray.module" } ["count"]=> int(3) ["items"]=> array(3) { [0]=> string(44) "/de/adressen/adressdaten/familientreff-bern/" [1]=> string(50) "/de/adressen/adressdaten/muetterzentrum-bern-west/" [2]=> string(45) "/de/adressen/adressdaten/stiftung-idee-sport/" } ["total"]=> int(3) ["start"]=> int(0) ["limit"]=> int(0) ["selectors"]=> string(38) "template=adress_daten, adr_rubrik=1273" } var_dump($list) where field is empty: no output. Link to comment Share on other sites More sharing options...
Martijn Geerts Posted October 22, 2015 Share Posted October 22, 2015 You've a complicated setup , the below is written in the browser. <?php $out = ''; $rubrik = false; if ($page->template->has('adr_rubrik')) { $rubrik = $page->adr_rubrik; } if ($rubik && count($rubik)) { $list = $pages->find("template=adress_daten, adr_rubrik=$rubik, include=all"); if (count($list)) { $out .= '<table id="myTable" class="tablesorter adressliste display">'; // ..... etc. etc. foreach($list as $l) { // ..... etc. etc. } } } Link to comment Share on other sites More sharing options...
joe_ma Posted October 22, 2015 Author Share Posted October 22, 2015 You've a complicated setup , Doesn't really surprise me … The adr_rubrik field allows more than one category to be selected. Therefore I thought, I had to iterate through these as well. I tried to adopt your code. It now looks like this: $out = ''; $rubrik = false; if ($page->template->has('adr_rubrik')) { $rubrik = $page->adr_rubrik; } if ($rubrik && count($rubrik)) { $list = $pages->find("template=adress_daten, adr_rubrik=$rubrik, include=all"); if (count($list)) { $out .= '<table id="myTable" class="tablesorter adressliste display"> <thead> <tr> <th class="inst">Institution</th> <th class="plz">PLZ</th> <th class="ort">Ort</th> <th class="inst_info">Beschreibung</th> </tr> </thead> <tbody>'; foreach($list as $l) { $out .= "<tr><td>"; if($l->adr_firma) $out .= "$l->adr_firma <br>"; if($l->adr_abteilung) $out .= "$l->adr_abteilung <br>"; if($l->adr_vorname) $out .= "$l->adr_vorname <br>"; if($l->adr_name) $out .= "$l->adr_name <br>"; if($l->adr_strasse) $out .= "$l->adr_strasse <br>"; if($l->adr_postfach) $out .= "$l->adr_postfach <br>"; if($l->adr_tel) $out .= "$l->adr_tel <br>"; if($l->adr_fax) $out .= "$l->adr_fax <br>"; if($l->adr_email) $out .= "$l->adr_email <br>"; if($l->adr_web) $out .= "<a target='_blank' href='$l->adr_web'>website</a> <br>"; if($l->adr_webzusatz) $out .= "<a target='_blank' href='$l->adr_webzusatz'>zusätzliche Website</a>"; $out .= "</td><td>"; $out .= $l->adr_plz; $out .= "</td>"; $out .= "<td>"; $out .= $l->adr_ort; $out .= "</td>"; $out .= "<td>"; $out .= $l->adr_beschreibung; $out .= "</td></tr>"; } } $out .= '</tbody></table>'; $content .= $out; } This leads to nothing being output in both cases. Link to comment Share on other sites More sharing options...
joe_ma Posted October 22, 2015 Author Share Posted October 22, 2015 OK, so I changed line 4 of the code here above to if ($page->fields->adr_rubrik) { And now everything works fine. Thank you all for your patience and help. 1 Link to comment Share on other sites More sharing options...
Martijn Geerts Posted October 22, 2015 Share Posted October 22, 2015 (edited) What is the reason you don't know if the page has the field 'adr_rubrik'? Are you sure that the PageField adr_rubrik is set to Multiple? Edited October 22, 2015 by Martijn Geerts Had a phone call, so this post is late. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now