Jump to content

Clandestinex337

Members
  • Posts

    48
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

Clandestinex337's Achievements

Member

Member (2/5)

0

Reputation

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