Jump to content

Arrays


14862875

Recommended Posts

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!!!!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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);

 

}

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

<?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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.

×
×
  • 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.