Passing values of a variables upon clicking some links

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
khaki_monster
Forum Commoner
Posts: 73
Joined: Tue Oct 11, 2005 12:36 am
Location: Philippines
Contact:

Passing values of a variables upon clicking some links

Post by khaki_monster »

hi guyz,

how can i pass a particular value of variables from 1 page('Links') to another?
say like:

$ID = 12; of page1.php

then passing $ID value to page2.php upon clicking some links that leads to page2.php

i hope my question is clear... hehe coz im too is in a bit of confusion.

tanx in advanced.

cheerz!
IAD
Forum Commoner
Posts: 42
Joined: Wed Dec 28, 2005 12:36 pm
Location: Israel, Soon Canada :)

Re: Passing values of a variables upon clicking some links

Post by IAD »

khaki_monster wrote:hi guyz,

how can i pass a particular value of variables from 1 page('Links') to another?
say like:

$ID = 12; of page1.php

then passing $ID value to page2.php upon clicking some links that leads to page2.php

i hope my question is clear... hehe coz im too is in a bit of confusion.

tanx in advanced.

cheerz!
Sessions?

Try to use sessions, in the next form:

Code: Select all

<?Php
session_start();

$_SESSION['id'] = 12;

?>
After you did that, the ID will go with you until you'll close the browser.

For more information: php.net/sessions

Hope i helped :)
User avatar
khaki_monster
Forum Commoner
Posts: 73
Joined: Tue Oct 11, 2005 12:36 am
Location: Philippines
Contact:

Post by khaki_monster »

so you mean to say this topic is more likely regards with "Sessions" or is their any other solution?

by the way dont worry bout your english... coz mind is much worser than any other guyz in here :lol:

tanx with your contribution... hope others will help also.

cheerz!
IAD
Forum Commoner
Posts: 42
Joined: Wed Dec 28, 2005 12:36 pm
Location: Israel, Soon Canada :)

Post by IAD »

No! my english worst!

Try to use global variables like "GET".

For example:

Code: Select all

page.php?id=12
<?Php
    $id = $_GET['id'];

print $id; // will print 12
?>
But look out, try to block any security problems.

starting with: is_numeric, to check the id.


Hope i helped, Tal.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

If you can avoid it,don't use $_GET unless you are ONLY selecting information with it. And even then, make sure you hit the passed value with a lot of validation.

Sessions are a good way to remember information from page to page, as are cookies. But I think sessions might be the best way to go. What are you doing with this variable?
User avatar
khaki_monster
Forum Commoner
Posts: 73
Joined: Tue Oct 11, 2005 12:36 am
Location: Philippines
Contact:

Post by khaki_monster »

Everah: What are you doing with this variable?

:) actually i have some queries to list all entries of some books were it will display the books title only and it supposed to be
clickable("and pop another page") to view the particular book in details or just to update some info. regarding the book.

hope to get some indeep help regarding this :)

tanx for those who contibuted...
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

So you're making a drill-down. This is a very common task (and a topic that is commonly asked about in these forums). Since all you are doing is selecting information with the passed variable you should be able to get away with pulling all the books from the database. Make your output a list of books with each item in the list a hyperlink to a detail sheet of the book identified by the book ID.

To do this from one page to the next all you would do is set up a link from page a to page b and add a query string to the link that includes the book ID, like this...

Code: Select all

<?php

$result = mysql_query($book_list_query); //substitute your SQL here and ERROR CHECK!

while ($row = mysql_fetch_array($result))
{
    echo '<a href="book_detail.php?' . $row['book_id'] . '">' . $row['book_name'] . '</a><br />';
}

?>
Then in your book_detail.php page you would use $_GET['book_id'] to SELECT the book the user chose.

For a bit of an example of the way this works go the DevNetwork Forums homepage and choose one of the forums listed within the list of categories. Then, from inside the forum, choose a topic. You will see inside the topic a list of threads. You have just drilled down. Take a look at the address bar as you do this and you will see the logic behind it.
User avatar
khaki_monster
Forum Commoner
Posts: 73
Joined: Tue Oct 11, 2005 12:36 am
Location: Philippines
Contact:

Post by khaki_monster »

tanx! i really appreciated your help("actually all you guyz who helped"). i already got the listing part of the items("books"), my confusion is just how to pass the value of a variables from 1 page to another pages so that user can view or updet/ edit the selected item on the list. :)

any tanx again... i will try your suggestion.

cheerz!
User avatar
khaki_monster
Forum Commoner
Posts: 73
Joined: Tue Oct 11, 2005 12:36 am
Location: Philippines
Contact:

Post by khaki_monster »

hehe. id like to ask again. what if i have more than 1 values to pass through links? how would i do that?

cheerz!!!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Just piggy back them in your query string...

Code: Select all

echo '<a href="link.php?var0=' . $val[0] . '&var1=' . $var[1] . '&var2=' . $var[2] . '">Link text</a>';
User avatar
khaki_monster
Forum Commoner
Posts: 73
Joined: Tue Oct 11, 2005 12:36 am
Location: Philippines
Contact:

Post by khaki_monster »

twigletmac | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


hi again,

bellow are the codes from two pages were "article_list.php" should pass some value to "view_article.php" but it wouldn't work.
given the connection is doing ok. "article_list.php" is running OK. my problem is the "view_article.php" where it gives me a blank output.

please... i need some help here. 

//
// article_list.php
//

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<link href="../../default.css" rel="stylesheet" type="text/css">
<link href="../../balita.css" rel="stylesheet" type="text/css">
</head>
<?php 
require("../../config.php"); 
conn_db($host, $user, $pass, $dbse);
?>
<body>
<div align="center">
<table width="396" border="1" cellpadding="0" cellspacing="0" bordercolor="#666666" class="tble">  
<?php
	$query = 'select news_mas.location, news_mas.section, news_dtl.title from news_mas, news_dtl
			  where news_mas.id = news_dtl.idx and news_mas.section = news_dtl.section order by news_mas.location ASC';
	$result = mysql_query($query);
	
	while($ctr = mysql_fetch_array($result)){ //Begin While
	$xsec = $ctr[section];
	switch ($xsec){ // Begin Switch
	case 1; $sec = 'Balita'; break;	case 2; $sec = 'Opinyon'; break;
	case 3; $sec = 'Kalingawan'; break;	case 4; $sec = 'Sports'; break;
	} // End Switch
	
	echo '<tr><td height="20"><span class="article_title">'.$ctr['location'].' - '.$sec.': '.'<a href="view_article.php?id='.$ctr['id'].'">'.$ctr['title'].'</a></span></td>';
	echo '<td width="40" height="20"><div align="center"><span class="maintxt">Edit</span></div></td></tr>';
	}// End While
  ?>
</table>
</div>

</body>
</html>


// -------------------------------------------
// view_article.php
// -------------------------------------------

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<link href="../../default.css" rel="stylesheet" type="text/css">
<link href="../../balita.css" rel="stylesheet" type="text/css">
</head>
<?php
require ("../../config.php");
conn_db($host, $user, $pass, $dbse);
?>
<body>
<div align="center">
  <table width="590" border="0" cellpadding="3" cellspacing="0">
  <?php
  
  $id = $_GET['id'];
  
  $query = "select title, author, body from news_dtl
		    where idx = '$id'";
  $result = mysql_query($query);
  $row = mysql_fetch_array($result);
  echo'
    <tr>
      <td width="594" height="79"><div id="divalign">
          <div align="left" class="dtitle">'.$row[title].'</div>
          <p class="byline">'.$row[author].'</p>
          <br>
          <p>'.nl2br($row[body]).'</p>
        </div></td>
    </tr>';
	?>
  </table>
</div>
</body>
</html>
twigletmac | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

First rule, wrap your code in PHP tags (the little PHP button at the top left of the "Post a reply" text area.

Second, use quotes around your array indeces (ex. $array['index'], not $array[index]) when your indeces are NOT integers.

Third, try this...

Code: Select all

while($ctr = mysql_fetch_array($result)){ //Begin While
    $xsec = $ctr['section'];
    switch ($xsec){ // Begin Switch
        case 1; 
        $sec = 'Balita'; 
        break; 

        case 2; 
        $sec = 'Opinyon'; 
        break;

        case 3; 
        $sec = 'Kalingawan'; 
        break; 

        case 4; 
        $sec = 'Sports'; 
        break;
    } // End Switch

    echo '<tr><td height="20"><span class="article_title">'.$ctr['location'].' - '.$sec.': '.'<a href="view_article.php?id='.$ctr['id'].'">'.$ctr['title'].'</a></span></td>';
    echo '<td width="40" height="20"><div align="center"><span class="maintxt">Edit</span></div></td></tr>';
}// End While
Now look at the links that are output. Roll over them and see what they are linking to. You fetch code for your second page should wrap array indeces in quote also. Try that. I am off to work right now so I can't spend more time on it but give it a go and see what comes of it. Also, use a lot of error trapping to see if there might be errors in your script step by step.
User avatar
khaki_monster
Forum Commoner
Posts: 73
Joined: Tue Oct 11, 2005 12:36 am
Location: Philippines
Contact:

Post by khaki_monster »

hehe... sorry for the mixed'up code. anyway i tried your suggestion but still didn't
work :( is their any configuration about it?

pls more help here :roll:

cheerz!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Is the first page working (displaying what you want, showing links, etc)? If it is, what do the links show in your status bar?

Can you run the page 2 query in phpMyAdmin to see if the query is working correctly? If it is, add error checking to your code to see if there are errors in the script.

Make sure that what is expected as output on the first page is actually what you are getting. Then move on to the second page.
User avatar
khaki_monster
Forum Commoner
Posts: 73
Joined: Tue Oct 11, 2005 12:36 am
Location: Philippines
Contact:

Post by khaki_monster »

yes, the first page("article_list.php") is working according to how it should be. while the status bar is showing something like:

http://localhost/Super/script/preview/a ... st.php?id=

to each links on the list. are this links should supposed give some values(after = ) right?
with the second page("view_article.php") is run manually and giving some value to $id its actually working.

but when i do this. getting some value from $id = $_GET['id'];
it just wont work... 8O

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<link href="../../default.css" rel="stylesheet" type="text/css">
<link href="../../balita.css" rel="stylesheet" type="text/css">
</head>
<?php
require ("../../config.php");
conn_db($host, $user, $pass, $dbse);
?>
<body>
<div align="center">
  <table width="590" border="0" cellpadding="3" cellspacing="0">
  <?php
  
  if(!isset($_GET['id']))
  {
  	header('Location: article_list.php');
	exit;
  }
  
  $id = $_GET['id'];
  
  $query = "select title, author, body from news_dtl
		    where idx = '$id'";
  $result = mysql_query($query);
  $row = mysql_fetch_array($result);
  echo'
    <tr>
      <td width="594" height="79"><div id="divalign">
          <div align="left" class="dtitle">'.$row['title'].'</div>
          <p class="byline">'.$row['author'].'</p>
          <br>
          <p>'.nl2br($row['body']).'</p>
        </div></td>
    </tr>';
	?>
  </table>
</div>
</body>
</html>
Post Reply