Jump to content
  • Advertisement
Sign in to follow this  
TriSwords

[web] Help carrying variables over across pages

This topic is 4848 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This is the code for getting the last 5 news posts and displaying them in the right spot of my page (works fine). It lists the last 5 sql entries no problem, and gives them the link news.php?post=X, where x is the number of the post. It is index based so it starts at 0. I need the 0(or whatever number) to carry to the next page, so it knows what post to get out of the database. However I either get it to be blank (or null, I dunno what PHP defaults to), or if I create a variable in my require.php I get the -1 I initialized that too. <?php //Go through the last 5 post titles and display them for($a = 0; $a < 5; $a++) { //Get the first line of the second query we did if( $moreInfo = mysql_fetch_array($title_res)) { //URL encode the row so that the database knows what row of code to write on //the new page //urlencode($a); -- Guess I dont need it to just make post a int print(" [ $moreInfo[title] ]

"); } elseif ($a == 0) { print("No news available

"); } } ?> on my next page I simple have the layout and then a print "$post"; but I dont have any luck. Can you help me out? Thanks TriSwords

Share this post


Link to post
Share on other sites
Advertisement
I'm not too sure exactly what the problem is, but can't you just append the number to your link? Eg

<a href="www.yoursite.com/news.php?post=<?php echo $a?>">



This is the usual way to do it. If that doesn't work then you could try storing the data in a session or cookie, but this isn't really the ideal way to do it. You then get the data via $GET[post] (or maybe $GET['post'], I can't remember). Eg

echo $GET['post']

This could be where you are going wrong. You can easily check in the address bar whether the number is being appended or not, and if using $GET doesn't work could you check to see if it is? This will let us know if the problem is on the page which is passing the variable or the one recieving it

Share this post


Link to post
Share on other sites
$GET['variable_name'] shouldn't work - you need to use $_GET['variable_name'] instead. Same for $_POST['variable_name'], $_SESSION['variable_name'], $_COOKIE['variable_name'], $_REQUEST['variable_name']...

Anyway, once you refresh a page you can't access variables from the last one. You can send variables over in a number of ways, though:

$_POST variables are used on forms. For example,

page1.php:
<form action="page2.php" method="post">
<input type="text" name="variable_to_send" />
<input type="submit" name="submit" value="Transfer!" />
</form>


page2.php:
<?php
echo $_POST['variable_to_send'];
?>


...would have a form (on page1.php) that could be submitted, and page2.php would display the value that was entered into the text box on page1.php.

If you build up an URL using the format like this:
http://example.com/page3.php?name=Ben&age=19&sex=Male&hair=Brown
(where variables are seperated with & (hte first one needs a ? instead, though), you can get access to the variables using $_GET:

page3.php:
<?php
echo "Hello {$_GET['name']}, you are {$_GET['age']} years old, are {$_GET['sex']} and have {$_GET['hair']} hair.";
?>


Another (slightly complicated, in that they can confuse) way to do this is to save sessions. You need to "start" them with a call to session_start() at the beginning of each page (before headers are sent) but can then read and write them with $_SESSION['variable_name']='Value'.

Share this post


Link to post
Share on other sites
With all this talk about the different ways to store and pass data, let's look at what there uses are:

Sessions are for storing data about the client that is currently connected. Things like weither or not they are logged in, and if so, what thier username and access level is.

Cookies are for storing data on the client's end that you want to know about the next time they visit your page, such as thier username and password for an auto-login script.

POST data is for transfering information to a script that will then take some action on the data (I.E., store it in a database).

GET data is for making generic requests of a script, such as which content area to display.

With all that having been said, you're all ready on the right track, as you are currently using GET data. I'll show you how to properly use it.

Link: {a href="news.php?post=4"}{/a\} (replace curly brackets with HTML tags)

Code:


SomeNewsFunc($_GET['post']);



Hope that isn't too confusing.

QBRADQ

Share this post


Link to post
Share on other sites
With all this talk about the different ways to store and pass data, let's look at what there uses are:

Sessions are for storing data about the client that is currently connected. Things like weither or not they are logged in, and if so, what thier username and access level is.

Cookies are for storing data on the client's end that you want to know about the next time they visit your page, such as thier username and password for an auto-login script.

POST data is for transfering information to a script that will then take some action on the data (I.E., store it in a database).

GET data is for making generic requests of a script, such as which content area to display.

With all that having been said, you're all ready on the right track, as you are currently using GET data. I'll show you how to properly use it.

Link: {a href="news.php?post=4"}{/a\} (replace curly brackets with HTML tags)

Code:


SomeNewsFunc($_GET['post']);



Hope that isn't too confusing.

QBRADQ

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!