Welcome! Log In Create A New Profile

Advanced

Task 2C

Posted by Johan Potgieter 
Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
Task 2C
March 11, 2007 04:02PM
Completed task. Like to use functions, it is useful if a feature is required in multiple places in your script, no repeating of code smiling smiley
Re: Task 2C
March 12, 2007 08:49PM
Completed this Task.

Would have been kewl to cover PHP Objects in this course as well.
avatar Re: Task 2C
March 13, 2007 05:32AM
Done

Functions Rock!!!


Cheers

A Successful Nature Inspires Other People...
Re: Task 2C
March 13, 2007 08:06PM
Hello,

Some help please, my function PHP looks like this:

<html>
<head><title>Calculating the purchase price</title></head>
<body>
<?php

$total =$_POST['function'];
$price =$_POST['price'];
$items =$_POST['items'];

function total ($price,$items)
{
$cost = $price * $items;
return $cost;
}
echo "The Total Price Of Your Purcahse Is R" . (total($price,$items));
?>

</body>
</html>

It works and returns the right values but i think by putting in the following lines:

$total =$_POST['function'];
$price =$_POST['price'];
$items =$_POST['items'];

I bypass the function idea, cause i delcare the var. I'm i correct in saying this?

If i don't have those 3 lines then my function dosen't work and in any way how does the html page know where to put the values that we feed it?

If this is correct then it is completed otherwise back to the drawing board.

Thanx
Nino Cloete
Re: Task 2C
March 13, 2007 10:50PM
Hi Nino,

PHP automatically creates the variables for price and items. Theyre the same as the fields on the form in cost.html.The form uses GET method, so using $_POST won't work to get those values. Not sure how you got the right values from the code you pasted, as I couldn't replicate same smile The values for price and items are passed onto function.php where the variables are assigned those values automatically by PHP.

Hope this helps you.
Re: Task 2C
March 14, 2007 12:00AM
It took me a little longer than it should have to realise that $cost was not passsed from cost.html to functions.php....

Task completed.
avatar
Mac
Re: Task 2C
March 14, 2007 07:30AM
It's a good idea to always test echo your variables on the top of the page they're being sent to. It will save you time looking for errors that aren't there...
Re: Task 2C
March 15, 2007 01:12PM
Completed smiling smiley
Re: Task 2C
March 15, 2007 05:43PM
Completed. thumbs up smiley
Re: Task 2C
March 17, 2007 07:56PM
This is what I did and it worked fine. Task completed correctly I hope smile

<html>
<head><title>Calculating the purchase price</title></head>
<body>
<!--INSERT CODE HERE-->
<?php
@extract($_GET);
function total ($price, $items)
{
$cost = $price * $items;
return $cost;
}
echo "The total cost of your purchase is $" . (total ($price,$items));
echo ".";
?>



</body>
</html>
Re: Task 2C
March 18, 2007 11:18AM
completed
Re: Task 2C
March 25, 2007 07:20PM
Completed

Changed to post to a single form again ! Also added in a few field checks to make sure data has been put in.

Prefer to use $_GET over $_POST so i can see the variables in the URL - but echo on the top of the form works too.

<?php
function total($price, $items)
{
$cost = $price * $items;
return $cost;
}

isset($_GET['price']) ? $price = ($_GET['price']) : $price = "";
isset($_GET['items']) ? $items = ($_GET['items']) : $items = "";

echo "
<html>
<head></head>
<body>
<form method='GET' action='function.php'>
How much does the book cost?
<input name='price' TYPE='text'>
<br>
How many copies of the book are you purchasing?
<input name='items' TYPE='text'>
<br>
<br>
<input type=submit value='Submit your request'>
</form>
</body>
</html>
";

if (!empty($price))
{
if (!empty($items))
{
echo "The total price of your purchase is R" . total($price,$items);
echo ".";
}
else
{
echo "Note :- No Items Selected";
}
}
else
{
echo "Note :- No Price input";
}

?>

Cheers !!
Re: Task 2C
March 25, 2007 09:27PM
$price=$_GET['price']; will be more than sufficient instead of isset($_GET['price']) ? $price = ($_GET['price']) : $price = "";

www.newshost.za.net
Re: Task 2C
March 26, 2007 05:51PM
Since the page posts to itself , on initial load the 'price' session variable would not be defined, so my bit of code initializes the variable and gives it a default value.

Well that was my thinking anyway !! Comments . .
avatar
Mac
Re: Task 2C
March 26, 2007 06:44PM
Keep it simple.....@extract($_POST) ..... That's it. Trust me, the more intense the coding, the shorter the shortcuts you'll be looking forsmiling smiley

And read (or search) all the previous semester/s forum posts as well - I'm not going to repeat myself year in and year out smiling smiley You have access to a a library of information on this course!
Re: Task 2C
April 16, 2007 08:42PM
Completed.
Re: Task 2C
April 24, 2007 11:11AM
completed
Re: Task 2C
April 28, 2007 11:40AM
Completed

Cheers
Nino Cloete
Sorry, only registered users may post in this forum.

Click here to login