Jump to content

Calculate Total Price


doubledee

Recommended Posts

I have an HTML table with the following columns:

- Event

- Price

- # of Attendees

- Total

- Choose One

 

The values are populated from a database, and I am using a loop to create the Table.

 

The problem is that I have some syntax error where I am trying to calculate the Total Ticket Price?!

 

Can someone help me figure what is wrong with my code?

 

		<!-- Body -->
		<tbody>
			<?php
				// Fetch Comment record.
				$x=0;
				while (mysqli_stmt_fetch($stmt)){
					//<!-- Row 1 -->
					echo '
					<tr>
						<th scope="row" class="headerCol">';
					echo 
							$eventName . '<br />' .
							$eventLocation . '<br />' .
							$eventDate . '
						</th>
						<td class="col2">' .
							$eventPrice .
							'<input type="hidden" name="eventCost[' . $x . ']" value="' . $eventPrice . '" />
						</td>
						<td class="col3">
							<select name="eventAttendees[' . $x . ']">
								<option value="">--</option>
								<option value="1">1</option>
								<option value="2">2</option>
								<option value="3">3</option>
								<option value="4">4</option>
							</select>
						</td>
						<td class="col4">' . $eventCost*eventAttendees[$x] . '</td>
						<td class="col5">
							<input name="eventChosen[' . $x . ']" type="submit" value="Buy Tickets" />
						</td>
					</tr>';
				}
			?>
		</tbody>

 

What I am trying to do/say on each row is "Take the Event Price and multiple it times the # of Attendees selected in the Row's Drop-Down field."

 

Thanks,

 

 

Debbie

 

 

Link to comment
https://forums.phpfreaks.com/topic/249680-calculate-total-price/
Share on other sites

Well, it would have been nice to see the actual error you received. I believe your problem is due to the line where you do the actual calculation

echo ' . . .
<td class="col4">' . $eventCost*eventAttendees[$x] . '</td>
. . . ';

 

Because you are trying to calculate something while at the same time append it to the echo. The PHP parser is getting confused, because it first tries to append "$eventCost" to the string and then it encounters the asterisk indicating you want to multiply the previous value (which is the entire string up to the point) and the next value "eventAttendees[$x]". Well, there are two problems there, the entire string preceding that asterisk is not a mathematical value and "eventAttendees[$x]" isn't a variable. I assume there should be a dollar sign precedence that.

 

 

Just like in match, you sometimes need to put some operations within parens to determine the order. You can do that within the echo, but a better way is to simply calculate the total and assign to a variable before you start the echo. You can also make that code much more readable than it is now.

			<!-- Body -->
		<tbody>
			<?php
				// Fetch Comment record.
				$x=0;
				while (mysqli_stmt_fetch($stmt))
                    {
					//<!-- Row 1 -->
					$sub_total = $eventCost * $eventAttendees[$x];
					echo "
					<tr>
						<th scope='row' class='headerCol'>
                              $eventName<br />
                              $eventLocation<br />
                              $eventDate
						</th>
						<td class='col2'>
                              $eventPrice
                              <input type='hidden' name='eventCost[$x]' value='$eventPrice' />
                            </td>
                            <td class='col3'>
                              <select name='eventAttendees[$x]'>
                                <option value=''>--</option>
                                <option value='1'>1</option>
                                <option value='2'>2</option>
                                <option value='3'>3</option>
                                <option value='4'>4</option>
                              </select>
                            </td>
                            <td class='col4'>$sub_total</td>
						<td class='col5'>
							<input name='eventChosen[$x]' type='submit' value='Buy Tickets' />
						</td>
					</tr>";
				}
			?>
		</tbody>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.