Jump to content

Same file for multiple pages


wavix
Go to solution Solved by QuickOldCar,

Recommended Posts

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 by wavix
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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`".

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • Solution

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')

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.