Jump to content

mattheww

Members
  • Posts

    19
  • Joined

  • Last visited

About mattheww

  • Birthday 10/05/1992

Profile Information

  • Gender
    Male
  • Location
    Leeds, UK

mattheww's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. I'm fairly new to object orientated PHP and I was trying to think of a better way to connect to a MySQLi database within classes and functions, instead of just putting down an include everywhere. So far I came up with this: database.php <?php class database { protected $db_name = 'database'; protected $db_user = 'root'; protected $db_password = 'password'; protected $db_host = 'localhost'; function __construct(){ $this->connect(); } public function connect(){ $this->db = new mysqli($this->db_host, $this->db_user, $this->db_password, $this->db_name); } } $db = new database; ?> test.php class test{ private function getDb(){ include("database.php"); return $db; } public function test1(){ $find = $this->getDb()->db->query("SELECT * FROM user"); $rows = $find->num_rows; return $rows; } } and then just putting echo $test->test1(); on my page. I did have other functions inside the database class, but I've since scrapped those, so it just looks like that atm. It does work... however, I'm not sure how good or bad it is. In particular the first line in the test1() function. I have a feeling its a terrible way to do it... Or is it fine? like efficiency wise etc? before I would just use an include within every function. so I'd just do: $db->query(" "); Eventually I will have a few classes like user, etc, and these would all need to manipulate the database. So I was just thinking of plopping the getDb() function at the top of each of them and just include the database stuff... I tried looking at how phpBB and wordpress handle databases, but theirs are just full of unnecessary stuff for me and its just difficult to decipher. They use a global I think?
  2. Yeah, from a database... And thanks (Y) That's what I wanted!
  3. You could use something like this... //If user has submitted data into username & password fields if ($username&&$password) { include('includes/connect.php'); //select user data from your user's table $query = mysql_query("SELECT username, password FROM users WHERE username='$username' LIMIT 1") or die(mysql_error()); $numrows = mysql_num_rows($query); //if the rows related to the query are more than zero if ($numrows > 0) { $row = mysql_fetch_assoc($query); $dbuser = $row['username']; // IF the username is equal to the username in the database, and the password is equal to the password in the database if ($username == $dbuser && $password == $row['password']) { //set a session of the users' username, and redirect them to a member page. $_SESSION['username'] = $dbuser; header("Location: logged_in.php"); } else echo "Username or Password is incorrect"; } else echo"Username not registered"; } else echo"Please fill in all fields"; Then on each protected page, you could have at the top: session_start(); $user = $_SESSION['username']; if(!$user){ die("You must be logged in to view this page"); }
  4. Yeah, you can do it by getting a user's IP and from that you can work out what country they are from, and also what city, I think?
  5. I want to display pictures in a table using a PHP loop or something, for example 3 pictures in one row, and then when that row has filled, move to the next row, So I can have a grid of pictures, (similar to that on facebook, when your viewing photo albums)... But I've no idea how to do it!
  6. unfortunately it didn't work , thanks for your suggestion anyway!
  7. Its fine creating intial directories, but if i want to create a directory within a directory i am running into problems, here teh code i have so far... if ($_POST['createdir']){ $newdir = $_POST['name']; $dir = $_GET['dir']; $root = getcwd(); $create = mkdir("$root/dir/$dir/$newdir", 0700); } it basically takes the directory name from the URL... where it says /$dir if i replace that for it's text equivalent, and don't use a variable it works fine, but i need it to work... However i don't know what i'm doing wrong!
  8. That worked previously, which is good however i need the mime file thingo so i did $filetype = mime_content_type($path.$file); and it hasn't worked saying a similar thing!
  9. ahh, that makes sense, Thanks lemmin!
  10. What one would like to do is show a particular directory's files, filesize and filetype of each file, I have the first bit where it echo's the filename out, but when I try filesize and filetype, it goes all wrong! I just can't seem to find out whats wrong! I keep getting: Warning: filetype() [function.filetype]: Lstat failed for Koala.jpg in ... on line 42 <?php $path = "dir/"; $dir_handle = @opendir($path) or die("Unable to open $path"); while ($file = readdir($dir_handle)) { $filetype = filetype($file); if($file!="." && $file!=".."){ echo "<tr class='dir'>"; echo "<td><a href='dir/$file'>$file</a></td>"; echo "<td>$filetype</td>"; echo "<td></td>"; echo "</tr>"; } } closedir($dir_handle); ?> I'm sure its something simple but, its really bugging me!
  11. Basically I have a text area, a query to send the data to the table, etc, etc... but the problem is, MySQL won't allow certain characters, for example the Apostrophe. I know this stops people doing MySQL injection, but how do I get it so PHP or MySQL replaces the character for something else, and then converts it back when I query it?
  12. I see that's useful... and what if i was to type in exact words, like if a MySQL result returns "hello" and I wanted the php code to only change a word/exact phrase red if it said "Hello" How would I do that?
  13. I want to do the sort of formatting like in Microsoft excel. I want to do where for example, I typed in "1" and it turned red, I typed in "2" It turned blue Is it possible? I presuming it is... but HOW, I'm not sure how to go about this...
×
×
  • 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.