wavix Posted May 14, 2015 Share Posted May 14, 2015 (edited) Hello. I have index.php containg a div with AJAX content via content.php. Now, I need yesterday.php and tomorrow.php, like index.php, the only difference will be a variable in content.php. Something like: if($currentPage == 'yesterday.php') { $difference = 'yesterday'; } if($currentPage == 'index.php') { $difference = 'today'; } if($currentPage == 'tomorrow.php') { $difference = 'tomorrow'; } $select = "SELECT * FROM `".$difference."`"; It is possible to obtain $currentPage that I need using a single file (index.php)? How should I do? Hope you understand. Thanks! Edited May 14, 2015 by wavix Quote Link to comment https://forums.phpfreaks.com/topic/296327-same-file-for-multiple-pages/ Share on other sites More sharing options...
fastsol Posted May 14, 2015 Share Posted May 14, 2015 Without knowing a bit more about how you are building the $currentPage var or how you are directing people to the other pages, it's kind of hard to say for sure. Here is an example though using $_GET vars to decide what page it needs to ajax info for. $currentPage = (!empty($_GET['page'])) ? $_GET['page'] : ''; $difference = (empty($currentPage)) ? 'index' : $currentPage; $select = "SELECT * FROM `".$difference."`"; BUT, links that are for anything besides the index would need to have a $_GET var attached. So like index.php?page=tomorrow. Also based on the single $select string I can tell that your DB structure is highly flawed. I am sure you are just learning at this point, so we don't have to get to deep into that right now. I also assume you are using the basic mysql functions, if so, I would highly suggest stopping that practice now before you learn to much of the wrong thing. The mysql functions have now been deprecated and will be removed from PHP in the coming versions. You should learn mysqli of more preferably PDO. If you're learning, then now is the time to get in good habits. Lastly, I truly hope that this is just practice code, cause it's not secure in any fashion. It's also not common to use a var to indicate a table name to gather FROM. It can be done but it's usually for very specific reasons and with several lines of code to ensure it's secure to do so. Quote Link to comment https://forums.phpfreaks.com/topic/296327-same-file-for-multiple-pages/#findComment-1511913 Share on other sites More sharing options...
wavix Posted May 14, 2015 Author Share Posted May 14, 2015 Without knowing a bit more about how you are building the $currentPage var or how you are directing people to the other pages, it's kind of hard to say for sure. Here is an example though using $_GET vars to decide what page it needs to ajax info for. $currentPage = (!empty($_GET['page'])) ? $_GET['page'] : ''; $difference = (empty($currentPage)) ? 'index' : $currentPage; $select = "SELECT * FROM `".$difference."`"; BUT, links that are for anything besides the index would need to have a $_GET var attached. So like index.php?page=tomorrow. Also based on the single $select string I can tell that your DB structure is highly flawed. I am sure you are just learning at this point, so we don't have to get to deep into that right now. I also assume you are using the basic mysql functions, if so, I would highly suggest stopping that practice now before you learn to much of the wrong thing. The mysql functions have now been deprecated and will be removed from PHP in the coming versions. You should learn mysqli of more preferably PDO. If you're learning, then now is the time to get in good habits. Lastly, I truly hope that this is just practice code, cause it's not secure in any fashion. It's also not common to use a var to indicate a table name to gather FROM. It can be done but it's usually for very specific reasons and with several lines of code to ensure it's secure to do so. Thank you. But how can I send $_GET var to content.php? It is just example code, in fact I don't have $currentPage. This is what I need. Quote Link to comment https://forums.phpfreaks.com/topic/296327-same-file-for-multiple-pages/#findComment-1511915 Share on other sites More sharing options...
fastsol Posted May 14, 2015 Share Posted May 14, 2015 You're going to have to give us more code than that then. You're basically asking us to blindly write code for you on something that you seemingly don't have written. We can help you if you have issues along the way but we aren't here to write the code for you, especially without previous code provided by you. There are tons of youtube videos on how to use the jquery ajax stuff, start there if you don't know how to do this or where to start. Quote Link to comment https://forums.phpfreaks.com/topic/296327-same-file-for-multiple-pages/#findComment-1511916 Share on other sites More sharing options...
wavix Posted May 14, 2015 Author Share Posted May 14, 2015 I don't know how to do. So, I have index.php?page=yesterday, how can I send yesterday to content.php? Tried this: $.ajax({ type: 'POST', url: 'content.php', data: { page: 'yesterday' }, success: function(data) {} }); no success. Quote Link to comment https://forums.phpfreaks.com/topic/296327-same-file-for-multiple-pages/#findComment-1511917 Share on other sites More sharing options...
fastsol Posted May 14, 2015 Share Posted May 14, 2015 What does your content.php code look like? Quote Link to comment https://forums.phpfreaks.com/topic/296327-same-file-for-multiple-pages/#findComment-1511920 Share on other sites More sharing options...
wavix Posted May 14, 2015 Author Share Posted May 14, 2015 include('config.php'); $sql = "SELECT * FROM `today`"; $mysql = mysql_query($select); while($row = mysql_fetch_array($mysql)) { $id = $row['id']; echo $id; } I want today to be replaced with index.php $_GET var, ex.: index.php?page=yesterday to change $sql in "SELECT * FROM `yesterday`". Quote Link to comment https://forums.phpfreaks.com/topic/296327-same-file-for-multiple-pages/#findComment-1511922 Share on other sites More sharing options...
fastsol Posted May 14, 2015 Share Posted May 14, 2015 include('config.php'); $sql = "SELECT * FROM `".$_POST['page']."`"; $mysql = mysql_query($sql); // This was wrong, you tried to run query on $select, which doesn't exist. while($row = mysql_fetch_array($mysql)) { $id = $row['id']; echo $id; } As for how to make the ajax change, well that's another issue. There are good ways to do it, but I need more of your jquery code that tells the page to run the ajax, so the click event and stuff. Quote Link to comment https://forums.phpfreaks.com/topic/296327-same-file-for-multiple-pages/#findComment-1511928 Share on other sites More sharing options...
wavix Posted May 14, 2015 Author Share Posted May 14, 2015 include('config.php'); $sql = "SELECT * FROM `".$_POST['page']."`"; $mysql = mysql_query($sql); // This was wrong, you tried to run query on $select, which doesn't exist. while($row = mysql_fetch_array($mysql)) { $id = $row['id']; echo $id; } As for how to make the ajax change, well that's another issue. There are good ways to do it, but I need more of your jquery code that tells the page to run the ajax, so the click event and stuff. I forgot to change $select, sorry. AJAX code is above, included in index.php between <script></script> tags, on document ready. Quote Link to comment https://forums.phpfreaks.com/topic/296327-same-file-for-multiple-pages/#findComment-1511931 Share on other sites More sharing options...
fastsol Posted May 14, 2015 Share Posted May 14, 2015 As long as the jquery is on the index.php page then this method will work. $.ajax({ type: 'POST', url: 'content.php', data: { page: '<?php echo htmlentities($_GET['page']); ?>' }, success: function(data) {} }); Also, as long as this is purely for testing and learning, this whole concept is fine. But overall, your use of ajax in this scenario is completely pointless and makes the page even less secure. There is no reason at all to load content via ajax when you're simply telling it get that info on pageload only. The use of the ajax is to get different info AFTER the page is loaded and some kind of event has happened like a button click. Your content.php is not secured in any manner either, but I think I touched on that earlier. Quote Link to comment https://forums.phpfreaks.com/topic/296327-same-file-for-multiple-pages/#findComment-1511933 Share on other sites More sharing options...
Solution QuickOldCar Posted May 15, 2015 Solution Share Posted May 15, 2015 Forget the ajax part of it for now until get the basics working. Store the dates in the database with datetime formatted as YYYY-MM-DD HH:MM:SS The today date would be the current date, then you modify the current date with +24 or -24 hours using strtotime() A query would look something like this. $query = "SELECT * FROM table_name BETWEEN ('".$from."' AND '".$to."')"; or $query = "SELECT * FROM table_name WHERE date >= '$from' AND date <= '$to'"; I would use a default date as today within index.php if no parameters set in the url, otherwise use what is specified. This is just one of many ways possible. I wrote up one using as you described just yesterday,today and tomorrow but I felt using dates as the parameter was a better way so can do any date. index.php?date=2015-5-15 no date parameter defaults to the current date <?php date_default_timezone_set("America/New_York"); if (isset($_GET['date']) && trim($_GET['date']) != '') { $date = trim($_GET['date']); } else { $date = date('Y-m-d'); } if (($timestamp = strtotime($date)) === false) { $date = date('Y-m-d'); } $query = "SELECT * FROM table_name WHERE `timestamp` BETWEEN ('".$date."' AND '".$date."')"; $current = date('Y-m-d'); $previous = date('Y-m-d', strtotime("$date - 24 HOURS")); $next = date('Y-m-d', strtotime("$date + 24 HOURS")); echo "<a href='?date=$current'>Today</a> "; echo "<a href='?date=$previous'>Previous</a> "; echo "<a href='?date=$next'>Next</a><br />"; echo $query; ?> Results: SELECT * FROM table_name WHERE `timestamp` BETWEEN ('2015-05-15' AND '2015-05-15') Quote Link to comment https://forums.phpfreaks.com/topic/296327-same-file-for-multiple-pages/#findComment-1511965 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.