Jump to content

ibnclaudius

Members
  • Posts

    40
  • Joined

  • Last visited

    Never

Everything posted by ibnclaudius

  1. It's a function to format the bloomp (post).
  2. Is there a better way to do this? $formatted_bloomp = '<div data-id="' . $bloomp['id'] . '" class="bloomp invisible">'; if ($bloomp['image_id']) { $formatted_bloomp .= '<div class="media image">'; $formatted_bloomp .= '<img src="' . base_url('upload/image/' . $bloomp['image_id']) . '">'; $formatted_bloomp .= '</div>'; } elseif ($bloomp['video_id'] && $bloomp['video_provider']) { $formatted_bloomp .= '<div class="media video">'; if ($bloomp['video_provider'] == 'youtube') { $formatted_bloomp .= '<iframe width="310" height="233" src="http://www.youtube.com/embed/' . $bloomp['video_id'] . '?rel=0&wmode=transparent&iv_load_policy=3&color=white" frameborder="0" allowfullscreen></iframe>'; } if ($bloomp['video_provider'] == 'vimeo') { $formatted_bloomp .= '<iframe src="http://player.vimeo.com/video/' . $bloomp['video_id'] . '?color=#ffffff" width="310" height="233" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>'; } $formatted_bloomp .= '</div>'; } $formatted_bloomp .= '<span class="text">' . $bloomp['text'] . '</span>'; $formatted_bloomp .= '<div class="info">'; $formatted_bloomp .= '<span>'; $formatted_bloomp .= '<a class="author" href="#">'; $formatted_bloomp .= '<img src="' . base_url('upload/picture/' . $bloomp['author_picture']) . '">'; $formatted_bloomp .= '<span class="name">' . $bloomp['author_name'] . '</span>'; $formatted_bloomp .= '</a>'; $formatted_bloomp .= '</span>'; $formatted_bloomp .= '<span>' . $bloomp['likes_number'] . ' curtidas</span>'; $formatted_bloomp .= '<span>' . $bloomp['comments_number'] . ' coment?rios</span>'; $formatted_bloomp .= '</div>'; if ($bloomp['comments_number'] > 0) { $formatted_bloomp .= '<div class="comments">'; foreach ($this->_CI->quickstart_model->read_comments(3, $bloomp['id']) as $comment) { $formatted_bloomp .= '<div data-id="' . $comment['id'] . '" class="comment clearfix">'; $formatted_bloomp .= '<a href="#"><img class="float-left" src="' . base_url('upload/picture/'. $comment['author_picture']) . '"></a>'; $formatted_bloomp .= '<span class="float-right">'; $formatted_bloomp .= '<a href="#">' . $comment['author_name'] . '</a> ' . $comment['text']; $formatted_bloomp .= '</span>'; $formatted_bloomp .= '</div>'; } if ($bloomp['comments_number'] > 3) { $formatted_bloomp .= '<div class="comment all">'; $formatted_bloomp .= '<a href="#">Todos os ' . $bloomp['comments_number'] . ' coment?rios...</a>'; $formatted_bloomp .= '</div>'; } $formatted_bloomp .= '</div>'; } $formatted_bloomp .= '</div>'; return $formatted_bloomp;
  3. First I get all posts, then, with a foreach, I get the comments. Is it possible to get everything with only one query?
  4. Is there a better way to do this? $i = 0; foreach ($posts as $post) { $posts[$i]['function_return'] = function($post['function_param']); $i++; }
  5. I want to vertical align the label with the input no matter the label size. Here's my code and what it output: <div class="form-item"> <div class="form-label strong left">Nome:</div> <div class="form-input-wrapper left"> <input class="form-input" type="text"> </div> </div> <div class="form-item"> <div class="form-label strong left">Email:</div> <div class="form-input-wrapper left"> <input class="form-input" type="text"> </div> </div> <div class="form-item"> <div class="form-label strong left">Email novamenteEEEEEEEEEEEEEE:</div> <div class="form-input-wrapper left"> <input class="form-input" type="text"> </div> </div> .form-item { border-bottom:1px solid #eee; overflow:hidden; padding:15px 20px } .form-label { color:#666; position:absolute; text-align:right; width:100px; } .form-input { display:block; width:232px; }
  6. Fixed: if ($count_school_update_likes >= 1) Thanks, all!
  7. Thanks, all! I made some changes, almost everything is correct now, the only problem is when there's only one user that liked, his name or you do not output. if ($count_school_update_likes > 0) { $likers = array(); if ($count_school_update_likes > 1) { $i = 0; if ($count_school_update_liked == 1) { $likers[] = 'Você'; $i = 1; } if ($get_school_update_likes) { foreach ($get_school_update_likes->result_array() as $liker) { $likers[] = '<a id="' . $liker['user_id'] . '" href="' . $liker['user_id'] . '">' . $liker['user_name'] . '</a>'; $i++; if ($i == 3) { $count_school_update_likes = $count_school_update_likes - 3; if ($count_school_update_likes > 0) { $likers[] = 'mais <a id="stream-updates-item-likes-view" href="#">' . $count_school_update_likes . '</a> '; } break; } } } } $last_liker = count($likers) > 1 ? ' e ' . array_pop($likers) . ' curtiram' : ' curtiu'; $likers = implode(', ', $likers) . $last_liker . ' isto'; echo $likers; }
  8. And when more than one user liked, returned this: fixed with: $likers[] = '<a id="' . $liker['user_id'] . '" href="' . $liker['user_id'] . '">' . $liker['user_name'] . '</a>';
  9. jcbones, I liked that you keep the format But there's some little bugs. when only the user likers, returned this: Fixed with: $last_likers = count($likers) > 1 ? ' and ' . array_pop($likers) : ''; And when more than one user liked, returned this:
  10. A preg_replace like this would also work: if ($count_school_update_likes == '1') { //remove last comma from $likers } else if ($count_school_update_likes > '1') { //remove last comma from $likers and replace the second last comma for "and" } Or something that work on both cases...
  11. When i ran: $likers = $get_school_update_likes->result_array(); $lastLiker = count($likers) > 1 ? ' and ' . array_pop($likers) : ''; $likers = implode(', ', $likers); echo $likers . $lastLiker . ' like this.'; i got this: A PHP Error was encountered Severity: Notice Message: Array to string conversion Filename: libraries/action.php Line Number: 230 Array and Array like this And: print_r($likers) outputs something like this: Array ( [0] => Array ( [user_id] => 3 [user_name] => Derp ) [1] => Array ( [user_id] => 4 [user_name] => Derpina ) )
  12. No, but how am I going to do the "and 999 others like this"?
  13. Here's my code: if ($count_school_update_likes > '0') { $likers = ''; if ($count_school_update_liked == "1") { $likers .= 'You, '; } if ($count_school_update_likes > '1') { $i = 0; foreach ($get_school_update_likes->result_array() as $liker) { $likers .= '<a id="' . $liker['user_id'] . '" href="' . $liker['user_id'] . '">' . $liker['user_name'] . '</a>, '; $i++; if ($i == 2) { $count_school_update_likes = $count_school_update_likes - 3; if ($count_school_update_likes > "0") { $likers .= ' and <a id="stream-updates-item-likes-view" href="#">' . $count_school_update_likes . ' others</a> '; } break; } } } $likers .= ' like this.'; echo preg_replace("/,([^,]*)$/", " $1", $likers); //is not working correctly } Here is all the possibles outputs I would like to have: You, like this => You like this You, Derp, like this => You and Derp like this You, Derp, Derpina, like this => You, Derp and Derpina like this You, Derp, Derpina, Derpson1, Derpson2, Derpson3, Derpson4, like this => You, Derp, Derpina and other 4 like this Derp, like this => Derp like this Derp, Derpina, like this => Derp and Derpina like this Derp, Derpina, Derpson, like this => Derp, Derpina and Derpson like this Derp, Derpina, Derpson1, Derpson2, Derpson3, Derpson4, like this => Derp, Derpina, Derpson1 and other 3 like this
  14. How can I replace the comma from a string like this: You, like this => You like this You, Derp, like this => You and Derp like this You, Derp, Derpina, like this => You, Derp and Derpina like this You, Derp, Derpina, Derpson, like this => You, Derp, Derpina and Derpison like this
  15. Not sure if everything is working right because I am in the school computer and here I can not install apache / php / etc. I'm not sure about how to use cookies. Here is my code, any errors or suggestions just talk. Probably does not work, because as I said, not yet tested. login.php <?php session_start(); include 'class.php'; if (isset($_POST['username']) && isset($_POST['password'])) { $user = new User($_POST['username']); if ($user->exists()) { $login = $user->login($_POST['password']); if ($login) { $_SESSION['user_id'] = $login; session_write_close(); } else { echo "Login failed."; } } else { header("Location: register.php"); } } ?> <!DOCTYPE html> <html> <head> <title>Login Form</title> </head> <body> <form action="" method="post"> <label for="username">Username: </label> <input type="text" name="username" /><br /> <label for="password">Password: </label> <input type="password" name="password" /><br /> <input type="submit" value="Submit" /> </form> </body> </html> class.php <?php class User { protected $id; protected $username; protected $email; protected $sql; private $exists = FALSE; public function __construct($username) { if (empty($username)) { throw new Exception('Username cannot be blank.'); } $this->username = $username; $this->sql = new PDO(DSN, DBUSER, DBPASS); $this->exists = $this->validate(); } private function createLoginToken($id) { $token = $id . md5(microtime()); $expires = new DateTime(); $expires->add(new DateInterval('P30D')); $query = "INSERT INTO sessions (userID, token, expires) VALUES (:id, :token, :expires)"; $stmt = $this->sql->prepare($query); $stmt->execute(array(':id' => $id, ':token' => $token, ':expires' => $expires->format('Y-m-d H:i:s'))); setcookie('token', $token, $expires->getTimestamp(), '/'); } private function hashPassword($password, $salt) { $string = PASSWORD_SALT . $password . md5($salt); $hashed = crypt($string, '$2a$12$' . substr(md5($salt), 0, 22)); return $hashed; } private function validate() { $query = "SELECT COUNT(id) FROM users WHERE username = :username"; $stmt = $this->sql->prepare($query); $stmt->execute(array(':username' => $this->username)); $count = $stmt->fetchColumn(); return ($count > 0) ? TRUE : FALSE; } public function exists() { return $this->exists; } public function login($password, $remember = FALSE) { $query = "SELECT id, password, UNIX_TIMESTAMP(created) AS salt FROM users WHERE username = :username"; $stmt = $this->sql->prepare($query); $stmt->execute(array(':username' => $this->username)); $row = $stmt->fetch(PDO::FETCH_OBJ); $hashed = $this->hashPassword($password, $row->salt); if ($row->password == $hashed) { if ($remember) { $this->createLoginToken($row->id); } return $row->id; } return FALSE; } public function random() { $random = mt_random(1000,9999); return $random; } public function registerUser($email) { if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { throw new Exception('Email does not appear to be valid.'); } $this->email = $email; $date = new DateTime(); $hashed = $this->hashPassword($pass->random(), $date->getTimestamp()); $query = "INSERT INTO users (username, password, email, created) VALUES (:username, :password, :email, :created)"; $stmt = $this->sql->prepare($query); $success = $stmt->execute(array(':username' => $this->username, ':password' => $hashed, ':email' => $email, ':created' => $date->format('Y-m-d H:i:s'))); return ($success === TRUE) ? $this->sql->lastInsertId() : FALSE; } public function verifyCookie($token) { $query = "SELECT userID FROM sessions WHERE token = :token AND expires > NOW()"; $stmt = $this->sql->prepare($query); $stmt->execute(array(':token' => $token)); return $stmt->fetchColumn(); } } ?> db.sql CREATE TABLE IF NOT EXISTS `users` ( `id` int(11) DEFAULT NULL AUTO_INCREMENT, `username` varchar(30) DEFAULT NULL, `password` varchar(60) DEFAULT NULL, `email` varchar(100) DEFAULT NULL UNIQUE, `created` datetime DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB; CREATE TABLE IF NOT EXISTS `sessions` ( `id` int(11) DEFAULT NULL AUTO_INCREMENT, `userID` int(10) DEFAULT NULL, `token` varchar(50) DEFAULT NULL, `expires` datetime DEFAULT NULL, KEY `userID` (`id`,`userID`,`token`,`expires`), KEY `token` (`id`,`userID`,`token`,`expires`), PRIMARY KEY (`id`) ) ENGINE=InnoDB;
  16. A few more changes, check it. <?php class network { public $userID; public $schoolID; public $userEnrollment; public $userName; public $dbUserTable; public $dbSchoolTable; protected $sql; public function __construct($dbHost, $dbUser, $dbPass, $dbName) { $dsn = "mysql:host={$dbHost};dbname={$dbName}"; try { $this->sql = new PDO($dsn, $dbUser, $dbPass); } catch (PDOException $e) { throw new Exceptopn($e->getMessage()); } $this->dbUserTable = $dbUserTable; $this->dbSchoolTable = $dbSchoolTable; } public function registerUser($userEnrollment, $userName, $userPass) { $this->userName = $userName; $hashedPass = $this->hashPassword($userPass); $query = "INSERT INTO {$this->dbUserTable} VALUES (NULL, :enrollment, :username, :password)"; $stmt = $this->sql->prepare($query); $result = $stmt->execute(array(':enrollment' => $userEnrollment, ':username' => $userName, ':password' => $hashedPass)); return ($result === TRUE) ? $this->sql->lastInsertId() : FALSE; } public function registerSchool($schoolName) { $this->schoolName = $schoolName; $query = "INSERT INTO {$this->dbSchoolTable} VALUES (NULL, :schoolName)"; $stmt = $this->sql->prepare($query); $result = $stmt->execute(array(':schoolName' => $schoolName)); return ($result === TRUE) ? $this->sql->lastInsertId() : FALSE; } public function userLogin() { $dbLink = mysql_connect($this->dbHost, $this->dbUser, $this->dbPass); if (!$dbLink) die("Could not connect to database: " . mysql_error()); mysql_select_db($this->dbName); $query = "SELECT * FROM $this->dbUserTable WHERE userEnrollment = \"$this->userEnrollment\" AND userPass = \"$this->userPass\" LIMIT 1"; $result = mysql_query($query); if (!$result) { echo "Fail."; } else { $row = mysql_fetch_array($result); session_regenerate_id(); $_SESSION['userEnrollment'] = $this->userEnrollment; session_write_close(); } mysql_close($dbLink); } public function changePass($newPass) { $query = "SELECT COUNT(*) FROM {$this->dbUserTable} WHERE userName = :username LIMIT 1"; $stmt = $this->sql->prepare($query); $result = $stmt->execute(array(':username' => $this->userName)); if (!$result) { throw new Exception('User does not exist.'); } else { $hashedPass = $this->hashPassword($newPass); $query = "UPDATE {$this->dbUserTable} SET userPass = :password WHERE userName = :username"; $stmt = $this->sql->prepare($query); $result = $stmt->execute(array(':password' => $hashedPass, ':username' => $this->userName)); return ($result === TRUE) ? TRUE : FALSE; } } private function hashPassword($password) { $salt = "This shouldn't really be hard-coded into the function"; $hashed = crypt($password, '$2a$12$' . substr(md5($salt), 0, 22)); return $hashed; } } ?> What should I change to the code have a better "functioning"? I'm not sure about how to call this class and function. How should I make a login form?
  17. Oh, I didnt restart it. Now I see the erros. I fixed the ')' problem and change this: var $userID, $userEnrollment, $userPass, $dbHost, $dbUser, $dbName, $dbPass, $dbUserTable; to: public $userID; public $userEnrollment; public $userPass; public $dbHost; public $dbUser; public $dbName; public $dbPass; public $dbUserTable; Now seems to be correct, I got this error because I dind set dbInfo yet: I'll set it and give feedback soon.
  18. I'm newbie in PHP and english is not my native language, this makes it a little harder Could you give me an example of what you said? I made some changes, check it! <?php class network { public $userID; public $schoolID; public $userEnrollment; public $userName; public $userPass; public $dbHost; public $dbUser; public $dbName; public $dbPass; public $dbUserTable; public $dbSchoolTable; function dbInfo() { $this->dbHost = 'localhost'; $this->dbUser = ''; $this->dbPass = ''; $this->dbName = ''; $this->dbUserTable = ''; $this->dbSchoolTable = ''; } function registerUser($userEnrollment, $userName, $userPass) { $dbLink = mysql_connect($this->dbHost, $this->dbUser, $this->dbPass); if(!$dbLink) die("Could not connect to database: " . mysql_error()); mysql_select_db($this->dbName); $query = "INSERT INTO $this->dbUserTable VALUES (NULL, \"$userEnrollment\", \"$userName\", \"$userPass\")"; $result = mysql_query($query); if(!$result) { echo "Fail."; } else { $this->userID = mysql_insert_id(); } mysql_close($dbLink); $this->userName = $userName; $this->userPass = $userPass; } function registerSchool($schoolName) { $dbLink = mysql_connect($this->dbHost, $this->dbUser, $this->dbPass); if(!$dbLink) die("Could not connect to database: " . mysql_error()); mysql_select_db($this->dbName); $query = "INSERT INTO $this->dbSchoolTable VALUES (NULL, \"$schoolName\")"; $result = mysql_query($query); if(!$result) { echo "Fail."; } else { $this->schoolID = mysql_insert_id(); } mysql_close($dbLink); $this->schoolName = $schoolName; } function userLogin() { $dbLink = mysql_connect($this->dbHost, $this->dbUser, $this->dbPass); if(!$dbLink) die("Could not connect to database: " . mysql_error()); mysql_select_db($this->dbName); $query = "SELECT * FROM $this->dbUserTable WHERE userEnrollment = \"$this->userEnrollment\" AND userPass = \"$this->userPass\" LIMIT 1"; $result = mysql_query($query); if(!$result) { echo "Fail."; } else { $row = mysql_fetch_array($result); session_regenerate_id(); $_SESSION['userEnrollment'] = $this->userEnrollment; session_write_close(); } mysql_close($dbLink); } function changePass($newPass) { $dbLink = mysql_connect($this->dbHost, $this->dbUser, $this->dbPass); if(!$dbLink) die("Could not connect to database: " . mysql_error()); mysql_select_db($this->dbName); $query = "SELECT * FROM $this->dbUserTable WHERE userName = \"$this->userName\" LIMIT 1"; $result = mysql_query($query); if(!$result) { echo "Fail."; } else { $query = "UPDATE $this->dbUserTable SET userPass = \"$newPass\" WHERE userName = \"$this->userName\""; $result = mysql_query($query); if(!$result) { echo "Fail"; } else { $this->userPass = $newPass; } } mysql_close($dbLink); } } ?> Thank you for your help!!!
  19. I changed the pnp.ini, but i still can't see the errors.
  20. I changed: <? to <?php echo "$_SESSION['userEnrollment']"; to echo $_SESSION['userEnrollment']; $D = new network; to $D = new network(); But ajax_login.php still gives me a blank page, even with no dbInfo setted. Should give a MySQL error...
  21. ajax_login.php gives a blank page... login.php <form method="post" action="ajax_login.php"> Matrpicula: <input type="text" name="userEnrollment" maxlength="32"><br> Senha: <input type="password" name="userPass" maxlength="32"><br> <input type="submit"> </form> ajax_login.php <? session_start(); include 'class/network.php'; $D = new network; $D->userEnrollment = mysql_real_escape_string($_POST['userEnrollment']); $D->userPassword = hash('sha512', $_POST['userPass']); $D->userLogin(); echo "$_SESSION['userEnrollment']"; ?> class/network.php <? class network { var $userID, $userEnrollment, $userPass, $dbHost, $dbUser, $dbName, $dbPass, $dbUserTable; function dbInfo() { $this->dbHost = 'localhost'; $this->dbUser = 'user'; $this->dbPass = 'pass'; $this->dbName = 'dbname'; $this->dbUserTable = 'usertable'; } function userLogin() { $dbLink = mysql_connect($this->dbHost, $this->dbUser, $this->dbPass); if(!$dbLink) die("Could not connect to database: " . mysql_error()); mysql_select_db($this->dbName); $query = "SELECT * FROM $this->dbUserTable WHERE userEnrollment = \"$this->userEnrollment\" AND userPass = \"$this->userPass\" LIMIT 1"; $result = mysql_query($query); if(!$result) { echo "Fail."; } else { $row = mysql_fetch_array($result)) session_regenerate_id(); $_SESSION['userEnrollment'] = $this->userEnrollment; session_write_close(); } mysql_close($dbLink); } } ?>
  22. I am new to PHP. I developed this class, I wonder if there's anything wrong or that I can improve. I could not test it because I'm in school. Thanks in advance. <? class user { var $userID, $schoolID, $userName, $userPass, $dbHost, $dbUser, $dbName, $dbPass, $dbUserTable; $dbSchoolTable; function dbInfo() { $this->dbHost = 'localhost'; $this->dbUser = ''; $this->dbName = ''; $this->dbPass = ''; $this->dbUserTable = ''; $this->dbSchoolTable = ''; } function registerUser($userName, $userPass) { $dbLink = mysql_connect($this->dbHost, $this->dbUser, $this->dbPass); if(!$dbLink) die("Could not connect to database: " . mysql_error()); mysql_select_db($this->dbName); $query = "INSERT INTO $this->dbUserTable VALUES (NULL, \"$userName\", \"$userPass\")"; $result = mysql_query($query); if(!$result) { echo "Fail."; } else { $this->userID = mysql_insert_id(); } mysql_close($dbLink); $this->userName = $userName; $this->userPass = $userPass; } function registerSchool($schoolName) { $dbLink = mysql_connect($this->dbHost, $this->dbUser, $this->dbPass); if(!$dbLink) die("Could not connect to database: " . mysql_error()); mysql_select_db($this->dbName); $query = "INSERT INTO $this->dbSchoolTable VALUES (NULL, \"$schoolName\")"; $result = mysql_query($query); if(!$result) { echo "Fail."; } else { $this->schoolID = mysql_insert_id(); } mysql_close($dbLink); $this->schoolName = $schoolName; } function userLogin() { $dbLink = mysql_connect($this->dbHost, $this->dbUser, $this->dbPass); if(!$dbLink) die("Could not connect to database: " . mysql_error()); mysql_select_db($this->dbName); $query = "SELECT * FROM $this->dbUserTable WHERE userName = \"$this->userName\" AND userPass = \"$this->userPass\" LIMIT 1"; $result = mysql_query($query); if(!$result) { echo "Fail."; } else { while($row = mysql_fetch_array($result)) { session_start(); $_SESSION['userID'] = $row['userID']; session_write_close(); } } mysql_close($dbLink); } function changePass($newPass) { $dbLink = mysql_connect($this->dbHost, $this->dbUser, $this->dbPass); if(!$dbLink) die("Could not connect to database: " . mysql_error()); mysql_select_db($this->dbName); $query = "SELECT * FROM $this->dbUserTable WHERE userName = \"$this->userName\" LIMIT 1"; $result = mysql_query($query); if(!$result) { echo "Fail."; } else { $query = "UPDATE $this->dbUserTable SET userPass = \"$newPass\" WHERE userName = \"$this->userName\""; $result = mysql_query($query); if(!$result) { echo "Fail"; } else { $this->userPass = $newPass; } } mysql_close($dbLink); } } ?>
  23. oh, got it... but i'm not understanding this part: echo'display:none;' ;
  24. What is this? I'm not understanding it. style="<?= $D->tabs_state['all']==0||$D->tab=='all'?'display:none;':'' ?>"
×
×
  • 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.