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 Marvin
      Hello, i want to ask, i maintain a website that using a processwire and php, and i want to make an archive at my website using a subfolder system, but when i try,
      the sebfolder is show but when i click the files in that subfolder not show, and my browser just show me an error Invalid argument supplied for foreach(), i don't know why it error
      Here i attach my code and my screenshoot website :
      This is my code

      This is result of my website

      This is my error

       
       
    • By Mithlesh
      Hi there,
      My form is not getting submitted, it is showing:
      Unable to verify successful email delivery of this form submission.
      Attaching for your reference as well: 

      In the Backend, it is showing Connection timed out with smtp.gmail.com
      Pl guide me how to resolve that
    • By CareerTeam GmbH
      Hi there,
      We are an executive search agency based in Germany looking for a freelancer (2-5 days per week) supporting us with the development and design of our websites. The position will be located in Hamburg, Germany and it would be great if you are on short call. German language knowledge is mandatory. 
      You can reach me via email jobs@careerteam.de.
      Thank you!
      Regards
      Annemie
    • By louisstephens
      I was really unsure of how to actually title this post, so I do apologize (if someone has a better idea, I will gladly edit it). I am using the profields: pagetable field to allow people to create their own "content" (copy, image, button, etc etc) and rearrange it. I also included a field called "column_size" using the RangeSlider set to (1-12).
      I guess I'll clarify a bit more on this. I am using flexbox where the "row" is <section></section> and the columns are <div class="column"></div> have given the "columns"  flex: 1 1 0; so no matter how many columns you have, the columns will automatically adjust for new content. Where my confusion is coming in: If a user has set up 3 copy items (with 12, 5, 7 respectfully for the column_size), how do I actually output this in my template? I was going to use a switch statement to handle the various items which I thought made it quite easy, but with closing sections and columns I have confused myself as I assume I need an if statement to check if the column size is > 12, or = 12 to determine the actual closing/opening of sections. I apologize if I have not made this very clear. I am a bit unsure how to word this let alone to go about this. 
      Im very appreciative of for any insight into this.
       
       
    • By louisstephens
      So I have been hard at work creating url segments for a template (api) and everything is working swimmingly in creating a simple end point for svelte.js. I have however, run into a few questions that I can wrap my head around.
      In my api template I have:
      if($input->urlSegment1 === 'clients') { header('Content-Type: application/json'); $clients = $pages->find("template=clients"); $client_array = array(); foreach ($clients as $client) { $id = $client->id; $title = $client->title; $url = $client->url; $clientName = $client->client_name; $clientColor = $client->client_color->value; $assigned = $client->assigned_to->user_full_name; $client_array[] = array( 'id' => $id, 'code' => $title, 'name' => $clientName, 'associated_users' => $assigned, 'url' => $url ); } $client_json = json_encode($client_array, true); echo $client_json; } The output json from this is:
      [ { "id":1644, "code":"abc", "name":"Test Name", "associated_users":null, "url":"\/pw\/clients\/abc\/" }, { "id": 1645, "code": "xyz", "name": "Test Name", "associated_users": null, "url": "\/pw\/clients\/xyz\/" }, ] I was curious is it possible to add in "clients" before this output json so it would appear as 
      clients: [ { "id":1644, "code":"abc", "name":"Test Name", "associated_users":null, "url":"\/pw\/clients\/abc\/" }, { "id": 1645, "code": "xyz", "name": "Test Name", "associated_users": null, "url": "\/pw\/clients\/xyz\/" }, ] I was not really sure of how to tackle this in my php code, and have spent more time than I care to admit trying to figure it out. Another question I have is that "associated_users" is returning null, which in this instance is correct. It is a multi page field that is set to pull a custom name field from the users template, ie "Louis Stephens" would be associated with the first page. I understand that I need to use a foreach to get the correct data, but I was really unsure of how to place this inside an array, or update the array with the new data. Any help with any of this would greatly be appreciated.
×
×
  • Create New...