Welcome! Log In Create A New Profile

Advanced

Topic 2B

Posted by Anonymous User 
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
Anonymous User
Topic 2B
January 16, 2006 08:21AM
This is the thread for problems/comments related to Topic 2B.
Re: Topic 2B
February 21, 2006 08:21AM
This topic is completed, no problems.
Re: Topic 2B
February 21, 2006 04:00PM
Well it seems Miekie is ahead of us all again
sorry but this is NOT working for me

as the output I get

The author of is . ie the arrayinformation is not being output, although the text is.

I am not getting any errors






my code is

<html>
<head><title>Who wrote that book?</title></head>
<body>

<?php
$books["Romeo and Juliet"] = "William Shakespear";
$books["Exobiology: An Introduction"] = "Jonathan Drake";
$books["War and Peace"] = "Leo Tolstory";
$books["Portrait of the Artist"] = "James Joyce";
$books["The Lion, The Witch, and the Wardrobe"] = "CS Lewis";

$author = $books[$SelectedBook];

echo "The author of $SelectedBook is $author.";

?>

</body>
</html>
Re: Topic 2B
February 21, 2006 09:13PM
Mac already gave us the solution: Remember action='POST' in the HTML file and the $var = $_POST['var']; in the .php file. Then you will always get the values. Look in thread Topic 2A.. winking smiley
Re: Topic 2B
February 22, 2006 10:38AM
Thank you Miekie

sorry about the blond moment

I am also done with this section.

I do not quite get the array thing though!!

why not simply place all the data into a database and live on from there?.

I do not see why I would need to hold so much data inside the front end.
avatar
Mac
Re: Topic 2B
February 22, 2006 10:59AM
Sometimes you need to pass a lot of information from one page to another page and perhaps to another page before you can insert it into the db (it wouldn't make sense to insert everytime you're between pages). So an array is a nice simple "placeholder" to hold all this information for a while.

Here's an example. You want to generate a form with the days of the month on it. People then select the days they will be on leave.

So we want a form something like this:

[] 1 [] 2 [] 3 ...
[] 29 [] 30 [] 31


Here is the checkbox for day 1 of the calendar:

<input type='checkbox' name='Leave1' value='1'> etc.

So if if I tick the 1st day, I'll have in the next form a variable $Leave1 with a value of 1.

So how about the other 31 checkboxes??

<input type='checkbox' name='Leave2' value='2>
<input type='checkbox' name='Leave3' value='3>
......


Now you sit with 31 variables named from Leave1 to Leave31. A lot of typing and keeping head if they all really refer to the same thing - Leave.

Better to have

<input type='checkbox' name='Leave[$i]' value='1'>
<input type='checkbox' name='Leave[$i]' value='2'>
<input type='checkbox' name='Leave[$i]' value='3'>


where $i will be a value from 1-31

Even better:

<input type='checkbox' name='Leave[$i]' value='$i'>
<input type='checkbox' name='Leave[$i]' value='$i'>
<input type='checkbox' name='Leave[$i]' value='$i'>


We'll typically put this in a for loop :

for($i=1; $i < 32; $i++){

echo "<input type='checkbox' name='Leave[$i]' value='$i'>$i<br>";

}

Thus, in plain english: for (starting with i having a value of 1; and for as long as i has a value that is than 32; increase the value of i by one){

.... //$i wil be one, then 2, then 3 etc.

}

So this is going to happen 31 times,and each time $i's value will inrease by one.

So now we have 1 variable called Leave[$i] (ok, it is 31 condensed into 1) with 31 values. But the point it you only have to write it once.

Easy to extract the values.

Don't worry about how for now.

Hope it makes more sense why arrays are useful.


How do I validate an e-mail address
February 22, 2006 12:12PM
Hi guys. My problem is an easy one I hope, I just cannot figure it out though. I have a problem with validating an e-mail address when ever a user registers on my site. How do I go about coding.... Please HELP.

Feel free to spoon feed me with an already exsisting code that you have lying around somewhere on your hard disk


It's urgent!!!!

Cheers
avatar
Mac
Re: How do I validate an e-mail address
February 22, 2006 12:59PM
Visit cs-cert.unisa.ac.za, register for this course, then I'll tell you smile
Re: Topic 2B
March 04, 2006 02:33PM
working fine - I added
$SelectedBook=$_POST['SelectedBook'];
to array.php
Re: How do I validate an e-mail address
March 06, 2006 04:32PM
i woild like to order prescibed books using my e- mail address
Sorry, you do not have permission to post/reply in this forum.