Jump to content

PaulRyan

Members
  • Posts

    876
  • Joined

  • Last visited

  • Days Won

    4

Everything posted by PaulRyan

  1. I have the following query, which selects threads from a specific category (in the case I set it as "1"), it also queries another table to get when the thread was last viewed to accommodate a "thread view" feature, displaying a different image if it has been viewed or not. SELECT `ft`.`id`, `ft`.`title`, `ft`.`author_id`, `ft`.`reply_count`, `ft`.`view_count`, `ft`.`timestamp`, `ft`.`locked`, `u`.`username`, IFNULL(`u2`.`username`, `u`.`username`) AS `last_activity_name`, IFNULL(`ftr`.`author_id`, `ft`.`author_id`) AS `last_activity_id`, IFNULL(`ftr`.`timestamp`, `ft`.`timestamp`) AS `last_activity_time`, `ftvl`.`timestamp`, ftr.timestamp FROM `forum_threads` AS `ft` LEFT JOIN `users` AS `u` ON `u`.`id` = `ft`.`author_id` LEFT JOIN (SELECT `id`, `thread_id`, `author_id`, `timestamp` FROM `forum_thread_replies` ORDER BY `timestamp` DESC) AS `ftr` ON `ftr`.`thread_id` = `ft`.`id` LEFT JOIN `users` AS `u2` ON `u2`.`id` = `ftr`.`author_id` LEFT JOIN `forum_thread_view_log` AS `ftvl` ON `ftvl`.`thread_id` = `ft`.`id` AND `ftvl`.`user_id` = 4 WHERE `ft`.`category_id` = 1 GROUP BY (`ft`.`id`) ORDER BY `ft`.`id` DESC LIMIT 0, 25 I've performed an explain on the query and I'm not really liking how its querying, as I'm sure there is a better way to do it. Here is an image of the EXPLAIN on the query. Here are my table structures also: CREATE TABLE IF NOT EXISTS `forum_threads` ( `id` mediumint(5) NOT NULL AUTO_INCREMENT, `category_id` tinyint(1) NOT NULL, `title` varchar(255) NOT NULL, `content` text NOT NULL, `author_id` int(11) NOT NULL, `reply_count` smallint(4) NOT NULL DEFAULT '0', `view_count` mediumint(7) NOT NULL DEFAULT '0', `locked` tinyint(1) NOT NULL DEFAULT '0', `timestamp` datetime NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ; -- -- Dumping data for table `forum_threads` -- INSERT INTO `forum_threads` (`id`, `category_id`, `title`, `content`, `author_id`, `reply_count`, `view_count`, `locked`, `timestamp`) VALUES (1, 3, '234', '123', 4, 3, 3, 0, '2013-04-28 02:19:15'), (2, 1, 'asdsdf', 'sfdsfsdf', 4, 1, 6, 1, '2013-04-28 02:20:11'), (3, 2, 'sss', 'asdasdads', 3, 1, 1, 0, '2013-04-28 02:37:25'), (4, 1, 'sdfsdf', 'dsdfsdf', 3, 3, 4, 0, '2013-04-28 02:52:16'), (5, 1, 'Another thread?', 'Another thread?', 3, 8, 4, 0, '2013-04-28 02:54:16'); CREATE TABLE IF NOT EXISTS `forum_thread_replies` ( `id` mediumint(7) NOT NULL AUTO_INCREMENT, `thread_id` mediumint(5) NOT NULL, `content` text NOT NULL, `author_id` int(11) NOT NULL, `timestamp` datetime NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=17 ; -- -- Dumping data for table `forum_thread_replies` -- INSERT INTO `forum_thread_replies` (`id`, `thread_id`, `content`, `author_id`, `timestamp`) VALUES (1, 2, 'dsdf', 19, '2013-04-28 02:22:00'), (2, 2, 'dsfsdf', 4, '2013-04-28 02:25:00'), (3, 1, '12345', 3, '2013-04-28 02:50:05'), (4, 4, 'Test reply.', 3, '2013-07-01 22:33:37'), (5, 5, 'Another test.', 3, '2013-07-01 22:33:50'), (6, 5, 'sdfsdfsdf', 3, '2013-07-01 22:36:07'), (7, 5, 'sdfsfsdf', 3, '2013-07-01 22:40:22'), (8, 5, 'sfdsfdsdfsfd', 3, '2013-07-01 22:40:34'), (9, 5, '1', 3, '2013-07-01 22:47:09'), (10, 4, '2', 3, '2013-07-01 22:47:18'), (11, 1, '242424', 3, '2013-07-01 22:48:26'), (12, 5, '234', 3, '2013-07-01 22:49:05'), (13, 5, 'sfdsdf', 3, '2013-07-01 22:50:47'), (14, 4, 'sfdsfsf', 3, '2013-07-01 22:51:01'), (15, 5, 'wer', 3, '2013-07-01 22:54:53'), (16, 3, 'adad', 3, '2013-07-01 23:18:07'); CREATE TABLE IF NOT EXISTS `forum_thread_view_log` ( `user_id` int(11) NOT NULL, `thread_id` mediumint(5) NOT NULL, `timestamp` datetime NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; -- -- Dumping data for table `forum_thread_view_log` -- INSERT INTO `forum_thread_view_log` (`user_id`, `thread_id`, `timestamp`) VALUES (3, 1, '2013-07-01 22:48:35'), (3, 2, '2013-07-01 22:48:11'), (3, 3, '2013-07-01 23:18:08'), (3, 4, '2013-07-01 22:53:51'), (3, 5, '2013-07-01 22:54:53'), (19, 2, '2013-04-28 03:10:24'), (5, 2, '2013-04-28 03:11:23'), (4, 2, '2013-07-02 00:26:37'), (4, 4, '2013-07-01 22:39:47'), (4, 5, '2013-07-02 00:29:53'), (4, 1, '2013-07-02 00:12:39'); So after all of that, my question is: Can someone help improve that query? If you need any more information, please let me know. Thanks, Paul.
  2. explode $toArr = explode(',', $to); filter_var function validate_email($email) { return filter_var($email, FILTER_VALIDATE_EMAIL); }
  3. After you have set the session variable, make sure you re-direct to the page AND exit after the re-direct? <?PHP if (blah == blah) { $_SESSION['message'] = 'Blahhhhh'; header('Location: blah.php'); exit; } ?>
  4. You have HTML inside of your PHP tags, you need to echo or print that data, or close the tags and open them when needed. Something like this: <?PHP // Create connection $con=mysqli_connect("localhost","root","","inshapewebsite"); // Check connection if (mysqli_connect_errno($con)) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); exit; } ?> <select name="foodType" onchange="autoSubmit();"> <option VALUE=""></option> <?PHP $result = "SELECT Food,Calories FROM food"; $food = mysql_query($sql,$conn); while($row = mysql_fetch_array($food)) { echo ("<option VALUE=\"{$row['Food']}\" " . ($food == $row['Food'] ? " selected" : "") . ">{$row['Calories']}</option>"); } ?> </select>
  5. I always use $_SESSION variables, something like this will work. <?PHP session_start(); if(isset($_SESSION['message'])) { echo $_SESSION['message']; unset($_SESSION['message']); } ?>
  6. Most likely the domain is a blacklisted keyword. Did you try with multiple different e-mail providers?
  7. The errors on your website tell you exactly what is wrong, that is pretty much what they are there for. As the error says "No such file" it means that the file you are including does not exist.
  8. No problem, if all is done, please mark as solved
  9. You added an extra question mark after the $ip variable. $content = "<META HTTP-EQUIV=\"refresh\" CONTENT=\"$timeout;url=$pageurl?ip=$ip&tries=$tries\">\n";
  10. The above string is whats called a ternary operator (I believe) It is short hand for an if else statement, it is equivalent to the following: <?PHP if(isset($_GET['tries'])) { $tries = (int)$_GET['tries']; } else { $tries = 1; } ?> And yes, it replaces $tries = 1;
  11. Well $tries is carried, along with $pageurl, in the META refresh tag. $tries needs to be something like: $tries = isset($_GET['tries']) ? (int)$_GET['tries'] : 1 ;
  12. Jessica said "number instead of a string". This mean $tries = 1; NOT $tries = "1"; Any variable that is a number, you do not need any quotes around.
  13. $name = isset($_GET['UserName']) ? $_GET['UserName'] : NULL ; Ternary Operator
  14. There are 3 different types of new lines, all depending on the system the PHP is running on. There are: \r \n \r\n
  15. Show us some example input, what you expect to be returned and what is actually returned as well as the code you currently have.
  16. Try this: if(preg_match('#http:#', $notes)) {
  17. <?PHP $store_array = array('demo', 'giorgios'); function chain_report2($store_session) { $output = ''; global $store_array; if(in_array($store_session,$store_array)){ $output .= "Demo 1"; } return $output; } echo chain_report2($_SESSION['store_name']); ?> You actually have to echo out the returned data for it to show.
  18. No one will just write this code for you. Either show us some code to show you have attempted what you want, otherwise post in the PHP Freelancing section of PHP Freaks.
  19. <?PHP function get_stores() { $store_array = array(); $stores = array('store1','store2'); foreach ($stores as $store_temp) { $store_array[] = $store_temp; } return print_r($store_array, TRUE); } echo get_stores(); ?>
  20. Glad you fixed your issue, I didn't get time to test my code unfortunately. It's good that you've learnt something from this also
  21. You should read the documentation again. json_decode takes a second argument of a boolean value (TRUE) If the second argument if present, it returns an array instead of an object. Read up before you comment. Enjoy the new tip
  22. You have your select query, BEFORE your delete query. Do it the other way around.
  23. How is that wrong? It returns the exact data you get, just in an array instead of an object. The "return" is for the PHP command line, it's just what he used.
×
×
  • 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.