Jump to content

Creating HTML table from single template


JerryDi
 Share

Recommended Posts

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

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

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

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

  • 2 weeks later...

I've made some progress with this. It now renders one line but there should be 9 rows:

Capture.JPG.e88c6ea4feed9681c259778f15a3fbab.JPG

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

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

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

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

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

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

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

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

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.

  • Like 1
Link to comment
Share on other sites

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

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...