0

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 5 and then the value of the product itself is added. THey also have a multiplier.

eg. if a price is 210.13 dollars, the administration cost needs to be added so the price becomes 215.13 dollars. Then it has to be multiplied by 1.85 so the new price will be: 397,99. The prices also need to be rounded per 5 dollars and always up. So in this case, the final price will be 400. I already got that function, but it only seems to work for one column...

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

Lemme first give my code:

<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['price']}<br></td>
	<td align="left" width="0%"></td>

HTML;

    $row = mysql_fetch_assoc($result);
    if (!$row){
      echo "<td>&nbsp;</td><td>&nbsp;</td>";
    } else {

    echo <<<HTML

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

	{$row['name']}<br>
	{$row['description']}<br />
	{$row['price']}<br></td>
	<td align="left" width="0%"></td>
HTML;
	}

    $row = mysql_fetch_assoc($result);
    if (!$row){
      echo "<td>&nbsp;</td><td>&nbsp;</td>";
    } else {

    echo <<<HTML

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

	{$row['name']}<br>
	{$row['description']}<br />
	{$row['price']}<br></td>
	<td align="left" width="0%"></td>

HTML;

    $row = mysql_fetch_assoc($result);
    if (!$row){
      echo "<td>&nbsp;</td><td>&nbsp;</td>";
    } else {

    echo <<<HTML

    <td align="center" width="25%"><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>

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 really don't see it, nor can I find anthing on the web. Been searching endlessly for days now.

thanks!

3
Contributors
6
Replies
7
Views
7 Years
Discussion Span
Last Post by Bar2aYunie
0

In line 11, you are assigning your calculation to $row and never using it anywhere.

Your calculations do have to be done for each product record if you want them adjusted.

There should be some price showing for all your records providing there is a price in the DB. But it definitely won't be adjusted the way you have it.

NOTE: I didn't try to run this or look for all errors. I just restructured a bit and commented.

// Since you're doing this multiple times, put it in a function
function ouputproduct ($_row) 
{
    if (!$row)
    {
    	echo "<td>&nbsp;</td><td>&nbsp;</td>";
    } else {
		$_row['price'] = (ceil(($_row['price']+5) *1.85)/5)*5; // calculation does have to be done everytime
		
	    echo <<<HTML
	
	    <td align="center" width="25%"><img src={$_row['thumb_name']}><br>
	
		{$_row['name']}<br>
		{$_row['description']}<br />
		{$_row['price']}<br></td>
		<td align="left" width="0%"></td>
	
		HTML;
    }
}  // end outputproduct()

	echo <<<HTML

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

HTML;

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

	echo "<tr>";
	outputproduct($row);
	$row = mysql_fetch_assoc($result) // fetching $row inside while is not my ideal way of doing this
	outputproduct($row);
	$row = mysql_fetch_assoc($result)
	outputproduct($row);
	$row = mysql_fetch_assoc($result)
	outputproduct($row);
	
	echo "</tr>";
} //end while

echo <<<HTML

</tbody>
<tfoot>
<tr>
    <p><th colspan="4">
        $pageNavBar
    </th>
</tr>
</tfoot>
</table>

HTML;
0

Thank you for your reply, though I get a parse error at the end of the wholen page. I've checked the whole script a few times, but I don't see what's missing.

Maybe do you need the rest of the coding as well? (though I haven't changed anything there, and that part also doesn't seem to miss any code...

0

please post the whole page so we can see what you're working with now.

I just noticed that I forgot my ; on lines 36,38,40

May be your problem if you didn't catch that already.

Edited by LethargicCoder: n/a

0

Yeah I already had fixed those three, but that was not the fix for the problem.

Here's my compleet original code. I just replaced the whole table on my code with your table. Then I added a closing HTML; above your code and deleted the double closing HTML; after your code (I already had one there).

Then later, I also deleted the whole <style> part and the navi parts of code below your code (so that after your code the script would be done). That also gave me the same parse error. I thought I might need the navi part, but if the code works better without it, I can leave it out and just have everything on the same page. I think it might even be better. (it was not when I creatd the code, but now... leave it out)

Here's my latest code, using your script for the table construction. That still gives me the parse error.

I think these codes might help you to see the whole script and I thought it might be better to post it online, instead of on this page, so the page won't be that long, haha. You can simply paste the code into an editor (like dreamweaver) to read it better if you'd like.

thx for helping out!!

0

This is the complete error:

Parse error: syntax error, unexpected $end in /Folders_Formaten.php  on line 304

That line is referring to the compleet end of the page. Actually that line is emtpy, but the last bit of code is </html>

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.