Solved

Load value from SQL table in HTML textarea

Posted on 2014-10-27
15
393 Views
Last Modified: 2014-10-29
Hello
I have a working page called test.php it has 3 text area boxes that use a PrintLabelnew.js file to print
the labels onto a Dymo Label Printer

I also have a working Php script printphppage.php that gets information for a SQL Database and displays it into a Table Format.

I taught that once I had both the Test.php and the printphppage.php working separately that I could easily make them work together SO SO wrong.

Test.php
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Print a Label</title> 
<script src = "http://labelwriter.com/software/dls/sdk/js/DYMO.Label.Framework.latest.js" type="text/javascript" charset="UTF-8"> </script>
<script src = "PrintLabelnew.js" type="text/javascript" charset="UTF-8"> </script>
</head>

<body>
<h1>DYMO Label Framework JavaScript Library Samples: Print Label test 2</h1> 


    <div id="printDiv">
	<label for="textTextArea">Label 1 text:</label><br/>
        <textarea name="textTextArea" id="1textTextArea"  rows='5' cols='40'></textarea>
            <button id="1printButton" onclick="print1();return false;">Print</button>
        </div>
		
        <div id="printDiv">
		<label for="textTextArea">Label 2 text:</label><br/>
        <textarea name="textTextArea" id="2textTextArea"  rows='5' cols='40'></textarea>
            <button id="2printButton" onclick="print2();return false;">Print</button>
        </div>
        <div id="printDiv">
		<label for="textTextArea">Label 3 text:</label><br/>
        <textarea name="textTextArea" id="3textTextArea"  rows='5' cols='40'></textarea>
            <button id="3printButton" onclick="print3();return false;">Print</button>
        </div>
        <div id="printAll">
            <button id="printAllBtn" onclick="print1(); print2(); print3(); return false">Print All</button>
        </div>

</body> 
 
</html>

Open in new window


In Line 16 of test.php  I inserted my Php Script just before  </textarea>  and it did display the info from the Database
but it also displayed all the tags that make up the layout of the Table  "<table border='1' height='50' font size='20'>
<tr> <td> </td> </tr> </table>  

PrintLabelnew.js
function printStuff(what)
{
            try
            {
                // open label
                var labelXml = '<?xml version="1.0" encoding="utf-8"?>\
    <DieCutLabel Version="8.0" Units="twips">\
        <PaperOrientation>Landscape</PaperOrientation>\
        <Id>Address</Id>\
        <PaperName>30252 Address</PaperName>\
        <DrawCommands/>\
        <ObjectInfo>\
            <TextObject>\
                <Name>Text</Name>\
                <ForeColor Alpha="255" Red="0" Green="0" Blue="0" />\
                <BackColor Alpha="0" Red="255" Green="255" Blue="255" />\
                <LinkedObjectName></LinkedObjectName>\
                <Rotation>Rotation0</Rotation>\
                <IsMirrored>False</IsMirrored>\
                <IsVariable>True</IsVariable>\
                <HorizontalAlignment>Left</HorizontalAlignment>\
                <VerticalAlignment>Middle</VerticalAlignment>\
                <TextFitMode>ShrinkToFit</TextFitMode>\
                <UseFullFontHeight>True</UseFullFontHeight>\
                <Verticalized>False</Verticalized>\
                <StyledText/>\
            </TextObject>\
            <Bounds X="332" Y="150" Width="4455" Height="1260" />\
        </ObjectInfo>\
    </DieCutLabel>';
                var label = dymo.label.framework.openLabelXml(labelXml);

                // set label text
                label.setObjectText("Text", what);
                
                // select printer to print on
                // for simplicity sake just use the first LabelWriter printer
                var printers = dymo.label.framework.getPrinters();
                if (printers.length == 0)
                    throw "No DYMO printers are installed. Install DYMO printers.";

                var printerName = "";
                for (var i = 0; i < printers.length; ++i)
                {
                    var printer = printers[i];
                    if (printer.printerType == "LabelWriterPrinter")
                    {
                        printerName = printer.name;
                        break;
                    }
                }
                
                if (printerName == "")
                    throw "No LabelWriter printers found. Install LabelWriter printer";

                // finally print the label
                label.print(printerName);
            }
            catch(e)
            {
                alert(e.message || e);
            }
}
function print1()
{
    var textTextArea = document.getElementById('1textTextArea');
    printStuff(textTextArea.value);
}
function print2()
{
    var textTextArea = document.getElementById('2textTextArea');
    printStuff(textTextArea.value);
}
function print3()
{
    var textTextArea = document.getElementById('3textTextArea');
    printStuff(textTextArea.value);
}

Open in new window


printphppage.php
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php 
 $con = mysqli_connect("localhost","root","PASSWORD","test"); 
 // Check connection 
 if (mysqli_connect_errno()) 
 { 
 echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
 } 

$result = mysqli_query($con,"SELECT w1monsick2.*, w1monsick1.* FROM w1monsick2, w1monsick1 WHERE w1monsick2.id2 =2 = w1monsick1.id =1"); 

 
$tr1= '<tr><td>Date</td>';
$tr2= '<tr><td>Sick Teacher</td>';
$tr3= '<tr><td>Period</td>';
$tr4= '<tr><td>Cover Teacher</td>';
$tr5= '<tr><td>Subject</td>';
$tr6= '<tr><td>Class</td>';
$tr7= '<tr><td>Room</td>';
 
  echo "<table border='1' height='50' font size='20'> 

 "; 

 while($row = mysqli_fetch_array($result)) 
 { 
 
 $tr1 .="<td>" . $row['datepicker'] . "</td>"; 
 $tr2 .="<td>" . $row['sickteacher2'] . "</td>"; 
 $tr3 .="<td>" . $row['period2'] . "</td>"; 
 $tr4 .= "<td>" . $row['coverteacher2'] . "</td>"; 
 $tr5 .= "<td>" . $row['subject2'] . "</td>"; 
 $tr6 .= "<td>" . $row['class2'] . "</td>"; 
 $tr7 .= "<td>" . $row['room2'] . "</td>"; 
 } 
 $tr1 .= '</tr>';
 $tr2 .= '</tr>';
 $tr3 .= '</tr>';
 $tr4 .= '</tr>';
 $tr5 .= '</tr>';
 $tr6 .= '</tr>';
 $tr7 .= '</tr>';

 echo 
 $tr1,'
 ',$tr2,'
 ',$tr3,'
 ',$tr4,'
 ',$tr5,'
 ',$tr6,'
  ',$tr7,'
 </table>';  

 mysqli_close($con); 
 ?> 
</body>
</html>

Open in new window


Is there any way I can populate the text area with just the
headings and the relevant SQL data
Date:              SQL data
Sickteacher:  SQL data
Period:           SQL data
Cover:             SQL data
Subject:          SQL data
Class:              SQL data
Room:            SQL data
0
Comment
Question by:paddy086
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 7
15 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 40407470
Does Test.php work if you hard-code information into the textarea controls?
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 40407476
It looks like you've got a script there that puts HTML tags in with the data.  Have you tried omitting the HTML tags?

A suggestion: When you're working on scripts like this with many fields, make up a simple test case that just has one field.  You will be able to do proof-of-concept experiments very quickly that way, and you can go back in later and add the other fields. So instead of manually working with all of these:

Date:              SQL data
Sickteacher:  SQL data
Period:           SQL data
Cover:             SQL data
Subject:          SQL data
Class:              SQL data
Room:            SQL data

Try just one or two data elements until you have it right:

Date:              SQL data
Sickteacher:  SQL data
0
 

Author Comment

by:paddy086
ID: 40407487
Hi Ray
My test.php textarea works if I manualy put in text into the textarea and it prints.

I will limit my php script to only work with 2 values for testing purposes. As far as omitting the html tags not sure how to do that.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 110

Expert Comment

by:Ray Paseur
ID: 40407552
omitting the HTML tags...
This code snippet adds HTML tags to the document.  All those "$tr" variables contain HTML markup.  You might try omitting them from the document.  You might also try removing the HTML that is wrapped around the PHP script.  As a general rule it's wise to start with exactly what you understand and nothing more when you're writing a PHP script.  Then add things one-at-a-time so you can test each new addition to see if it is something that makes sense and enhances the output to produce the document you want.

Some of the theory behind the incremental build of an application is shown in this article.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_7830-A-Quick-Tour-of-Test-Driven-Development.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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php 
 $con = mysqli_connect("localhost","root","PASSWORD","test"); 
 // Check connection 
 if (mysqli_connect_errno()) 
 { 
 echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
 } 

$result = mysqli_query($con,"SELECT w1monsick2.*, w1monsick1.* FROM w1monsick2, w1monsick1 WHERE w1monsick2.id2 =2 = w1monsick1.id =1"); 

 
$tr1= '<tr><td>Date</td>';
$tr2= '<tr><td>Sick Teacher</td>';
$tr3= '<tr><td>Period</td>';
$tr4= '<tr><td>Cover Teacher</td>';
$tr5= '<tr><td>Subject</td>';
$tr6= '<tr><td>Class</td>';
$tr7= '<tr><td>Room</td>';
 
  echo "<table border='1' height='50' font size='20'> 

 "; 

 while($row = mysqli_fetch_array($result)) 
 { 
 
 $tr1 .="<td>" . $row['datepicker'] . "</td>"; 
 $tr2 .="<td>" . $row['sickteacher2'] . "</td>"; 
 $tr3 .="<td>" . $row['period2'] . "</td>"; 
 $tr4 .= "<td>" . $row['coverteacher2'] . "</td>"; 
 $tr5 .= "<td>" . $row['subject2'] . "</td>"; 
 $tr6 .= "<td>" . $row['class2'] . "</td>"; 
 $tr7 .= "<td>" . $row['room2'] . "</td>"; 
 } 
 $tr1 .= '</tr>';
 $tr2 .= '</tr>';
 $tr3 .= '</tr>';
 $tr4 .= '</tr>';
 $tr5 .= '</tr>';
 $tr6 .= '</tr>';
 $tr7 .= '</tr>';

 echo 
 $tr1,'
 ',$tr2,'
 ',$tr3,'
 ',$tr4,'
 ',$tr5,'
 ',$tr6,'
  ',$tr7,'
 </table>';  

 mysqli_close($con); 
 ?> 
</body>
</html>

Open in new window

0
 

Author Comment

by:paddy086
ID: 40408200
Ok so this is what I have working so far but I need to  echo each $row on a new line with out using any HTML tags in the data
e.g

Date:
Absent
Cover
Period
Class
Room

At the moment they are displaying like
Date Absent Cover Period Class Room

<?php
$servername = "localhost";
$username = "root";
$password = "PASSWORD";
$dbname = "test";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
 } 

$sql = "SELECT datepicker, coverteacher2, sickteacher2, room2, period2, class2 FROM w1monsick2, w1monsick1 WHERE w1monsick2.id2 =2 = w1monsick1.id =1";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo " DATE:". $row["datepicker"]. "ABSENT:  ". $row["sickteacher2"]. " COVER:  ". $row["coverteacher2"]. "PERIOD:  ". $row["period2"]."CLASS:  ". $row["class2"]. "ROOM: " . $row["room2"];
    }
} else {
    echo "0 results";
}
 $conn->close();
 ?>

Open in new window

0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 40408352
I need to  echo each $row on a new line with out using any HTML tags...
This is a little like saying, "I need a bicycle without using any wheels."  If you want to produce formatted browser output you need to have some kind of class or id attributes attached to the document elements so you can format the elements with CSS.  Without that you'll get the default browser formatting which is "collapse whitespace to one blank."

If the text is inside an HTML <textarea> tag you may be able to use the basic PHP_EOL constant to create new lines, or just hard-code the new line characters.  You may have to use "view source" to see the effect of the newline characters.

Have a look at this and see if it makes sense for your needs.  You may find that heredoc is your friend!  Heed, but do not be put off by the warning (it should not be a warning - just a tech tip).
http://iconoun.com/demo/temp_paddy086.php

<?php // demo/temp_paddy086.php
error_reporting(E_ALL);

// SHOW THE INPUT DATA
if (!empty($_POST)) var_dump($_POST);

// CONTENTS OF A TEXTAREA INPUT CONTROL
$contents = <<<EOD
Date:
Absent
Cover
EOD;

// CREATE THE HTML USING HEREDOC NOTATION
$htm = <<<EOD
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>HTML Textarea - Default Functionality</title>

<style type="text/css">
#abc{
    height:6em;
}
</style>

</head>
<body>
<form method="post">

<textarea id="abc" name="xyz">
$contents
</textarea>

<input type="submit" />
</form>
</body>
</html>
EOD;

echo $htm;

Open in new window

0
 

Author Comment

by:paddy086
ID: 40408491
Hi Ray

what I meant by not using any tags
I was thinking in the php script if I could do something like this

 
if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
echo " DATE:". $row["datepicker"];
echo "ABSENT:  ". $row["sickteacher2"]; 

Open in new window

and so on that it might produce the desired affect
0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 400 total points
ID: 40408507
Try it like this:
if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
echo " DATE:". $row["datepicker"] . PHP_EOL;
echo "ABSENT:  ". $row["sickteacher2"] . PHP_EOL; 

Open in new window

0
 

Author Comment

by:paddy086
ID: 40408882
Hi Ray
So as you said before it using the default browser formatting even if I do the code as below
if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
echo " DATE:". $row["datepicker"] . PHP_EOL;
echo "ABSENT:  ". $row["sickteacher2"] . PHP_EOL;

Open in new window


and I don't know how to implement this code into my test.html page

<?php // demo/temp_paddy086.php
error_reporting(E_ALL);

// SHOW THE INPUT DATA
if (!empty($_POST)) var_dump($_POST);

// CONTENTS OF A TEXTAREA INPUT CONTROL
$contents = <<<EOD
Date:
Absent
Cover
EOD;

// CREATE THE HTML USING HEREDOC NOTATION
$htm = <<<EOD
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>HTML Textarea - Default Functionality</title>

<style type="text/css">
#abc{
    height:6em;
}
</style>

</head>
<body>
<form method="post">

<textarea id="abc" name="xyz">
$contents
</textarea>

<input type="submit" />
</form>
</body>
</html>
EOD;

echo $htm;

Open in new window

0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 40408918
To be honest, I'm kind of at a loss to know what to try to do next.  I don't have a Dymo to test with.  And I may be misunderstanding your application design.

At this point my recommendation would be to consider hiring a professional developer who can get "hands on" with the scripts and data.  I have a feeling that it will not cost you very much and will get you a good answer fairly quickly.  The alternative, of course, is to learn everything a professional developer already knows, then do it yourself.  But that is likely to take quite some time.

If you're new to web application development and want to learn about it, this article may help lead you to some good learning resources.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html

Best of luck with the project, ~Ray
0
 

Author Comment

by:paddy086
ID: 40409256
Ok Ray
Thanks for your help so far.
I am going to leave this question open for a while as I am still working on it.

I just wish I could use a php script to create a table the way I wanted and use another php script to display it between the
<textarea> </textarea> so that the <th> <tr> tags would not be displayed
0
 
LVL 43

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 100 total points
ID: 40409886
Hey paddy,

I'm not sure if you're saying the data inside the textarea doesn't look correct on screen, or that it doesn't print correctly to your printer.

A textarea shouldn't have any HTML tags in it at all, and because of this you're somewhat limited to how you can lay things out. It will respect newlines, but you can't guarantee that things will line up properly like the columns of an HTML table.

If it looks OK on screen, but doesn't print correctly, then that's more of an issue with your printer library and not your PHP and textarea.

To check that the output of your printphppage.php is working OK, open it in your browser, but then look at the source of the page (usually Ctrl+U). It will display in your browser without the linebreaks and look wrong, but the source will show the linebreaks correctly. Your printphppage.php should have absolutely NO html in it at all, and as Ray pointed out, you add a linebreak by echoing PHP_EOL:

echo "DATE: " . $row["datepicker"] . PHP_EOL; 
echo "ABSENT: " . $row["sickteacher2"] . PHP_EOL;
echo "COVER: " . $row["coverteacher2"] . PHP_EOL; 
echo "PERIOD: " . $row["period2"] . PHP_EOL;
echo "CLASS: " . $row["class2"] . PHP_EOL;
echo "ROOM: " . $row["room2"] . PHP_EOL;

Open in new window


Once you know that's working, then we can move on to getting it into your test.php textarea.
0
 

Author Comment

by:paddy086
ID: 40410348
I final got it working script below.
<?php
$servername = "localhost";
$username = "root";
$password = "PASSWORD";
$dbname = "test";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
 } 

$sql = "SELECT datepicker, coverteacher2, sickteacher2, room2, period2, class2 FROM w1monsick2, w1monsick1 WHERE w1monsick2.id2 =2 = w1monsick1.id =1";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
		echo "DATE: " . $row["datepicker"]."\n";
 		echo "ABSENT: " . $row["sickteacher2"]."\n"; 
 		echo "COVER: " . $row["coverteacher2"]."\n"; 
		echo "PERIOD: " . $row["period2"]."\n";
		echo "CLASS: " . $row["class2"]."\n";
		echo "ROOM: " . $row["room2"]; 

    }
} else {
    echo "0 results";
}
 $conn->close();
 ?>

Open in new window


From what I have been looking at   PHP_EOL;  Should have created a page break or a new line but for some reason it would not work for me I am not sure if its to do with XAMPP on a windows computer.
but   "\n";  Worked for my project I was testing my php script on a new webpage and it worked I then put my php script into the textarea of my test page and I have got the layout I needed to print
0
 

Author Closing Comment

by:paddy086
ID: 40410410
Thanks for both your help
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 40411646
Thanks for the points and best of luck with your project, ~Ray
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

623 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