Jump to content

Calculate in php


Bar2aYunie

Recommended Posts

Hello,

 

I have a sql database which contains the names and descriptions of a product and the price. However, all products have a starting price of 10 and then the value of the product itself is added. Meaning, if a product costs 100 dollars, there's a standard cost of 10, so the total price would be 110 dollars.

 

Now I could calculate that manually, but as there are many products... I am looking for a way to do that with php.

 

This is my code:

 

<?php include ("../Connection.txt"); ?>



<?php



$id = $_GET['id'];


$maxcol = 4;

$result = mysql_query("SELECT * FROM Folders order by id desc") or die (mysql_error());

$count = mysql_num_rows($result);

$rows = ceil($count/$maxcol);




echo "<table>";



$limit = 20;

$table = 'Folders';

//get requested page

$page = empty($_GET['page']) ? 1 : (int) $_GET['page'];

//calculate offset

$offset = ($page - 1) * $limit;



//construct query

$query = "Select SQL_CALC_FOUND_ROWS * from $table order by name desc LIMIT $limit OFFSET $offset";



//execute query

$result = mysql_query($query) or die (mysql_error());

$cResult = mysql_query("Select found_rows()") or die(mysql_error());

list($count) = mysql_fetch_array($cResult, MYSQL_NUM);

$pageNavBar = getPages($limit, $count, $page);

//start the output

echo <<<HTML

<table width="100%" border="0" rules="groups" table class="table">

<thead>

</thead>

<tbody>

HTML;

while ($row = mysql_fetch_assoc($result)){

    echo <<<HTML

<tr>

    <td align="center" width="50%"><img src={$row['thumb_name']}><br>

{$row['name']}<br>
{$row['description']}<br />
{$row['price']}<br>

</td>
<td align="left" width="0%"></td>

HTML;

} //end if

   echo "</tr>";

} //end while



echo <<<HTML

</tbody>

<tfoot>

<tr>

    <p><th colspan="4">

        $pageNavBar

    </th>

</tr>

</tfoot>

</table>

 

In the middle, you'll see: {$row['price']}. Now somewhere, the value + 10 needs to be added. I've tried endless things, but either it doesn't work and I get an error, or the normal price is displayed or only the added number (thus 10).

 

Can somebody please help me out?

 

thanks!!

Link to comment
Share on other sites

You won't be able to do math inside of a heredoc block. So you can either split it up or just do this:

 

while ($row = mysql_fetch_assoc($result)){
$row['price'] += 10;
...

Link to comment
Share on other sites

The only line you need to insert is:

 

$row['price'] += 10;

 

I posted another part of you code just to show you a reference for where it should go. Right after:

 

while ($row = mysql_fetch_assoc($result)){

Link to comment
Share on other sites

Ah I'm sorry, I didn't see it. Thank you for the help.

However, the price for a product is for example 285,25. After adding that line, the price has been changed to 925. Which actually isn't exactly + 10. Any idea why this is happening?

Link to comment
Share on other sites

Yeah I know, I'm not working with even prices.... For my first product on the page, the price is 285,25 dollar. And it has a starting price (administration costs) of 10 dollars.

 

Another product also has a multiplyer of 1,85 (which is 185%). And the starting price is 5. I also have to insert that somewhere. Do you know how, and if so, could you please tell me?

 

Thank you again!

Link to comment
Share on other sites

well I have a table containing one of the products, and in the table there's the name, description, size and so on and then the price so it states:

 

amount1 = 500

price1 = 285,25

amount2 = 1000

price2 = 423,67

(10 amounts and 10 prices per product)

 

and so on.

Previously, I was calculating all of the prices manually before inserting the prices into the tables. But now I'd like to have the standard prices for the prouducts and then insert the multiplyer and the administration cost into the script. So that when those change later on, I don't have to change thousands of prices manually, but just those two values in the script.

 

Was that the info you needed?

Link to comment
Share on other sites

Well I'm guessing you're not using a numerical data type to store the prices, as I don't believe MySQL has support for using a comma as the decimal separator, I'm not entirely certain but it doesn't make any mention of it here. So you should be storing the prices in a field of type FLOAT using periods as the decimal separator. Then you can do whatever math you want with the number in PHP. If you want to convert it back to using a comma as the decimal separator that is something you can have it change before it's outputted.

Link to comment
Share on other sites

Well I'm not exactly sure what you meant...  But I found out that I can use . instead of , to calculate it. So I changed the value of 285,25 in the table to 285.25 (since I noticed that the ,25 a the end was deleted once I set the row to float).

 

And (while I was waiting for your reply) I did some more searching and I found this.

 

That's how I got it to have 2 decimals! :D So now I changed that last part of code to:

 

$row['price1'] = round ((($row['price1']+5) *1.85),2);

 

And, as price1 has a value of 285,25.... I now get a value of 536.96!

 

The only thing is, some of the prices need to be rouned per 5 dollars. Meaning 536.96 should be rounded to 540. Any idea how I can round those?

Link to comment
Share on other sites

I think I've got something...

 

	$row['prijs1'] = round ((($row['prijs1']+5) *1.85),0);
$base_m=5;
$row['prijs11'] = $base_m*(ceil(($row['prijs1'])/$base_m));

 

That gives me 540 as a result. So looks good. I do have another problem....

 

I left a part of the code out, cos I thought it might be too much at once. But that part gives me no result at all at this point. Here's the total code:

 

<?php include ("../Connection.txt"); ?>



<?php



$id = $_GET['id'];


$maxcol = 4;

$result = mysql_query("SELECT * FROM Folders order by id desc") or die (mysql_error());

$count = mysql_num_rows($result);

$rows = ceil($count/$maxcol);




echo "<table>";



$limit = 20;

$table = 'Folders';

//get requested page

$page = empty($_GET['page']) ? 1 : (int) $_GET['page'];

//calculate offset

$offset = ($page - 1) * $limit;



//construct query

$query = "Select SQL_CALC_FOUND_ROWS * from $table order by formaat desc LIMIT $limit OFFSET $offset";



//execute query

$result = mysql_query($query) or die (mysql_error());

$cResult = mysql_query("Select found_rows()") or die(mysql_error());

list($count) = mysql_fetch_array($cResult, MYSQL_NUM);



$pageNavBar = getPages($limit, $count, $page);

//start the output

echo <<<HTML

<style type="text/css">

/*style links */

.pageNav a {text-decoration:none; cursor:crosshair; color: #131d31;}

.pageNav a:visited {text-decoration:none; color: #131d31;}

/*make the page numbering smaller generally */

.pageNav{ font-size:smaller;}

.page{ border: thin dotted #060409; background-color:#fdffeb;}

.navi{ padding: 5px; margin: 2px; width: 2em;}

.prev{}

.last{ background-color:;}

.first{ background-color:;}

.next{}

.active {backround-color:grey; color:red;}

.select select {}

.table {font-family: verdana; font-size: 12px;}







</style>

<table width="100%" border="0" rules="groups" table class="table">

<thead>

</thead>

<tbody>

HTML;

while ($row = mysql_fetch_assoc($result)){
$row['prijs1'] = round ((($row['prijs1']+5) *1.85),0);
$base_m=5;
$row['prijs11'] = $base_m*(ceil(($row['prijs1'])/$base_m));


    echo <<<HTML

<tr>

    <td align="center" width="50%"><a href="/Producten/Flyers/Flyers_Bedrukking_A7L.html"><img src={$row['thumb_formaat']}><br>

{$row['formaat']}<br>
{$row['eindformaat']}<br />
{$row['prijs11']}</a><br>

</td>
<td align="left" width="0%"></td>

HTML;

    $row = mysql_fetch_assoc($result);

    if (!$row){

      echo "<td> </td><td> </td>";

    } else {

    echo <<<HTML

    <td align="center" width="50%"><a href="/Producten/Flyers/Flyers_Bedrukking_A7L.html"><img src={$row['thumb_formaat']}><br>

{$row['formaat']}<br>
{$row['eindformaat']}<br />
{$row['prijs1']}</a><br>

</td>
<td align="left" width="0%"></td>

HTML;



} //end if

   echo "</tr>";

} //end while



echo <<<HTML

</tbody>

<tfoot>

<tr>

    <p><th colspan="4">

        $pageNavBar

    </th>

</tr>

</tfoot>

</table>

HTML;



function getPages($limit, $count, $page){

    //put the url into a variable

    $s = "http://" . $_SERVER['HTTP_HOST'] .'/'.  ltrim($_SERVER["SCRIPT_NAME"] ,'/');

    //calculate the number of pages needed

    $nPages = ceil($count/$limit);

    //do the first/last prev/next buttons

    $first = <<<HTML

<span class="first navi">

    <a href="$s?page=1"><<<</a>

</span>

HTML;

    $last = <<<HTML

<span class="last navi">

    <a href="$s?page=$nPages">>>></a>

</span>

HTML;

    if ($page  > 1) {

        $p = $page - 1;

        $prev = <<<HTML

    <span class="next navi">

        <a href="$s?page=$p"><</a>

    </span>

HTML;

    } else {

        $prev = ' ';

        $first = ' ';

    }

    if ($page < $nPages) {

        $p = $page + 1;

        $next = <<<HTML

    <span class="next navi">

        <a href="$s?page=$p">></a>

    </span>

HTML;

    } else {


        $next = ' ';

        $last = ' ';

    }

    

    //now construct the pages

    //if more than 10 then use a select

    if ($nPages > 10){

        $output = <<<HTMLJS

<span class="navi select">

    <select name="page" onchange="window.location='{$s}?page=' + this.options[this.selectedIndex].value;">

HTMLJS;

    

        for ($p=1; $p <=$nPages; $p++){

            $output .= "<option value=\"$p\">$p</option>";

        }

        $output .= '</select></span>';

    } else {

        $output = '';

        for ($p=1; $p<=$nPages; $p++){

            $active = ($p == $page) ? 'active' : '';

            $output .= "<span class=\"navi page {$active}\"><a href=\"$s?page=$p\">$p</a></span>";

        }

    }

    return '<div class="pageNav">' . $first . $prev. $output . $next .  $last . '</div>';

}



?>

 

As you can see I've got another column with the same set of data... I actually need four columns with in all four columns the same data. It has four columns at this point, but two of them are empty. If I fill the second and fourth column with the same code, it only copies the code from the previous column, thus column one and three.

 

In column three, there's no price at all.... Any idea why? Maybe it's easier to fix if I point out my plan in the end... I need four columns like this:

 

image1  - image2  -  image3  -  image4

infotxt1 -  infotxt2 -  infotxt3  -  infotxt4

 

I can't seem to get that fixed either. Can you help out with these two problems?

Link to comment
Share on other sites

(don't know where to edit my post, so I need to post an update)

 

I fixed my column problem, now I only got the calculating problem there. Let me post the table one more time the way it is now:

 

<table width="100%" border="0" rules="groups" table class="table">

<thead>

</thead>

<tbody>

HTML;

while ($row = mysql_fetch_assoc($result)){
$row['price'] = round ((($row['price']+5) *1.85),0);
$base_m=5;
$row['price1'] = $base_m*(ceil(($row['price'])/$base_m));


    echo <<<HTML

<tr>

    <td align="center" width="25%"><img src={$row['thumb_name']}><br>

{$row['name']}<br>
{$row['description']}<br />
{$row['price1']}<br>

</td>
<td align="left" width="0%"></td>

HTML;

    $row = mysql_fetch_assoc($result);

    if (!$row){

      echo "<td> </td><td> </td>";

    } else {

    echo <<<HTML

    <td align="center" width="25%"><img src={$row['thumb_name']}><br>

{$row['name']}<br>
{$row['description']}<br />
{$row['price1']}<br>

</td>
<td align="left" width="0%"></td>

HTML;

}

    $row = mysql_fetch_assoc($result);

    if (!$row){

      echo "<td> </td><td> </td>";

    } else {

    echo <<<HTML

    <td align="center" width="25%"><img src={$row['thumb_name']}><br>

{$row['name']}<br>
{$row['descrription']}<br />
{$row['price1']}<br>

</td>
<td align="left" width="0%"></td>

HTML;

    $row = mysql_fetch_assoc($result);

    if (!$row){

      echo "<td> </td><td> </td>";

    } else {

    echo <<<HTML

    <td align="center" width="25%"><img src={$row['thumb_name']}><br>

{$row['name']}<br>
{$row['description']}<br />
{$row['price1']}<br>

</td>
<td align="left" width="0%"></td>

HTML;
}

} //end if

   echo "</tr>";

} //end while



echo <<<HTML

</tbody>

<tfoot>

<tr>

    <p><th colspan="4">

        $pageNavBar

    </th>

</tr>

</tfoot>

</table>

 

As you can see, I added a function to get the price from the database and then add the administration fee and then add the multiplier. However, this function only seems to work for the first column. The others won't display any price.

 

Does anybody know how to fix this? Am I doing something wrong? I also tried adding the same line in again and renaming the function (to price2 with same calculation), but that also dosen't work. Do I need to apply the function for all columns for it work or something?

 

I'd be grateful if somebody can help me out, I don't see it!

 

Thx!

Link to comment
Share on other sites

OP? You mean the database?

 

Well, I am using european decimals, lol... But the database is set to float and I must use us decimals... not that either one of those bothers me, haha.

 

Why you ask? Or did this even answer your question?

Link to comment
Share on other sites

Ah right, I get it. So now for my curiosity... what is OP?

 

Well it is a number, it's a price in fact. Do you perhaps know the answer? I cannot figure out why it doesn't work, or (even worse) how ti fix it... I already have the function... I only need to know how to apply it to all columns....

Link to comment
Share on other sites

another way ignore php coding if its just too complicated for you, do it with a view in mysql where the view adds 10 euros right in the result sent to php  ::)

 

Sounds good, can I add the multiplier there as well? If so, how do I do that?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.