itconsultant1
asked on
small PHP function
I kind of new to PHP/javascript and i need to create a function that would perform the following:
Two text box inputs on the page:
1. Number that i want to count to
2. Numbers that i want to highlight separates by commas
The goal is to display the numbers that i want to count to( though to use the exploded in PHP) and highlight in a color the numbers in the second text box.
For example:
the number that i inserted in the count to text box is a 100 and the number that i want to highlight is 15 and 20. As a result i will get a table displayed on the page with numbers from 1 to 100 in a table format and the numbers 15 and 20 would be highlighted in a color.
Two text box inputs on the page:
1. Number that i want to count to
2. Numbers that i want to highlight separates by commas
The goal is to display the numbers that i want to count to( though to use the exploded in PHP) and highlight in a color the numbers in the second text box.
For example:
the number that i inserted in the count to text box is a 100 and the number that i want to highlight is 15 and 20. As a result i will get a table displayed on the page with numbers from 1 to 100 in a table format and the numbers 15 and 20 would be highlighted in a color.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The parameters of the function are:
$total = 1. count to (text box)-indicates the number i want to count to
$selected = 2. numbers to highlight(text box) -indicates the numbers i want to highlight
Do you need that the result wil display at the same webpage of textbox and button (without reload page) or press button reload the entire webpage (submit)? In case that button reload the entire webpage (submit), you could have the webpage with something like this:
*** example.php ***
<?php
$count= $_GET["count"];
$numbers= $_GET["numbers"];
...
?>
...
<form... method="GET" action="example.php">
...
<input type="text" name="count" id="count" value="<?php echo $count; ?>"/>
<input type="text" name="numbers" id="numbers" value="<?php echo $numbers; ?>"/>
...
<input type="submit" name="btnSubmit" id="btnSubmit" value="Show table"/>
</form>
...
<?php
if (is_int($count)){
createTable($count,$number s);
}
?>
...
$total = 1. count to (text box)-indicates the number i want to count to
$selected = 2. numbers to highlight(text box) -indicates the numbers i want to highlight
Do you need that the result wil display at the same webpage of textbox and button (without reload page) or press button reload the entire webpage (submit)? In case that button reload the entire webpage (submit), you could have the webpage with something like this:
*** example.php ***
<?php
$count= $_GET["count"];
$numbers= $_GET["numbers"];
...
?>
...
<form... method="GET" action="example.php">
...
<input type="text" name="count" id="count" value="<?php echo $count; ?>"/>
<input type="text" name="numbers" id="numbers" value="<?php echo $numbers; ?>"/>
...
<input type="submit" name="btnSubmit" id="btnSubmit" value="Show table"/>
</form>
...
<?php
if (is_int($count)){
createTable($count,$number
}
?>
...
If you're new to PHP, this book will be a big help. I will try to show you a way of demonstrating the script your describe in a moment.
http://www.sitepoint.com/books/phpmysql4/
http://www.sitepoint.com/books/phpmysql4/
I think this general design pattern would be OK. You might want to filter the input and reformat the output a little bit, but the principles would be somewhat like what is illustrated here.
See www.laprbass.com/RAY_temp_itconsultant1.php
See www.laprbass.com/RAY_temp_itconsultant1.php
<?php // RAY_temp_itconsultant1.php
error_reporting(E_ALL);
// DEMONSTRATE THE USE OF A FORM WITH UNUSUAL INPUT FIELDS
// IF THE FORM HAS BEEN SUBMITTED
if (!empty($_GET))
{
// MIGHT WANT TO ADD SANITY CHECKS AND ERROR REPORTING HERE
$numbers = explode('-', $_GET["fromto"]);
$numbers = range($numbers[0], $numbers[1]);
$hilites = explode(',', $_GET["hilite"]);
// GENERATE THE OUTPUT STRING
$output = NULL;
foreach ($numbers as $num)
{
$span = '<span>';
if (in_array($num, $hilites))
{
$span = '<span style="font-weight:bold; color:red;">';
}
$output
.=
$span
. $num
. '</span>'
. PHP_EOL
;
}
// ONCE ALL NUMBERS ARE PROCESSED, WRITE THE OUTPUT STRING
echo $output;
}
// CREATE THE FORM
$form = <<<FORM
<br/>
<form>
RANGE OF NUMBERS LIKE 1-150: <input name="fromto" />
<br/>
CSV NUMBERS LIST LIKE 2, 12: <input name="hilite" />
<br/>
<input type="submit" value="SHOW HIGHLIGHTED NUMBERS IN RANGE" />
</form>
FORM;
echo $form;
ASKER
Guys,
Below is my code that i put together using both of your suggestions. I still can't get the highlight to work. I need to change the background color in the cells of my numbers that i want to highlight:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Title of document</title>
<style>
.box
{
border: 1px solid black;
height: 30px;
width: 50px;
padding-top: 15px;
text-align: center;
float: left;
margin-right: 3px;
margin-bottom: 3px;
}
.cool
{
background-color: blue;
color:white;
font-family:verdana;
}
</style>
</head>
<h1></h1>
<body>
<form action ="web.php" method="post">
<table border ='2' class ='cool'>
<caption></caption>
<tr >
<td> <label> Count To: </label> </td>
<td> <input type ='text' value='0' name="startNumber" /> </td>
</tr>
<tr >
<td> <label> Enter numbers to be higlighted: </label> </td>
<td> <input type='text' value='' name="endNumber" /> </td>
</tr>
<tr>
<td align = 'center' colspan = '2'>
<input type='submit' value='count' /> </td>
</tr>
</table>
</form>
<?php
$i = $_POST["startNumber"];
$b = 1;
while($b <= $i)
{
echo "<div class = 'box'> $b</div>";
$b++;
}
?>
</body>
</html>
right now it will create the desired table with the numbers but it will not do the highlighting part.
Thanks for all your help with this
Below is my code that i put together using both of your suggestions. I still can't get the highlight to work. I need to change the background color in the cells of my numbers that i want to highlight:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Title of document</title>
<style>
.box
{
border: 1px solid black;
height: 30px;
width: 50px;
padding-top: 15px;
text-align: center;
float: left;
margin-right: 3px;
margin-bottom: 3px;
}
.cool
{
background-color: blue;
color:white;
font-family:verdana;
}
</style>
</head>
<h1></h1>
<body>
<form action ="web.php" method="post">
<table border ='2' class ='cool'>
<caption></caption>
<tr >
<td> <label> Count To: </label> </td>
<td> <input type ='text' value='0' name="startNumber" /> </td>
</tr>
<tr >
<td> <label> Enter numbers to be higlighted: </label> </td>
<td> <input type='text' value='' name="endNumber" /> </td>
</tr>
<tr>
<td align = 'center' colspan = '2'>
<input type='submit' value='count' /> </td>
</tr>
</table>
</form>
<?php
$i = $_POST["startNumber"];
$b = 1;
while($b <= $i)
{
echo "<div class = 'box'> $b</div>";
$b++;
}
?>
</body>
</html>
right now it will create the desired table with the numbers but it will not do the highlighting part.
Thanks for all your help with this
Using the base code of [Ray_Paseur], you can modify you webpage like this:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Title of document</title>
<style>
.box
{
border: 1px solid black;
height: 30px;
width: 50px;
padding-top: 15px;
text-align: center;
float: left;
margin-right: 3px;
margin-bottom: 3px;
}
.box-selected{
color: red;
}
.cool
{
background-color: blue;
color:white;
font-family:verdana;
}
</style>
</head>
<h1></h1>
<body>
<form action ="web.php" method="post">
<table border ='2' class ='cool'>
<caption></caption>
<tr >
<td> <label> Count To: </label> </td>
<td> <input type ='text' value='0' name="startNumber" /> </td>
</tr>
<tr >
<td> <label> Enter numbers to be higlighted: </label> </td>
<td> <input type='text' value='' name="endNumber" /> </td>
</tr>
<tr>
<td align = 'center' colspan = '2'>
<input type='submit' value='count' /> </td>
</tr>
</table>
</form>
<?php
if (!empty($_POST)){
$startNumber = $_POST["startNumber"];
$endNumber = explode(',', $_POST["endNumber"]);
for($i=1;$i<=$startNumber; $i++){
if (in_array($i, $endNumber))
echo "<div class = 'box'> $i</div>";
else
echo "<div class = 'box box-selected'> $i</div>";
}
}
?>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Title of document</title>
<style>
.box
{
border: 1px solid black;
height: 30px;
width: 50px;
padding-top: 15px;
text-align: center;
float: left;
margin-right: 3px;
margin-bottom: 3px;
}
.box-selected{
color: red;
}
.cool
{
background-color: blue;
color:white;
font-family:verdana;
}
</style>
</head>
<h1></h1>
<body>
<form action ="web.php" method="post">
<table border ='2' class ='cool'>
<caption></caption>
<tr >
<td> <label> Count To: </label> </td>
<td> <input type ='text' value='0' name="startNumber" /> </td>
</tr>
<tr >
<td> <label> Enter numbers to be higlighted: </label> </td>
<td> <input type='text' value='' name="endNumber" /> </td>
</tr>
<tr>
<td align = 'center' colspan = '2'>
<input type='submit' value='count' /> </td>
</tr>
</table>
</form>
<?php
if (!empty($_POST)){
$startNumber = $_POST["startNumber"];
$endNumber = explode(',', $_POST["endNumber"]);
for($i=1;$i<=$startNumber;
if (in_array($i, $endNumber))
echo "<div class = 'box'> $i</div>";
else
echo "<div class = 'box box-selected'> $i</div>";
}
}
?>
</body>
</html>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
1. count to (text box)-indicates the number i want to count to
2. numbers to highlight(text box) -indicates the numbers i want to highlight
Finally i think this function can be executed by button so here is the order:
1. user inputs the number they want to count to
2. User inputs the numbers he wants highlighted
3. User clicks on the "generate" button to get his table displayed.