basic php form help (currency display) - php

You guys were very helpful yesterday. I am still a bit confused here though.
I want to make it so that the numbers on the rightmost column are rounded off to the nearest dollar:
http://www.nextadvisor.com/voip_services/voip_calculator.php?monthlybill=50&Submit=Submit
the code for the table looks like this:
I want $offer[1,2,3,4,5,6,7]calcsavingsann to be rounded, how can do this?
<table width="100%;" border="0" cellspacing="0" cellpadding="0"class="credit_table2" >
<tr class="credit_table2_brd">
<td class="credit_table2_brd_lbl" width="100px;">Services:</td>
<td class="credit_table2_brd_lbl" width="120px;">Our Ratings:</td>
<td class="credit_table2_brd_lbl" width="155px;">Monthly VoIP Bill:</td>
<td class="credit_table2_brd_lbl" width="155px;">Annual Savings:</td>
</tr>
<?php
$offer1price="24.99";
$offer2price="20.00";
$offer3price="21.95";
$offer4price="23.95";
$offer5price="19.95";
$offer6price="23.97";
$offer7price="24.99";
$offer1calcsavings= $monthlybill - $offer1price;
$offer2calcsavings= $monthlybill - $offer2price;
$offer3calcsavings= $monthlybill - $offer3price;
$offer4calcsavings= $monthlybill - $offer4price;
$offer5calcsavings= $monthlybill - $offer5price;
$offer6calcsavings= $monthlybill - $offer6price;
$offer7calcsavings= $monthlybill - $offer7price;
$monthybill="monthlybill";
$offer1calcsavingsann= $offer1calcsavings * 12;
$offer2calcsavingsann= $offer2calcsavings * 12;
$offer3calcsavingsann= $offer3calcsavings * 12;
$offer4calcsavingsann= $offer4calcsavings * 12;
$offer5calcsavingsann= $offer5calcsavings * 12;
$offer6calcsavingsann= $offer6calcsavings * 12;
$offer7calcsavingsann= $offer7calcsavings * 12;
$re=1;
$offer ='offer'.$re.'name';
$offername= ${$offer};
while($offername!=""){
$offerlo ='offer'.$re.'logo';
$offerlogo=${$offerlo};
$offerli ='offer'.$re.'link';
$offerlink=${$offerli};
$offeran ='offer'.$re.'anchor';
$offeranchor=${$offeran};
$offerst ='offer'.$re.'star1';
$offerstar=${$offerst};
$offerbot='offer'.$re.'bottomline';
$offerbottomline=${$offerbot};
$offerca ='offer'.$re.'calcsavings';
$offercalcsavings=${$offerca};
$offerpr ='offer'.$re.'price';
$offerprice=${$offerpr};
$offersavann ='offer'.$re.'calcsavingsann';
$offercalcsavingsann=${$offersavann};
echo '<tr >
<td >
<a href="'.$offerlink.'" target="blank"><img src="http://www.nextadvisor.com'.$offerlogo.'" alt="'.$offername.'" />
</a>
</td>
<td ><span class="rating_text">Rating:</span>
<span class="star_rating1">
<img src="http://www.nextadvisor.com'.$offerstar.'" alt="" />
</span>
<br />
<div style="margin-top:5px; color:#0000FF;">
Go to Site
<span style="margin:0px 7px 0px 7px;">|</span>Review
</div> </td>
<td >$'.$offerprice.'</td>
<td >$'.$offercalcsavingsann.'</td>
</tr>';
$re=$re+1;
$offer ='offer'.$re.'name';
$offername= ${$offer};
}
?>
</table>

Do you want rounded up/down/truncated to the nearest dollar?
Here are some suggested functions you can use:
Rounding
round
floor
ceil
Formatting/Truncating
sprintf

Grepsedawk's answer is good; the only thing I would add is that rather than displaying $336.6, for example, you could could use number_format to output $336.60.
(I know this wasn't your question, but looking at the link, I thought that might be useful for you.)
Edit - Thanks to Andy for suggesting money_format instead.

money_format() is a function that returns a string value of a formatted number. You have control over the formatting and, obviously, your number. A simple example, if you have your value in the variable $myNumber, you could incorporate the result into a given table's data cell like so;
<?php echo ("<td>".money_format('%n',$myNumber)."</td>"); ?>
And you would need to do this for every value, e.g. via a for loop if you had all your values in an array. The n here is one of the formatting options - there are several. A good place to look would be on the PHP web page at http://au2.php.net/manual/en/function.money-format.php
Hope this helps.

I can't seem to get the usage right. The way I am using echo is
echo '<tr >
<td ><img src="http://www.nextadvisor.com'.$offerlogo.'" alt="'.$offername.'" /></td>
<td ><span class="rating_text">Rating:</span><span class="star_rating1"><img src="http://www.nextadvisor.com'.$offerstar.'" alt="" /></span><br />
<div style="margin-top:5px; color:#0000FF;">Go to Site<span style="margin:0px 7px 0px 7px;">|</span>Review</div> </td>
<td >$'.$offerprice.'</td>
<td >$'.$offercalcsavingsann.'</td>
</tr>';
I put the "set locale" up where the
"<?php"
is. I don't understand how to write it, and every way I do just returns an error.

Related

Properly use single and double quatation in PHP

I have my PHP code like below and its work fine
<tr onclick="window.location='../info/<?php echo $ouid;?>';" class="tbl_rated_orders">
Now I am loading more data on press load more button via ajax
so I am trying like this
$tableData.= '<tr class="tbl_rated_orders_buyer" onclick="window.location='.'"../info/'.$ouid.';">
<td>'.$date_rated.'</td>
<td><img src="../../global_assets/uploads/users/'.$image.'" width="35" class="rounded-pill" alt="">
<span class="ml-2">'.$rated_row["full_name"].'</span>
</td>
<td>'.$cancelled_by_txt.'</td>
<td><i class="icon-paperplane text-dark mr-1"></i>'.$str1.'</td>
</tr>';
But now tr html code looks like this
<tr class="tbl_rated_orders_buyer" onclick="window.location=" ..="" info="" zxp2ibhfnu;"="">
so its not correct code for window location, its need like this
<tr onclick="window.location='../info/RQ5KWBIY6M';" class="tbl_cancelled_orders">
I am new in PHP and trying from half hour to make it working but not getting idea how I can do it, Let me know if anyone here can help me for do the same.
Thanks!
You will need to escape some single quotes for the single quotes used in the javascript in the onclick attribute.
$ouid = "asd0f8a08f0";
$date_rated = "12/03/2021";
$image = "north_pole.jpg";
$rated_row["full_name"] = "Mighty Mouse";
$cancelled_by_txt = "grinch";
$str1 = "Paper Plane";
$tableData = "";
$tableData.= '<tr class="tbl_rated_orders_buyer" onclick="window.location='.'\'../info/'.$ouid.';\'">
<td>'.$date_rated.'</td>
<td><img src="../../global_assets/uploads/users/'.$image.'" width="35" class="rounded-pill" alt="">
<span class="ml-2">'.$rated_row["full_name"].'</span>
</td>
<td>'.$cancelled_by_txt.'</td>
<td><i class="icon-paperplane text-dark mr-1"></i>'.$str1.'</td>
</tr>';
Output:
<tr class="tbl_rated_orders_buyer" onclick="window.location='../info/asd0f8a08f0;'">
<td>12/03/2021</td>
<td><img src="../../global_assets/uploads/users/north_pole.jpg" width="35" class="rounded-pill" alt="">
<span class="ml-2">Mighty Mouse</span>
</td>
<td>grinch</td>
<td><i class="icon-paperplane text-dark mr-1"></i>Paper Plane</td>

PHP equation within html table row, or in database itself

I have a site with a CSV upload, which pushes everything in the CSV to a temp table and then splits into smaller tables.
Currently, I have a display page that displays all of that info in HTML tables. Some sections, however, need to have a formulaic representation. In other words, as seen in the screenshot below, meter volume is divided by tester volume and the sum of that is multiplied by the tester accuracy. That number is divided by 100 to give the corrected accuracy.
Here is the code I have but it's not loading my web page and I think the PHP might be wrong in establishing the variables:
<table style="width:100%; border:none;
border-collapse:collapse;">
<? php
$test1FormA = $row['test1MeterVol'] / $row['test1TesterVol'];
$test1FormB = $test1FormA * $row['test1Accuracy'];
$test1FinalForm = $test1FormB / 100;
?>
<tr>
<td style="border:none; text-align: left;">Meter Volume: </td>
<td><? echo $row['test1MeterVol'];?> </td>
</tr>
<tr>
<td style="border:none; text-align: left;">Tester Volume: </td>
<td><? echo $row['test1TesterVol'];?> </td>
</tr>
<tr>
<td style="border:none; text-align: left;">Tester Accuracy: </td>
<td><? echo $row['test1Accuracy'];?> </td>
</tr>
<tr>
<td style="border:none; text-align: left;">Corrected Accuracy: </td>
<td><? echo $test1FinalForm;?> </td>
</tr>
</table>
There is currently no corrected accuracy in the database, so I wanted to find either a way to do it per line, like above, or a way to do that upon every CSV upload. There are 8 tests, so it would have to do that formula for 8 different fields per 5 records in the CSV upon upload. I currently have the CSV uploading into the temp table with a large insert statement.
Is there a way to just do this in the table row with PHP?
UPDATE: Screenshot of new results
UPDATE: Code for averaging issue
<?php
$sum=0;
for($i=1; $i<=8; $i++){
$testFormA = $row["test".$i."MeterVol"] / $row["test".$i."TesterVol"];
$testFormB = $testFormA * $row["test".$i."Accuracy"];
$testFinalForm = $testFormB / 100;
$sum += $testFinalForm;
echo"$sum";
?>
<td><?php echo round($testFinalForm,3) ?>%</td>
<?php }
$average = $sum / 8;
echo"$average";
?>
<td><?php echo round($average,3)?> </td>
There are many ways to do what you need. But the most important part is that you will need a loop structure to build your table with a pretty code, without repeating everything over and over.
In your case, you can use the following code to build your whole last row for all 8 tests:
<?php for($i=1; $i<=8; $i++){
$testFormA = $row["test".$i."MeterVol"] / $row["test".$i."TesterVol"];
$testFormB = $testFormA * $row["test".$i."Accuracy"];
$testFinalForm = $testFormB / 100;
?>
<td><?php echo $testFinalForm ?></td>
<?php } ?>
Instead of accessing indexes like "test4MeterVol" you use $i to build the index string to access the right value in each loop iteration.
Doing the equations inside the loop will give you different values based on each test.
Based on this code and the way to build indexes to access your data you should be able to build the other rows, which are much simpler.

Firefox not showing results of my php code

So I have problem with firefox not showing results of search form for query i enter, but code is working normally in Opera,Chrome,IE and Android default browser.
This is the search form :
<div id="menu_right">
<div id="searchBar">
<form id='search_form' method='POST' action='index.php?menu=search'>
<input id="topSearchText" class="inputText" type="text" name="movie" size="30" onfocus="this.value=''" value="Search" style="color: rgb(119, 119, 119);"/>
<input type="image" name="search_movies" value="Search" src="./images/search.png"/>
</form>
</div>
and this is the php part of the code:
<table width='100%' cellspacing='0' cellpadding='0' border='0'>
<tbody>
<tr>
<td class='normal_header' colspan='2'>Search Results</td>
<td class='normal_header' width='40' align='center' nowrap=''>Score</td>
</tr>
<?php
if(isset($_POST['search_movies']))
{
$movie=$tmdb->searchMovie($_POST['movie']);
foreach ($movie['results'] as $value) {
$filepath = $value['poster_path'];
$image_url = $tmdb->getImageUrl($filepath, TMDb::IMAGE_POSTER, 'w92');
echo "
<tr>
<td class='borderClass bgColor' width='50' valign='top'>
<div class='picSurround'>
<a href='http://www.themoviedb.org/movie/{$value['id']}'>
<img border='0' src='{$image_url}'>
</a>
</div>
</td>
<td class='borderClass bgColor' valign='top'>
<a href='http://www.themoviedb.org/movie/{$value['id']} '>
<strong>{$value['title']}</strong>
</a>
<a class='button_add' title='Quick add movie to my list' href='addmovie.php?id={$value['id']}'>add</a>
<div class='spaceit'>
<a href='http://www.themoviedb.org/movie/{$value['id']}'>Read more about: {$value['title']}</a>
</div>
</td>
<td class='borderClass bgColor' align='center'>{$value['vote_average']}</td>
</tr>";
}
}
?>
</tbody>
Is it something wrong with my query or maybe some tag because I cant seem to find the problem.
Have you checked the error_log that was possibly created?
Also.. and I might be wrong here.. but as far as I know php only interprets code between double quotes "", not between single quotes ''.
So for instance:
$test = 5;
echo "this string shows the value of $test"
echo 'this string just shows the string $test';
If this is a search form based on the results of the input field then it should be;
isset($_POST['movie'])
Look at this question :
HTML Input (type=image) not working on Firefox 4
Maybe your problem comes from the way firefox handles image's input tag.
Edit : More infos in the accepted answer to this question : Code working in Chrome but not in Firefox
This seem to be a common "problem" with image-type inputs.

Correct syntax using JS 'onclick' functions inside PHP echo statements AND with embedded XML fetching

I am likely to get b'*ch slapped because I haven't searched the forum enough before posting, but I really think I searched all relevant posts already, many seem not specifically covering the question I have, the others fly right over my beginner's head ( as I am new to PHP & js ). That being said...
I have built a PHP code to fetch data from an XML file using the $xml=simplexml_load_file();
No worries there, however one of the needed data 'entries' or 'fields' needs to exist within an onclick and/or an onmouseup javascript function.
The code is as follows:
<?php
$xml=simplexml_load_file('prod_test_feed_sameday.xml');
$max = 8;
$i = 1;
foreach($xml->product as $feed){
if ($i <= $max){
echo "<table id='{$feed->position}' class='prod_container'>
<td class='hidden_mask' id='{$feed->position}'>
</td>
<td class='hidden_image' id='{$feed->position}'><span style='background:url({$feed->prod_image_large}) no-repeat center;'/></span><div><a onmouseup='$('.hidden_image#{$feed->position}').slideToggle('slow');' onclick='$('.hidden_mask#{$feed->position}').hide();'><b>CLOSE</b></a></div>
</td>
<tr>
<td class='prod_image' id='{$feed->position}'>
<span style='background:url({$feed->prod_image_small}) no-repeat center; background-size:contain;'/></span>
</td>
<td rowspan='1' class='info_toggle' id='{$feed->position}'><a onmouseup='$('.hidden_image#{$feed->position}').slideToggle('slow');' onclick='$('.hidden_mask#{$feed->position}').show();><img src='images/zoom_02.png' title='See a larger image of these flowers' /></a>
</td>
</tr>
<tr>
<td colspan='2' class='prod_name' id='{$feed->position}'>{$feed->prod_name}
</td>
</tr>
<tr>
<td colspan='2' class='prod_price' id='{$feed->position}'><span id='{$feed->position}'>from: £{$feed->price}</span><a href='{$feed->prod_link}' target='_blank'><span class='buy_button'> Buy it now! </span></a></td>
</tr>
</table>";
$i++;
}
}
?>
The data and the CSS all work perfectly. There is a href link towards the end of the code, which also works perfectly.
I am racking my brain trying to find the error in my syntax within the onlick function.
I have tried all manners of backslashing, using trial and error, for exampel:
onclick='$(\'.hidden_mask#{$feed->position}\').hide();' or
onclick='\'$('.hidden_mask#{$feed->position}').hide();\'' or
onclick=\''$(\'.hidden_mask#{$feed->position}\').hide();\'' or even
onclick=\''$(\\'.hidden_mask#{$feed->position}\\').hide();\'' <--Freddy Krugar 'slashing' example
At any rate I am at a loss.
Try with double quotes and escape them:
echo " ...
onclick=\"$('.hidden_mask#{$feed->position}').hide();\"
...";
Or
echo " ...
onclick='$(\".hidden_mask#{$feed->position}\").hide();'
...";
DEMO
This way you do the escaping in the PHP only, without needing the Freddy Krugar escaping for the DOM parser.

Table inside a loop

I have one table in loop which come under li:
<?php
for($i=1;$i<=$tc;$i++)
{
$row=mysql_fetch_array($result);
?>
<li style="list-style:none; margin-left:-20px">
<table width="600" border="0" cellspacing="0" cellpadding="0">
<tr>
<td class="hline" style="width:267px"><?php echo $row['tit'] .",". $row['name'] ?></td>
<td class="vline" style="width:1px"> </td>
<td class="hline" style="width:100px"><?php echo $row['city']; ?></td>
</tr>
</table>
</li>
<?php
}
?>
The output comes like this:
alt text http://img20.imageshack.us/img20/4153/67396040.gif
I can't put table outside the loop, due to <li> sorting
if you can't use table outside the loop then i think best option will be use of
<div>
statement
for example
<div class="q-s na">
<div class="st" style="margin-right:10px; width:150px">
<div class="m-c"><?php echo $row['tit'] .",". $row['name'] ?></div>
</div>
this will be same as you one
<td>
you can define style according to your requirements.
for example
<style>
.q-s{overflow:hidden;width:664px;float:left;
padding-top:2px; padding-bottom:2px; height:25px}
.na .st{ float:left;}
.na .m-c {border-bottom:1px dotted #999; padding-bottom:10px; height:15px}
</style>
i can't put table outside the loop.
Why not? This is where it belongs. After all, you (logically) produce one table, not many of them. No need for the list item, either.
If you're unable to put the table outside of the loop, you should probably just use tags rather than creating tables, as you're defeating the purpose of even having a table by making each table a single row and trying to stack them.
Another thing to note if you are going to stick with tables:
If you're hard-coding the table width AND all of the table cell (column) widths, it may cause unexpected issues when they don't add up:
Table width = 600px
Cells = 267 + 1 + 100 = 368px

Categories