Write the PHP code for a web page that searches for names that contain a given l
ID: 3891879 • Letter: W
Question
Write the PHP code for a web page that searches for names that contain a given
letter a given number of times, case-insensitively. Your web service would be
located in a file named q4.php on the server. In this same directory is a file peeps.txt;
each line of this file contains a name. For example:
Helene Martin Robert Purple Marty Stepp Stuart Reges Bob Loblaw
The names are guaranteed to be non-empty and unique.
Your web service accepts a query parameter named letter indicating the character to be searched for, and a parameter named times indicating how many times that character must occur, case-insensitively. For each name that contains the letter at least the given number of times, an HTML paragraph should be printed indicating
this with the name in bold along with the number of times it contains the letter. For
example, if the following query is given, this output is produced:
q4.php?letter=R×=2
Robert Purple contains ‘R’ exactly 3 times.
Stuart Reges contains ‘R’ exactly 2 times.
If no names contain the given character the given number of times, output a
message saying so:
q4.php?letter=x×=1
No name contained ‘x’ enough times.
If one or both of the required query parameters is not passed, your service must produce an error message instructing the user about how to fix it. You should also generate an error message if the letter parameter is not a one-letter string or if the times parameter is not a positive number. (Any times value ? 0 is invalid and anything else is valid.)
q4.php?letter=thingy×=-4
q4.php?letter=t
q4.php?times=7
q4.php
The above are Invalid Request
Your code should not output a complete HTML page. Output a partial page
containing only the paragraphs indicated.
Explanation / Answer
Index.php
<?php
echo '<html>
<head>
</head>
<body>
<b>Contents of the file</b>';
echo '<br/><br/>';
# FILE CONTENTS
$fh = fopen("peeps.txt", 'r');
$pageText = fread($fh, 25000);
echo nl2br($pageText);
echo '<br/>';
echo '<br/><br/>';
echo '<input type="text" id="letter" placeholder="Letter"></input>';
echo '<input type="number" min="1" id="noofocc" placeholder="Frequency"></input>';
echo '<button id="#button">Get the Stats</button>';
echo '<br/><br/>';
echo '<b>Output</b>';
echo '<br/><br/><br/><br/>';
echo '<div id="output"></div>';
echo '<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
});
function getStats() {
debugger;
$letter = $("#letter").val();
if (!$letter || !isNaN($letter)) {
alert("Letter must be a char");
return false;
}
$times = $("#noofocc").val();
if ($times == "") {
alert("Frequency must be supplied");
return false;
}
if($letter && $times) {
$.ajax({
type: "get",
url:"q4.php?letter=" + $letter + "×=" + +$times,
complete: function (response) {
$("#output").html(response.responseText);
},
error: function () {
$("#output").html("Bummer: there was an error!");
}
});
return false;
}
};
</script>
</body>
</html>';
?>
--------------------------
peeps.txt
Robert Purple
Stuart Reges
Helene Martin Robert Purple Marty Stepp Stuart Reges Bob Loblaw
-----------------------------
q4.php
<?php
if(isset($_GET['letter']))
$letter = $_GET['letter'];
if(isset($_GET['times']))
$times = $_GET['times'];
$result;
if($letter && $times) {
$file = fopen("peeps.txt", "r") or exit("Unable to open file!");
//Output a line of the file until the end is reached
while(!feof($file))
{
$line = fgets($file);
$temp = substr_count(strtolower($line), strtolower($letter));
switch($temp) {
case 0:
$result = $result . "<br/> No name contained " . $letter . " enough times.";
break;
default:
$result = $result . "<br/> " . $line . " contains '" . $letter . "' exactly " . $temp . " times.";
}
}
fclose($file);
}
echo $result;
?>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.