Can you use PHP 'while' $count in Javascript?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Can you use PHP 'while' $count in Javascript?

Post by simonmlewis »

I have a page that uses Javascript to toggle Divs to reveal and hide.

This is what I am trying to write:

Code: Select all

<?php
$count = 0;
while ($count < 10)  {
$count = $count + 1;
echo "<script language=\"javascript\"> 
function toggle$count() {
	var ele = document.getElementById(\"toggleText$count\");
	var text = document.getElementById(\"displayText$count\");
	if(ele.style.display == \"block\") {
    		ele.style.display = \"none\";
		text.innerHTML = \"(view info) \";
  	}
	else {
		ele.style.display = \"block\";
		text.innerHTML = \"(close info) \";
	}
} 
</script>";
}
?>
As you can see, I'm trying to create it so the numbers appear in the Javascript.
I know JS is browser based, and PHP is client based, but can you use it in this way? If you can, how am I going wrong with this?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
jraede
Forum Contributor
Posts: 254
Joined: Tue Feb 16, 2010 5:39 pm

Re: Can you use PHP 'while' $count in Javascript?

Post by jraede »

Why don't you just use a while or for loop in JavaScript? No need for PHP here...
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Can you use PHP 'while' $count in Javascript?

Post by simonmlewis »

Sorry... how? I'm not a Javascript coder at all. But I need to have toggle1, toggle2 etc. Up to a set amount.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Can you use PHP 'while' $count in Javascript?

Post by pickle »

Correct me if I'm wrong, but it looks like you're using PHP to output a set number of elements, then you have a separate function for each of those elements that toggles it's display. What you're not showing is the code that is calling your toggle1(), toggle2(), etc functions. I can only guess you have each of the toggleX() functions being called when something is clicked.

If I'm right, then you could write 1 function that accepts 1 parameter, the "count". You can rewrite your function (specifically the document.getElementById() calls) to use the passed parameter in the id string. PHP would only be needed when outputing the elements, to give them the correct "count" ID, and when outputing the calling javascript, to include the correct "count" value.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply