4 Replies - 1497 Views - Last Post: 21 July 2012 - 03:43 PM Rate Topic: -----

#1 ssssunny  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 10
  • Joined: 17-July 12

how can i put php tag inside php tag?

Posted 21 July 2012 - 08:17 AM

<a href="javascript:;" onclick="doAction('<?php echo $postid;?>','like');">Like (<span id="<?php echo $postid;?>_likes"><?php echo $data->like;?></span>)</a>
		<a href="javascript:;" onclick="doAction('<?php echo $postid;?>','unlike');">Unlike (<span id="<?php echo $postid;?>_unlikes"><?php echo $data->unlike;?></span>)</a>


this script contain php tag and i want to put this script where there is already php tag how can i do this??

Is This A Good Question/Topic? 0
  • +

Replies To: how can i put php tag inside php tag?

#2 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1002
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: how can i put php tag inside php tag?

Posted 21 July 2012 - 09:09 AM

Okay the first thread was cute, but this is ridiculous - Learn PHP.

For when you do learn some more PHP, your options are (not exhaustive):
Break out of PHP for when you insert this code.
Put the code into a string and echo it out.
Use code separation to alleviate you from doing this altogether.

This post has been edited by RudiVisser: 21 July 2012 - 09:10 AM

Was This Post Helpful? 3
  • +
  • -

#3 Slice  Icon User is offline

  • sudo pacman -S moneyz


Reputation: 241
  • View blog
  • Posts: 700
  • Joined: 24-November 08

Re: how can i put php tag inside php tag?

Posted 21 July 2012 - 11:52 AM

Yo dawg, I heard you liked php, so we put some php in your php. Now you can php while you php!
Was This Post Helpful? 3
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,595
  • Joined: 16-October 07

Re: how can i put php tag inside php tag?

Posted 21 July 2012 - 12:24 PM

Depending on your php, you might need an explicit "php?>" on the close tag. However, with what you have, I'd just give up on the mixed markup.

Indeed, it's so convoluted you'd almost certainly benefit from some more white space. e.g.
	echo '<a href="javascript:;"'
	. ' onclick="'
	. "doAction('" . $postid . "','like');"
	. '">'
	. 'Like ('
	. '<span id="' . $postid .'_likes">'
	. $data->like;
	. '</span>)'
	. '</a>';
// you do the next one



A nice little function you could call would be nice.

I'm dubious of that href.

Hope this helps.
Was This Post Helpful? 1
  • +
  • -

#5 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3712
  • View blog
  • Posts: 5,964
  • Joined: 08-June 10

Re: how can i put php tag inside php tag?

Posted 21 July 2012 - 03:43 PM

View Postbaavgai, on 21 July 2012 - 07:24 PM, said:

Indeed, it's so convoluted you'd almost certainly benefit from some more white space. e.g.

I'd prefer using the heredoc syntax in that situation though.
echo <<<HTML
	<a href="javascript:;" onclick="doAction('{$postid}','like');">
		Like (<span id="{$postid}_likes">{$data->like}</span>)
	</a>
HTML;


Somehow looks cleaner.

View Postbaavgai, on 21 July 2012 - 07:24 PM, said:

I'm dubious of that href.

I'd say it's fine as it is. At least in that code. You'd typically see: href="javascript: void(0);", so that it would execute no statements, but leaving it at href="javascript:;" does basically the same thing. Both statements end up doing nothing at all.

Of course, these days neither option is ideal. You'd want to avoid the onclick attribute, and the dummy href, altogether and move the whole thing into Javascript code, preferably in a reusable way that degrades gracefully. Something more like: (Not taking into account old browser compatibility issues)
<script type="text/javascript">
window.addEventListener("DOMContentLoaded", function() {
	var elems = document.querySelectorAll("a.actionLink");
	if (elems && elems.length > 0) {
		var setLinkClickAction = function(link) {
			var hrefParts = link.getAttribute("href").split("/");
			if (hrefParts.length == 3) {
				link.addEventListener("click", function(e) {
					e.preventDefault();
					doAction(parseInt(hrefParts[2]), hrefParts[1]);
				}, false);
			}
		}
		for (var i = 0; i < elems.length; ++i) {
			setLinkClickAction(elems[i]);
		}
	}
}, false);
</script>

<a class="actionLink" href="doAction/like/{$postID}">Like</a>


This way it's reusable, better separated, and the link is usable for browsers without proper Javascript support. (You'd have to create the doAction/link/### page for that last part, of course.)

... But I'm thinking that may be a discussion better left for another time.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1