Jump to content


HTML Help


3 replies to this topic

#1 Genrail

    Semi-Pro

  • Project Team
  • 234 posts
  • Projects: Private Map Contractor

Posted 10 April 2011 - 02:23

In HTML i need a random variable to be assigned and of that variable i want it to desplay a line of code i specify, kind-of like an IF-THEN command :duh: ex:

IF
A=1
Then
<html code here>

A=2
<html code here>

Kind of like that, i think its javascript but i havent learned that yet :cool: If you cant its ok.
Thanks in advance 8|

|8 ~Genrail~ 8|
This place still exists?

#2 Slightly Wonky Robob

    Not a Wonky Gent.

  • Administrator
  • 9333 posts

Posted 10 April 2011 - 08:12

HTML is a markup language, not a programming language... With the exception of a bit of IE specific styling trickery, it's not possible to use conditional statements.

Depending on exactly what you want to do you will either need to use a client-side script (i.e. javascript) or more likely, a server-side script (e.g. PHP)

If you do want to use PHP, the above example would be pretty easy to achieve:

<?php if&#40;$a == 1&#41;{ ?>
	<!-- HTML GOES HERE -->
<?php }elseif&#40;$a == 2&#41;{ ?>
	<!-- SOME OTHER HTML GOES HERE -->
<?php } ?>

Spoiler


If you tell me exactly what you want to achieve, then I might be able to help you further.

Edited by Bob, 10 April 2011 - 08:14.

Posted Image
F O R T H E N S
Posted Image

#3 Genrail

    Semi-Pro

  • Project Team
  • 234 posts
  • Projects: Private Map Contractor

Posted 10 April 2011 - 18:44

I really dont know what that code is anywhay :cool:

I am on the school techteam and i am the one incharge of maintaining the school server/website, (this means i get keys to the school server room. Yay! :duh: ) In my spare time i improve the site, and add new things. As we have been told we are the best team the school has seen, we have made a "game" of sorts. To challange new techteam members...


When the page opens it will display 1 of 5 things. That will change everytime you visit, thats all i need it to do really

I may need help with other things later 8|

Edited by Genrail, 10 April 2011 - 18:46.

This place still exists?

#4 Slightly Wonky Robob

    Not a Wonky Gent.

  • Administrator
  • 9333 posts

Posted 11 April 2011 - 18:27

Well a server side script would generally be the way to go about it... but you would need to make sure that PHP (or something else) is installed on the server.

You can try a simple test to see if is by putting this into a file, with extension .php
<?php phpinfo&#40;&#41;; ?>


If PHP is installed, you should get a bunch of information about it when you load the page. From what I understand, you want 1 of 5 pieces of HTML to be displayed, chosen at random? This can be achieved with a slightly modified version of the code I posted earlier:
<?php
	// Assign a random number to the variable &#39;a&#39; between 1 and 5
	$a = rand&#40;1, 5&#41;;

	// Determine which piece of HTML to display
	if&#40;$a == 1&#41;{ 
		?><!-- HTML CODE 1 --><?php
	}elseif&#40;$a == 2&#41;{
		?><!-- HTML CODE 2 --><?php
	}elseif&#40;$a == 3&#41;{
		?><!-- HTML CODE 3 --><?php
	}elseif&#40;$a == 4&#41;{
		?><!-- HTML CODE 4 --><?php
	}else{
		?><!-- HTML CODE 5 --><?php
	}
?>

NOTE: Lines starting with 2 slashes ( // ) are comments.

If you want / have to use javascript, you can do something to the effect of:
<script type=&#34;text/javascript&#34;>
	// Math.random&#40;&#41; picks a random number between 0 and 1
	// Times that number by 5, and round downwards to get a random number between 0 and 4
	var a = Math.floor&#40;Math.random&#40;&#41;*5&#41;;
	
	// Determine which piece of HTML to write to the body
	if&#40;a == 0&#41;{
		document.write&#40;&#39;HTML CODE 1&#39;&#41;;
	}else if&#40;a == 1&#41;{
		document.write&#40;&#39;HTML CODE 2&#39;&#41;;
	}else if&#40;a == 2&#41;{
		document.write&#40;&#39;HTML CODE 3&#39;&#41;;
	}else if&#40;a == 3&#41;{
		document.write&#40;&#39;HTML CODE 4&#39;&#41;;
	}else{
		document.write&#40;&#39;HTML CODE 5&#39;&#41;;
	}
</script>

Edited by Bob, 11 April 2011 - 18:28.

Posted Image
F O R T H E N S
Posted Image



1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users