How to echo star from my rating table? - rating

I am pretty new to star rating system, I have made a rating from 1 to 5 and it successfully store in my back end system, the rating store at back end was in a range of number 1 to 5. However, I would like to echo these numbers using image star, I put in the if statement and it does not show in my website.
<?
if ('rating' == '1'){
echo '<img scr = "https://cdn1.iconfinder.com/data/icons/ledicons/star_2.png"/>';
}
if ('rating' == '2'){
echo '<img scr = "https://cdn1.iconfinder.com/data/icons/ledicons/star_2.png
https://cdn1.iconfinder.com/data/icons/ledicons/star_2.png"/>';
}
if ('rating' == '3'){
echo '<img scr = "https://cdn1.iconfinder.com/data/icons/ledicons/star_2.png
https://cdn1.iconfinder.com/data/icons/ledicons/star_2.png
https://cdn1.iconfinder.com/data/icons/ledicons/star_2.png"/>';
}
if ('rating' == '4'){
echo '<img scr = "https://cdn1.iconfinder.com/data/icons/ledicons/star_2.png
https://cdn1.iconfinder.com/data/icons/ledicons/star_2.png
https://cdn1.iconfinder.com/data/icons/ledicons/star_2.png
https://cdn1.iconfinder.com/data/icons/ledicons/star_2.png"/>';
}
if ('rating' == '5'){
echo '<img scr = "https://cdn1.iconfinder.com/data/icons/ledicons/star_2.png
https://cdn1.iconfinder.com/data/icons/ledicons/star_2.png
https://cdn1.iconfinder.com/data/icons/ledicons/star_2.png
https://cdn1.iconfinder.com/data/icons/ledicons/star_2.png
https://cdn1.iconfinder.com/data/icons/ledicons/star_2.png"/>';
}
?>
here is my code for the if statement, for you information, this is within the loop which I had added also $i=1 in the front and $i++ at the back, may I know did I put anything wrong?

For anyone coming here for a solution, this should work.
$stars_displayed = 0;
for ($i = 0; $i < $rating; $i++){
$stars_displayed++;
echo '<img src="fullstar_img.png" />';
}
for ($i = $stars_displayed; $i < 5; i++){
echo '<img src="emptystar_img.png" />';
}

Related

allocating a string based on PHP if statement

I quite new to PHP this seems to be causing me problems:
<?php
$ppe1="Water";
echo $ppe1;
if ($ppe1="Tap") {
$dog="time";
}
else
{$dog="travel";}
echo $dog;
?>
I am obviously being bery stupid but I cannot seem to allocate the $dog string based on the if statement
You should have a read on:
Comparison Operators.
In short: You are not comparing but assigning a value with =.
<?PHP
$ppe1 = "Walter";
$dog = $ppe1 === "Tap" ? "time" : "travel";
echo $dog;
This makes use of the ternary operator which is basically $variable = boolean expression ? then : else;
Or with your code structure:
<?PHP
$ppe1 = "Walter";
$dog = "";
if($ppe1 === "Tap")
{
$dog = "time";
}
else
{
$dog = "travel";
}
echo $dog;
Take a look at the Comparison Operators. Use the following code. Also as you said you are a beginner, I recommend you to read this.
<?php
$ppe1 = "Water";
echo $ppe1; // "Water"
if ($ppe1 == "Tap") {
$dog = "time";
} else {
$dog="travel";
}
echo $dog; // "travel"
?>

Get Title, URL, Description and insert it to database

I'm new here and need help to fix my code.
I'm trying to make a code using DomXPath to grab title, url and description from bing search and then save it to my DB.
Here is the code :
<?php
$s="something-words";
$keywords = strstr($s, '-') ? str_replace('-', '+', $s) : $s;
$html5 = new DOMDocument();
#$html5->loadHtmlFile('http://www.bing.com/search?q='.$keywords.'&go=&qs=bs&filt=all');
$xpath5 = new DOMXPath($html5);
$nodes = $xpath5->query('//div[#class="sb_tlst"]/h3');
$nodes = $xpath5->query('//div[#class="sb_meta"]/cite');
$nodes = $xpath5->query('//div[#id="results"]/ul[#id="wg0"]/li/div/div/p');
$data = array();
$data2 = array();
$data3 = array();
$i = 0;
foreach ($nodes as $node) {
$data = $node->textContent;
$i++;
// insert table urlgrab
mysql_query( "INSERT INTO urlgrab(title) Values ('$data')");
$data2 = $node->textContent;
$i++;
// update table urlgrab
dbConnect();
mysql_query( "UPDATE urlgrab SET url='$data2' WHERE title='$data'" );
$data3 = $node->textContent;
$i++;
// update table urlgrab
dbConnect();
mysql_query( "UPDATE urlgrab SET description='$data3' WHERE title='$data'" );
}
?>
the problem is I get same results in database for title,url,description. How to fix this code to get all data title,url and description save to my DB?
As you have messed up you code so it's hard to identified. But by assumption I have generated below code which should work for you.
$titles = $xpath5->query('//div[#class="sb_tlst"]/h3');
$urls = $xpath5->query('//div[#class="sb_meta"]/cite');
$descriptions = $xpath5->query('//div[#id="results"]/ul[#id="wg0"]/li/div/div/p');
$arrTitle = array();
foreach($titles as $title){
$arrTitle[] = $title->textContent;
}
$arrUrl = array();
foreach($urls as $url){
$arrUrl[] = $url->textContent;
}
$arrDescription = array();
foreach($descriptions as $description){
$arrDescription[] = $description->textContent;
}
$i = 0;
dbConnect();
foreach ($i=0; $i < count($arrTitle); $i++) {
$title = $arrTitle[$i];
$url = $arrUrl[$i];
$description = $arrDescription[$i];
mysql_query( "INSERT INTO urlgrab(`title`, `url`, `description`) Values ('$title', '$url', '$description')");
}
*Remove $i++; in loop and then run. Actually we're doing $i++ in for loop * And that will solve your issue.

Trying to compare web service response and expected xml from file

We're developing in Java for the most, but we want to integration test (using https://github.com/scottmuc/Pester) our web-services with ms as well. To do this I'm writing powershell scripts that connects to a web-service and compares the response to xml that I've loaded from a file.
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
$instance = New-WebServiceProxy -Uri "https://localhost:7002/service?WSDL" -Namespace "myspace"
$instance.Credentials = new-object System.Net.NetworkCredential("user", "pass")
...
$reply = $instance.fetchInformation($inputA, $inputB)
[xml]$expected = Get-Content ("expected.xml")
...
However, now I have a $reply that contains objects from the myspace namespace and an $expected that contains an XMLNode. I see two ways I can do this (there are probably many more):
Get the original XML response and compare that. However, I can't seem to find out how to get that.
Serialise the $expected XML into the myspace namespace objects. Is that possible?
You could serialize the response returned by the web service to XML and compare it with the contents of the expected.xml file as strings.
Here's an example:
$writer = New-Object System.IO.StringWriter
$serializer = New-Object System.Xml.Serialization.XmlSerializer($reply.GetType())
$serializer.Serialize($writer, $reply)
$replyAsXml = $writer.ToString()
$expectedReplyAsXml = Get-Content expected.xml
$replyAsXml -eq $expectedReplyAsXml
Note that in this example you need to make sure that XML contained in the expected.xml file matches the one returned by the XmlSerializer also in regard to spacing and indenting. In order to avoid that, you could strip all extra characters (such as spaces and newlines) from the two strings before comparing them.
I ended up with a completely different approach. The two XML's was quite different from each other so instead I created a custom comparator. This made it possible for me to simply write custom code to ignore uninteresting differences.
This lead to some pile of crude code that does the job:
# Assume two arrays of equal length
Function Zip {
Param($a1, $a2)
$sum = New-Object object[] $a1.Count
For ($i = 0; $i -lt $a1.Count; ++$i) {
$sum[$i] = New-Object object[] 2
$sum[$i][0] = $a1[$i]
$sum[$i][1] = $a2[$i]
}
Return ,$sum
}
Function XmlChildNodes2List{
param($nodes)
$myArray = New-Object object[] 0
For ($i = 0; $i -lt $nodes.Count; ++$i) {
$node = $nodes.Item($i)
If ($node -ne $null) {
$myArray += $node
}
}
Return ,$myArray
}
Function ShowContext{
Param($ctx)
" at " + $ctx
}
Function CompareNode{
Param($o1, $o2, $ctx)
Try {
Switch ($o1.GetType().Name) {
"XmlDocument" {
CompareXml $o1.ChildNodes $o2.ChildNodes
}
"XmlChildNodes" {
$olist1 = XmlChildNodes2List $o1 | Sort
$olist2 = XmlChildNodes2List $o2 | Sort
If ($olist1.Count -ne $olist2.Count) {
$msg = "Unequal child node count " + ($olist1 -join ",") + " and " + ($olist2 -join ",") + (ShowContext $ctx)
throw $msg
} Else {
$list = Zip $olist1 $olist2
$value = $true
foreach ($item in $list) {
if ($value -eq $true) {
$value = CompareXml $item[0] $item[1] $ctx
}
}
$value
}
}
"XmlElement" {
If ($o1.LocalName -eq $o2.LocalName) {
If ($o1.LocalName -eq "uninterestingElement" -or $o1.LocalName -eq "uninterestingElement2") {
$true
} Else {
CompareXML $o1.ChildNodes $o2.ChildNodes ($ctx + "/" + $o1.LocalName)
}
} Else {
throw ("Element " + $o1.LocalName + " != " + $o2.LocalName + (ShowContext $ctx))
}
}
"XmlDeclaration" {
$true
}
"XmlText" {
$result = $o1.InnerText.Replace("`r`n","`n")
$expect = $o2.InnerText.Replace("`r`n","`n")
# TODO: Hack to remove timezone from expected dates in format 2005-09-01+02:00, the webservice side of the
# reply to xml-conversion looses them
If ($expect -match "^(\d{4}-\d\d-\d\d)\+\d\d:\d\d$") {
$expect = $Matches[1]
}
If ($result -eq $expect) {
$true
} Else {
throw ($o1.InnerText + " is not equal to " + $o2.InnerText + (ShowContext $ctx))
}
}
Default {
throw ("What to do with node " + $o1.GetType().Name + (ShowContext $ctx))
}
}
} Catch [Exception] {
throw $_
}
}
Function CompareXML{
Param($o1, $o2, $ctx)
If ($o1 -eq $null -and $o2 -eq $null) {
$true
} ElseIf ($o1 -eq $null -or $o2 -eq $null) {
throw ("Response or expected is null")
} ElseIf ($o1.GetType() -eq $o2.GetType()) {
CompareNode $o1 $o2 $ctx
} Else {
throw ($o1.GetType().Name + " is not " + $o2.GetType().Name + (ShowContext $ctx))
}
}
This can then be run on two XML's like this:
CompareXML $result $expected ""

Text Pattern Processing in paragraph with unix linux utilities

I have a file with the following pattern (please note this is a file generated using sed,
awk, grep etc processing). The part of file input is as follows.
filename1,
BASE=a/b/c
CONFIG=$BASE/d
propertiesfile1=$CONFIG/e.properties
EndOfFilefilename1
filename2,
BASE=f/g/h
CONFIG=$BASE/i
propertiesfile1=$CONFIG/j.properties
EndOfFilefilename2
filename3,
BASE=k/l/m
CONFIG=$BASE/n
propertiesfile1=$CONFIG/o.properties
EndOfFilefilename3
I want the output like
filename1,a/b/c/d/e.properties,
filename2,f/g/h/i/j.properties,
filename3, k/l/m/n/o.properties,
I could not find a solution with sed or awk or grep. So I ams tuck. Please do let me know if you know the solution with these unix utilities or any other language, platform.
Regards,
Suhaas
Assuming you generated the original file, and therefore it is safe to execute it as a script:
sed -e 's/^.*,/FILE=&/' \
-e 's/^.*=\$CONFIG/PROPFILE=$CONFIG/' \
-e 's/^EndOfFile.*/echo $FILE $PROPFILE/' < yourInputFile | sh
This converts each section of your file into the form:
FILE=filename1,
BASE=a/b/c
CONFIG=$BASE/d
PROPFILE=$CONFIG/e.properties
echo $FILE $PROPFILE
... and then sends it into a shell for processing.
Line-by-line explanation:
Line 1: Searches for the lines ending in a comma (the filenames), and sets FILE to the name.
Line 2: Searches for lines that set the properties file, and renames the variable to PROPFILE.
Line 3: Replaces the EndOfFile lines with a command to echo the file name and the properties file, then pipes it into a shell.
This is an excellent use case for structural regular expressions, which have been implemented as a python library, amongst other places. Here's an article which descibes how to emulate SREs in Perl.
And here is an awk script to process that input and generate what you want:
BEGIN {
FS="="
state = 0;
base = "";
config = "";
prop = "";
filename = "";
dbg = 0;
}
/^BASE=/ {
if (dbg) {
print "BASE";
print $0;
}
if (state != 1) {
print "Error base!";
exit 1;
}
state++;
base = $2;
if (dbg > 1) printf ("BASE = %s\n", base);
}
/^CONFIG=/ {
if (dbg) {
print "CONFIG";
print $0;
}
if (state != 2) {
print "Error config!";
exit 1;
}
state++;
config = $2;
sub (/\$BASE/, base, config);
if (dbg > 1) printf ("CONFIG = %s\n", config);
}
/^propertiesfile1=/ {
if (dbg) {
print "PROP";
print $0;
}
if (state != 3) {
print "Error pF!";
exit 1;
}
state++;
prop = $2;
sub (/\$CONFIG/, config, prop);
}
/^EndOfFile/ {
if (dbg) {
print "EOF";
print $0;
}
if (state != 4) {
print "Error EOF!";
print state;
exit 1;
}
state = 0;
printf ("%s%s,\n", filename, prop);
}
/,$/{
if (dbg) {
print "FILENAME";
print $0;
}
if (state != 0) {
print "Error filename!";
print state;
exit 1;
}
state++;
filename = $1;
}
gawk
gawk -vRS= 'BEGIN{FS="BASE[=]?|CONFIG|\n"}
{
s=$1
for(i=1;i<=NF;i++){
if($i~/\// ){ s=s $i }
}
print s
s=""
}' file
output
$ more file
filename1,
BASE=a/b/c
CONFIG=$BASE/d
propertiesfile1=$CONFIG/e.properties
EndOfFilefilename1
filename2,
BASE=f/g/h
CONFIG=$BASE/i
propertiesfile1=$CONFIG/j.properties
EndOfFilefilename2
filename3,
BASE=k/l/m
CONFIG=$BASE/n
propertiesfile1=$CONFIG/o.properties
EndOfFilefilename3
$ ./shell.sh
filename1,a/b/c/d/e.properties
filename2,f/g/h/i/j.properties
filename3,k/l/m/n/o.properties
A perl script that does what you want would be something like (note this is untested)
while (<>) {
$base = $1 if (m/BASE=(.+)/);
$config = $1 if (m/CONFIG=(.+)/);
if (m/propertiesfile1=(.+)/) {
$props = $1;
$props =~ m/\$CONFIG/$config/;
$props =~ m/\$BASE/$base/;
print $ARGV . ", " . $props . "\n";
}
}
you give the script the filenames as arguments.
Multi-steps but it works!
cat yourInputFile | egrep ',|\/' | \
sed -e "s/^.*=//g" -e "s/\$.*\(\/.*\)/\1/g" | \
awk '{if($0 ~ "properties") print $0; else printf $0}'
The egrep grabs the lines containing a "," or a "/" and so eliminates the last line:
BASE=a/b/c
CONFIG=$BASE/d
propertiesfile1=$CONFIG/e.properties
The sed reduces the output to:
filename1,
a/b/c
/d
/e.properties
The awk portion reassembles the line to:
filename1,a/b/c/d/e.properties

Perl - Printing the next line

I am a noob Perl user trying to get my work done ASAP so I can go home on time today :)
Basically I need to print the next line of blank lines in a text file.
The following is what I have so far. It can locate blank lines perfectly fine. Now I just have to print the next line.
open (FOUT, '>>result.txt');
die "File is not available" unless (#ARGV ==1);
open (FIN, $ARGV[0]) or die "Cannot open $ARGV[0]: $!\n";
#rawData=<FIN>;
$count = 0;
foreach $LineVar (#rawData)
{
if($_ = ~/^\s*$/)
{
print "blank line \n";
#I need something HERE!!
}
print "$count \n";
$count++;
}
close (FOUT);
close (FIN);
Thanks a bunch :)
open (FOUT, '>>result.txt');
die "File is not available" unless (#ARGV ==1);
open (FIN, $ARGV[0]) or die "Cannot open $ARGV[0]: $!\n";
$count = 0;
while(<FIN>)
{
if($_ = ~/^\s*$/)
{
print "blank line \n";
count++;
<FIN>;
print $_;
}
print "$count \n";
$count++;
}
close (FOUT);
close (FIN);
not reading the entire file into #rawData saves memory, especially in the case of large files...
<FIN> as a command reads the next line into $_
print ; by itself is a synonym for print $_; (although I went for the more explicit variant this time...
Elaborating on Ron Savage's solution:
foreach $LineVar (#rawData)
{
if ( $lastLineWasBlank )
{
print $LineVar;
$lastLineWasBlank = 0;
}
if($LineVar =~ /^\s*$/)
{
print "blank line \n";
#I need something HERE!!
$lastLineWasBlank = 1;
}
print "$count \n";
$count++;
}
I'd go like this but there's probably other ways to do it:
for ( my $i = 0 ; $i < #rawData ; $i++ ){
if ( $rawData[$i] =~ /^\s*$/ ){
print $rawData[$i + 1] ; ## plus check this is not null
}
}
J.
sh> perl -ne 'if ($b) { print }; if ($b = !/\S/) { ++$c }; END { print $c,"\n" }'
Add input filename(s) to your liking.
Add a variable like $lastLineWasBlank, and set it at the end of each loop.
if ( $lastLineWasBlank )
{
print "blank line\n" . $LineVar;
}
something like that. :-)