Jump to content

CyberShot

Members
  • Posts

    322
  • Joined

  • Last visited

Everything posted by CyberShot

  1. I want to create a database for my person use. I am the treasurer for an HOA. I collect money for 14 units monthly. I currently use a spreadsheet but I figured it would be a great learning experience to try creating a database. ( very little skill ). I want to be able to select a unit and see how much is owed, when the last payment was made and any other notes needed. I can't decide how I should make it. Should I just make a plain database with simple records like a spreadsheet or make it more complicated? by simple, I was thinking one table. ID, name, address ( a four digit number ) , paid ( bool ), amount_paid, date, notes I have toyed with the idea of making it more tables but I don't know if it is needed. The idea is that I want the ability to select a unit number ( address ) and see what is owed up to the current date. If I have a unit that hasn't paid in a year, I want to be able to see that and the months owed. if they pay half of the amount, I want to be able to do the math on that and see that. I am trying to figure out if a one table database will work just fine.
  2. is there a general rule as to when to set a variable? I was watching a tutorial today and the author made a small function that would accept a string. Why would you set the string in the function like that? Would it work if it was just $string? function e($string=""){ do something return $string; }
  3. I have made a database and have been able to connect to it. I have a web form for inserting records. When I fill out the form and submit, I find that the database is empty. I don't see any errors. What am I doing wrong $con = new MySQLi("localhost", "user", "password", "database" ); if(mysqli_connect_errno()){ die( "Failed to connect to MySQL" . mysqli_connect_error() ); } else { echo "Connection Established!"; } ?> <form id="insert" name="insert" method="post"> <fieldset> <legend>Resident Info:</legend> Name: <input type="text" name="name" id="name"><br> Address: <input type="text" name="address"><br> Telephone: <input type="text" name="telephone"><br> Email: <input type="text" name="email"><br> Date Paid: <input type="text" name="date"><br> Method: <input type="text" name="method"><br> Amount: <input type="text" name="amount"><br> Amount Owed: <input type="text" name="amountOwed"> <button type="submit" name="submit">Submit</button> </fieldset> </form> <?php if( $_SERVER['REQUEST_METHOD'] == 'POST') { $name = $_POST["name"]; $address = $_POST["address"]; $telephone = $_POST["telephone"]; $email = $_POST["email"]; $date = $_POST["date"]; $method = $_POST["method"]; $amount = $_POST["amount"]; $amountOwed = $_POST["amountOwed"]; $name = strtolower($name); echo ucwords($name); $query = "INSERT INTO residents (name, address, telephone, email, datepaid, method, amount, amountowed) VALUES ('$name','$address','$telephone','$email','$date','$method','$amount','$amountOwed')"; $con->query($query); if(!$con->query($query)){ die( 'Error: ' . $con->error ); } } ?>
  4. Solved it $result = $myconn->query('SELECT SUM(amount) as total, SUM(gallons) as gallons FROM stats');
  5. I have a database that I made that keeps track of my gas receipts. I already have one query going $result = $myconn->query("SELECT * FROM stats ORDER BY date ASC"); while($row = mysqli_fetch_assoc($result)){ $date = str_replace('-','/', $row['date']); echo "<tr><td id='". $row['id'] . "'>" . $row['miles'] . '</td><td>' . '$' . number_format($row['amount'], 2) . '</td><td> ' . $row['gallons'] . '</td><td> ' . $date . "</td></tr>"; } I want to add two other query's to add up all the amounts that are in the amount column and all the gallons in the gallons column. So I made this query that gets the amount but I have no idea how to add up the gallons without making another query and loop. $result = $myconn->query('SELECT SUM(amount) as total FROM stats'); while($row = mysqli_fetch_assoc($result)){ $sum = $row['total']; } echo $sum;
  6. I think that was a big problem. It took me several hours throughout the entire day to decide that the date needs to be formatted going into the database. So getting it from my html form I have formatted it like so $purchase_date = date('Y-m-d H:i:s', strtotime($_POST['date'])); Now it's in the database in it's correct format as Y-M-D. Until I did that, it was returning 0000-00-00. So now I need to format it for my American eyes as M-D-Y as it's coming out of the database
  7. I added the date picker to the code using jquery. Does the date need to enter the database as a "Date" type? Right now, it's type is coming from a textbox with the type of text.
  8. Well, I have input dates in my form like this 11/25/2015 and 11-25-2015. The data type in the database is set to "date". Although I just started thinking that maybe I should just change it to text. I don't want to do that because I am thinking that it needs to be a date type so that I can sort the returns by date. I just started to realize that these date functions need to match the format of the data in the database by reading the comments here and a little of the documentation. I thought setting date(d-m-y) was formatting it to that format. I will try again and see what happens
  9. getting this right seems to be very difficult. I keep getting mixed results. Some dates are good, some it mixes up the month and date. I am not getting consistent results. I just tried using the date object and it is producing mixed results <?php if(!empty($_POST)){ $miles = $myconn->real_escape_string($_POST['miles']); $amount = $myconn->real_escape_string($_POST['amount']); $gallons = $myconn->real_escape_string($_POST['gallons']); $purchase_date = date('d-m-y', strtotime($_POST['date'])); $sql = "INSERT INTO `stats` (miles, amount, Gallons, date) VALUES ('$miles', '$amount', '$gallons', '$purchase_date')"; $myconn->query($sql) or die($myconn->error); header('Location:index.php'); } ?> while($row = mysqli_fetch_assoc($result)){ $date = str_replace('-','/', $row['date']); $dateob = new DateTime($date); $dt = $dateob->format('m-d-y'); echo "<tr><td id='". $row['id'] . "'>" . $row['miles'] . '</td><td>' . number_format($row['amount'], 2) . '</td><td> ' . $row['gallons'] . '</td><td> ' . $dt . "</td></tr>"; } I think at this point, I might just try getting it straight from the database with the query and tackle the issue at a later time.
  10. @Guru I know that when you are developing a database for the web, you might use a date format that is good for everyone. A universal format. this database is for my own personal use that will not be live on the internet. I am running it on my own machine using wamp. So I like the date format that I have been using for 35 years. It's also good practice to learn how to manipulate the date
  11. My current query $result = $myconn->query("SELECT * FROM stats"); Well, I figured it out. Seems I need to do it twice. In my html form, I have this $purchase_date = date('d-m-y', strtotime($_POST['date'])); and then when I fetch the results, I do this while($row = mysqli_fetch_assoc($result)){ $date = date('m-d-y', strtotime($row['date'])); echo $date; }
  12. This problem is playing tricks on my. I just updated my code to this $date = date('m-d-y', strtotime($row['date'])); when I refreshed my screen, the date worked. Then I insert a new record and all the of the sudden it no longer works. I thought about getting it straight from the database as you suggested Benanamen, but that would require another query. I would rather get it done with php.
  13. I have a date in my database like so 2015-09-08 I am trying to format it with the date() function like this date('m-d-y', $row['date']) but it's returning 01-01-70 for every date. If I dump $row['date'] I get 2015-09-08
  14. I found those issues you pointed out and fixed them. It's amazing how things tend to work when you don't mix things up. It is true that a lot of the online tutorials are extremely out dated. I have looked really hard in my neck of the woods for a college that offers a class in PHP. Unfortunately, the best they can do is no where good enough. I have altered my class to use private for the class variables. This database will only run locally on my machine using wamp. I don't plan on putting it live online. It's for my own personal use to try and learn this stuff.
  15. I have a field called amount in my database. I have set it to "double" data type. I have entered data like so 10.80 when my query returns the results from the database, I get this 10.8 Do I need to append the extra zero or is there some other data type I should use?
  16. I am trying to create my first class. learning how to do this. I have a database set up called "Gas". It's to keep track of my gas receipts to see how much I spend on fuel. I have one table called "stats" and have populated one row of information. I can connect to my database with my class but I am now getting an error saying that "No database selected". I don't understand why it's saying that when I have the database name set in the class variables. class database { var $server = "private"; var $db_user = "private"; // Enter your username var $db_pass = "private"; // Enter your password var $db_name = "Gas"; // Enter your database name private $result = array(); private function tableExists($table) { $tablesInDb = mysql_query('SHOW TABLES FROM '.$this->db_name.' LIKE "'.$table.'"'); if($tablesInDb) { if(mysql_num_rows($tablesInDb)==1) { return true; } else { return false; } } } function connect(){ $mysqli = new MySQLi($server, $db_user, $db_pass, $db_name); if($mysqli->connect_errno){ echo "Failed to connect to database: (" . $mysqli->connect_errno . ")" . $mysqli->connect_error; return false; } return $mysqli; } public function disconnect(){ } public function select($table, $rows = '*', $where = null, $order = null){ $q = 'SELECT '.$rows.' FROM '.$table; if($where != null) $q .= ' WHERE '.$where; if($order != null) $q .= ' ORDER BY '.$order; $query = mysqli_query($q); var_dump($q); // while( $row = mysql_fetch_assoc($query) ){ // echo $row['miles'] . ' ' . $row['amount'] . ' ' . $row['gallons'] . ' ' . $row['date']; // } } public function insert(){ } public function delete(){ } public function update(){ } } $mysqli = new database; ?> then in my index file, I did this $conn = $mysqli->connect(); $query = "SELECT * FROM stats"; //var_dump($query); $result = mysqli_query($conn, $query); if($result === false){ echo mysqli_error($conn); } var_dump($result); while($row = mysql_fetch_assoc($result)){ echo $row['miles']; } I know that the query is failing. It's returning FALSE. I am selecting my database in the class. So where am I going wrong?
  17. I don't understand what would happen when they go to choose the first slider and there is no case for that slider. I guess you are saying that it would default to default case. In that case. I understand what you are saying. That is a good suggestion. Thank you.
  18. well this is for wordpress. I am setting up a slider for a website. So a person will be able to choose in an admin interface the slider they want. So there will be several options. I had different files and was using includes but I decided to recode it and just put the html/php into a switch statement instead. So if they choose a particular slider, it will run that code.
  19. when you build a switch, does it have to have a default case? If so, is there a way to get it to default to the first case instead of re posting the code in the default?
  20. I am working with wordpress. I am coding some custom meta boxes following some code someone else had written. My code is working but I am not sure if I need to do it the way I am doing it. The person who wrote the code added the option like so $custom = get_post_meta($post->ID); $item_link = $custom["item_link"][0]; According to the wordpress codex, get_post_meta($post->ID) Returns a multidimensional array with all custom fields of a particular post or page. so when I added a second option, I followed with this $item_text = $custom["item_text"][0]; I guess my question would be..Do i need to add the index 0 to every option or should I increase it by 1 for every new option I add?
  21. that makes sense. What about the default in the switch statement? for this switch, I really don't need a default. So how do I handle that?
  22. I am working in wordpress. I am using a switch statement to call a php page. It looks like this $slider_type = $options['type']; switch($slider_type) { case "Half Page Slider" : include('nivo-default.php'); break; case "Full Page Slider" : include('nivo-fullpage.php'); break; default: include('nivo-fullpage.php'); } The code works fine. My question is this. I want to use the exact same code to call the css file that runs each page. So instead of inlcuding a php page, I would replace that with the css call. So is it a bad idea to use the variables in the head section and then again in the page a little further down? or should I change the names? What is the best coding practice here?
×
×
  • 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.