Jump to content

PHP Code Help Needed


ben_1uk

Recommended Posts

Hi everyone,

 

I'm hoping somebody can help me out with a change I need to make to a website that I have not designed / coded myself.

 

I have been asked to remove a "ticket counter" advertising the number of tickets left available for a number of upcoming events. I have included a copy of the .php file that is handling the ticket counter iteself below.

 

<?php

include('includes/includes_begin.php');

echo "<div id='heading'><h1><img src='images/tickets.gif' width='178' height='48' alt='Tickets' title='Tickets' /></h1></div>";

$page_title = "Tickets";

if (isset($_SESSION['checkout'])) {	$_SESSION['checkout'] = false; }

$conn = new dbConnect('private');

$query = "SELECT id, venue_name, venue_address, UNIX_TIMESTAMP(match_date) as m_date, tickets_left 
FROM venues 
WHERE sales_closed = 0
ORDER BY match_date ASC";

$venues = $conn->dbAll($query);

$conn->dbClose();

if (count($venues) == 0) {
	echo "<div class='error'>There are no more tickets available to buy</div>";
}	
else {
	echo "
	<form method='post' action='summary.php'>";
	$venuecount = 0;
	foreach ($venues as $v) {
		echo "<div class='venue'>
		<div class='match_date'><b>Match Date:</b> " . date(ymd, $v['m_date']) . "</div>
			<h2>" . escape($v['venue_name']) . "</h2>
			<div class='venue_address'>" . nl2br(escape($v['venue_address'])) . "</div>
			<div class='tickets_container'>
				<div class='tickets'>" . ($v['tickets_left'] < 30 ? "<span class='tickets_low'>" . escape($v['tickets_left']) . "</span>" : "" . escape($v['tickets_left']) . "") . " tickets left to buy for this match</div>";

		if ($v['tickets_left'] > 0) {
			echo "
				<div class='tickets_buy'>
					<input type='hidden' name='venue[]' value='" . escape($v['id']) . "' />
					Select how many tickets you wish to buy
					<select name='quantity[]' id='venue_" . escape($venuecount) . "' onchange='update_tickets()' onkeyup='update_tickets()'>
						<option value='0'>No tickets</option>
						<option value='1'" . (isset($_SESSION['tickets'][$v['id']]) && $_SESSION['tickets'][$v['id']] == '1' ? " selected='selected'" : "") . ">1 ticket</option>
						" . ($v['tickets_left'] > 5 ? "
						<option value='2'" . (isset($_SESSION['tickets'][$v['id']]) && $_SESSION['tickets'][$v['id']] == '2' ? " selected='selected'" : "") . ">2 tickets</option>
						<option value='3'" . (isset($_SESSION['tickets'][$v['id']]) && $_SESSION['tickets'][$v['id']] == '3' ? " selected='selected'" : "") . ">3 tickets</option>
						<option value='4'" . (isset($_SESSION['tickets'][$v['id']]) && $_SESSION['tickets'][$v['id']] == '4' ? " selected='selected'" : "") . ">4 tickets</option>
						<option value='5'" . (isset($_SESSION['tickets'][$v['id']]) && $_SESSION['tickets'][$v['id']] == '5' ? " selected='selected'" : "") . ">5 tickets</option>" : "") . "
					</select>
					<a href='#buy'>Confirm Ticket</a>
				</div>
			";

			$venuecount++;

		}

		echo "
			</div>
		</div>
		<hr />";

	}

	echo "<input type='hidden' id='venue_count' name='venuecount' value='" . escape($venuecount) . "' />
	<div id='buy'></div><noscript><input type='submit' id='selecttickets' name='submit' value='' title='Select these tickets'/></noscript></form>	";

	if (isset($_SESSION['tickets']) && array_sum($_SESSION['tickets']) > 0) {
		echo "<script type='text/javascript'>update_tickets();</script>";
	}
}

include('includes/includes_end.php');

?>

 

I have a limited understanding of how the ticket counter works in principal, but I'm anxious to start removing sections of code in case of unsuspected knock-on effects.

 

I want to leave the number of tickets people can purchase for an event intact - I just need to remove the ticket counter itself.

 

Can anybody offer any help?

 

Many thanks in advance.

Link to comment
Share on other sites

The php you gave us outputs this:

 

Match Date: The Date Here

venue_name

venue_address

(The Number of Tickets Left) tickets left to buy for this match

---------------------------------------------------------------------------------------------

For every venue.

Is it the bottom line you want removed ?

Then what do you want to remain?

Link to comment
Share on other sites

All you have to do is not display the areas that are showing how many tickets are left, this is the data coming from the database.

 

Comment out this section like below, I think is what you are after.

 

/*
echo "<div class='venue'>
		<div class='match_date'><b>Match Date:</b> " . date(ymd, $v['m_date']) . "</div>
			<h2>" . escape($v['venue_name']) . "</h2>
			<div class='venue_address'>" . nl2br(escape($v['venue_address'])) . "</div>
			<div class='tickets_container'>
				<div class='tickets'>" . ($v['tickets_left'] < 30 ? "<span class='tickets_low'>" . escape($v['tickets_left']) . "</span>" : "" . escape($v['tickets_left']) . "") . " tickets left to buy for this match</div>";
*/				

Link to comment
Share on other sites

Thanks for the replies both - I really appreciate it.

 

sunfighter:

The php you gave us outputs this:

 

Match Date: The Date Here

venue_name

venue_address

(The Number of Tickets Left) tickets left to buy for this match

---------------------------------------------------------------------------------------------

For every venue.

Is it the bottom line you want removed ?

Then what do you want to remain?

 

The line I want to remove from the code is 'XXX tickets left to buy for this match'.

 

QuickOldCar:

All you have to do is not display the areas that are showing how many tickets are left, this is the data coming from the database.

 

Comment out this section like below, I think is what you are after.

 

If I 'comment out' all of the above code, surely it will also remove the match date, venue name, venue address and ticket container? All I want to remove is the line that confirms how many tickets are left.

 

Can I simply comment out the code as follows:

 

/*<div class='tickets'>" . ($v['tickets_left'] < 30 ? "<span class='tickets_low'>" . escape($v['tickets_left']) . "</span>" : "" . escape($v['tickets_left']) . "") . " tickets left to buy for this match</div>";
*/

 

Thanks again.

Link to comment
Share on other sites

What I did was add the closing double quotes and semicolon to the last line being displayed. The commented line I made a new line with echo, now can just comment and uncomment it if need to.

 

echo "<div class='venue'>
		<div class='match_date'><b>Match Date:</b> " . date(ymd, $v['m_date']) . "</div>
			<h2>" . escape($v['venue_name']) . "</h2>
			<div class='venue_address'>" . nl2br(escape($v['venue_address'])) . "</div>
			<div class='tickets_container'>";
				//echo "<div class='tickets'>" . ($v['tickets_left'] < 30 ? "<span class='tickets_low'>" . escape($v['tickets_left']) . "</span>" : "" . escape($v['tickets_left']) . "") . " tickets left to buy for this match</div>";

Link to comment
Share on other sites

What I did was add the closing double quotes and semicolon to the last line being displayed. The commented line I made a new line with echo, now can just comment and uncomment it if need to.

 

echo "<div class='venue'>
<div class='match_date'><b>Match Date:</b> " . date(ymd, $v['m_date']) . "</div>
<h2>" . escape($v['venue_name']) . "</h2>
<div class='venue_address'>" . nl2br(escape($v['venue_address'])) . "</div>
<div class='tickets_container'>";
//echo "<div class='tickets'>" . ($v['tickets_left'] < 30 ? "<span class='tickets_low'>" . escape($v['tickets_left']) . "</span>" : "" . escape($v['tickets_left']) . "") . " tickets left to buy for this match</div>";

 

So, if I punctuate the original source code exactly as above, everything will remain intact, but the number of tickets left available for sale will not be displayed? It will have no effect on people being able to select the number of tickets they wish to purchase or data being posted to the SQL database? Sorry for the silly questions, but not having coded this myself, I really don't want to mess it up!

Link to comment
Share on other sites

  • 7 months later...

Hello QuickOldCar,

 

It's been a very long time since I was on here last - work is very busy!

 

I just wanted to let you know I have finally gotten round to trying the above code change and I'm happy to report that it worked. I haven't been able to test the system fully in as much as seeing if tickets will still go through the same process from POS to confirmation, but given how little the code has been changed, I don't see how it will stop working (fingers and toes crossed!).

 

Thanks again for your help.

Link to comment
Share on other sites

  • 3 months later...

Hi everyone,

 

Sorry to kick an old thread back to life, but I was wondering is anybody can offer any assistance with a new requirement now being asked of me using the above criteria..?

 

The ticket counter on the website has been removed completely by 'commenting out' the code as suggested by QuickOldCar, which has worked well. However, I now need the ticket counter to reappear when tickets reach a certain number to advertise that ticket sales are low for a particular venue - say for example, 30 tickets remaining.

 

Can anyone offer any advice or a way to do this by adapting the above code?

 

Many thanks in advance for any suggestions,

 

Ben_1uk.

Link to comment
Share on other sites

Just uncomment the code again, and wrap an IF block around it. Test the number of tickets, and if above your threshold execute the code that shows the counter.

 

Hi Christian,

 

Thanks for your prompt response. I'm not overly confident coding in PHP and would appreciate it if you could show me how to do what you have suggested using the necessary code. I did not code the original script and it's a little above my capability (for now!) to make any changes without being shown how.

 

Thank you.

Link to comment
Share on other sites

Instead of the commented out line, place this there. The below code should show only when there are 1 to 29 tickets left.

 

if($v['tickets_left'] < 30 && $v['tickets_left'] >= 1){
echo "<div class='tickets'>" . ($v['tickets_left'] < 30 ? "<span class='tickets_low'>" . escape($v['tickets_left']) . "</span>" : "" . escape($v['tickets_left']) . "") . " tickets left to buy for this match</div>";
}

Link to comment
Share on other sites

Thanks again QuickOldCar!

 

If I understand you correctly, you are effectively saying I should do something like this:

 

if($v['tickets_left'] < 30 && $v['tickets_left'] >= 1){

echo "<div class='tickets'>" . ($v['tickets_left'] < 30 ? "<span class='tickets_low'>" . escape($v['tickets_left']) . "</span>" : "" . escape($v['tickets_left']) . "") . " tickets left to buy for this match</div>";

}
				
			if ($v['tickets_left'] > 0) {
				echo "
					<div class='tickets_buy'>
						<input type='hidden' name='venue[]' value='" . escape($v['id']) . "' />
						Select how many tickets you wish to buy
						<select name='quantity[]' id='venue_" . escape($venuecount) . "' onchange='update_tickets()' onkeyup='update_tickets()'>
							<option value='0'>No tickets</option>
							<option value='1'" . (isset($_SESSION['tickets'][$v['id']]) && $_SESSION['tickets'][$v['id']] == '1' ? " selected='selected'" : "") . ">1 ticket</option>
							" . ($v['tickets_left'] > 5 ? "
							<option value='2'" . (isset($_SESSION['tickets'][$v['id']]) && $_SESSION['tickets'][$v['id']] == '2' ? " selected='selected'" : "") . ">2 tickets</option>
							<option value='3'" . (isset($_SESSION['tickets'][$v['id']]) && $_SESSION['tickets'][$v['id']] == '3' ? " selected='selected'" : "") . ">3 tickets</option>
							<option value='4'" . (isset($_SESSION['tickets'][$v['id']]) && $_SESSION['tickets'][$v['id']] == '4' ? " selected='selected'" : "") . ">4 tickets</option>
							<option value='5'" . (isset($_SESSION['tickets'][$v['id']]) && $_SESSION['tickets'][$v['id']] == '5' ? " selected='selected'" : "") . ">5 tickets</option>" : "") . "
						</select>
						<a href='#buy'>Confirm Ticket</a>
					</div>
				";
				
				$venuecount++;
					
			}

I don't think my attempt is quite right because I now have two IF commands and I'm not sure if I have enough curly brackets to wrap both of them..? Can you please advise?

 

I think the above code is now saying:

 

  • if tickets left is less than 30, but greater than 1, display "tickets_low".
  • Therfore, if tickets remaining is higher than 30, the counter still won't show..?
  • If the number of tickets left available is greater than 0, people have the option to buy tickets, which will still be handled by the existing code.

Once again, thank you very much for your help :)

Link to comment
Share on other sites

Yep, that is exactly what the code is saying.

 

Also, the curly braces in the code you posted are matched. If not, PHP would throw a fatal error complaining about it.

 

Are you saying the script will work before I start making any changes? I've had the misfortune of generating PHP errors before and the entire web page disappears :o

 

Thanks again both.

Link to comment
Share on other sites

Have you tried it? If so, did it work or not?<br />We cannot give you any guarantees that the code will work, as there are way too many factors involved. Not the least of them being the existing code you have. That said, the code looks right, and should work.<br /><br />If you get errors, investigate them and try to see if you can figure out why you get them. If you've tried, and still can't figure it out, then please post them here so that we can guide you on where to look.

Link to comment
Share on other sites

Have you tried it? If so, did it work or not?<br />We cannot give you any guarantees that the code will work, as there are way too many factors involved. Not the least of them being the existing code you have. That said, the code looks right, and should work.<br /><br />If you get errors, investigate them and try to see if you can figure out why you get them. If you've tried, and still can't figure it out, then please post them here so that we can guide you on where to look.

 

Hi Christian,

 

No, I haven't tried the code yet - I'm nervous to! I will try what you and the others have suggested above and let you know. I have a 'test' version of the website, which can only be accessed from my individual IP address so any changes I make will effectively be 'off-line'. Fingers crossed!

Link to comment
Share on other sites

  • 1 month later...

Hello,

 

When tickets for a venue sell out, I have set the code so it should generate a message to advise the public that tickets for that particular event are no longer available to buy. Here is the code:

if (count($venues) == 0) {
		echo "<div class='error'>There are no more tickets available to buy</div>";
	}	
	else {
		echo "
		<form method='post' action='summary.php'>";
		$venuecount = 0;
		foreach ($venues as $v) {
			echo "<div class='venue'>
			<div class='match_date'><b>Match Date:</b> " . date(ymd, $v['m_date']) . "</div>
				<h2>" . escape($v['venue_name']) . "</h2>
				<div class='venue_address'>" . nl2br(escape($v['venue_address'])) . "</div>
				<div class='tickets_container'>";

However, tickets for one of the venues have sold out, yet the message do not display??? What have I done wrong?

 

Thanks,

 

Ben_1uk

Link to comment
Share on other sites

the line if (count($venues) == 0) { being true means that there are no venues found/defined/entered in the database, not that there are no tickets available for any specific venue.

 


 

as you loop over each venue's data starting with the line foreach ($venues as $v) {, you would test the $v['tickets_left'] value and output whatever it is you want for the case where there are and are not any tickets left. since the code is already testing if ($v['tickets_left'] > 0) { ... code when there are tickets left ... }  if what you are asking is to output a message when there are no tickets left, just an an else { ... code when there are no tickets left... } statement on the end of that if(){} statement.

Link to comment
Share on other sites

the line if (count($venues) == 0) { being true means that there are no venues found/defined/entered in the database, not that there are no tickets available for any specific venue.

 


 

as you loop over each venue's data starting with the line foreach ($venues as $v) {, you would test the $v['tickets_left'] value and output whatever it is you want for the case where there are and are not any tickets left. since the code is already testing if ($v['tickets_left'] > 0) { ... code when there are tickets left ... }  if what you are asking is to output a message when there are no tickets left, just an an else { ... code when there are no tickets left... } statement on the end of that if(){} statement.

 

Thanks Mac,

 

Would you mind showing me what you mean?

 

Thanks.

Ben_1uk

Link to comment
Share on other sites

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.