Hey fellow PHP-developers, i ran into some problems with a script / class, and i keep getting undefined when its defined in the include file.

Class.number.php

<?php
 class DGSSeeker {

  private $nummer, $navn, $adr, $postnr, $by;

  function __construct($TelefonNummer){
      $this->nummer = $TelefonNummer;
      $this->GetData();
  }

  private function GetData(){
      $data = preg_replace('/\s+\s+/','',file_get_contents("http://www.krak.dk/query?what=wp&search_word=".$this->nummer)); //This is the place where we search, its a danish service called Krak

      preg_match('/<h2><span class="fn">(.*)<\/span><\/h2>/',$data,$dataNavn); //Its (.*) thats retrieved from the page. The bypass is to locate what we want to download - View the source code on the page you are looking from.

      $this->navn = $this->StripIt($dataNavn[0]);

      preg_match('/<span class="street-address">(.*)<\/span><br\/>/',$data,$dataAdr);
      $this->adr = $this->StripIt($dataAdr[0]);

      preg_match('/<span class="postal-code">(.*)<\/span><span class="locality">/',$data,$dataZip);
      $this->postnr = $this->StripIt($dataZip[0]);

      preg_match('/<span class="locality">(.*)<\/span><\/a>/',$data,$dataBy);
      $this->by = $this->StripIt($dataBy[0]);

      $data = null;

  }

  private function StripIt($str){
      $str = strip_tags($str);
      $str = str_replace(" ","",$str);
      $str = str_replace(" ","",$str);
      $str = str_replace("<span class=\"fn\">","",$str);
      $str = str_replace("<span class=\"street-address\">","",$str);
      return $str;
  }

  function getName()        {return $this->navn;}
  function getAdr()            {return $this->adr;}
  function getPostnr()    {return $this->postnr;}
  function getBy()            {return $this->by;}
  function getNummer()            {return $this->nummer;}

  }
 ?>

The index.php


<?php

include('class.number.php');

 if($_SERVER['REQUEST_METHOD'] == "POST" && $_GET['Hent']) {
             if($nummer == '') $status = '- You need to write a phone number.<br />';
             if($status == '') {
                 $dgs = new DGSSeeker("$nummer"); //Number youre looking up
                 $navn = $dgs->getName();
                 $adresse = $dgs->getAdr();
                 $post = $dgs->getPostnr();
                 $by = $dgs->getBy();
                 $tlf = $dgs->getNummer();
             }
             else $status = 'The data couldnt be collected ' . $status . '';
         }
     ?>

 <form action="index.php?Hent=1" method="post">

    Get information from Phone:    <input type="text" size="8" maxlength="8" name="nummer" value="" /> <input type="submit" name="submit" value="Hent" />

 </form>                    

 <p>
 <?php $status ?>
 <br />

 Navn: <input type="text" size="30" name="navn" value="<?php  $navn ?>" />
 <br />
 Adresse: <input type="text" size="30" name="adresse" value="<?php  $adresse ?>" />
 <br />
 Postnr.: <input type="text" size="30" name="post" value="<?php  $post ?>" />
 <br />
 By: <input type="text" size="30" name="by" value="<?php  $by ?>" />
 <br />
 Tlf.: <input type="text" size="30" name="tlf" value="<?php  $tlf ?>" />                     

 </p>

The code is danish itself, but it should be easy to understand, if anything i'll ofc translate the codes to english instead.
I just need some help on this and why it wont get the data, the query itself from http://www.krak.dk/query?what=wp&search_word=NUMBERHERE actually works, so it cant be that.

So can i request some help on this?

    Welcome to PHPBuilder!

    For one, what do you mean by "i keep getting undefined" ? Are you getting PHP error messages? If so, can you post the exact error message you're getting?

    Additionally, note that statements like this:

    <?php $status ?>

    do absolutely nothing and might as well not even be there. Perhaps you instead meant to [man]echo[/man]/[man]print[/man] the contents of that variable instead?

      As well as what bradgrafelman says, does changing the [man]include[/man] to [man]require[/man] make any difference? If problem lies there, require will fail sooner (closer to the cause of the problem).

      One reason I ask is that you call the class file "Class.number.php" and in the code you write "class.number.php", and wonder if the file actually has the first name and it's not being found because the code is looking for the second.

        bradgrafelman;11000147 wrote:

        Welcome to PHPBuilder!

        For one, what do you mean by "i keep getting undefined" ? Are you getting PHP error messages? If so, can you post the exact error message you're getting?

        Additionally, note that statements like this:

        <?php $status ?>

        do absolutely nothing and might as well not even be there. Perhaps you instead meant to [man]echo[/man]/[man]print[/man] the contents of that variable instead?

        Thanks.

        I keep getting this line " Notice: Undefined variable: nummer in E:/blablabla... on line 5 " in the index.php file when i send the data.

        http://i40.tinypic.com/3589imq.jpg

        Oh yeah... $status needed a echo, so i added that for echo'ing out " Couldnt collect data from number ".

        I also tried changing the include to a require, also with require_once, but non of these worked for me, i still got the undefined nummer on line 5 when i sent the data to index.php?hent=1

        but $nummer is already defined in both the include and on the index.php..

          randomanon;11000180 wrote:

          but $nummer is already defined in both the include and on the index.php.

          No it isn't.

          In index.php you have

          Attention! Leading blank line!! If you do have a blank line in your file
          (or any other data) before the opening PHP tag, output will start before
          PHP parsing begins which means not being able to set any HTTP headers
          (which in turns means no cookies or sessions for you)
          
          <?php
          # This is the same as putting all the code from the other file here,
          # and that file doesn't define $nummer
          include('class.number.php');
          
          # This line doesn't define $nummer
          if($_SERVER['REQUEST_METHOD'] == "POST" && $_GET['Hent']) {
          			# this line doesn't define $nummer,
          			# it tries to access an allready defined variable $nummer
          			# to see if it's ''.
                       if($nummer == '')
          

          And in the included file you define a class DGSSeeker which contains an instance propery $nummer, which has nothing to do with defining a $nummer variable since they have different scope.

          class DGSSeeker
          {
          	private $nummer
          }
          
            johanafm;11000191 wrote:

            No it isn't.

            In index.php you have

            Attention! Leading blank line!! If you do have a blank line in your file
            (or any other data) before the opening PHP tag, output will start before
            PHP parsing begins which means not being able to set any HTTP headers
            (which in turns means no cookies or sessions for you)
            
            <?php
            # This is the same as putting all the code from the other file here,
            # and that file doesn't define $nummer
            include('class.number.php');
            
            # This line doesn't define $nummer
            if($_SERVER['REQUEST_METHOD'] == "POST" && $_GET['Hent']) {
            			# this line doesn't define $nummer,
            			# it tries to access an allready defined variable $nummer
            			# to see if it's ''.
                         if($nummer == '')
            

            And in the included file you define a class DGSSeeker which contains an instance propery $nummer, which has nothing to do with defining a $nummer variable since they have different scope.

            class DGSSeeker
            {
            	private $nummer
            }
            

            Alright, then i gotta ask how you would define the class, since im pretty new when it comes to classes and such, if its not asking for too much of course.

                           if($nummer == '') $status = '- You need to write a phone number.<br />'; 

              Don't you want to get this from the submitted form, i.e. [font=monospace]$_POST['nummer'][/font]?

                Weedpacket;11000252 wrote:
                             if($nummer == '') $status = '- You need to write a phone number.<br />'; 

                Don't you want to get this from the submitted form, i.e. [font=monospace]$_POST['nummer'][/font]?

                When i submit the form using "nummer" , you send in the number and collect from the query of krak ( http://www.krak.dk/query?what=all&search_word= )
                The end of that form query?what=all&search_word=NUMBERINPUT is where i send it and it should return back the data to my form and fill up name, address etc.
                Its for a webshop for easy registration of customers.

                So i $_POST it into the query instead? and return it?

                  Sorry, I didn't understand any of that.

                  I just asking if you were wanting to check the number the user typed in the form.

                  Because if you are, you should be checking the value of [font=monospace]$_POST['nummer'][/font] (see Variables From External Sources).

                    Alright so i changed around abit and got some help.

                    index.php

                    <?php
                    		include ("class.tlf.php");
                    
                    	if($_SERVER['REQUEST_METHOD'] == "POST" && $_GET[hent]) {
                    		$nummer = $_POST['nummer']; // <--- THE EDIT WAS DONE HERE
                    		if($nummer == "") $status = "- Der skal skrives et telefonnummer.<br />";
                    		if($status == "") {
                    			$dgs = new DGSSeeker("$nummer"); //nummer du søger
                    			$navn = $dgs->getName();
                    			$adresse = $dgs->getAdr();
                    			$post = $dgs->getPostnr();
                    			$by = $dgs->getBy();
                    			$tlf = $dgs->getNummer();
                    		}
                    		else $status = "Oplysningerne blev ikke hentet " . $status . "";
                    	}
                    ?>
                    
                    <form action="index.php?hent=1" method="post">
                    
                    	Hent adresse via Telefonnummer:	<input type="text" size="8" maxlength="8" name="nummer" value="" /> <input type="submit" name="submit" value="Hent" />
                    
                    </form>                    
                    
                    <p>
                    <?= $status ?>
                    <br />
                    
                    Navn: <input type="text" size="30" name="navn" value="<?= $navn ?>" />
                    <br />
                    Adresse: <input type="text" size="30" name="adresse" value="<?= $adresse ?>" />
                    <br />
                    Postnr.: <input type="text" size="30" name="post" value="<?= $post ?>" />
                    <br />
                    By: <input type="text" size="30" name="by" value="<?= $by ?>" />
                    <br />
                    Tlf.: <input type="text" size="30" name="tlf" value="<?= $tlf ?>" />                     
                    
                    </p>
                    <?php
                     class DGSSeeker {
                    
                     private $nummer, $navn, $adr, $postnr, $by;
                    
                     function __construct($TelefonNummer){
                         $this->nummer = $TelefonNummer;
                         $this->GetData();
                     }
                    
                     private function GetData(){
                         $data = preg_replace('/\s+\s+/','',file_get_contents("http://www.krak.dk/query?what=wp&search_word=".$this->nummer)); //Dette er stedet du søger (Kan evt. også være på de gule sider)
                    
                         preg_match('/<h2><span class="fn">(.*)<\/span><\/h2>/',$data,$dataNavn); // Det er (.*) der hentes fra siden. Det uden om er for at lokalisere det vi vil hente - Se kildekoden på den side du søger fra.
                         $this->navn = $this->StripIt($dataNavn[0]);
                    
                    	 preg_match('/<span class="street-address">(.*)<\/span><br\/>/',$data,$dataAdr);
                         $this->adr = $this->StripIt($dataAdr[0]);
                    
                         preg_match('/<span class="postal-code">(.*)<\/span><span class="locality">/',$data,$dataZip);
                         $this->postnr = $this->StripIt($dataZip[0]);
                    
                         preg_match('/<span class="locality">(.*)<\/span><\/a>/',$data,$dataBy);
                         $this->by = $this->StripIt($dataBy[0]);
                    
                         $data = null;
                    
                     }
                    
                     private function StripIt($str){
                         $str = strip_tags($str);
                         $str = str_replace(" ","",$str);
                         $str = str_replace(" ","",$str);
                         $str = str_replace("<span class=\"fn\">","",$str);
                         $str = str_replace("<span class=\"street-address\">","",$str);
                         return $str;
                     }
                    
                     function getName()        {return $this->navn;}
                     function getAdr()            {return $this->adr;}
                     function getPostnr()    {return $this->postnr;}
                     function getBy()            {return $this->by;}
                     function getNummer()            {return $this->nummer;}
                    
                     }
                     ?>
                    

                    But i still cant get any data from the service like name etc.
                    Also sorry for being newbish around this topic and such, i really appreciate the help of you people.

                      Alright a mate of mine helped me through it, thanks for the replies though!
                      The thread can be closed now.

                        Write a Reply...