• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 197
  • Last Modified:

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.
0
itconsultant1
Asked:
itconsultant1
  • 3
  • 3
  • 2
2 Solutions
 
fundacionrtsAdministrador de SistemasCommented:
function createTable($total, $selected){
echo "<table>";
for ($i = 1; $i <= $total; $i++) {
      if (strpos(",$selected,",",$i,") !== false){
            echo "<tr><td style='background-color:red';>$i</td></tr>";
      }
      else{
            echo "<tr><td>$i</td></tr>";
      }
}
echo "</table>";
}

Instead setting an specific color at

echo "<tr><td style='background-color:red';>$i</td></tr>";

you could use styles an set this line

echo "<tr><td class='selected';>$i</td></tr>";

an declare a css style selected with the style that you want.
0
 
itconsultant1Author Commented:
how will this function work with the text boxes
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.
0
 
fundacionrtsAdministrador de SistemasCommented:
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,$numbers);
}
?>
...
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
Ray PaseurCommented:
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/
0
 
Ray PaseurCommented:
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
<?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;

Open in new window

0
 
itconsultant1Author Commented:
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
0
 
fundacionrtsAdministrador de SistemasCommented:
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>
0
 
Ray PaseurCommented:
Please do not start a PHP script with the HTML header block.  It's one of those things you can "get away with" like driving on the wrong side of the road.  Eventually it will result in huge problems that will be hard to remedy.  Instead, start your script with the PHP code.  Inside the PHP code, generate all of the HTML statements using the data from the client request and the underlying data model.  Only when the data is complete should you begin outputting the XML, HTML, or whatever else you want to create in the response string.

But that said, I think you might want to invest a little money and some time in this book, so I will suggest it again.  It will help you get a basic understanding of the PHP part of things.  You will need separate learning resources for HTML and CSS.  For better or worse, web pages area made from a lot of different technologies.
http://www.sitepoint.com/books/phpmysql4/

This script highlights the numbers by changing the background color You can experiment with this script on my server.  Click this link and play with it, or just copy the original script, install it on your server and run it to see the moving parts in action.
http://www.laprbass.com/RAY_temp_itconsultant1.php?fromto=5-30&hilite=2%2C6%2C12%2C29%2C39%2C31

The only change to the original demonstration script is to change the CSS on line 23:
            $span = '<span style="background-color:blue; color:white;">';

Open in new window

If you want a different tabular display or some other kind of formatting, you should be able to add some HTML and CSS statements fairly easily.  If you still do not understand the relationship between the external variables in the request and the HTML that is generated by this script, please post back and I'll try to help with any questions.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now