Jump to content

Clandestinex337

Members
  • Posts

    48
  • Joined

  • Last visited

    Never

Everything posted by Clandestinex337

  1. sorry, that makes sense. I want to make it so that I am able to add links to the rows, and I thought it was bad practice to add html to functions. So I don't want t to add href's to the while loop and have to go into the function every time to change the links.. So I want to be able to call the rows through my switch similar to something like this case index: $user = new User($connect); $user->showAll(); echo '<a href="#">' . $user->getUsername() . '<a/>' . '<br/>'; echo '<a href="#">' . $user->getPassword() . '<a/>' . '<br/>'; break; Thanks
  2. Ok, so I have my function that shows all the rows in my db public function showAll() { $query = mysql_query("SELECT `username`, `password` FROM `user` ORDER BY `id` DESC"); $arr = array(); while($result = mysql_fetch_assoc($query)) { $arr[] = $result; echo $result['username'] . '<br/>'; echo $result['password']; } } but with that I believe its going to be a pain and the wrong way to add a href= to them when I want to add links for them so I thought this way would work public function showAll() { $query = mysql_query("SELECT `username`, `password` FROM `user` ORDER BY `id` DESC"); $arr = array(); while($result = mysql_fetch_assoc($query)) { $arr[] = $result; $this->username = $result['username']; $this->password = $result['password']; } } but in my switch when I call everything case index: $user = new User($connect); $user->showAll(); echo $user->getUsername() . '<br/>'; echo $user->getPassword() . '<br/>'; break; it only shows the first row in the database.. Any idea on how I could make this work? Thanks
  3. I want it to run the register switch.. so when I go to ?view=register it shows the form, when I click submit it submits the data to my database. I just now want to redirect to the index once I hit submit and the data is sent to the database. Right now when I hit submit it just opens a new register page.
  4. no, that its showing already users that are in the database. with mysql_query and mysql_fetch
  5. So this is something really easy I would guess. I just can't get it to work. I have a switch setup to run my functions <?php include 'db_fns.php'; $controller = 'view'; $view = empty($_GET['view']) ? 'index' : $_GET['view']; switch($view) { case index: $user = new User($connect, 1); echo $user->getUsername() . '<br/>'; echo $user->getPassword(); break; case register: $page = 'register'; $user = new User($connect); $user->setUsername($_POST['username']); $user->setPassword($_POST['password']); $id = $user->save(); break; } if (isset($page)) { include ($_SERVER['DOCUMENT_ROOT'] . 'img_nexus/views/photos/' . $page . '.php'); } ?> and then I have my form <form method="POST"> <input type="text" name="username" value="Username" onclick="this.value = ''" /><br/> <input type="text" name="password" value="Password" onclick="this.value = ''" /><br/> <input type="submit" name="submit" /> </form> I tried target="index.php" but if I do that, then it doesn't grab the values from the _POST
  6. I am still really new to php aswell, but maybe a loop so if $loop <= 2 do random code?
  7. I believe I have to implement my fetch($sql) into the _construct, but i am not sure where it is supposed to fit
  8. So my other functions work, where I create new user and update user, but showing the users doesn't seem to work class MySql { var $host; var $user; var $password; var $database; public $db_handle; public function connect($db_host, $db_user, $db_password, $db_database) { $this->host = $db_host; $this->user = $db_user; $this->password = $db_password; $this->database = $db_database; $this->db_handle = mysql_connect($this->host, $this->user, $this->password)or die('could not connect to db'); mysql_select_db($this->database)or die('could not select db'); } public function query($sql) { return mysql_query($sql, $this->db_handle) or die (mysql_error()); } public function fetch($sql) { return mysql_fetch_assoc($this->query($sql), $this->db_handle); } } class User { public $db; public $id; public $username; public $password; public function __construct(MySql $db, $id = '0') { $this->db = $db; $this->id = $id; if($this->$id !== 0) { $result = $this->db->query("SELECT `username`, `password` FROM `user` WHERE `id` ='{$this->id}'"); if($result) { $this->username = $result->username; $this->password = $result->password; } } } $user = new User($connect, 31); echo $user->getUsername(); I can't seem to spot why its not working This is my print_r let me know if you need the full code.. Cheers
  9. Thanks, that makes sense. Instead of making another post, I will just put it in here. The other part of the code I am trying to learn has public function __construct(MySql $db, $id = 0) I am confused on the MySql $db. Does it give the class MySql = $db? Thanks again!
  10. I understand what $this->db means, its accessing the variable db, but when you add the extra -> to the end what does that do? Its very hard to research information on this since Google doesn't help to much when using -> in the search. Thanks
  11. Thanks again, I don't want to bother you to explain the code step by step. Would you happen to know a tutorials step by step on setting up an optimal class to do pretty much what you have there, but they go more in depth? Thanks a lot, and I do appreciate the time to help me out.
  12. Hey Thorpe, thanks for the code you posted. That seems a lot more logical to put it that way With that code, I am confused with $id = $this->db->query Where did query come from? Is this based of my MySql class? And also the public function __construct(MySql $db, $id = 0) is the MySql based of the class above? Thanks a lot..
  13. Thanks for the reply! And yea, I am learning it as I go. I can't read something and learn that way, I have to build something to fully get how it works
  14. Thanks for the reply, and tips. That helps out a lot. This is the mysql_error(); I am getting now after the changes. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' )' at line 1CreateUser Object ( [username:CreateUser:private] => devyn [password:CreateUser:private] => password ) this is what I changed $newUser = new CreateUser(); $newUser->setUsername('devyn'); $newUser->setPassword('password'); $connect->query("INSERT INTO `user` (`username`, `password`) VALUES ({$CreateUser['username']}, {$CreateUser['password']})"); echo mysql_error(); print_r($newUser); and after what thorpe said, it makes sense to have the sql query to be in the class that is using it. Would you mind helping me implement that in my current code? I posted the function in the post above, just let me know if I am on the right track edit: or instead of making the query a function in the CreateUser class, I can just make it a public variable? public $sql = "INSERT INTO `user` (`username`, `password`) VALUES ({$CreateUser['username']}, {$CreateUser['password']})";
  15. sorry for my ignorance, but would you be able to help with an example? edit: class CreateUser { var $username; var $password; public function setUsername($name) { $this->username = $name; } public function setPassword($password) { $this->password = $password; } public function query($sql) { $sql = "INSERT INTO `user` WHERE `username` = $this->username, `password` = $this->password"; } } like so? and if so, how would I get that function to connect to the query function is the MySql class.
  16. I changed the code to $mysql->query("INSERT INTO `user` WHERE `username` = $CreateUser[username], `password` = $CreateUser[password]"); and now I am getting this error instead Warning: mysql_query() expects parameter 2 to be resource, null hrmm..
  17. Thanks for the catch, I fixed it, but its still gives me the error
  18. I tried $mysql->query("INSERT INTO `user` WHERE `username` = $newUser[username], `password` = $newPassword[password]"); but I got Fatal error: Cannot use object of type CreateUser as array in
  19. Hello, here is my code so far. <?php include 'config_fns.php'; class MySql { private $host; private $user; private $password; private $database; public $db_handle; public function connect($db_host, $db_user, $db_password, $db_database) { $this->host = $db_host; $this->user = $db_user; $this->passowrd = $db_password; $this->database = $db_database; $this->db_handle = mysql_connect($this->host, $this->user, $this->password)or die('could not connect to db'); mysql_select_db($this->database)or die('could not select db'); } public function query($sql) { return mysql_query($sql,$this->db_handle); } public function fetch($sql) { return mysql_fetch_assoc($this->query($sql)); } } $connect = new MySql(); $connect->connect($db_host, $db_username, $db_password, $db_name); class CreateUser { var $username; var $password; public function setUsername($name) { $this->username = $name; } public function setPassword($password) { $this->password = $password; } } $newUser = new CreateUser(); $newUser->setUsername('devyn'); $newUser->setPassword('password'); $mysql = new MySql(); $mysql->query("INSERT INTO `user` WHERE `username` = ???") print_r($newUser); ?> I am stuck at the $mysql->query("INSERT") I am able to see with the print_r that they are in an array and retrieving the information for username and password, I am just not sure how to pass that into the sql query. Also, I noticed when I put private $username private $password I get this with the print_r CreateUser Object ( [username:CreateUser:private] => devyn [password:CreateUser:private] => password ) And when I use the var $username var $password I get this CreateUser Object ( [username] => devyn [password] => password ) is there a difference? Thanks
  20. Thanks for that, as I said this is for fun and to increase my skill. So I would like to hard code everything
  21. Thanks for the reply, and sorry for posting in the wrong area.. I guess I used the wrong term social network. I want to create a site similar to plenty of fish. Looking at the site seems simple. When user registers they fill out a form of information. And then other users are able to browse for people with similar interest and that are local. And then they are able to communicate with each other trough either PM or just sending emails. So from what I know it doesn't seem to hard. User registers with information about that user. Users are able to search according to information other users have put in their profile. If they are interested in each other they contact. And potentially down the line there will be a on site chat function.
  22. Ok, I just want to make a social network site for fun and an exercise. What are the best steps to go about finding out where I should start? I am intermediate at php I would believe. Thanks for any help. Cheers.
×
×
  • 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.