Jump to content
n0sleeves

Can someone explain PHP's <<<EOT syntax without referring me to the PHP manual?

Recommended Posts

I'm following the FAQ post here / trying to create password protected page. The code they supplied was this: 

 

<?php

if ($_POST['awesome_password'] != $page->page_password) {

echo <<<EOT
<h1>Login</h1>
    <form name='form' method='post' action='$page->url'>
    <input type='password' title='Enter your password' name='awesome_password' />
    <br />
    <input type='submit' name='Submit' value='Login' />
	</form>
EOT;
} else {
echo "<p>This is the protected page. Content goes here.</p>";
}

?> 

That works beautifully. However, I want to spit out more than just a simple ECHO in the else statement. In fact, I want some pretty simple Html mixed with php.  Now I tried modifying the original code supplied by adding another <<< thinking that it would work but of course it didn't :P

else {
echo <<<EOD

	
	<!--Begin Table For Catalog Data -->
	<table id="catalog" class="display" cellspacing="0" width="100%">
		<thead>
	    	<tr>
		        <th>Artist</th>
		        <th>Track Name</th>
		        <th>Album</th>
		        <th>Length</th>
		        <th>Songwriter(s)</th>
		        <th>Genre</th>
		        <th><span data-tooltip class="has-tip" title="Displays the following mood / emotion / characteristic">Traits</span></th>
		        <th><span data-tooltip class="has-tip" title="All songs are originals unless designated cover (letter C)">Type</span></th>
		        <th></th>
		    </tr>
		</thead>

		<tbody>

		<?php //Table Loops
		$tracks = $pages->find("has_parent=/artists/, template=track");

		if($tracks) {
		  foreach($tracks as $track) {
		  	echo "<tr>";
		  	echo "<td id='artistName'>" . $track->parent->parent->title . "</td>"; // artist name
		    echo "<td id='trackTitle'>" . $track->title . "</td>";
		    echo "<td id='albumTitle'>" . $track->parent->title . "</td>"; // this is the album name
		    echo "<td id='trackLength'>" . $track->track_length . "</td>"; // track length
		    echo "<td>" . $track->songwriter . "</td>"; // track songwriter

		    echo "<td id='genres'>";
		    foreach ($track->genres as $genre) {
		    	echo $genre->title . "/";
		    };
		    echo "</td>";

		    echo "<td id='traits'>";
		    foreach ($track->moods as $mood) {
		    	echo $mood->title . "/ ";
		    }; 
		    echo "</td>";

		    echo "<td>";
		    // determin cover song or not
		    if($track->song_type != null) {
		    	echo '<img id="logo" src="' . $config->urls->templates . 'img/letterC.png">';
		    } else {
		    	echo "";
		    }; //end determin cover
		    echo "</td>";

		    echo "<td>" . "<a href=' " . $track->song_file->url . " ' class='sm2_button'>Play</a>" ."</td>";
		    echo "</tr>";
		    		
		  }
		}
		?> 

    </tbody>
	</table>
	<!-- End Catalog Table -->

	<!-- Initialize DataTable -->
	<script> 
	$(document).ready(function () {
    	$('#catalog').DataTable();
	} );
	</script>

EOD;
}
?> 

I have never worked with <<< Heredoc syntax before, Apparently you can't put whatever you want between it? No? 

Share this post


Link to post
Share on other sites

EOT is only to echo large texts within PHP vars i think....so try this untested...

else {
//ending PHP part start HTML
?> 

	
	<!--Begin Table For Catalog Data -->
	<table id="catalog" class="display" cellspacing="0" width="100%">
		<thead>
	    	<tr>
		        <th>Artist</th>
		        <th>Track Name</th>
		        <th>Album</th>
		        <th>Length</th>
		        <th>Songwriter(s)</th>
		        <th>Genre</th>
		        <th><span data-tooltip class="has-tip" title="Displays the following mood / emotion / characteristic">Traits</span></th>
		        <th><span data-tooltip class="has-tip" title="All songs are originals unless designated cover (letter C)">Type</span></th>
		        <th></th>
		    </tr>
		</thead>

		<tbody>

		<?php //Table Loops   --starts PHP is correct for the loop for your tracks
		$tracks = $pages->find("has_parent=/artists/, template=track");

		if($tracks) {
		  foreach($tracks as $track) {
		  	echo "<tr>";
		  	echo "<td id='artistName'>" . $track->parent->parent->title . "</td>"; // artist name
		    echo "<td id='trackTitle'>" . $track->title . "</td>";
		    echo "<td id='albumTitle'>" . $track->parent->title . "</td>"; // this is the album name
		    echo "<td id='trackLength'>" . $track->track_length . "</td>"; // track length
		    echo "<td>" . $track->songwriter . "</td>"; // track songwriter

		    echo "<td id='genres'>";
		    foreach ($track->genres as $genre) {
		    	echo $genre->title . "/";
		    };
		    echo "</td>";

		    echo "<td id='traits'>";
		    foreach ($track->moods as $mood) {
		    	echo $mood->title . "/ ";
		    }; 
		    echo "</td>";

		    echo "<td>";
		    // determin cover song or not
		    if($track->song_type != null) {
		    	echo '<img id="logo" src="' . $config->urls->templates . 'img/letterC.png">';
		    } else {
		    	echo "";
		    }; //end determin cover
		    echo "</td>";

		    echo "<td>" . "<a href=' " . $track->song_file->url . " ' class='sm2_button'>Play</a>" ."</td>";
		    echo "</tr>";
		    		
		  }
		}
                // ends PHP part again is correct from your code
		?>  

    </tbody>
	</table>
	<!-- End Catalog Table -->

	<!-- Initialize DataTable -->
	<script> 
	$(document).ready(function () {
    	$('#catalog').DataTable();
	} );
	</script>

<?php //start PHP for last closing brace for the if/else contruct
// no need for EOD;
}
?> 

But i'am no real PHP Coder....so be aware this is only a hint and may someone else could do this more fancy ;)

Best Regards

Share this post


Link to post
Share on other sites

Mr-fan, I am now a fan because you are the man! It seemed too easy and too good to be true, but that totally worked!!! Thank you!! 
How come I have never seen this method mentioned in the books I am reading is beyond me  ???  Maybe reading the wrong books? lol.

Is there a name for this?

  • Like 1

Share this post


Link to post
Share on other sites

Look up "Php heredoc" on Google and you will find everything you need on the subject :)

The first result in the list at Php.net explains it pretty well.

  • Like 1

Share this post


Link to post
Share on other sites

Not sure about EOT (so much so that I removed it from the example above) but I'm using the example to echo out a mix of HTML and PHP with loops, etc. I'll post what I've got in the original thread when I get chance if that's of any use?

  • Like 1

Share this post


Link to post
Share on other sites

I think your problem was a simple typo, it has to be EOT, not EOD

echo <<<EOT
[...]
EOT;

Share this post


Link to post
Share on other sites

I think your problem was a simple typo, it has to be EOT, not EOD

echo <<<EOT
[...]
EOT;

Not really. Identifier in can be any valid label. This, for an example, is entirely valid:

<?php
echo <<<TEPPO
Hello world.
TEPPO;
  • Like 1

Share this post


Link to post
Share on other sites

I think your problem was a simple typo, it has to be EOT, not EOD

The most common typo is any white-space character before the closing label or after the semicolon.

// This fails, note the space before EOS;
echo <<<EOS
Hello world.
 EOS;

Share this post


Link to post
Share on other sites

Good to know, I never really used it, but I can't remember seeing something different than EOT. 

Share this post


Link to post
Share on other sites
but that totally worked!!! Thank you!!

i thought it's solved....

...EOT or what ever is named is only for large texts within PHP vars without loops or something else...but may i'm wrong since i'm no pro.

kind regards mr-fan

Share this post


Link to post
Share on other sites

Oh wow, someone obviously took my post the wrong way and changed the title of this topic.

Plenty of good examples over on the PHP site with explanations of the functionality so please anyone don't be surprised or offended if anyone here refers you to docs or other sites sometimes or we would repeat ourselves and duplicate content found elsewhere.

  • Like 1

Share this post


Link to post
Share on other sites

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By anderson
      Hi,
      For the purpose of learning, as shown in this photo, I created a repeater field "we", then a template, then a page.
      But in /templates/testrepeater.php, I has some problem, the "foreach" part does not work as expected.
      <html> <body> <?php echo "<h1>$page->title</h1><br>"; ?> <? foreach($page->we as $member); ?> <img src="<?php echo $member->images->url; ?>" alt=""> <?php echo $member->wemember; ?><br> <? endforeach;?> </body> </html> Could anybody please help point out my error?
      Thanks in advance.

    • By gregory
      Hi guys, I need help.
      how do I translate Next Page?
      <?php if($page->next->id) {echo "<div class='float-right'><a class='button' href='{$page->next->url}'> Next Page </a></div>";} ?> I usually use this:
      <?php $lang = $user->language->name; if($lang == 'default') {echo "Next Page";} else {echo "Pagina successiva";} ?> or
      <?php echo __("Next Page"); ?> Thanks
    • By nuel
      Hi there
      Basically I want to call code within a ProcessWire page that isn't used as a template. Example: www.mypwpage.com/myphpfile.php
      I have a working PW Website with a couple of pages like /artists, /releases, /videos etc. Now I need a page /download without any editable fields in the backend, just calling some PHP code (that was coded by another guy) containing a form that checks unique download-codes in a second database and starts the download of the desired file. The script is working fine right now as part of a static website, but since I built PW behind the site, this independent «Download Section» of the page doesn't work anymore.
      Right now I have the main file download.php as a page template on a newly created empty page called /download, so until now the form is working (wow). After sending the form containing the download-code, the file check_code.php in a subfolder /site/templates/download is called and that's where I get an error.
      Any help?
    • By celfred
      Hello,
      Another newbie question. My Processwire sites is growing and I'm wondering if my way of doing things sounds goog to you. I tend to avoid what I consider 'heavy and frequent' database requests in my functions. For example :
      // In functions.php myfunction($player) { wire('$pages')->find("myselectors"); // This means about 300 database requests do something... (like $p->newTmpField = 1;) return $player; } // In template.php $allPlayers = $pages->find("template=player"); // About 300 players foreach ($allPlayers as $p) { myfunction($p); } // Now I have access to newTmpField, for example.  I tend to replace with :
      // In function.php myfunction($allPlayers) { foreach($allPlayers as $p) { // do something like set $p->newTmpField = 1 } return $allPlayers; } // In template.php $allPlayers = $pages->find("template=player"); myfunction($allPlayers); // Now $players have been modified. For example, I have $allPlayer->find("newTmpField=1"); So for the moment, I tend to choose the second solution which makes only 1 database request, but what do more experimented developers would recommend ?
      What I find useful with Page objects in functons arguments is that I have the whole 'tree' I can then 'find' in my function. But as you can see, I need to understand things better. I have a feeling my 'practical' explanation is not really convincing 🙂 
      Thanks if someone can take a few minutes to give me advice !
    • By rolisx
      Hi all!
      I have a little problem here. I want to sort a list of addresses by streetname and then by number. First, I just had a field "prod_objekt" (address) containing both and then have a foreach loop like this: $pages->get('/produzenten/')->prod_repeater->sort('prod_objekt') as $produzent. Unfortunately, this would not sort by streetnumber if I had the same streetname but multiple numbers.
      Now, I thought I could sort by two fields. prod_object (adress) first and then by prod_hausnummer (streetnumber). But I just don't know the code for it. I tried this: $pages->get('/produzenten/')->prod_repeater->sort('prod_objekt.prod_hausnummer') as $produzent, but it wouldn't work.
      Any ideas on how to solve this?
      Thanks for your help
      Roli
×
×
  • Create New...