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
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>

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.