Clandestinex337 Posted June 26, 2011 Share Posted June 26, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/240459-getting-my-while-function-to-loop-dynamically-from-my-switch-statement/ Share on other sites More sharing options...
mikesta707 Posted June 26, 2011 Share Posted June 26, 2011 The reason you only get two output is because you overwrite the value of $this->username and $this->password in every iteration of the loop. I'm unsure of what exactly you want the links to be. Can you explain that a little better? Quote Link to comment https://forums.phpfreaks.com/topic/240459-getting-my-while-function-to-loop-dynamically-from-my-switch-statement/#findComment-1235094 Share on other sites More sharing options...
Clandestinex337 Posted June 26, 2011 Author Share Posted June 26, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/240459-getting-my-while-function-to-loop-dynamically-from-my-switch-statement/#findComment-1235096 Share on other sites More sharing options...
mgoodman Posted June 26, 2011 Share Posted June 26, 2011 You need to do something like this: class User { public $users = array(); public function getAll() { $query = mysql_query("SELECT `username`, `password` FROM `user` ORDER BY `id` DESC"); while($result = mysql_fetch_assoc($query)) { $this->users[] = $result; } } // the rest of your User class code } and then in your switch: case index: $user = new User($connect); $user->getAll(); foreach ($user->users as $userInfo) { echo echo '<a href="#">' . $userInfo['username'] . '<a/><br/>'; echo echo '<a href="#">' . $userInfo['password'] . '<a/><br/>'; } break; Quote Link to comment https://forums.phpfreaks.com/topic/240459-getting-my-while-function-to-loop-dynamically-from-my-switch-statement/#findComment-1235104 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.