How to create table for my perl output using perl cgi? - cgi

Here is my code and output i was unable to create the different row for my each line using perl?
Here is my code:
use strict;
use warnings;
use CGI;
open(my $file1,"as.txt");
my $firstline=<$file1>;
$firstline=~s/.*=//g;
my #words=split /,/,$firstline;
my $finalline=join("\n",#words);
close $file1;
print "Content-type:text/html\n\n"
print<<"EOF";
<html><body>
<table style="width:100%">
<tr>
<th>UserName</th>
<th>Access</th>
</tr>
<tr>
<td>$finalline</td>
<td>
<input type="checkbox" value="check2" mulitple checked>Read
<input type="checkbox" value="check2" mulitple>Write
<input type="checkbox" value="check2" mulitple>Owner
</td>
</tr>
</table></body></html>
EOF
MY OUTPUT FOR PERL (I.E $finalline)
sankar
morien3
i got the following table as my output in table format:
UserName Access
sankar morien3 Read Write Owner
Expected output:
UserName Access
sankar Read Write Owner
morien3 Read Write Owner
Input file:(i.e as.txt)
cskTeam = sankar, mobrien3
[csk:/]
* = r
#cskTeam = rw

You must know about how HTML layout will work.
Even your code won't give your expected result in terminal.
In html \n not make sense, <br> it is for new line in html. But this logic also won't work in your code.
You are joining the array with \n, then printing the data it will execute the comment line by line what you have mentioned.
First you have printing the $finalline variable so it result is
sankar \n morien3
\n will not consider in html. Storing in one cell as per <td>
Then you are creating the another cell which holds the permission detail.
Finally your code should be as follows.
#!/usr/bin/perl
use warnings;
use strict;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
print "Content-Type: text/html \n\n";
open my $file1,"<","as.txt";
my $firstline=<$file1>;
$firstline=~s/.*=//g;
my #words=split /,/,$firstline;
close $file1;
print<<"EOF";
<html><body>
<table style="width:50%; ">
<tr>
<th style="text-align:left">UserName</th>
<th style="text-align:left">Access</th>
</tr>
EOF
foreach (#words)
{
print <<EOF;
<tr>
<td>$_</td>
<td>
<input type="checkbox" value="check2" mulitple checked>Read
<input type="checkbox" value="check2" mulitple>Write
<input type="checkbox" value="check2" mulitple>Owner
</td>
</tr>
EOF
}
print <<EOF;
</table></body></html>
EOF

Related

grabTextFrom finds anticipated string that assertion using Locator::contains cannot

I have an interesting case with the following HTML and a Codeception Acceptance test using PhpBrowser, which I've not been able to find a similar issue for on StackOverflow.
I am looking to assert that the content of a P tag (as part of the only table on the page) contains an anticipated phrase.
// representative code
$anticipatedValue = "updated text description";
$xpath = "//table/tbody/tr[position() = 1]/td[position() = 2]/descendant::p";
$val = $I->grabTextFrom($xpath); // this equals the value of $anticipatedValue;
echo "the value is[" .$val."][".$anticipatedValue."]"; // these match perfectly, no trimming needed
$I->see(Locator::contains($xpath,$anticipatedValue)); //this fails
$I->see(Locator::contains($xpath,$val)); //so does this
The fail HTML output does not display anything out of the ordinary. I am hoping someone with more experience of XPath in PhpBrowser can point out what I am missing.
The HTML portion I am looking at is below. This is a legacy application I am adding tests to, so the HTML is at present, as you can see, not optimal.
<table id="calendar" class="unitable" cellspacing=0 cellpadding=0>
<thead>
<tr>
<th>Time</th>
<th></th>
</tr>
</thead>
<tbody>
<tr class="past"><td>10 Dec 00:00</td>
<td class="entry" rowspan=2>
<div class="time">Tue 10 09:00 - Tue 10 17:00</div>
<div class="stage">new stage B</div>
<p class="description">updated text description</p>
</td>
</tr>
<tr class="past">
<td>10 Dec 12:00</td>
</tr>
<tr class="past">
<td>11 Dec 00:00</td>
<td class="entry" rowspan=1>
<div class="time">Wed 11 09:00 - Wed 11 11:30</div>
<div class="stage">new stage C</div>
<p class="description">this is new stage C</p>
</td>
</tr>
<tr class="past">
<td>11 Dec 12:00</td>
<td></td>
</tr>
<tr class="past">
<td>12 Dec 00:00</td>
<td></td>
</tr>
<tr class="past">
<td>12 Dec 12:00</td>
<td class="entry" rowspan=1>
<div class="time">Thu 12 13:30 - Thu 12 17:00</div>
<div class="stage">new stage D</div>
<p class="description">this is new stage D</p>
</td>
</tr>
</tbody>
<tfoot></tfoot>
</table>
and the failure report reads:
Failed asserting that on page ...
{short snippet of upper page HTML - not from the area under scrutiny, followed by}
[Content too long to display. See complete response in '/var/www/public/vhosts/system/tests/_output/' directory]
--> contains "//table/tbody/tr[position() = 1]/td[position() = 2]/descendant::p[contains(., 'updated text description')]".
Many thanks for your consideration of this issue.
Your problem is that you pass result of Locator::contains to $I->see() method,
see expects to get a string as a first parameter and it is looking for that exact text in HTML.
Start with
$I->see('updated text description');
If you want to check if the text is displayed in specific location, pass XPath expression as a second parameter:
$I->see('updated text description', '//table/tbody/tr[position() = 1]/td[position() = 2]/descendant::p');
as documented at https://codeception.com/docs/modules/PhpBrowser#see

pdo count not working

I have a page and all the function is working my only problem now is ciunying the record from the databse..
Class.user.php
Public function data($count)
{
$stmt=$this->db->prepare("SELECT COUNT(*) FROM login");
$result=$this->db->prepare($count);
$result->execute();
$number_of_rows=$result->fetchColumn();
}
Index.php
<table>
<thead>
<tr>
<th>2014</th>
</tr>
</thead>
<tbody>
<?php
$count="SELECT COUNT(*) FROM login";
$crud->data($count);
?>
The problem is that its not showing the count..
You're writing the same query as function argument, but also inside the function itself. And you're only really using one. This is nonsense. Dedicate your method to return the count, don't make the query a parameter.
prepareing the statement is pointless in this case, since you're neither reusing it nor are you binding any values. You can simply query() it directly.
The clincher: you're neither outputting nor returning the count, so it's very very expected that it doesn't show up anywhere.
Here's a sane version:
public function getCount() {
$result = $this->db->query('SELECT COUNT(*) FROM login');
return $result->fetchColumn();
}
<tbody>
<tr>
<td>
<?php echo $crud->getCount(); ?>
I think I usually do it like this link
$sql= "SELECT COUNT(*) FROM login";
$stmt = $pdo->prepare($sql);
$stmt->execute();
$row =$stmt->fetchObject();
then to output it you would have to ECHO
<table>
<tbody>
<tr>
<td>
<?php echo $row['count'];?>
</td>
</tr>
</tbody>
</table>

SQL select statement with AND and OR operators issue

I'm fetching records from the SQL Server 2000 database using Classic ASP. This is what the code I have used.
ASP Code:
<form name="search" method="post" onsubmit="return attention();">
<table width="60%" border="0" cellpadding="2" cellspacing="0" style="margin:0 auto">
<tr>
<td colspan="2"><h1 style="color:#003399;">Search for Certificate of Analysis</h1></td>
</tr>
<tr>
<td valign="bottom">Product number <sup style="color:#EE0000;font-size:1.3em">*</sup></td>
<td valign="bottom">Lot number</td>
</tr>
<tr>
<td width="30%" valign="top">
<input type="text" name="input1" size="20"/>
</td>
<td valign="top">
<input type="text" size="20" name="input2"/>
<input style="background-color:#ffffff;border:0px;cursor:pointer" size="10" type="submit" name="sub" value=">>" />
</td>
</tr>
</table>
<%
if request.Form("sub") <> "" then
Dim fname, lname
fname= request.Form("input1")
lname= request.Form("input2")
session("n") = fname & lname
sql = "Select * from search where cat_no_batch_no LIKE '"&request.Form("input1")&"%' OR cat_no_batch_no='"&session("n")&"'"
rs.open sql, con, 1, 2
if rs.eof then
response.write("Please provide a valid Cat No.")
else
do while not rs.eof
%>
<table border="1" cellpadding="5" cellspacing="0" width="60%" style="margin:0 auto; border-collapse:collapse;border-color:#999999">
<tr>
<td width="50%"><%=rs("cat_no_batch_no")%></td>
<td width="10%">Click to open <a target="_blank" href="http://localhost/search1/<%=rs("pdf_path")%>"><%=rs("cat_no_batch_no")%></a></td>
</tr>
</table>
<%
rs.movenext
loop
end if
rs.close
end if
%>
</form>
Above the LIKE statement works as expected and displays multiple records which contains similar Cat No..
Issue rises when I input a Cat No in first Input box and Lot Number in another. My desired output should have been just a single record to display as I have given Cat No. and Lot Number , but it shows multiple records.
I have provided the images to be more clear.
In this image below I have put 6106021 as the product number so it displays two entries.
In this image I have put 6106021 as the product number and 218111 as the Lot Number, it shows two entries instead of 1. This is what the issue, it should show one entry as Lot number are unique while cat number can be the same.
Read your question to yourself and think about what you are saying:
You state "My desired output should have been just a single record to display as I have given Cat No. and Lot Number"
But in your SQL you write where cat_no_batch_no LIKE '"&request.Form("input1")&"%' OR cat_no_batch_no='"&session("n")&"'
The key words are AND vs. OR
In your mind you are thinking both conditions must be true, but your query says otherwise. If you want both conditions to be true then you must use the AND operator.
Naoki is on to something - you are going to have to modify your SQL depending upon which criteria are specified.
I guess you can write two separate SQL queries, according as the "Lot number" is provided or not.
Your code may goes like below:
if lname = "" then
sql = "Select * from search where cat_no_batch_no LIKE '"&request.Form("input1")&"%'"
else
sql = "Select * from search where cat_no_batch_no LIKE '"&session("n")&"%'"
end if
I hope this could help

SQL Join in CodeIgniter

I have one file, then for every file there are multiple comments about that file. I was able to successfully join the two tables, comments and files, however when I try to view the page where it displays the file and its comments, the page displays the name of file n times (n corresponds to the number of records of comments of that certain file).
How will I be able to display just once the file name as well as displaying the comments?
<table width="40%" align="center">
<?php foreach($rows as $file):?>
<tr>
<td width="70%" id="title"><?php echo $file->name; ?></td>
</tr>
<?php endforeach;?>
</table>
<table width="40%" align="center" frame="above" style="margin-top:10px; border-top-width:3px; border-top-color:#666666">
<?php foreach($rows as $file):?>
<tr>
<td width="15%"></td>
<td width="45%" id="name">FILESIZE:</td>
<td width="40%" id="txt3"><?php echo $file->size; ?></td>
</tr>
<tr>
<td></td>
<td id="name">DATE UPLOADED:</td>
<td id="txt3"><?php echo $file->dateUploaded;?></td>
</tr>
<?php endforeach;?>
</table>
<!--COMMENTS -->
<table align="center" frame="above" style="margin-top:10px; border-top-width:3px; border-top-color:#666666" width="40%">
<tr>
<td><h3>Comments</h3></td>
</tr>
<tr><?php foreach($rows as $comment):?>
<td><?php echo $comment->comm;?></td>
</tr><?php endforeach;?>
</table>
You can use
GROUP_CONCAT()
in your join.
SELECT person.id AS id, name, GROUP_CONCAT(role.role SEPARATOR ',') AS roles
FROM person
LEFT OUTER JOIN person_role ON person.id=person_role.person_id
LEFT OUTER JOIN role ON person_role.role_id=role.id
GROUP BY id
ORDER BY id;
is an example from a tutorial at
http://dev-loki.blogspot.com/2008/01/aggregate-join-results-with-mysql-using.html
can't give much better without seeing your schema.
EDIT: trying anyway.
SELECT files.*, GROUP_CONCAT(comments.comment SEPARATOR ',') as comments_list
FROM files
LEFT JOIN comments on files.id = comments.id
GROUP BY id
ORDER BY id
Adding onto orbit, in codeigniter if your using activerecord it would be:
(Sorry, I couldn't format code in a comment)
$this->db->select('person.id AS id, name, GROUP_CONCAT(role.role SEPARATOR ',') AS roles',false);
$this->db->from('person');
$this->db->join('person_role', 'person.id = person_role.person_id', 'left outer');
$this->db->join('role', 'person_role.role_id = role.id', 'left outer');
$this->db->group_by('id');
$this->db->order_by('id'):
$this->db->get();
well it looks like you're echoing the filename for each row that is returned
your rows are getting returned like
name | size | comment1
name | size | comment2
so just don't do foreach ($rows as $file) that will print out the filename each time, just change
<?php foreach($rows as $file):?>
<tr>
<td width="70%" id="title"><?php echo $file->name; ?></td>
</tr>
<?php endforeach;?>
to
<tr>
<td width="70%" id="title"><?php echo $rows[0]->name; ?></td>
</tr>

Codeigniter inserting value into array

I asked a question on SO a few hours back.
How to insert an Array of field names from a form into SQL database in Codeigniter.
<tr>
<td><input type="text" name="user[0][name]" value=""></td>
<td><input type="text" name="user[0][address]" value=""><br></td>
<td><input type="text" name="user[0][age]" value=""></td>
<td><input type="text" name="user[0][email]" value=""></td>
<tr>
<td><input type="text" name="user[1][name]" value=""></td>
<td><input type="text" name="user[1][address]" value=""><br></td>
<td><input type="text" name="user[1][age]" value=""></td>
<td><input type="text" name="user[1][email]" value=""></td>
</tr>
..........//so on
This is the best answer I got
foreach($_POST['user'] as $user)
{
$this->db->insert('mytable', $user);
}
Now I want to pass a id generated from user session data into this array + a few other values like current time
Is it possible to tweak the above solution?
The Previous Discussion Here
foreach($_POST['user'] as &$user)
{
$user['additional_data'] = $_SESSION['additional_data'];
$user['current_time'] = time();
$this->db->insert('mytable', $user);
}
Note the & in &$user which is a pass by reference which allows manipulation of an array within a foreach loop. If you reference user later remember to unset($user) to remove the reference to the last element of the $_POST['user'] array.
I don't know if I'm missing something here, but can't you just remove the foreach and build your code like this:
$var1 = $_POST['user_var1'] * 3 + 1 / 5;
$this->db->insert('mytable', $var1);
$var2 = gettime();
$this->db->insert('mytable', $var2);
....
(where gettime() should be replace with whatever the PHP command for getting time is, plus maybe a formatting function)