Jump to content

Pain

Members
  • Posts

    178
  • Joined

  • Last visited

Posts posted by Pain

  1. Hello. Been programming OOP for the past year. I am very ashamed to admit that I have not learned about exceptions yet. Well actually Im not sure...

     

    Am I using them correctly?

     

    public function indexAction(Request $request)
    {
            try {
                if(!is_object($request))
                    throw new \Exception('Param msut be an object');
            } catch(Exception $e) {
                $this->logger = $this->get('logger');
                $this->logger->info("You have the following error: { $error }", array("error" => $e->getMessage()));
            }
    }

    I throw an exception in order for the error to appear on the screen. Then I catch the error and log it. Is this the correct usage? Or is there more? 

    Thanks!

     

  2. Thanks for your effort. I have solved it by doing this:

     

     

     
    <?php $php_cities_array = array('smth', 'smth2'); 
     
    function js_str($s)
    {
        return '"' . addcslashes($s, "\0..\37\"\\") . '"';
    }
     
    function js_array($array)
    {
        $temp = array_map('js_str', $array);
        return '[' . implode(',', $temp) . ']';
    }
     
    $array = 'var availableTags = ' . js_array($php_cities_array) . ';';
     
    ?>
    <script>
     
    <?php echo $array; ?>
     
    </script>
    
  3. Hello. I was wondering how can I add a php array to a js array containing json values.

    var availableTags = [
      "AppleScript",
      "Asp",
      "BASIC",
      "C",
      "C++",
      "Clojure",
      "COBOL",
      "ColdFusion",
      "Erlang",
      "Fortran",
      "Groovy",
      "Haskell",
      "Java",
      "JavaScript",
      "Lisp",
      "Perl",
      "PHP",
      "Python",
      "Ruby",
      "Scala",
      "Scheme"
    ];
    
    

    I want to add this to the js array:

    <?php $arr = array('red', 'green', 'blue'); ?>
    
    

    I've tried doing this, but its definitely the wrong approach as it just sums everything up into one element "redgreenblue"

     
    var availableTags = [
      "<?php foreach($arr as $row) { echo $row; } ?>",
      "AppleScript",
      "Asp",
      "BASIC",
    ....
    
    

    Thanks for any kind of help!

  4. Hi. I am fairly new with cURL.

     

    I have a URL like example.com/index.php?something=value&somethingtwo=valuetwo

     

    How can I get those values and print them out?

     

    I've got this code, but have no idea what to do next, please help guys!

     

     

    <?php
     
     
    function get($url, $params=array()) 
    { 
     
        $url = $url.'?'.http_build_query($params, '', '&');
        
        $ch = curl_init();
        
        curl_setopt($ch, CURLOPT_URL, $url);
        
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
        
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            
        $response = curl_exec($ch);
        
        curl_close($ch);
        
        return $response;
    }
     
    // Sample call
     
    echo get('https://www.example.com/index.php', array('something'=>'value', 'somethingtwo'=>'valuetwo'));
     
     
    ?>
    
  5. Hi. I'm curious what settings/other things are people usually defining to make their lives a bit easier (i'm talking about constants)?

     

    And if possible please give an explanation (why is it useful to define a particular constant).

     

    As you can see my list is nothing special, therefore I'm looking forward to hearing what you have to say.

    ...
    define("DB_HOST", "localhost");
    define("DB_USER", "user");
    define("DB_PASS", "pass");
    define("DB", "db_name");
    define("BASE_URL", "http://www.something.com");
    ...

    Thanx:)

  6. Thanks for the reply.

     

    It works, thank you. However this code should be inside a class and i don't want my class to echo. I want to use these variables on another page. So if I have something like this in my class:

    $i = 0;
    while ($row = $qstmt->fetch(PDO::FETCH_ASSOC))
    {
    $email[$i] = $row['email'];
    $testname[$i] = $row['name'];
    $testlastname[$i] = $row['lastname'];
    $i++;
    }
    

    How would i echo out $lastname[5] or any other random variable. 

     

    So far i've got this :/

    require_once('connect.php');
    require_once('class/profile.php');
     
    $profile = new Profile($db);
    echo $profile->testGetAll();
    
  7. Thanks for the answer. 

     

    When i retrieve those values from the db like this:

    $i = 0;
    foreach($profile->testGetAll() as $value[$i]) {
    $value[$i];
    $i++;
    } 
     
     
    
    
     
    i get a big bunch of rows. How can i assign a variable to each row? $name would be name, $lastname would be lastname etc
  8. Hello, i need some help with pdo. I'm struggling while trying to echo out all results.

    class Test {
    
    public $testName;
    public $testEmail;
    public $testLastname;
    
    public function testGetAll() {
     
    $query = $this->db->prepare('SELECT * FROM customers');
    $query->execute();
     
    $this->testEmail        = $data['email'];
    $this->testName       = $data['name'];
    $this->testLastname = $data['lastname'];
     
    }
    }
    
  9. put placeholders in your email message and then use str_replace to replace the placeholders with the values from the variables.

     

    example:

    $content = "Hello {{NAME}}, your email address is: {{EMAIL}}";
    $name = 'your name';
    $email = 'yname@gmail.com';
    
    $replace = array(
      '{{NAME}}' => $name,
      '{{EMAIL}}' => $email
      // etc..
    );
      
    $content = str_replace(array_keys($replace),$replace,$content);
    

    genius-meme.jpg

  10. Hi.

     

    Well since you are a beginner I recommend doing this.

     

    Retrieve info from the database. Lets say 'id'. Then your first line of the form should be something like this:

    <form name="form1" action="profile.php?id=<?php echo $id; ?>" method="POST"  />
    

    Then in your profile.php get that id from the URL by doing this:

    $id = $_GET['id'];
    

    Now you can use 'id' in WHERE clause and retrieve info from the database (that belongs to this id).

     

     

    Note: this is a bad practice, you should not use this in the future, however this might help you understand a bit more PHP.

  11. I have a question regarding setters and getters.

     

    Why most people use a separate setter for each property?

     
    public function setTitle($title) {
     
    $this->title = $title;
     
    }
     
    
    public function setDescription($description) {
     
    $this->description = $description;
     
    }
     
    
    public function setPostcode($postcode) {
     
    $this->postcode = $postcode;
     
    }
     
    
    public function setPicture($picture) {
     
    $this->picture = $picture;
     
    }
    
    
    
     
    public function getTitle() {
     
    return $this->title;
     
    }
     
    
    public function getDescription() {
     
    return $this->description;
     
    }
     
    
    public function getPostcode() {
     
    return $this->postcode;
     
    }
     
    
    public function getPicture() {
     
    return $this->picture;
     
    }
    
    

    Why not just do this?

    public function setDetails($title, $description, $postcode, $picture) {
     
    $this->title       = $title;
    $this->description = $description; 
    $this->postcode    = $postcode;
    $this->picture     = $picture;
     
    }
     
    public function getTitle() {
     
    return $this->title;
     
    }
     
    
    public function getDescription() {
     
    return $this->description;
     
    }
     
    
    public function getPostcode() {
     
    return $this->postcode;
     
    }
     
    
    public function getPicture() {
     
    return $this->picture;
     
    }
    
    
    
    
  12. I'm sorry I should've been more clear on this. Each email has to be different. Please take a look at the full version of my class.

    <?php
     
     
    class Advert {
      
    private $db;
     
        public $title;
        public $description;
        public $postcode;
        public $picture;
        public $email;
        public $date_posted;
        public $cost;
     
        public $message;
        public $subject;
        public $email_tag;
     
        public $link;
        public $id;
     
     
     
    public function __construct($database) {
       $this->db = $database;
    } 
     
     
     
        public function setDetails($title, $description, $postcode, $picture, $email, $date_posted, $cost) {
     
         $this->title       = $title;
    $this->description = $description; 
    $this->postcode    = $postcode;
    $this->picture     = $picture;
    $this->email       = $email;
    $this->date_posted = $date_posted;
    $this->cost        = $cost;
     
        }
     
     
     
    public function insertAdvert() {
     
    $query = $this->db->prepare("INSERT INTO rooms (title, description, postcode, picture, email, date_posted, cost) VALUES (?, ?, ?, ?, ?, ?, ?)");
     
    $query->bindValue(1, $this->title);
    $query->bindValue(2, $this->description);
    $query->bindValue(3, $this->postcode);
    $query->bindValue(4, $this->picture);
    $query->bindValue(5, $this->email);
    $query->bindValue(6, $this->date_posted);
    $query->bindValue(7, $this->cost);
     
    $query->execute();
     
    }
     
     
     
    public function setAdvertLink() {
     
    $query = $this->db->prepare('SELECT id FROM rooms WHERE title = ? AND description = ? AND postcode = ? AND date_posted = ? AND email = ?');
     
    $query->bindValue(1, $this->title);
    $query->bindValue(2, $this->description);
    $query->bindValue(3, $this->postcode);
    $query->bindValue(4, $this->date_posted);
    $query->bindValue(5, $this->email);
     
    $query->execute();
     
    $data = $query->fetch();
    $id   = $data['id'];
            
            $this->link = 'http://www.home-decorators.co.uk/rooms/uk/london/'.$id;
            $_SESSION['last_id'] = $id;
     
    }
     
     
     
        public function retrieveConfirmationEmail() {
     
         $this->email_tag = 'Room Confirmation';
     
         $query = $this->db->prepare('SELECT * FROM emails WHERE email_tag = ?');
         $query->bindValue(1, $this->email_tag);
         $query->execute();
     
         $data = $query->fetch();
     
         $message = $data['message'];
         $subject = $data['subject'];
     
         $this->message  = $message;
         $this->subject  = $subject;
     
        }
     
     
     
    public function sendConfirmationEmail() {
     
    $headers  = "From: admin@homelocator.com" . "\r\n";
       $headers .= "Reply-To: admin@homelocator.com". "\r\n";
            $headers .= "MIME-Version: 1.0\r\n";
            $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
     
            mail($this->email, $this->subject, $this->message, $headers);
     
    }
     
     
     
    public function redirect() {
     
    header('location: success.php');
     
    }
     
     
     
    }
     
     
     
    ?>
    
    

    As you can see I'm getting those variables from $_POST. 

     

     

    Obviously I could just write a welcome email like this:

    $this->message = 'Hello ' . $this->name . ' your email is ' .$this->email;
    
    

    But i want to hold this in the db. Hope that's a bit more clear. Sorry I'm really bad at explaining :)

  13. Hello. 

     

    I am building a website where I need to send welcome emails.

     

    Welcome email text is stored in the database, so i first have to retrieve it, then send.

     

    Now I came across this issue - how do I make my code treat the text from the database AS a variable(s).

     
        public function retrieveConfirmationEmail() {
     
         $this->email_tag = 'Room Confirmation';
     
         $query = $this->db->prepare('SELECT * FROM emails WHERE email_tag = ?');
         $query->bindValue(1, $this->email_tag);
         $query->execute();
     
         $data = $query->fetch();
     
         $message = $data['message'];
         $subject = $data['subject'];
     
         $this->message  = $message;
         $this->subject  = $subject;
     
        }
     
     
     
    public function sendConfirmationEmail() {
     
    $headers  = "From: admin@homelocator.com" . "\r\n";
       $headers .= "Reply-To: admin@homelocator.com". "\r\n";
            $headers .= "MIME-Version: 1.0\r\n";
            $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
     
            mail($this->email, $this->subject, $this->message, $headers);
     
    }
    
    

    The first method retrieves 'Message'. That message has to contain PHP variables such as $this->name, $this->title etc.

     

     

     

    Any advice? :)

     

    thx

  14. Hello. 

     

    Im starting to understand about interfaces a bit, however I can't seem to get them working. So i figured i'd go here and ask a few questions.

     

    This is my class programming.php

    <?php
     
    class Programming implements Intfac {
     
    public function sayHello() {
    echo 'Just Hello';
    }
    
    public function sayBye() {
    echo 'Bye';
    }
     
    }
     
    ?>
    
    

    This is my interface intfac.php

    <?php
     
    interface Intfac {
    
    public function sayHello();
    public function sayBye();
    
    }
      
    ?>
    
    

    How do I run the interface now?

    <?php
     
    require('class/programming.php');
    require('interface/intfac.php');
     
    $programming = new Programming;
     
     
    ?>
    
    

    Thanks:)

  15. Sorry guys, my explanation was really poor. Here is what I want to do.

     

    I want to use the Join Class to manipulate the database.

     

    I want my class.join.php to look somewhat like this:

    class Join {
     
        public $database;
        
    private $name;
    private $lastname;
    private $email_repeat;
    private $password;
     
    public $email;
     
    public $subject = 'Welcome to HomeLocator';
    public $message;
     
     
    public function newUser($name, $lastname, $email, $email_repeat, $password) {
     
    $this->name = $name;
    $this->lastname = $lastname;
    $this->email = $email;
    $this->email_repeat = $email_repeat;
    $this->password = $password;
        
        if(!empty($this->name) && !empty($this->lastname) && !empty($this->email) && !empty($this->email_repeat) && !empty($this->password)) {
        
        $database->query("INSERT INTO users (name, lastname, email, password, date_joined) VALUES (:name, :lastname, :email, :password, :date_joined)");
        $database->bind(':name', $name);
        $database->bind(':lastname', $lastname);
        $database->bind(':email', $email);
        $database->bind(':password', $password);
        $database->bind(':date_joined', $date_joined);
        $database->execute();
     
        $_SESSION['email'] = $this->email;
        return TRUE;
     
        }
     
     
     
    }
    }
    
    

    So in the index.php file I would obviously instantiate the Join class

    $join = new Join;
    
    

    and try to insert into the db:

     

    $join->newUser();

     

    Note that my database class is in yet another file (class.database.php)

     

    I think this approach Im taking is wrong.
     

  16. Hi there. Can someone give me an example of how to use PDO in a class?

     

    I have this class called Join in class.join.php

    class Join {
     
    private $name;
    private $lastname;
    private $email;
    private $email_repeat;
    private $password;
     
     
    public function newUser($name, $lastname, $email, $email_repeat, $password) {
     
    $this->name = $name;
    $this->lastname = $lastname;
    $this->email = $email;
    $this->email_repeat = $email_repeat;
    $this->password = $password;
        
        if(!empty($this->name) && !empty($this->lastname) && !empty($this->email) && !empty($this->email_repeat) && !empty($this->password)) {
     
    
        $database->query("INSERT INTO users (name, lastname, email, password) VALUES (:name, :lastname, :email, :password)");
        $database->bind(':name', $this->name);
        $database->bind(':lastname', $this->lastname);
        $database->bind(':email', $this->email);
        $database->bind(':password', $this->password);
        $database->execute();
        return TRUE;
     
        }
     
     
     
    }
     
     
       
     
     
    }
    
    

    Then i have my Database class in class.database.php

    class Database {
        
        
        private $host      = DB_HOST;
        private $user      = DB_USER;
        private $pass      = DB_PASS;
        private $dbname    = DB_NAME;
     
        private $dbh;
        private $error;
        private $stmt;
     
        public function __construct(){
            // Set DSN
            $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
            // Set options
            $options = array(
                PDO::ATTR_PERSISTENT    => true,
                PDO::ATTR_ERRMODE       => PDO::ERRMODE_EXCEPTION
            );
            // Create a new PDO instanace
            try{
                $this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
            }
            // Catch any errors
            catch(PDOException $e){
                $this->error = $e->getMessage();
            }
        }
     
    public function query($query){
        $this->stmt = $this->dbh->prepare($query);
    }
     
     
    public function bind($param, $value, $type = null){
        if (is_null($type)) {
            switch (true) {
                case is_int($value):
                    $type = PDO::PARAM_INT;
                    break;
                case is_bool($value):
                    $type = PDO::PARAM_BOOL;
                    break;
                case is_null($value):
                    $type = PDO::PARAM_NULL;
                    break;
                default:
                    $type = PDO::PARAM_STR;
            }
        }
        $this->stmt->bindValue($param, $value, $type);
    }
     
     
    public function execute(){
        return $this->stmt->execute();
    }
     
     
    public function resultset(){
        $this->execute();
        return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
    }
     
     
    public function single(){
        $this->execute();
        return $this->stmt->fetch(PDO::FETCH_ASSOC);
    }
     
     
    public function rowCount(){
        return $this->stmt->rowCount();
    }
     
    }
    
    

    any help is appreciated, thank you!

  17. I did put all the emails into an array. 

    $email = array();
    $name = array();
     
     
     $query = mysql_query("SELECT * FROM test");
     while($row = mysql_fetch_assoc($query)) {
     
                $name[] = $row['name'];
                $email[] = $row['email'];
     
     
     
             }
     
    $email = implode(",", $email);
    $name = implode(" ", $name);
    
    
    This is now the main problem as the email gets sent to the first recipient in the database only.
     
    $mail = new SendGrid\Mail();
                 $mail->
                 addTo($email)->
                 setFrom('test@test.com')->
                 setSubject($subject)->
                 setText($message)->
                 setHtml($message);
      
                 $sendgrid->web->send($mail); 
    
    

    PHPs built-in mail () function works properly, but i have to use sendgrid for this one. I'm lost:/

  18. Hello. I have a problem. 

     

    I am trying to send approximately 1200 emails at once. Each email has to be retrieved from the database. 

    <?php
    ...
    $query = mysql_query("SELECT * FROM mass_subscribers");
    while($row = mysql_fetch_assoc($query)) {
       $email = $row['email'];
       $name = $row['name'];
     
       $mail = new SendGrid\Mail();
       $mail->
       setTo($email)->
       setFrom('me@yahoo.com')->
       setSubject($subject)->
       setText($message)->
       setHtml($message);
      
       $sendgrid->web->send($mail);
    }
    ...
    ?>
    
     
    Problem is the loop gets executed 1200 times which causes 500 error. Is there any way around this? Thank you, any advice appreciated guys.
  19. Hi there. I have to add a file upload facility to the form. Theres just one problem though - the form is submitted via AJAX.

    $("#s_prop_full").validate({
        submitHandler: function (form) {
            $.ajax({
                type: "POST",
                url: "submit-proposal-full.php",
                data: $("#s_prop_full").serialize(),
    beforeSend: function() {
           $('#loader3').fadeIn();
       },
    complete: function(){
         $('#loader3').fadeOut();
     
       }
            }).done(function (msg) {
                $('#loader3').fadeOut();
     
     
    $('#s_prop_full').fadeOut();
    $('.s-form-full-thanks').fadeIn();
     
            });
        },
    .....
    
    

    It's pretty standard PHP after that:

     

     move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);

    What do i have to add to the AJAX function? Thank you

×
×
  • 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.