Solved

small PHP function

Posted on 2012-03-11
8
182 Views
Last Modified: 2012-03-15
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
Comment
Question by:itconsultant1
  • 3
  • 3
  • 2
8 Comments
 
LVL 8

Assisted Solution

by:fundacionrts
fundacionrts earned 150 total points
ID: 37707790
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
 

Author Comment

by:itconsultant1
ID: 37707849
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
 
LVL 8

Expert Comment

by:fundacionrts
ID: 37707863
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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 37707900
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 108

Expert Comment

by:Ray Paseur
ID: 37707933
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
 

Author Comment

by:itconsultant1
ID: 37708297
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
 
LVL 8

Expert Comment

by:fundacionrts
ID: 37708655
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
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 350 total points
ID: 37709666
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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now