couple of class problems

Discussion in 'PHP' started by Jeremy Benson, Jun 30, 2014.

  1. #1
    I'm have some problems with my class. One is using require, and the other is with output one of my functions is making...

    my require line
    require('../../../data/sqldata.php');
    PHP:
    I can ctr + click that in expression web 4 and it will open in a new tab, so I know the path is right, but when I run my page I get and error that says stream can't open, or file can't be found. Its made it so I have to hardcode my sql variables into my PDO declarations, so I know I gotta fix it...

    The other problem is this function..

    
        $nextShow = '<span>' . $venue . ',' . ' ' . $state . ',' . ' ' . $country . ':' . ' ' . $month . ' ' . $day . '<sup>' . self::echo_date_sup($day) . '</sup>' . ',' . ' ' . $dateExplode[0] . ', at ' . $time . '.' . '</span>';
    
    PHP:
    You can see I call a class function there, all that does is echo st, or rd, depending on the day, like 3rd..

    that line of code outputs to this div on my profile page..

    <span id="nextshowoutput"><?php $viewedUser->echo_next_musicshow() ?></span>
    HTML:
    but it turns out like this on the page
    stNitro, Maine, Canada: Jul 01, 2014, at 07:00PM.

    When I view page source in the browser it looks like this..

    
    <span id="nextshowoutput">st<span>Nitro, Maine, Canada: Jul 01<sup></sup>, 2014, at 07:00PM.</span></span>
    
    HTML:
    Thanks again for any help I get with these problems :)
     
    Jeremy Benson, Jun 30, 2014 IP
  2. kipigames

    kipigames Greenhorn

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #2
    can you try this for the require part?:

    require(__DIR__.'/../../../data/sqldata.php');
    PHP:
     
    kipigames, Jun 30, 2014 IP
  3. Jeremy Benson

    Jeremy Benson Well-Known Member

    Messages:
    364
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    123
    #3
    That didn't work, but there was an improvement...

    before I was getting this error.
    require(../../../data/sqldata.php): failed to open stream: No such file or directory in C:\wamp\www\mechanics\scripts\classes\php\cuser.php on line

    With your line of code it's just telling me that my variables are undefined. Saying no $dsn, $dbUserName, or $dbPassword... They're defined in the file through. Same variables I've been including and using in all other scripts... Not sure why this one is acting differently.
     
    Jeremy Benson, Jun 30, 2014 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    Are you sure self::echo_date_sup() returns the string, and does not echo it instead?
     
    nico_swd, Jun 30, 2014 IP
  5. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #5
    Post the code for this file: data/sqldata.php (remove sensitive info if any before doing so).
     
    ThePHPMaster, Jun 30, 2014 IP
  6. sarahk

    sarahk iTamer Staff

    Messages:
    28,897
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #6
    $nextShow='<span>'.$venue.','.' '.$state.','.' '.$country.':'.' '.$month.' '.$day.'<sup>'.self::echo_date_sup($day).'</sup>'.','.' '.$dateExplode[0].', at '.$time.'.'.'</span>';
    Code (markup):
    would be more readable as
    $supoptions = array(1 => 'st', 2 => 'nd', 3 => 'rd');
    if (substr($day,-1) > 3) $sup = 'th';
    else $sup = supoptions[intval($day)];
    
    $nextShow="<span>{$venue}, {$state}, {$country}: {$month} {$day}<sup>{$sup}</sup>, {$dateExplode[0]}, at {$time}</span>";
    Code (markup):
    http://sandbox.onlinephpfunctions.com/code/d337eeee58d92bfd68fc393aefcb8139ffbf4451
     
    Last edited: Jun 30, 2014
    sarahk, Jun 30, 2014 IP
  7. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #7
    Or, of course, one could just use the built-in PHPs dateTime-class.
    
    $date = new DateTime($datevariable);
    $day = $date->format('jS');
    echo $day;
    
    PHP:
     
    PoPSiCLe, Jul 1, 2014 IP
  8. Jeremy Benson

    Jeremy Benson Well-Known Member

    Messages:
    364
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    123
    #8
    I fixed the echo_date_sup() function. It was echoing the string. I had to make it return the value and assign it to a variable. That worked like a charm... How come I couldn't just echo the string in this case?

    I still can't solve the require problem in the class.
    This is my data.php as requested.
    
    $dbHost = "localhost";
    $dbPort = "3306";
    $dbUserName = "root";
    $dbPassword = "";
    $dbName = "famous4";
    
    $dsn = "mysql:host=$dbHost;port=$dbPort;dbname=$dbName";
    
    Code (markup):
    I'm actually have a bit of problems with require. In one case it's creating undesirable white space.
     
    Jeremy Benson, Jul 1, 2014 IP
  9. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #9
    Take a look at the source code of your rendered page. I suspect that the issue you're having is 'cause of the missing <?php tags in this file.

    This would explain why require finds the file, but PDO later complains about undefined variables.

    Because it would just echo the value wherever you call the function. echo does not return a value, so it never gets appended to your string.

    Functions should rarely ever echo anything, really. Let the function do its job and return the value. Then let the user (in this case you) decide what to do with this value.
     
    nico_swd, Jul 1, 2014 IP
  10. Jeremy Benson

    Jeremy Benson Well-Known Member

    Messages:
    364
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    123
    #10
    Not sure what you mean. I can't view the source code, because the only thing that comes up on the page when I try to use the variables in the required file is 4 php errors... Three undefined variables and invalid data source. (php code doesn't show up in source code anyway?)

    I'll try and recap what I've got going on..

    I have profile.php which requires the user class, and the user class requires the file with my database variables...

    here's the contents of the file required in the user class...
    
    <?php
    
       //Remember to change pages based on regStep.
       //Remember to get help with class. Using const in cuser class.
       //Code image upload from profile settings music.
       //Show information from profilesettings form went into the database twice(note- clicked button while disabled.)
       //Remember to create user's file directories.
       //Remember to create a download option or artists to allow fans to download a track.  
       //Remember to add special chars check to music profile settings.
       //Login home page...bad information makes index output php errors.  
       //Edit maxlength for all field data and be sure of special character checks.
       //Countries need to be swapped out for abbreviations.  
       //Bug in profile/cuser old shows not being filtered out...
       // Profile settings form too short.
            
    $dbHost = "localhost";
    $dbPort = "3306";
    $dbUserName = "root";
    $dbPassword = "";
    $dbName = "famous4";
    
    $dsn = "mysql:host=$dbHost;port=$dbPort;dbname=$dbName";
    
    ?>
    
    PHP:
    here's how I'm requiring the file in user class

    
    require(__DIR__ . '/../../../data/sqldata.php');
    
    PHP:
    Here's the function I'm calling in the user class to test the required file...

    
    function set_basicinfo()
       {
         $retrievedBasicInfo = [];
        
         $dbConnect = new PDO($dsn, $dbUserName, $dbPassword, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
        
         $sqlStatement = $dbConnect->prepare("SELECT `userName`, `country`, `city`, `bio`, `regStep`, `password` FROM `userinfo` WHERE `ID` = ?");
        
         try{ 
        
           $sqlStatement->execute([$this->IDCheck]);
           $retrievedBasicInfo = $sqlStatement->fetchAll();
        
         }catch(\PDOException $e){  }
        
           $this->userName = $retrievedBasicInfo[0]['userName'];
           $this->country = $retrievedBasicInfo[0]['country'];
           $this->city = $retrievedBasicInfo[0]['city'];
           $this->bio = $retrievedBasicInfo[0]['bio'];
           $this->regStep = $retrievedBasicInfo[0]['regStep'];
           $this->password = $retrievedBasicInfo[0]['password'];
        
         return NULL;
       }
    
    PHP:
    here's how I call the function on profile.php

    
    $viewedUser->set_basicinfo();
    
    PHP:
    That above function call is after necessary set up function calls, to give other class members values... Works fine when PDO is instantiated with hard-coded values... Just can't solve this one..

    Is the problem the same reason I was getting whitespace in another instance? Should I open the file in notpad++ and save without BOM?
     
    Jeremy Benson, Jul 2, 2014 IP
  11. Jeremy Benson

    Jeremy Benson Well-Known Member

    Messages:
    364
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    123
    #11
    The internet fixed my problem :) I had to define the variables as global inside the function using them. Not sure why, but it worked..

    global $dsn, $dbUserName, $dbPassword...

    Not sure why it has to be inside the function either... Would be more efficient if I could define them with other class members..
     
    Jeremy Benson, Jul 2, 2014 IP
  12. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #12
    The reason is that a function is its own scope, and defined variables are not available inside the function - one thing you can do is just declare the $dbconnect as a class variable before any of the functions, and access it inside the function via class->dbvariable;
     
    PoPSiCLe, Jul 2, 2014 IP
  13. Jeremy Benson

    Jeremy Benson Well-Known Member

    Messages:
    364
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    123
    #13
    Cool thanks guys :) solved all these problems now.
     
    Jeremy Benson, Jul 3, 2014 IP
  14. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #14
    Guess that depends on your definition of "readable" ... or efficient, since double quote crap is slower...

    I wanted to make that legible, I'd just slap some carriage returns and tabs in there.

    One big bit of advice on this though...

    require(__DIR__ . '/../../../data/sqldata.php');

    If you have to up-tree link that many times, there is something horrifically and terrifyingly wrong with your directory structure... either that or you're getting WAY too complex for your own good. Usually it's better to have your libs as subdirectories UNDER your user-callable locations, not the other way around...

    Though if /data is in access root, you could probably get away with: require('/data/sqldata.php');
     
    deathshadow, Jul 4, 2014 IP
  15. Jeremy Benson

    Jeremy Benson Well-Known Member

    Messages:
    364
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    123
    #15
    hmm.. my structure is...

    mechanics/
    mechanics/data
    mechanics/scripts
    mechanics/scripts/classes/php
    mechanics/scripts/php
    mechanics/scripts/javascript
    [index.php and other root pages.]
     
    Jeremy Benson, Jul 7, 2014 IP
  16. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #16
    are the index.php and other user callable PHP files all in /mechanics or /? If the latter what I said should apply...

    in fact, if all user callable routines are in mechanics/ you could even lose the leading slash, and just make it:

    require(data/sqldata.php);

    It's why in my code I usually have just one index.php "to rule them all" -- all user requests are routed through the one file so I have one security point, and all includes only need to worry about one location. (since once that base script is running, ALL includes treat that location as the current directory). Just helps keep things cleaner.
     
    deathshadow, Jul 7, 2014 IP