Jump to content

Coreinsanity

Member
  • Posts

    16
  • Joined

  • Last visited

  • Days Won

    1

Coreinsanity last won the day on September 29 2011

Coreinsanity had the most liked content!

Coreinsanity's Achievements

Newbie

Newbie (1/14)

1

Reputation

  1. strtotime returns boolean false when '0000-00-00' is passed to it. When false is provided as the second parameter in date() it returns 01-01-1970. If I were you, I would do something like: if ($row['secondgirld'] == '0000-00-00') { echo ''; } else { echo date('m-d-Y', strtotime($row['secondgirld'])); }
  2. I have actually thought about making my own forum, for the practice, for a while. I'm actually waiting for a friend of mine to pick up PHP, though. That way we can collaborate ideas and theories on how to do things a lot better. Getting to your question: Assuming this style of database layout: threads table - This table would contain the thread name / Poster ID / whatever else is related to that specific thread specifically. thread_replies table- This table would contain the post information (The various replies to the thread, containing the thread ID of the thread they belong to.) Example fields for the table `threads` (Because that's the one you'll need to focus on for this): threads: thread_id (Auto-Incremented Thread ID) op_id (The Original Poster's (OP) user/account ID) thread_title start_date (MySQL DATETIME of the date the thread was created.) Then, I would add a field `last_post_time` as either MySQL DATETIME or an INT containing the time() ( http://us3.php.net/manual/en/function.time.php ) timestamp of the latest post (Update it every time some one replies). Then in the section that lists the threads (not the replies, just the threads like you mentioned), do an: ORDER BY `last_post_time` DESC At the end, making the whole query something like (Just guessing, since I don't have your code I don't know the exact query or database structure you use): SELECT * FROM `threads` ORDER BY `last_post_time` DESC And it will return a list of threads ordering the latest post times at the top. Hope that helps Edit: Reading this post I'm not really sure if you have made a forum or a comment section on a blog post like thing (And are in fact wanting the comments to go to the top). Either way, I will leave this here. It's easily adaptable to whatever you might need it for. The main point is the DATETIME / Timestamp storage and then ordering by it descending.
  3. I don't actually see $id_number defined in your code on that file. Are you sure you don't mean $id instead? Because I see: $id = $_GET['id'];
  4. Coreinsanity

    OOP PHP

    Unless the properties aren't public (eg: Private and I believe protected) then you should be able to do either: $object->$object_properties['prop_name']; or $object->{$object_properties['prop_name']}; Both appear to work equally for me: <?php class someclass { public $something = 356; public function __construct() {} } $c = new someclass(); $obj_props['prop_name'] = 'something'; echo $c->$obj_props['prop_name']; ?>
  5. Can you paste the code you have so far? I will look at it more.
  6. I think I might see your problem. First off, the var_dump helps you debug your application, in this case it might have pointed something out that could be the problem. mysql_fetch_array() by default does MYSQL_BOTH for return (Both numeric and associative arrays). So stripping out the numeric and leaving the associative ones for readability we come up with: (Note: If you don't know what I'm talking about with the MYSQL_BOTH, numeric and associative array stuff read: http://us.php.net/manual/en/function.mysql-fetch-array.php ) array ( 'id' => string '9' (length=1) 'product_name' => string 'hat' (length=3) 'price' => string '10' (length=2) 'details' => string 'brill' (length=5) 'category' => string 'Clothing' (length=8) 'subcategory' => string 'Hats' (length=4) 'date_Added' => string '2011-09-28' (length=10) ) If you look at the last one, the field appears to be named date_Added // Notice the upper case A in Added and not date_added // lower case a in added So then your code for displaying the date should be: <?php //This block grabs the whole list for viewing $product_list = ""; $sql = mysql_query("SELECT * FROM products"); $productCount = mysql_num_rows($sql);//count the the output ammount if ($productCount > 0){ while($row = mysql_fetch_array($sql)){ $id = $row["id"]; $price = $row["price"]; $product_name = $row["product_name"]; $date_added = strftime("%b %d, %Y", strtotime($row["date_Added"])); // Notice here how we changed it from _added to _Added $product_list .= "$id-$product_name <a href='#'>edit</a> •<a href='#'> delete</a><br/>"; } }else{ $product_list = "You have no products listed in your store Yet"; } ?> Also, var_dump basically prints out the contents and variable type. So something like <?php $somevar = 10; vardump($somevar); ?> would print out something like: int(10) To learn more about var_dump I would recommend reading: http://us.php.net/manual/en/function.var-dump.php I find it very useful for debugging some stuff. Try that and see if it helps
  7. //This block grabs the whole list for viewing $product_list = ""; $sql = mysql_query("SELECT * FROM products"); $productCount = mysql_num_rows($sql);//count the the output ammount if ($productCount > 0){ while($row = mysql_fetch_array($sql)){ echo '<pre>'; var_dump($row); echo '</pre>'; } }else{ $product_list = "You have no products listed in your store Yet"; } Also to clarify something, you were on the right track doing $date_added = strftime("%b %d, %Y", strtotime($row["date_added"])); As doing $date_added = strftime("%b %d, %Y", strtotime($date_added)); is accessing $date_added before it exists. The date_added field is going to be just like any of the other fields in your database (id, product_name, etc).
  8. Maybe you should var_dump the $row variable: while($row = mysql_fetch_array($sql)){ echo '<pre>'; var_dump($row); echo '</pre>'; } Because if you are doing $price = $row["price"]; Then the variable $price should at least exist. Also, please put your code in the code tags. Look here for a reference on them: http://www.killersites.com/community/index.php?app=forums&module=extras&section=legends&do=bbcode (Scroll down until you see the bold and green "Code" section. It should be near the top). It would make it a heck of a lot easier to read.
  9. I looked at your code, I had to clean it up a little in notepad++ to read it though. The problem is you are accessing variables that aren't created. If you look here: if (isset($_POST['product_name'])) { $product_name = mysql_real_escape_string($_POST['product_name']); $price = mysql_real_escape_string($_POST['price']); $category = mysql_real_escape_string($_POST['category']); $subcategory = mysql_real_escape_string($_POST['subcategory']); $details = mysql_real_escape_string($_POST['details']); // See if that product name is an identical match to another product in the system $sql = mysql_query("SELECT id FROM products WHERE product_name='$product_name' LIMIT 1"); $productMatch = mysql_num_rows($sql); // count the output amount if ($productMatch > 0) { echo 'Sorry you tried to place a duplicate "Product Name" into the system, <a href="inventory_list.php">click here</a>'; exit(); } // Add this product into the database now $sql = mysql_query("INSERT INTO products (product_name, price, details, category, subcategory, date_added) VALUES('$product_name','$price','$details','$category','$subcategory',now())") or die (mysql_error()); //now adds todays date to whatever you are doing $pid = mysql_insert_id(); // Place image in the folder $newname = "$pid.jpg"; move_uploaded_file( $_FILES['fileField']['tmp_name'], "../inventory_images/$newname"); header("location: inventory_list.php"); exit(); } That is the only place you set $price, and you don't even define $date_added anywhere. And that spot redirects to inventory_list.php Now look at this: //This block grabs the whole list for viewing $product_list = ""; $sql = mysql_query("SELECT * FROM products"); $productCount = mysql_num_rows($sql); //count the output ammount if ($productCount > 0) { while($row =mysql_fetch_array($sql)) {//loops will interate over an array of data $id = $row["id"]; $product_name = $row["product_name"]; $product_list .= "Product ID: $id - <strong>$product_name</strong> - $price - <em>Added $date_added</em> <a href='inventory_edit.php?pid=$id'>edit</a> • <a href='inventory_list.php?deleteid=$id'>delete</a><br />"; } } else { $product_list = "You have no products listed in your store yet"; } In here you do a mysql_fetch_array putting the result in $row. What you need to do is make the two variables in that while loop, just like you did $product_name and $id. Like so, assuming I am correctly looking at how your database and all that are layed out. while($row =mysql_fetch_array($sql)) {//loops will interate over an array of data $id = $row["id"]; $product_name = $row["product_name"]; $date_added = $row["date_added"]; $price = $row["price"]; $product_list .= "Product ID: $id - <strong>$product_name</strong> - $$price - <em>Added $date_added</em> <a href='inventory_edit.php?pid=$id'>edit</a> • <a href='inventory_list.php?deleteid=$id'>delete</a><br />"; } Like Ben said, the error you were experiencing was due to a variable being called that didn't exist. You can't attempt to get the value of something that doesn't exist. If you note the one place you do set price is around line 32 I believe, and that looks to just be when you add the item. At the end of that if block of code it hits a header() which redirects the browser in tern destroying all of those variables you set in that if block. As far as I can tell, that is the problem you mentioned anyway.
  10. Yeah, that's what I was saying (or intended to, anyway). Most tutorials don't go over (or ever say) to look into that. Thus, most people who I talked to who learned PHP from them simply think it's normal. For this reason, I generally handle my own errors. I would agree. I was mostly wondering what the reasoning behind not using it here was. (Thanks for answering, btw). That is one reason. It's like I said in my post, disables error reporting - runs the code - sets error reporting to what it was: error_reporting(E_NONE); mysql_connect() ... error_reporting(whatever it was before); since this essentially turns one statement into 3, I can see how it would effect performance if used widely. True. With display_errors you can enable/disable them globally as you please, depending on if you're in the development environment or live. Never would actually plan on doing that (Though, that is why in my example I said 'Could not connect to DB') Generally, I have a nice error page setup for displaying them so that it's build into the template. Yeah, leaving the errors displaying enabled by default is bad for a non-development environment. Though I always recommend logging errors (displayed or not) with some environment conditions (variables, etc, if available), and providing a way for users to report them. This way you can go back and see what happened, when it happened, and some environment stuff durring the time. Anyway, thanks again for your reply
  11. Nice tutorial, though I do have a question. This may be beyond the scope of the tutorial, but I have seen it done a lot in tutorials a friend of mine looks at to learn PHP: $db = mysql_connect('localhost','wronguser','or_wrongpass') or die (mysql_error()); Now, with my wamp configuration (Pretty much default) and my hosting configuration (through a2 hosting) I get (as expected) the MySQL error printed out twice: > Once for the actual PHP error (due to what the error level is set to > And again for the or die (mysql_error());, except this time it's just the error reported from mysql. As such I have always done $db = @mysql_connect('localhost','wronguser','or_wrongpass') or die ('Could not connect to the DB'); Utilizing the @ error control operator to ignore the PHP error for this statement and then removing the mysql_error (I don't want my database user printed out). I am not sure if this is best practice or not, since (to my understanding) @ simply disables error reporting then executes the statement, and returns it to normal. http://us3.php.net/manual/en/language.operators.errorcontrol.php If anyone wants to read up more on it, and doesn't already know about it. So my question, is there any real reason people don't use the @ error control operator (Sparingly, of course) for stuff like this?
  12. Hey, Sorry for the late reply, been real busy lately and forgot :/ Thanks for the link, was a good read. For now I think I am going to stick with CI, though. Mostly due to how simple and quick it is for me to set up, plus what you said. After I finish teaching a friend of mine PHP I may check out ZF so both of us can sift through their docs and make sense of it better.
  13. I currently use (and like) CodeIgniter. But I keep hearing a bunch of people (Had a friend, and seen a bunch of videos) that hint that Zend Framework might be better. I am wanting to know, why? What kind of Pros and Cons are there compared with CodeIgniter? I am not dead set on using one over the other, as with a lot of things I imagine it depends on your needs.
  14. For anyone who is interested in speeding databases up: I talked to a friend of mine who is a DBA. He said that what I am doing is basically "Shotgun Indexing" which is fail. Instead, you should make an index that covers the fields in your WHERE and ON clauses (ON being inside of JOINS). He also said you should avoid sub-queries like the plague. I turned my query into two queries, the original (without sub-queries) and then the sub-query remade in a way that could be run by itself and still get all the information needed in one run. This made it run almost 2x as fast. Lastly, he also said the default WAMP configuration sucked. So I investigated the configuration files and found that innodb_ settings were commented out, I un-commented innodb_buffer_pool_size and set it to 1000M and now it runs the same query that used to take 70 seconds in about 4 - 16 with 11million entries in the database. Before all of this it took about 3 minutes to run the query with only 3 million things in the database. Anyway, hopefully some of this can help some one looking to speed up their database.
×
×
  • Create New...