Jump to content

Need assistance in PHP task


voflansas
 Share

Recommended Posts

EDIT: It would seem i placed this in the wrong section. Can we have a mod move this? :)

I'm gonna go ahead and admit that this is a task that my school left me for my course in PHP and mySQL but i won't just ask for someone to construct the code for me but just to give me advice and assistance in my code.

What i'm trying to do is request a number from a user and generate random integers between 1 to 100 as many as the user specified.

The task told me to place all the numbers in an array and that i can use a foreach loop to go through and indicate what numbers are lower than 50 and what is higher than 50.

 

The output should be similar to this:

 

All your numbers:

45

23

14

79

99

22

56

 

Numbers lower than 50:

45

23

14

22

 

Numbers higher than 50:

79

99

56

 

 

This is my code:

<!DOCTYPE html PUBLIC "-//w3c//DTD XHTMLm 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmnlns="http://www.w3.org/1999/xhtml" xml:lang="sv" lang="sv">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset="utf-8" />
	<title>Task</title>		
</head>

<body>


<?php
$Nummer=$_REQUEST['nummer'];
 for ($count=1;$count <= $Nummer; $count++){
$testSlump = rand(1, 100);
echo $testSlump . "</br>";
	}

$testArray = array($testSlump);
foreach ($testArray as $n) {
if ($testArray > 50){
echo $testArray . echo 'this number is lesser than 50';
}
}
?>

</body>
</html>

 

 

 

I've googled, read through the litterature but none seem to help me. I thought to myself that one solution might be placing all values generated in a for loop in an array and then using the foreach loop to confirm what is what. Class notes and our schools PHP help site doesnt have anything that would help me finish this task.

 

How do you place for loop values in an array? (if i'm on the right track)

 

Any help etc is greatly appreciated.

Link to comment
Share on other sites

Save this as index.php. :D

 

<!DOCTYPE html PUBLIC "-//w3c//DTD XHTMLm 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmnlns="http://www.w3.org/1999/xhtml" xml:lang="sv" lang="sv">
<head>
<meta http-equiv="Content-Type" content="text/html; charset="utf-8" />
<title>Task</title>
</head>
<body>

<form method="post" action="index.php">
Number: <input type="text" name="number" />
<input type="submit" name="submit" value="Submit" />
</form>
<br /><hr /><br />

<?php

$number = $_POST['number'];

if(isset($_POST['submit'])) {
echo "All of your numbers:<br />";
for($count = 1; $count <= $number; $count++) {
	$randNumber = rand(1, 100);
	$allNumbers[] = $randNumber;
	echo $randNumber."<br />";
}

foreach($allNumbers as $n) {
	if($n > 50) {
		$overFifty[] = $n;
	} else {
		$underFifty[] = $n;
	}
}

echo "<br />Numbers under (or equal to) 50:<br />";
foreach($underFifty as $n) {
	echo $n."<br />";
}

echo "<br />Numbers over 50:<br />";
foreach($overFifty as $n) {
	echo $n."<br />";
}
}

?>

</body>
</html>

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.