Jump to content

php isset question


houssam_ballout

Recommended Posts

Hello all,I've built a single page, that retrieves a data from database.For example, on the php page, I've added the link:<a href='test1.php?id=1'>anything </a><a href='test1.php?id=2'>anything </a>when I click on one of it, I want the next page to display the results from sql query, but I don't know how to use it id (like: id=1) from the isset, to use it??Any help

Link to comment
Share on other sites

You mean like this?

$id = intval($_GET['id']);$q = mysql_query("SELECT * FROM pages WHERE id = '$id'");$row = mysql_fetch_assoc($q);echo 'Welcome to this page called ' . htmlentities(stripslashes($row['title']));

Link to comment
Share on other sites

Well, I'd a table with ID, title, and contents fieldsI had page name called display.php, and 3 links inside it display1.php?id=1......................=2......................=3For example, if I clicked on first link, I must get the records where ID=1 inthe database, but how can I know via isset which link did the user presses?

Link to comment
Share on other sites

First, isset() only checks if a variable/value is set or not:

if (isset($_GET['id']))  // $_GET['id'] exists/is setif (isset($foo))  // $foo exists/is set

Second: Take a look at Anders' code again, that's what you are looking for.values from the querystring (the values after ? in the URL) is stored in $_GET, say you go to file.php?id=42&foo=bart&more=less and the code looks like this:

$id = intval($_GET['id']);  // intval makes sure that you get a integer, nothing else.// The variable $id now contains 42// We could also display it like this:$foo = $_GET['foo'];echo 'ID:' . $id . ', ' . $_GET['id'] . '; foo:' . $foo . ', ' . $_GET['foo'] . '; bar:' . $_GET['bar'] . '; more:' . $_GET['more'] . "\n";

The code would output something like this:

ID:42, 42; foo:bart, bart; bar:;more:less

There's no value by bar:, because bar isn't set in the querystring.If we would try with this link instead file.php?id=42lol&foo=bart&bar=more, the output would be:

ID:42, 42lol; foo:bart, bart;bar:more;more:

now you see two different values for ID, first 42 and then 42lol (which is the value you gave it in the querystring), this is beacuse we used intval() on $_GET['id'] when we stored it in $id, which makes it to an integer, and the second value of id is "pure", directly from the querystring. Now we also see a value of bart, but no value for more, as we removed it...Hope that helped...

Link to comment
Share on other sites

you may use the if statement and the switch statementsample if ($_GET[id]==1) do this else do that the other is switch switch ($_GET[id]) case '1':dothisbreak;case '2':do thatbreak;

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...