JerryDi Posted December 27, 2023 Share Posted December 27, 2023 Hi, I am trying to present selected fields from a single template in a simple HTML table. Here is my code: <?php $countymatch = $pages->find("template=county-match-result, year={$page->id}"); ?> <table> <strong> <tr> <th>Opponents</th> <th>Venue</th> <th>Points for</th> <th>Points against</th> <th>Result</th> </strong> </tr> <tr> <?php foreach($countymatch as $match): ?> <td><?php echo "<a href='$match->url'> $match->title - {$match->title}</a>"; ?></td> <td><?php if ($match->home_away->title = 'Home'){ echo $match->home_match_venue("<a href='{url}'>{title}</a>"); } else{ echo $match->away_match_venue; }; ?></td> <td><?php echo $match->cheshire_points; ?></td> <td><?php echo $match->opponent_points; ?></td> <td><?php echo $match->match_result->title; ?></td> </tr> <?php endforeach; ?> </table> All I get when I run this is the header row. I know the contents of each column work as I've run these outside of a table. Any suggestions would be much appreciated Thanks Jerry Link to comment Share on other sites More sharing options...
da² Posted December 27, 2023 Share Posted December 27, 2023 Hello, First you should start from a valid table skeleton: https://developer.mozilla.org/fr/docs/Web/HTML/Element/table Then your loop is not creating rows but columns, <tr> is a row and should have only 5 items in your case, but you're adding in a single row as much <td> as you have matches. Also you need a "==" here: if ($match->home_away->title = 'Home') Link to comment Share on other sites More sharing options...
JerryDi Posted December 27, 2023 Author Share Posted December 27, 2023 Thanks. I have checked and my table HTML is valid. So the remaining problem is the foreach loop. What I am doing is retrieving the page for matches played in the year. I then want to have rows displaying opponents, venue, points for, points against and result. All of these fields are in the same template. Could you give me some guidance on how to construct the correct loop? many thanks Jerry Link to comment Share on other sites More sharing options...
da² Posted December 27, 2023 Share Posted December 27, 2023 Look closely what the loop is creating, something is missing. ? <tr> <?php foreach($countymatch as $match): ?> <td></td> </tr> <?php endforeach; ?> You should indent your code to make it more readable and easier to debug. For example this is way easier to understand at first look (I didn't test it): <?php $countymatch = $pages->find("template=county-match-result, year={$page->id}"); ?> <table> <thead> <tr> <th>Opponents</th> <th>Venue</th> <th>Points for</th> <th>Points against</th> <th>Result</th> </tr> </thead> <tbody> <?php foreach($countymatch as $match): ?> <tr> <td><a href="<?= $match->url ?>"><?= $match->title ?></a></td> <td><?php if ($match->home_away->title == 'Home') echo "<a href='{$match->home_match_venue->url}'>{$match->home_match_venue->title}</a>"; else echo $match->away_match_venue; ?></td> <td><?= $match->cheshire_points; ?></td> <td><?= $match->opponent_points; ?></td> <td><?= $match->match_result->title; ?></td> </tr> <?php endforeach; ?> </tbody> </table> Link to comment Share on other sites More sharing options...
JerryDi Posted December 29, 2023 Author Share Posted December 29, 2023 Hi, do I need to specify the fields required? Apologies, I am a php novice so would appreciate a pointer on what to do here Many thanks J Link to comment Share on other sites More sharing options...
da² Posted December 29, 2023 Share Posted December 29, 2023 1 hour ago, JerryDi said: do I need to specify the fields required? It depends on your needs. Link to comment Share on other sites More sharing options...
JerryDi Posted December 29, 2023 Author Share Posted December 29, 2023 It's simply a list of matches played in a given year, showing the fields you can see inside the table tags. The number of matches played in a year will vary. Link to comment Share on other sites More sharing options...
JerryDi Posted January 8 Author Share Posted January 8 I've made some progress with this. It now renders one line but there should be 9 rows: This is my code: <?php $countymatch = $pages->find("template=county-match-result, year={$page->id} "); ?> <tbody> <tr> <?php foreach($countymatch as $match) ?> <td><?php echo "<a href='$match->url'> $match->title - {$match->title}</a>"; ?></td> <td> <?php if ($match->home_away->title == 'Home'){ echo $match->home_match_venue->title("<a href='{url}'>{title}</a>"); } else{ echo $match->away_match_venue; }; ?> </td> <td><?php echo $match->cheshire_points; ?></td> <td><?php echo $match->opponent_points; ?></td> <td><?php echo $match->match_result->title; ?></td> </tr> </tbody> </table> I've tried various other ways to do this with no success. I'd very much appreciate some direction. Thanks Link to comment Share on other sites More sharing options...
Klenkes Posted January 8 Share Posted January 8 You have to put the <tr> inside the foreach loop! Try this: <?php $countymatch = $pages->find("template=county-match-result, year={$page->id} "); ?> <tbody> <?php foreach($countymatch as $match){ ?> <tr> <td><?php echo "<a href='$match->url'> $match->title - {$match->title}</a>"; ?></td> <td> <?php if ($match->home_away->title == 'Home'){ echo $match->home_match_venue->title("<a href='{url}'>{title}</a>"); } else{ echo $match->away_match_venue; }; ?> </td> <td><?php echo $match->cheshire_points; ?></td> <td><?php echo $match->opponent_points; ?></td> <td><?php echo $match->match_result->title; ?></td> </tr> <?php } ?> </tbody> </table> Link to comment Share on other sites More sharing options...
JerryDi Posted January 9 Author Share Posted January 9 Thanks for your suggestion. I have tried this but it does not return any data in the table. Is there a different way to do this [my php knowledge is minimal]? many thanks Link to comment Share on other sites More sharing options...
Klenkes Posted January 9 Share Posted January 9 I assume your are not familiar with TracyDebugger? Lets check how many matches your selector returns: <?php $countymatch = $pages->find("template=county-match-result, year={$page->id} "); echo '<caption>'.count($countymatch).'</caption'; ?> What number does this count show? And.... are you sure that the current watched page with its ID is a value in the field year? Because you ask for example a 1284 in a field named year? Link to comment Share on other sites More sharing options...
Klenkes Posted January 9 Share Posted January 9 Better would be: <?php $countymatch = $pages->find("template=county-match-result, year=$page"); echo '<caption>'.count($countymatch).'</caption'; ?> Neither the curly brackets nor the ->id are necessary. Link to comment Share on other sites More sharing options...
JerryDi Posted January 9 Author Share Posted January 9 Thank you. The counter returned a zero, despite me seeing one row in the table. I tested it on various viewed pages and I also get a single row returned on these DA2 had said that there is something missing in the loop, but I just don't know what may be. Link to comment Share on other sites More sharing options...
Klenkes Posted January 9 Share Posted January 9 Is the only row returned(2023 Yorkshire) by any chance data from the current viewed page? Perhaps your $match gets overwritten by the current page? If count($countymatch) is 0, then your selector is wrong. But hard to tell for anyone else... Link to comment Share on other sites More sharing options...
Gideon So Posted January 10 Share Posted January 10 Hi @JerryDi On 1/9/2024 at 2:53 AM, JerryDi said: $countymatch = $pages->find("template=county-match-result, year={$page->id} "); Is the field "year" a page reference field? If so, how do you set up the field? Gideon Link to comment Share on other sites More sharing options...
Klenkes Posted January 10 Share Posted January 10 @JerryDi What happens if you ask directly for the matches of 2023: $countymatch = $pages->find("template=county-match-result, year=2023"); Link to comment Share on other sites More sharing options...
JerryDi Posted January 10 Author Share Posted January 10 Thank you for your replies. The Year field is not a page reference, it is a simple text field. I get the same result on any year I choose to look at ie just one row is returned. Changing the find to year=2023 produces the same result ie a single row. If I use the same find and output the page titles to a list it works fine and delivers all the matches played in a given year. So my logic tells me it must be something to do with the foreach loop, but I really don't know what the issue is. Any thoughts would be much appreciated Thanks again Jerry Link to comment Share on other sites More sharing options...
da² Posted January 11 Share Posted January 11 On 1/10/2024 at 10:48 AM, JerryDi said: If I use the same find and output the page titles to a list it works fine and delivers all the matches played in a given year. So my logic tells me it must be something to do with the foreach loop, but I really don't know what the issue is. Any thoughts would be much appreciated You should show the code that's working with a list. Link to comment Share on other sites More sharing options...
bernhard Posted January 11 Share Posted January 11 3 hours ago, ColtNikolaus said: Replying for the updates. FYI: You can also "follow" a topic on the top right and then choose when to get notifications about new posts, eg asap or once a day. ? You can even subscribe to whole categories. 1 Link to comment Share on other sites More sharing options...
da² Posted January 11 Share Posted January 11 Yes, and replying to a topic doesn't register you to this topic, you always need to register manually. ? Link to comment Share on other sites More sharing options...
JerryDi Posted January 11 Author Share Posted January 11 5 hours ago, ColtNikolaus said: Replying for the updates. This is my code that attempts to output results to a table: <table border=1> <thead> <tr> <th>Opponents</th> <th>Venue</th> <th>Points for</th> <th>Points against</th> <th>Result</th> </tr> </thead> <?php $countymatch = $pages->find("template=county-match-result, year={$page->title} "); ?> <tbody> <tr> <?php foreach($countymatch as $match) ?> <td><?php echo "<a href='$match->url'> {$match->title}</a>"; ?></td> <td> <?php if ($match->home_away->title == 'Home'){ echo $match->home_match_venue->title("<a href='{url}'>{title}</a>"); } else{ echo $match->away_match_venue; }; ?> </td> <td><?php echo $match->cheshire_points; ?></td> <td><?php echo $match->opponent_points; ?></td> <td><?php echo $match->match_result->title; ?></td> </tr> </tbody> </table> and this is the code which I used to output to a list. This delivers all 11 matches perfectly, whereas I only get one with the table code <?php $countymatch = $pages->find("template=county-match-result, year={$page->title}"); foreach($countymatch as $match) { echo "<li><a href='$match->url'> $match->title</a></li>"; } ?> Link to comment Share on other sites More sharing options...
da² Posted January 11 Share Posted January 11 Looks like your foreach is wrong, there's not even a "{" or ":" in the opening and I don't see end of loop. ? Could you use the same syntax as in my message? <?php foreach($countymatch as $match): ?> <?php endforeach; ?> Link to comment Share on other sites More sharing options...
JerryDi Posted January 11 Author Share Posted January 11 I've used your syntax but it's produced error messages again: Notice: Undefined variable: countymatch in /home/w5j4zigjq498/public_html/processwire/site/templates/year-summary.php on line 176 Warning: Invalid argument supplied for foreach() in /home/w5j4zigjq498/public_html/processwire/site/templates/year-summary.php on line 176 line 176 is: <?php foreach($countymatch as $match): ?> Link to comment Share on other sites More sharing options...
da² Posted January 11 Share Posted January 11 Be sure that the variable is defined before the loop. Link to comment Share on other sites More sharing options...
Gideon So Posted January 12 Share Posted January 12 Hi @JerryDi, Try the follow code: <table border=1> <thead> <tr> <th>Opponents</th> <th>Venue</th> <th>Points for</th> <th>Points against</th> <th>Result</th> </tr> </thead> <tbody> <?php $countymatch = $pages->find("template=county-match-result, year={$page->title} "); foreach($countymatch as $match): ?> <tr> <td><?php echo "<a href='$match->url'> {$match->title}</a>"; ?></td> <td> <?php if ($match->home_away->title == 'Home'){ //echo $match->home_match_venue->title("<a href='{url}'>{title}</a>"); // I don't really understand this line, so I comment it and see if you get more results } else{ echo $match->away_match_venue; }; ?> </td> <td><?php echo $match->cheshire_points; ?></td> <td><?php echo $match->opponent_points; ?></td> <td><?php echo $match->match_result->title; ?></td> </tr> <?php endforeach; ?> </tbody> </table> Gideon 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