Nawk script that appends text to html - html

So, I have been working on my own on something for a week now and am getting nowhere. I am taking a very advance CIS class that I have done well in so far but towards the end I am working with stuff that I haven't learned yet. I should have waited a couple of semesters to talk this class because I am unprepared for what I am doing now. My main problem is not knowing HTML (I haven't ever had a reason to learn it).
Any ways I have a nawk script template that I have figured out that I need to use. It is derived from a color generator script and is the following:
#!/bin/nawk -f
#-----------------------------------------------------------------------------
#YOUR COMMENTS HERE
#-----------------------------------------------------------------------------
BEGIN {
print "<html>"
print "<body>"
print " <table border=2>"
print " <tr>"
print " <th>$first $last</th>" ###Change this
print " <th>$username</th>" ###Change this
print " <th>Color</th>" ###Change this
print " </tr>"
}
{
print " <tr>"
print " <td>" $1 "</td>" ###Change this
print " <td>" $2 "</td>" ###Change this
print " <td>" $3 "</td>" ###Change this
print " </tr>"
}
END {
print "</table>"
print "</body>"
print "</html>"
}
As you can see on the 12th and 13th line I have added $first $last, and $username respectively. Am I doing this right. Because I don't know HTML, every time I look at it, it is confusing the !##$ out of me.
The following is supposed to be the output:
<html>
<body>
<table border=2>
<tr><th>Name</th><th>Username</th><th>Email</th></tr>
<tr>
<td>Michael Raby</td>
<td>mraby</td>
<td>mike1071#yahoo.com</td>
</tr>
<tr>
<td>Hajar Alaoui</td>
<td>halaoui</td>
<td>hajar6#hotmail.com</td>
</tr>
<tr>
<td>Anne Lemar</td>
<td>alemar</td>
<td>anne.lemar#asu.edu</td>
</tr>
<tr>
<td>Russell Crotts</td>
<td>rcrotts</td>
<td>Russell.Crotts#asu.edu</td>
</tr>
<tr>
<td>Dan Mazzola</td>
<td>dmazzola</td>
<td>dan.mazzola#sun.com</td>
</tr>
<tr>
<td>Bill Boyton</td>
<td>bboyton</td>
<td>boytonb#earthlink.net</td>
</tr>
</table>
</body>
</html>
The following is a sample of what the HTML table is supposed to look like:
Raby Michael mike1071#yahoo.com
Alaoui Hajar hajar6#hotmail.com
Lemar Anne anne.lemar#asu.edu
Crotts Russell Russell.Crotts#asu.edu
Mazzola Dan dan.mazzola#sun.com
Boyton Bill boytonb#earthlink.net
Can someone PLEASE help me? I've been trying to figure out this on my own for a week now.

It looks like the following will do what you want:
#!/bin/nawk -f
#-----------------------------------------------------------------------------
#YOUR COMMENTS HERE
#-----------------------------------------------------------------------------
BEGIN {
print "<html>"
print "<body>"
print " <table border=2>"
print " <tr>"
print " <th>Name</th>" ###Change this
print " <th>Username</th>" ###Change this
print " <th>Email</th>" ###Change this
print " </tr>"
}
{
print " <tr>"
print " <td>" $1 "</td>" ###Change this
print " <td>" $2 "</td>" ###Change this
print " <td>" $3 "</td>" ###Change this
print " </tr>"
}
END {
print "</table>"
print "</body>"
print "</html>"
}

Related

How to get table data after parsing HTML from a url in iOS Swift 3

I am developing an iOS app in which I need to parse html from a link with kanna api. I have done it. But it's shown all of the html file. I need to show just a portion of data like this
<table width="880" border="1" cellspacing="0" cellpadding="0">
<tr>
<td width="81"><strong>Trip Name </strong></td>
<td width="159"><div align="center"><strong>Starting Time from Campus </strong></div></td>
<td width="186"><div align="center"><strong>Starting Spot & Time </strong></div></td>
<td width="444"><strong>Remarks</strong></td>
</tr>
<tr>
<td><div align="center">Normal-1</div></td>
<td><div align="center">6:30 AM </div></td>
<td>Rupsha, 7:20 AM </td>
<td>Will back via Royalmore & Ferighat </td>
</tr>
<tr>
<td><div align="center">Normal-1</div></td>
<td><div align="center">6:45 AM </div></td>
<td>Moylapota, 7:25 AM </td>
<td>Will back via Shibbari - Sonadangha </td>
</tr>
</table>
I have used this portion of code in swift 3 to show all html data.
let html = "<html>...</html>"
if let doc = HTML(html: html, encoding: .utf8) {
// Search for nodes by XPath
for link in doc.xpath("//a | //link") {
print(link["href"])
}
}
Now I want to get only the table portion of html data. How can I do it?
This will do the trick:
//I replaced " with ' to make it easier. But it shouldn't be a problem in your case.
let html = "<table width='880' border='1' cellspacing='0' cellpadding='0'>"
+ "<tr>"
+ "<td width='81'><strong>Trip Name </strong></td>"
+ "<td width='159'><div align='center'><strong>Starting Time from Campus </strong></div></td>"
+ "<td width='186'><div align='center'><strong>Starting Spot & Time </strong></div></td>"
+ "<td width='444'><strong>Remarks</strong></td>"
+ "</tr>"
+ "<tr>"
+ "<td><div align='center'>Normal-1</div></td>"
+ "<td><div align='center'>6:30 AM </div></td>"
+ "<td>Rupsha, 7:20 AM </td>"
+ "<td>Will back via Royalmore & Ferighat </td>"
+ "</tr>"
+ "<tr>"
+ "<td><div align='center'>Normal-1</div></td>"
+ "<td><div align='center'>6:45 AM </div></td>"
+ "<td>Moylapota, 7:25 AM </td>"
+ "<td>Will back via Shibbari - Sonadangha </td>"
+ "</tr>"
+ "</table>"
var infoFromHTML : [[String]] = []
if let doc = try? HTML(html: html, encoding: .utf8) {
var row : [String] = []
//look for td tags, not a or link tags
for (index, td) in doc.css("td").enumerated(){
row.append(td.text as! String)
if index%4 == 3{
//if all row text are added to the row array, append it to info, and reset
infoFromHTML.append(row)
row = []
}
}
}
print(infoFromHTML)
Here is your output:
[
["Trip Name ", "Starting Time from Campus ", "Starting Spot & Time ", "Remarks"],
["Normal-1", "6:30 AM ", "Rupsha, 7:20 AM ", "Will back via Royalmore & Ferighat "],
["Normal-1", "6:45 AM ", "Moylapota, 7:25 AM ", "Will back via Shibbari - Sonadangha "]
]
If you don't want to use headers of the table, just pretend like array's first element is not 0, it is 1.
If we talk about why you couldn't make it work; you are looking for a and link tags here doc.xpath("//a | //link"). But you have td tags. So you should do this: doc.css("td"). I used css function but I may use XPath too.
Good luck!
EDIT: You still should ask for an API. Preferably a RESTful JSON API.
EDIT 2: I used Swift 4 version of the Kanna. You may need to do some changes to use this with Swift 3.

How can I echo out a variable from a script in database row

I have a div layer with a voting script, how can I print it out in a row echo?
SELECT `image`,`Name`,`item`, (`totalrate` / `nrrates`) AS `rank` FROM `rtgitems` WHERE item
REGEXP 'Total'
ORDER BY (`totalrate` / `nrrates`)
echo "<td align='center' width='200'>" . $row['Name'] . "</td>";
echo "<td align='center' width='200'>" . "<img src=\"{$row['image']}\">" . "</td>";
echo "<td align='center' width='200'>" . $row['rank'] . "</td>";
echo "<td align='center' width='200'>" . <div class="srtgs" id="$row['item']"></div> . "</td>";
The variable srtgs is called in the <script src="../ratingfiles/ratings.js" type="text/javascript"></script> in the head
What am I missing?
Your div needs to be echoed as a literal, just like the tds.
echo "<td align='center' width='200'><div class='srtgs' id=" . $row['item'] . "></div</td>";

data not uploading properly due to >

Is there a way that i can escape >, I have a combo box which has an option below. I tried replacing > with > but it's still not uploading properly.
<select name="Category3" id="Category3">
<option value="Request |Running > 1 hour">Request |Running > 1 hour</option>
What it should upload is just Request |Running > 1 hour
but what is being uploaded is: Request |Running > 1 hour 1 hour' />
i dont know where its getting the other 1 hour' />
i tried removing the > and it just uploaded Request |Running 1 hour without the excess 1 hour' /> but i need it for consistency
here is the php code that im using to upload to db
<?php
error_reporting(0);
require 'include/DB_Open.php';
$Category3 = mysql_real_escape_string ($_POST['Category3']);
if (isset($ABC))
{
$sql="INSERT into XXX category_2
VALUES ('".$Category3."')";
$result=mysql_query($sql)or die(mysql_error());
}
?>
here is my code for retrieving the data
$sql="SELECT category_2
FROM XXX
WHERE resolved_date BETWEEN '" . $date . "' AND '" . $date1 . "'
ORDER BY resolved_date";
$myData = mysql_query($sql);
//to count if there are any results
$numrow = mysql_num_rows($myData);
if($numrow == 0)
{
echo "No results found.";
}
else
{
echo "$numrow";
}
{
echo "<table width='auto' cellpadding='1px' cellspacing='0px' border=1 align='center'>
<tr>
<th align='center'><strong>Category 3</strong></th>
</tr>";
while($info = mysql_fetch_array($myData))
{
echo "<form action='retrieve.php' method='post'>";
echo"<tr>";
echo "<td align='center'>" . $info['category_2'] . "<input type=hidden name=category_2 value=" . $info['category_2'] . "' /> </td>";
echo "</tr>";
echo "</form>";
}
}
echo "</table>";
Whenever you're displaying text that might include special HTML characters, you should use htmlentities() to encode it properly, so these characters don't cause the HTML to be misparsed (or worse, allow script injection).
$cat2 = htmlentities($info['category_2']);
echo "<td align='center'>" . $cat2 . "<input type='hidden' name='category_2[]' value='" . $cat2 . "' /> </td>";
You also were missing the ' after value=. I've added that (and quotes around all the other attributes).
Since you're creating multiple inputs with the same name, you need to give them an array-style name so that the server script can get all the values. $_POST['category_2'] will be an array.

how to have a vertical header and textarea

i have a code...server side and cant seem to make it load wherein HEADER would be vertical, i have tried the code below,
<?php
require 'include/DB_Open.php';
$ea_name = $_POST['ea_name'];
$sql="SELECT * FROM ea_error WHERE ea_name = '" . $ea_name . "'";
$myData = mysql_query($sql) or die(mysql_error());
//to count if there are any results
$numrow = mysql_num_rows($myData) ;
if($numrow == 0)
{
echo "No results found.";
}
else
{
echo '<fieldset><legend><strong>Information</strong></legend>
<table width="auto" border="0" align="center">
<tr><th scope="row">Error</th></tr>
<tr><th scope="row">Resolution</th></tr>
<tr><th scope="row">Contact/s</th></tr>';
while($info = mysql_fetch_array($myData))
{
echo "<form action='retrieve.php' method='post'>";
echo "<td align='center'>" . "<textarea readonly=readonly name=error cols=75 rows=8> " . $info['error'] . "</textarea></td>";
echo "<td align='center'>" . "<textarea readonly=readonly name=resolution cols=75 rows=8> " . $info['resolution'] . "</textarea></td>";
echo "<td align='center'>" . "<textarea readonly=readonly name=contacts cols=75 rows=8> " . $info['contacts'] . "</textarea></td>";
echo "</form>";
echo "</table>";
}
}
echo "</fieldset>";
include 'include/DB_Close.php';
?>
whats showing with this code is like below
Error
Resolution
Contact/s
then i would have the three text areas here on a single row
what i want to happen is
Error - TEXTAREA
Resolution - TEXTAREA
Contact/s - TEXTAREA
pls help...i also tried using a css style to no avail
table, td, th {
border: 1px solid red;
}
thead {
float: left;
}
ive also tried to use the code below,
echo "<form action='retrieve.php' method='post'>";
echo "<tr>";
echo "<td align='center'>" . "<textarea readonly=readonly name=error cols=75 rows=8> " . $info['error'] . "</textarea></td>";
echo "</tr>";
echo "<tr>";
echo "<td align='center'>" . "<textarea readonly=readonly name=resolution cols=75 rows=8> " . $info['resolution'] . "</textarea></td>";
echo "</tr>";
echo "<tr>";
echo "<td align='center'>" . "<textarea readonly=readonly name=contacts cols=75 rows=8> " . $info['contacts'] . "</textarea></td>";
echo "</tr>";
but what i am getting is
Error
Resolution
Contact/s
TEXTAREA
TEXTAREA
TEXTAREA
If I understood the question correctly, this could be an answer (although I would TOTALLY avoid using tables for layout design and not just tabular data):
$myRes = "<form action='retrieve.php' method='post'>
<fieldset>
<legend><strong>Information</strong></legend>
<table width='auto' border='0' align='center'>
<tr>
<th scope='row'>Error</th>
<td align='center'><textarea readonly=readonly name=error cols=75 rows=8>" . $info['error'] . "</textarea></td>
</tr>
<tr>
<th scope='row'>Resolution</th>
<td align='center'><textarea readonly=readonly name=resolution cols=75 rows=8>" . $info['resolution'] . "</textarea></td>
</tr>
<tr>
<th scope='row'>Contact/s</th>
<td align='center'><textarea readonly=readonly name=contacts cols=75 rows=8>" . $info['contacts'] . "</textarea></td>
</tr>
</table>
</fieldset>
</form>";
echo $myRes;

getting rid of nbsp

Help, Im really sick of using this lots of nbsps in my results page. Im just a beginner. Can you recommend me some techniques so that I will not be copy pasting this lots of nbsp just to get the space and line breaks I need.
while($row = mysql_fetch_array($result))
{
echo "Patient #:". " ". " ". " ". " ". $row['PNUM'];
echo "<B>"."Hospital #:"."</B>". " ". " ". " ". " ". $row['HOSPNUM']." "." "." ";
echo "<B>"."Room:". " ". " ". " ". " ". $row['ROOMNUM'];
echo "<B>"."Lastname:". " ". " ". " ". " ". $row['LASTNAME'];
echo "<B>"."Firstname:". " ". " ". " ". " ". $row['FIRSTNAME'];
echo "<B>"."Middlename:". " ". " ". " ". " ". $row['MIDNAME'];
echo "<B>"."Admission Date:". " ". " ". " ". " ". $row['ADATE'];
echo "<B>"."Admission Time:". " ". " ". " ". " ". $row['ADTIME'];
echo "<B>"."Patient #:". " ". " ". " ". " ". $row['PNUM'];
}
Use a HTML table tag.
Does it have to be plain text or can you display this in a table?
<table>
<tr>
<th>Patient #</th>
<th>Hospital #</th>
<th>Room</th>
<!-- etc. -->
</tr>
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>$row['PNUM']</td>";
echo "<td>$row['HOSPNUM']</td>";
echo "<td>$row['ROOMNUM']</td>";
// etc.
echo "</tr>";
}
</table>
Consider tables or definition lists
if your output is mainly for debugging, you can output your results in pre tag, pre tag, preserve space and line breaks
if not, you should learn some html (and css), a few possibilities are lists, tables, or custom markup with css style