Home instantiate get form data in a class php
Reply: 0

instantiate get form data in a class php

user6814
1#
user6814 Published in May 21, 2018, 9:04 am

I have a simple php form submitting a query into a few classes. The idea of app is to pass in a band name and check a venue to see if that band is playing. If that band is playing it the app will echo out the band and the date that band is playing. I have managed to get the app to work when its all one php file but i am having trouble spitting the single php file into separate classes.

html form :

<form action = "Ass1.php" method = "GET">
	<b>Enter band name : </b>
	<input type="text" name="bandName" />
	<input name="submit" type="submit" value="Click" />
</form>

The entire php file that works:

<?php

        class Concert {

            public $Artist = '';
            public $Date = '';

            function SetConcert( $NewArtist = '', $NewDate = '' ) {

                $this->Artist = $NewArtist;
                $this->Date = $NewDate;

            }

            function GetArtist() {

                return $this->Artist;

            }

            function GetDate() {

                return $this->Date;

            }

        }

    class Venue {

                public $VenueName = '';
                public $ConcertsInVenue = array();

        function SetVenueName( $NewVenue = '' ) {

                    $this->VenueName = $NewVenue;

        }

                function GetVenueName() {

                    return $this->VenueName;

                }

                function AddConcert( $NewArtist = '', $NewDate = '' ) {

                    $NewConcert = new Concert;
                    $NewConcert->SetConcert( $NewArtist, $NewDate );

                    $this->ConcertsInVenue[] = $NewConcert;


                }

                function ListConcertsInVenue( $SearchQuery = '' ) {

                    //Assume Results Will Be Filtered
                    $ListAllConcerts = FALSE;

                    //If Search Query Is Blank, List All Concerts
                    if( $SearchQuery == '' ){

                        $ListAllConcerts = TRUE;

                    }

                    echo 'Concerts In '.$this->VenueName.':';

                    //Loop Through All Concerts In This Venue
                    foreach( $this->ConcertsInVenue as $tmpConcert ){

                        if( $SearchQuery == $tmpConcert->GetArtist() || $ListAllConcerts == TRUE ){

                            echo '<p>';
                            echo $tmpConcert->GetArtist().', ';
                            echo $tmpConcert->GetDate();
                            echo '</p>';

                        }

                    }

                }

        }

        //Create New Venue Object
    $tmpVenue = new Venue;

        //Set Venue Name
        $tmpVenue->SetVenueName( 'Wembley' );

        //Add Concerts To Venue
        $tmpVenue->AddConcert( 'Rollin\' Empire', '05-02-2018' );
        $tmpVenue->AddConcert( 'Metallica', '06-02-2018' );
        $tmpVenue->AddConcert( 'Led Zeppelin', '07-02-2018' );
        $tmpVenue->AddConcert( 'Rollin\' Empire', '19-02-2018' );

        //Search For Artist In Venue ( Leave Blank To List All )
        $tmpVenue->ListConcertsInVenue($_GET['bandName']);

?>

Ive tries creating 3 classes Venue.class.php Concert.class.php and app.class.php and its not working

concert.class.php

<?php

include ("Venue.class.php");

class Concert {

				public $Artist = '';
				public $Date = '';

				function SetConcert( $NewArtist = '', $NewDate = '' ) {

					$this->Artist = $NewArtist;
					$this->Date = $NewDate;

				}

				function GetArtist() {

					return $this->Artist;

				}

				function GetDate() {

					return $this->Date;

				}

			}
?>

Venue.class.php

<?php

include ("Concert.class.php");

	class Venue {

					public $VenueName = '';
					public $ConcertsInVenue = array();

        	function SetVenueName( $NewVenue = '' ) {

						$this->VenueName = $NewVenue;

        	}

					function GetVenueName() {

						return $this->VenueName;

					}

					function AddConcert( $NewArtist = '', $NewDate = '' ) {

						$NewConcert = new Concert;
						$NewConcert->SetConcert( $NewArtist, $NewDate );

						$this->ConcertsInVenue[] = $NewConcert;


					}

					function ListConcertsInVenue( $SearchQuery = '' ) {

						//Assume Results Will Be Filtered
						$ListAllConcerts = FALSE;

						//If Search Query Is Blank, List All Concerts
						if( $SearchQuery == '' ){

							$ListAllConcerts = TRUE;

						}

						echo 'Concerts In '.$this->VenueName.':';

						//Loop Through All Concerts In This Venue
						foreach( $this->ConcertsInVenue as $tmpConcert ){

							if( $SearchQuery == $tmpConcert->GetArtist() || $ListAllConcerts == TRUE ){

								echo '<p>';
								echo $tmpConcert->GetArtist().', ';
								echo $tmpConcert->GetDate();
								echo '</p>';

							}

						}

					}

    		}

?>

app.php

<?php

include ("Venue.class.php");
include ("Concert.class.php");

			//Create New Venue Object
    	$tmpVenue = new Venue;

			//Set Venue Name
			$tmpVenue->SetVenueName( 'Wembley' );

			//Add Concerts To Venue
			$tmpVenue->AddConcert( 'Rollin\' Empire', '05-02-2018' );
			$tmpVenue->AddConcert( 'Metallica', '06-02-2018' );
			$tmpVenue->AddConcert( 'Led Zeppelin', '07-02-2018' );
			$tmpVenue->AddConcert( 'Rollin\' Empire', '19-02-2018' );

			//Search For Artist In Venue ( Leave Blank To List All )
			$tmpVenue->ListConcertsInVenue($_GET['bandName']);

?>

Would I need to create a new class in app.php to handle the form input and pass it to the other classes?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.395298 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO