Jump to content

ANdrww

New Members
  • Posts

    9
  • Joined

  • Last visited

ANdrww's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. I haven't read all the posts, but I use Sublime Text 2 + some addons. It's soo fast and light. Why isn't that mentioned in the poll?
  2. I see. Thanks a lot for the quick replies.
  3. Thanks a lot. I knew there was a simple way. Someone on stackoverflow suggested this but couldnt explain to me why he used a foreach loop twice foreach($posts as $postInfo) { foreach($postInfo as $key => $val) { echo '<p>' .$key . ':&nbsp' . $val. '</p>'; } }
  4. Hello, I have yet another problem I am trying to understand. The thing I don't get is the foreach loop. Here's my code. I have a function to get the posts from the db as follows: function get_posts($connection) { $posts = array(); $sql = "SELECT * FROM posts ORDER BY stamp DESC"; $result = mysqli_query($connection, $sql); while ($post = mysqli_fetch_object($result)) { $posts = array('body' => $post->body, 'stamp' => $post->stamp, 'post_id' => $post->id, 'user_id' => $post->user_id); } return $posts; } And I'm (incorrectly) using a foreach loop to display the results like this: $posts = get_posts($connection); foreach ($posts as $key => $value) { echo $posts['body'] . "<hr>"; } If I echo $posts['body'] I get the first record showing up 4 times. I've used var_dump($posts) to make sure it's an array, and it is, and it shows the same first record 4 times. Any idea? I'm currently re-reading http://www.php.net/manual/en/control-structures.foreach.php to try and understand this. Thanks, Andrei
  5. Sorry, sorry sorry. I've just read that I misspelled $connection. It's now working as expected, and I'm currently trying to figure out the other functions I've wrote as well. Please don't close the topic (though solved) as I might (will for sure) ask other questions as well. Thanks a lot. Turns out I need to be more careful when writing the names.
  6. Thanks a lot.. still not quite there though :\ Functions.php function show_users($conection) { $users = array(); $sql = "SELECT id, username FROM users WHERE status = 'active' ORDER BY username"; $result = mysqli_query($connection, $sql); while ($data = mysqli_fetch_array($result)) { $users[$data->id] = $data->username; } return $users; } Users.php <?php $users = show_users($connection); foreach ($users as $key => $value) { echo $key . " " . $value; } ?> Error Notice: Undefined variable: connection in /Applications/MAMP/htdocs/blog/includes/functions.php on line 22 Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /Applications/MAMP/htdocs/blog/includes/functions.php on line 22 Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in /Applications/MAMP/htdocs/blog/includes/functions.php on line 24 It still doesn't get the connection. What am I missing? I've included the database.php file in users.php, I have the variable $connection = mysqli_connect($host, $username, $password, $database); Frustrating
  7. Thanks for the reply Ch0cu3r. I've tried passing the $connection variable to my function as follows: function show_users($conection) { $users = array(); $sql = "SELECT id, username FROM users WHERE status = 'active' ORDER BY username"; $result = mysqli_query($connection, $sql); while ($data = mysqli_fetch_array($result)) { $users[$data->id] = $data->username; } return $users; } That throws the folloing error: Warning: Missing argument 1 for show_users(), called in /Applications/MAMP/htdocs/blog/users.php on line 18 and defined in /Applications/MAMP/htdocs/blog/includes/functions.php on line 19 Notice: Undefined variable: connection in /Applications/MAMP/htdocs/blog/includes/functions.php on line 22 Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /Applications/MAMP/htdocs/blog/includes/functions.php on line 22 Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in /Applications/MAMP/htdocs/blog/includes/functions.php on line 24 I should pass the $connection variable when calling the function in users.php ? Like this? <?php $users = show_users($connection); foreach ($users as $key => $value) { echo $key . " " . $value; } ?> Thanks Kevin for pointing that out.
  8. I am trying to create a script (from a tutorial I try to adapt to my own needs) and I am running into a few problems. Here I go... I have an /includes folder which contains the following: database.php and functions.php with the following content: database.php <?php // Database connectivity stuff $host = "localhost"; // Hostname for the database. Usually localhost $username = "root"; // Username used to connect to the database $password = "root"; // Password for the username used to connect to the database $database = "blog"; // The database used // Connect to the database using mysqli_connect $connection = mysqli_connect($host, $username, $password, $database); // Check the connection for errors if (mysqli_connect_errno($connection)) { // Stop the whole page from loading if errors occur die("<br />Could not connect to the database. Please check the settings and try again.") . mysqli_connect_error() . mysqli_connect_errno(); } ?> functions.php <?php // Functions file for the system function show_posts($user_id) { $posts = array(); $sql = "SELECT body, stamp from posts where user_id = '$user_id' order by stamp desc"; $result = mysqli_query($connection, $sql); while ($data = mysqli_fetch_assoc($result)) { $posts = array( 'stamp' => $data->stamp, 'user_id' => $user_id, 'body' => $data->body ); } return $posts; } function show_users() { $users = array(); $sql = "SELECT id, username FROM users WHERE status = 'active' ORDER BY username"; $result = mysqli_query($connection, $sql); while ($data = mysqli_fetch_array($result)) { $users[$data->id] = $data->username; } return $users; } function following($user_id) { $users = array(); $sql = "SELECT DISTINCT user_id FROM following WHERE follower_id = $user_id"; $result = mysqli_query($connection, $sql); while ($data = mysqli_fetch_assoc($result)) { array_push($users, $data->user_id); } return $users; } ?> And here's the code that I run to display the users in users.php <?php $users = show_users(); foreach ($users as $key => $value) { echo $key . " " . $value; } ?> That throws me the following errors: Notice: Undefined variable: connection in /Applications/MAMP/htdocs/blog/includes/functions.php on line 22 Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /Applications/MAMP/htdocs/blog/includes/functions.php on line 22 Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in /Applications/MAMP/htdocs/blog/includes/functions.php on line 24 I've tried to pass the $connection value to the show_users(); function with no results... any help would be appreciated. I am able to display the users from the database if I introduce the following code in users.php, but I want to try and keep things as clear as possible, without cluttering the files unnecessary. <?php $users = array(); $query = "SELECT id, username FROM users WHERE status = 'active' ORDER BY username"; $result = mysqli_query($connection, $query); if ($result) { while ($member = mysqli_fetch_assoc($result)) { echo strip_tags($member['username']) . "<br />"; } } else { echo "There are no posts to display. Why not add a new one?"; } mysqli_free_result($result); mysqli_close($connection); ?> PS: The users.php file also has require('includes/database.php'); and require('includes/functions.php'). I guess I'm trying to recreate the last bit of code into a working function... and I can't seem to do it. Any help is appreciated. I hope it makes an sense... -- Andrei
  9. Hello, I'm Andrei. I come from a design background, and I've recently started to learn php and MySQL. Anyway, the reason I ended up being here is because Stack Overflow didn't get me nowhere with my questions, so I thought I'd try this place too. It seems like a nice community so I think I will stick around. You were warned: I might (will) bomb the forums with all kings of stupid questions I guess... Well, that being said, it's nice to be here, hope I'll get some answers to some of my questions as well. -- Andrei
×
×
  • 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.