Calling value from another page

Asked

Viewed 363 times

0

Hi, guys. I’m having a little problem with passing data from one page to another. I’ve tried by Session and not quite sure and I’ll explain why:

I have a news page that distributes them by a for in $arr[] where I fit the title, body of the news etc. Then, when entering a specific news (that read more), I have to give a SELECT in the database again and distribute the title, News body etc and this I do by her ID in the database. It happens that to pass the ID of the news that he clicked pro SELECT of the other page I got all confused. In my attempts something like:

first saving the data in the variable: $variable = array("$arr[2]")

2nd taking this given by the session: SELECT bla bla bla WHERE id = $variable and it does not return the value of this variable I did more or less above, of course I opened the sessions and everything more kkkkkk

Anyway, thanks in advance!

  • Friend can not determine the fault so, please read the link: http://answall.com/help/mcve

  • Place the code of the second page and the link or how you pass the variable from the first to the second.

  • 1

    When you use $variavel = array("$arr[2]"); do you want to use $variavel = $_POST("$arr[2]");? Notice that they are different things.

  • 1

    If you want to save that to a session variable, you have to log in. If you want to send this to another page where you place the request you must have a variable Return. I use ajax to make requests to the server

  • 1

    Give me a return later if it worked out okay? Good luck there!

  • @Allanandrade I did the following: I created a session and passed the value of the array. But, when trying to redeem this value on another page, it only shows the ID of value 1. What I didn’t understand is that when giving an echo on the page I passed the array, it shows the ID of the news perfectly in each repetition for. The problem is on the page where "resgato" this value. I was able to clearly explain this problem of mine?

  • Put the code you made that I take a look.

Show 2 more comments

2 answers

1

I couldn’t quite understand your doubt, but I’ll try to help you.

If what you meant is this: "You have a news site and want to get the ID of the news when the visitor click 'Read more...'.

If so, the correct procedure for you to get the ID would be as follows. To load the news you use the method $_GET to load news content.

So the site will look like this: When the visitor clicks "Read more..." he will be redirected to a page with the 'site.com/? noticia=3' (News=3, is an example of $_GET['noticia'], this is at your disposal.

I hope you’ve solved your doubt.

  • I’m trying to go through session. It turns out that within repetition is by echo an $_SESSION[id]= $arr[2], it shows the ID perfectly. But when I redeem this value on another page it only shows the value 1.

1

Trying to be didactic, I think you could try this way... (I don’t know which database you use, I made an example using MYSQL).

On the page that lists the news, on the link "read more" of each news do so:

for($i = 0; $i < mysql_num_rows($rs_noticias); $i++){    
     echo '<div>';
     echo '<h2>' . mysql_result($rs_noticias, $i, 'titulo_noticia') . '</h2>';
     echo mysql_result($rs_noticias, $i, 'texto_noticia');
     echo '<a href="leia_mais.php?id_noticia=' . mysql_result($rs_noticias, $i, 'codigo_noticia') . '">Leia mais...</a>';
     echo '</div>';
}

On the page that will detail the news, which in the example is called 'leia_mais.php', you can do the following::

<?php
$codigo_noticia = $_GET['id_noticia'];
$sql = 'SELECT bla bla bla WHERE id =' . $codigo_noticia;
echo $sql;

I hope I helped! Any questions just ask!

  • 2

    Remember that the mysql_* functions are obsolete. It is recommended to use mysqli or PDO.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.