14862875 Posted March 16, 2009 Share Posted March 16, 2009 Hey guys, i'm knew here so not familliar with how things work, so please bare with me.. i have to create an index.php page that takes in a users comment...then i have to create another page that handles that comment..this page must produce the comment (or each word) in alphabetical order and each word must be an array element (explode function), duplicate words must also be removed (array_unique function), i must also use the strtolower function seeing that all the words should be in lower case..and finally i need to sort the words..and print of course. [1st part] [2nd part] this should produce the words and the number of times it appears on the page.. for example Box (3)times etc.. please guys help me out, i'm struggling with php and would really appreciate your help.. below is my index.php page <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>Practical 5</title> </head> <body bgcolor="black"> <center> <br/><br/> <table border="0"> <tr> <td background='b1.jpg' width='710' height='490' valign='top' style="text-align:left;color:white;font-family:tahoma;font-size:12;border:1 solid white;padding:20px;"> <span style="font-size:35">Practical 6</span><br/><br/>Please provide a paragraph:<br/> <form name="basic" action="handle_form.php" method="post"> <table cellpadding="8" style="text-align:left;color:white;font-family:tahoma;font-size:12;padding:20px;"> <tr><td><textarea cols="40" rows="10" name='paragraph'></textarea></td></tr> <tr><td><input type="submit" value="Submit"></td></tr> </table> </form> </body> </html> my handle_form.php page <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>Practical 5</title> </head> <body bgcolor="black"> <center> <br/><br/> <table border="0"> <tr> <td background='b1.jpg' width='710' height='490' valign='top' style="text-align:left;color:white;font-family:tahoma;font-size:12;border:1 solid white;padding:20px;"> <span style="font-size:35">Practical 6</span><br/><br/> <table cellpadding="8" style="text-align:left;color:white;font-family:tahoma;font-size:12;padding:20px;"> <tr><td>In aplphabetical order:</td></tr> <?php $par1=array($_POST['paragraph']); $par1=strtolower($par1); $par1=explode(' ',$par1); $par2=array_unique($par1); foreach($par2 as $par) print"<tr><td>$par</td></tr>"; ?> what am i doing wrong please help!!!! Quote Link to comment Share on other sites More sharing options...
syed Posted March 16, 2009 Share Posted March 16, 2009 Have you competed part one? is not what is left? Quote Link to comment Share on other sites More sharing options...
syed Posted March 16, 2009 Share Posted March 16, 2009 to sort the array, sort($par2); foreach($par2 as $par) print $par . "<br/>"; Quote Link to comment Share on other sites More sharing options...
kickstart Posted March 16, 2009 Share Posted March 16, 2009 Hi Looks like all that you need still is a line to sort the array:- <?php $par1=array($_POST['paragraph']); $par1=strtolower($par1); $par1=explode(' ',$par1); sort($par1); $par2=array_unique($par1); foreach($par2 as $par) print"<tr><td>$par</td></tr>"; ?> All the best Keith Quote Link to comment Share on other sites More sharing options...
syed Posted March 16, 2009 Share Posted March 16, 2009 Counting the occurance of eash word. Try this function. WordCount($par1); function WordCount($ar){ $hash = array(); foreach($ar as $a){ if (array_key_exists($a, $hash)){ $hash[$a]++; }else{ $hash[$a]=1; } } print_r($hash); } Quote Link to comment Share on other sites More sharing options...
14862875 Posted March 16, 2009 Author Share Posted March 16, 2009 thanx guys, but the 1st part doesn't wanna work.. <?php $par1=array($_POST['paragraph']); $par1=strtolower($par1); $par1=explode(' ',$par1); sort($par1); $par2=array_unique($par1); foreach($par2 as $par) print"<tr><td>$par</td></tr>"; ?> this code seems not to be working.. every time i run the page it doesn't kick out the words it just give me Array...am i not turning the comment into an array or am i not using the correct syntax for printing the array? Quote Link to comment Share on other sites More sharing options...
syed Posted March 16, 2009 Share Posted March 16, 2009 edit this $par1=array($_POST['paragraph']); to this $par1=$_POST['paragraph']; Quote Link to comment Share on other sites More sharing options...
14862875 Posted March 16, 2009 Author Share Posted March 16, 2009 Thanx syed!! its working! thanx a lot man!! a slight concern that i have is, are the $par1 variable now a array? or just a variable because it needs to be an array.. its for a practical that i'm doing so the lecturer wants us to use arrays, anyway can you please help me with the second part? now i have to count the amount of times each word was used...i'm gonna try and use your code above, if its not working i'll come back to you.. thanx again Quote Link to comment Share on other sites More sharing options...
syed Posted March 16, 2009 Share Posted March 16, 2009 The post variable is actually a key value array. If you post many variables to a form, you can use print_r($_POST) to see what the $_POST variable contains. $par1 in this case is not an array it is a variable getting data from the $_POST array variable. Quote Link to comment Share on other sites More sharing options...
kickstart Posted March 16, 2009 Share Posted March 16, 2009 Hi Yes that code puts them into an array. Note that you have deleted the duplicates hence no real way to count the occurances after that code. You need to save the array before you remove duplicates or count the values beforehand. There is a php function to count the values, although not sure it is exactly what you want:- array_count_values All the best Keith Quote Link to comment Share on other sites More sharing options...
syed Posted March 16, 2009 Share Posted March 16, 2009 Sorry $par1 becomes an array when you use the expode() function, before then it is just a variable. Quote Link to comment Share on other sites More sharing options...
14862875 Posted March 16, 2009 Author Share Posted March 16, 2009 <?php $count_words=array[]; foreach($par2 as $par) { if (array_key_exists($par2,$count_words)) { $count_words[$par2]++; else { $count_words=[$par2]=1; } } print"<tr><td>$par2</td></tr>"; ?> can any of you please help with that code, struggling to count the amount of times each word occurs in comment Quote Link to comment Share on other sites More sharing options...
kickstart Posted March 17, 2009 Share Posted March 17, 2009 Hi Give this a try:- <?php $par1=strtolower($_POST['paragraph']); $par1=explode(' ',$par1); sort($par1); $count_words = array_count_values($par1); foreach($count_words as $field => $value) { echo "<tr><td>$field</td><td>$value</td></tr>"; } ?> All the best Keith Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.