Connecting Tech Pros Worldwide Help | Site Map

ahref link with dynamic data

Member
 
Join Date: Jan 2009
Location: USA
Posts: 118
#1: 4 Weeks Ago
hello -

trying to get a link to pass the value to another page from a mysql query in php through the url.

or i could use sessions. most of my site is with sessions. Can we create an array of sessions? session[0], session[1]? because each id would need to have a session id.

final outcome:
Expand|Select|Wrap|Line Numbers
  1. <td><a href="url?parameter">parameter name</a><?php echo $parameter['Id']; ?></td>
  2.  
thanks in advance for your help.

theo werntz ii
dlite922's Avatar
Expert
 
Join Date: Dec 2007
Location: Moon, Dark Side
Posts: 1,094
#2: 4 Weeks Ago

re: ahref link with dynamic data


You can put an array into a session by serializing it first and unserializing it later

check out php.net/serialize

url?parameter is not right, you need a value like url?parameter=123ABC

to have a second parameter passed, separate them by a ampersand like this

url?param1=123&param2=Horse



Dan
Member
 
Join Date: Jan 2009
Location: USA
Posts: 118
#3: 4 Weeks Ago

re: ahref link with dynamic data


good morning - will post my results in a while thanks for the response!
Member
 
Join Date: Jan 2009
Location: USA
Posts: 118
#4: 4 Weeks Ago

re: ahref link with dynamic data


hello -

i've tried what your explaining to me. can you give me a better example with using sessions? to pass the variable to the next page.

so the query can pull multiple results. and each result will have its own id. i want that id to be set in session and passed to the next page.

i have it working fine with my current sessions on a single value but not multiple values.

so the multiple values will be in the array but only the choosen one will be passed.

i get an error when i try it says that the mysql result set error. i moved the session variable down below the query and still same error.

thanks in advance for your help,

theo werntz ii
TheServant's Avatar
Expert
 
Join Date: Feb 2008
Location: Australia
Posts: 913
#5: 4 Weeks Ago

re: ahref link with dynamic data


I am not entirely sure what you have tried but you can store session variables like:
Expand|Select|Wrap|Line Numbers
  1. $_SESSION['id'] = 1;
  2. $_SESSION['name'] = "John";
  3. $_SESSION['email'] = "john@bytes.com";
  4. etc.
Output is as simple as:
Expand|Select|Wrap|Line Numbers
  1. echo $_SESSION['id'];
The problem with results from databases is that each row or result is an array itself. Making a 2D array is not a problem, but the $_SESSION array is not really designed for that, so serializing is required. This basically turns an array into a single value which can be stored in $_SESSION and then that value can be unserialized into an array again when needed. That's how I understand it atleast.

Can you include some code? You said you got a "mysql result set error"? Haven't seen that one before, but show us how you are handling the query and $_SESSIONs as I suspect there is some misunderstanding.
dlite922's Avatar
Expert
 
Join Date: Dec 2007
Location: Moon, Dark Side
Posts: 1,094
#6: 4 Weeks Ago

re: ahref link with dynamic data


session singular variables he knows, arrays or multiple he doesn't.

Your myself result error is a different error. Debug that one first.

Did you read the php doc on serialize? Here's an example of what I was talking about:

Expand|Select|Wrap|Line Numbers
  1.  
  2. //page.php: 
  3. $myarr = array("foo","bar"); 
  4.  
  5. $_SESSION['myarr_s'] = serialize($myarr); 
  6.  
  7. // secondpage.php
  8.  
  9. $myarr = unserialize($_SESSION['myarr_s']); 
  10.  
  11.  
Echo the serialized variable to see what it looks like. It's just a coded string that php understand and can reconstruct.



Dan
Member
 
Join Date: Jan 2009
Location: USA
Posts: 118
#7: 4 Weeks Ago

re: ahref link with dynamic data


good morning -

thanks for the responses.

this is what i have so far:

i would not like to use the get url string from url for security reasons so im sticking with sessions.

i was able to get the session working for the link but its only storing one record id in the array here is my code:

Expand|Select|Wrap|Line Numbers
  1. // session variables for links
  2. $result = mysql_result($query,0,'Id');
  3. $_SESSION['Id']=$result;
  4.  
  5. $sesId=$_SESSION['Id'];
  6.  
  7. <td><a href="url.php"><?php echo $sesId['Id']; ?></a></td>
  8.  
yes i did read on serialize but didn't find a good way to use it with a mysql query. i just found that u needed to enter the array values.

could i use the serialize like:

Expand|Select|Wrap|Line Numbers
  1. $myarr = array(mysql_query($query,0,'Id')); 
  2.  
  3.  $_SESSION['myarr_s'] = serialize($myarr); 
  4.  
  5. $sesId=$_Session['myarr_s'];
  6.  
  7. <td><a href="url.php"><?php echo $sesId['Id']; ?></a></td>
  8.  
  9.  $myarr = unserialize($_SESSION['myarr_s']); 
  10.  
thanks again in advance for your help,
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#8: 4 Weeks Ago

re: ahref link with dynamic data


Quote:

Originally Posted by wizardry View Post

Expand|Select|Wrap|Line Numbers
  1. $myarr = array(mysql_query($query,0,'Id'));

there are Database Abstraction Layers (e.g. PDO) that can return the complete SQL result as array. I'm not sure, whether saving the resource is a good idea.
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,936
#9: 4 Weeks Ago

re: ahref link with dynamic data


Quote:

Originally Posted by Dormilich View Post

there are Database Abstraction Layers (e.g. PDO) that can return the complete SQL result as array. I'm not sure, whether saving the resource is a good idea.

Off-topic: it annoys me that the PHP MySQL wrapper does not provide a native way to return ALL results into an array. I was considering writing a patch for the PHP MySQL extension to include this function - something like mysql_fetch_all().
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,936
#10: 4 Weeks Ago

re: ahref link with dynamic data


Quote:

Originally Posted by wizardry View Post

good morning -

thanks for the responses.

this is what i have so far:

i would not like to use the get url string from url for security reasons so im sticking with sessions.

i was able to get the session working for the link but its only storing one record id in the array here is my code:

Expand|Select|Wrap|Line Numbers
  1. // session variables for links
  2. $result = mysql_result($query,0,'Id');
  3. $_SESSION['Id']=$result;
  4.  
  5. $sesId=$_SESSION['Id'];
  6.  
  7. <td><a href="url.php"><?php echo $sesId['Id']; ?></a></td>
  8.  
yes i did read on serialize but didn't find a good way to use it with a mysql query. i just found that u needed to enter the array values.

could i use the serialize like:

Expand|Select|Wrap|Line Numbers
  1. $myarr = array(mysql_query($query,0,'Id')); 
  2.  
  3.  $_SESSION['myarr_s'] = serialize($myarr); 
  4.  
  5. $sesId=$_Session['myarr_s'];
  6.  
  7. <td><a href="url.php"><?php echo $sesId['Id']; ?></a></td>
  8.  
  9.  $myarr = unserialize($_SESSION['myarr_s']); 
  10.  
thanks again in advance for your help,

You cannot serialize() a resource.

Quote:

Originally Posted by php.net

The value to be serialized. serialize() handles all types, except the resource-type.

Member
 
Join Date: Jan 2009
Location: USA
Posts: 118
#11: 4 Weeks Ago

re: ahref link with dynamic data


markus -

could you give me a detail usage on dynamic session arrays?

or am i doing something wrong? i've been racking my brain with this; and googling(dynamic session arrays) ; the array is set but in the session variable it only outputs one variable.

have i not set the array correctly?

thanks in advance for your help.
Member
 
Join Date: Jan 2009
Location: USA
Posts: 118
#12: 4 Weeks Ago

re: ahref link with dynamic data


hello -

im still having a problem. this array will set but only shows one record from the array. when there are 3 demo data sets.

thanks in advance for your help.

i want to store the variable id in an array and access that array through session id hyper link on the same page. so that when that hyper link is clicked the session id is passed to the next page.


here is the page code:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. if (!isset($_SESSION)) {
  3.   session_start();
  4. }
  5. ?>
  6. <?php require_once('conn.php'); ?>
  7. <?php
  8. if (!function_exists("GetSQLValueString")) {
  9. function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
  10. {
  11.   if (PHP_VERSION < 6) {
  12.     $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  13.   }
  14.  
  15.   $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
  16.  
  17.   switch ($theType) {
  18.     case "text":
  19.       $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  20.       break;    
  21.     case "long":
  22.     case "int":
  23.       $theValue = ($theValue != "") ? intval($theValue) : "NULL";
  24.       break;
  25.     case "double":
  26.       $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
  27.       break;
  28.     case "date":
  29.       $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  30.       break;
  31.     case "defined":
  32.       $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
  33.       break;
  34.   }
  35.   return $theValue;
  36. }
  37. }
  38.  
  39.  
  40. $Id = $_SESSION['Id']; //get users id to pass to record
  41. $Name = $_SESSION['name']; // display users name
  42. $Group = $_SESSION['Group']; // access control group for moderators, members, users
  43.  
  44. $maxRows_List = 10;
  45. $pageNum_List = 0;
  46. if (isset($_GET['pageNum_List'])) {
  47.   $pageNum_List = $_GET['pageNum_List'];
  48. }
  49. $startRow_List = $pageNum_List * $maxRows_List;
  50.  
  51. mysql_select_db($database_List, $List);
  52. $query_List = "SELECT * WHERE b.UId1='$UserId'";
  53. $query_limit_List = sprintf("%s LIMIT %d, %d", $query_List, $startRow_List, $maxRows_List);
  54. $List = mysql_query($query_limit_List, $List) or die(mysql_error());
  55. $row_List = mysql_fetch_assoc($List);
  56.  
  57. $List_Array = array();
  58.  
  59. while ($row = mysql_fetch_array($List))
  60. {
  61.  
  62. foreach($row['Id'] as $i=>$val)
  63.  
  64. $List_Array = $val;
  65.  
  66. //return $List_Array;
  67. $_SESSION['Id'] = $List_Array;
  68.  
  69. $myList_Array = $_SESSION['Id'];
  70. }
  71.  
  72.  
  73. if (isset($_GET['totalRows_List'])) {
  74.   $totalRows_List = $_GET['totalRows_List'];
  75. } else {
  76.   $all_List = mysql_query($query_List);
  77.   $totalRows_List = mysql_num_rows($all_List);
  78. }
  79. $totalPages_List = ceil($totalRows_List/$maxRows_List)-1;
  80.  
  81. ?>
  82. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  83. <html xmlns="http://www.w3.org/1999/xhtml">
  84. <head>
  85. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  86. <title>Untitled Document</title>
  87. <style type="text/css"> 
  88. <!-- 
  89. body  {
  90.     font: 100% Verdana, Arial, Helvetica, sans-serif;
  91.     background: #666666;
  92.     margin: 0; /* it's good practice to zero the margin and padding of the body element to account for differing browser defaults */
  93.     padding: 0;
  94.     text-align: center; /* this centers the container in IE 5* browsers. The text is then set to the left aligned default in the #container selector */
  95.     color: #000000;
  96.     background-color: #0E03D6;
  97. }
  98. .thrColLiqHdr #container { 
  99.     width: 80%;  /* this will create a container 80% of the browser width */
  100.     background: #FFFFFF;
  101.     margin: 0 auto; /* the auto margins (in conjunction with a width) center the page */
  102.     border: 1px solid #000000;
  103.     text-align: left; /* this overrides the text-align: center on the body element. */
  104. .thrColLiqHdr #header { 
  105.     background: #FFFFFF; 
  106.     padding: 0 10px;  /* this padding matches the left alignment of the elements in the divs that appear beneath it. If an image is used in the #header instead of text, you may want to remove the padding. */
  107. .thrColLiqHdr #header h1 {
  108.     margin: 0; /* zeroing the margin of the last element in the #header div will avoid margin collapse - an unexplainable space between divs. If the div has a border around it, this is not necessary as that also avoids the margin collapse */
  109.     padding: 10px 0; /* using padding instead of margin will allow you to keep the element away from the edges of the div */
  110. }
  111.  
  112. /* Tips for sidebars:
  113. 1. Since we are working in percentages, it's best not to use side padding on the sidebars. It will be added to the width for standards compliant browsers creating an unknown actual width. 
  114. 2. Space between the side of the div and the elements within it can be created by placing a left and right margin on those elements as seen in the ".thrColLiqHdr #sidebar1 p" rule.
  115. 3. Since Explorer calculates widths after the parent element is rendered, you may occasionally run into unexplained bugs with percentage-based columns. If you need more predictable results, you may choose to change to pixel sized columns.
  116. */
  117. .thrColLiqHdr #sidebar1 {
  118.     float: left; /* this element must precede in the source order any element you would like it be positioned next to */
  119.     width: 22%; /* since this element is floated, a width must be given */
  120.     background: #FFFFFF; /* the background color will be displayed for the length of the content in the column, but no further */
  121.     padding: 15px 0; /* top and bottom padding create visual space within this div  */
  122. }
  123. .thrColLiqHdr #sidebar2 {
  124.     float: right; /* this element must precede in the source order any element you would like it be positioned next to */
  125.     width: 23%; /* since this element is floated, a width must be given */
  126.     background: #FFFFFF; /* the background color will be displayed for the length of the content in the column, but no further */
  127.     padding: 15px 0; /* top and bottom padding create visual space within this div */
  128. }
  129. .thrColLiqHdr #sidebar1 p, .thrColLiqHdr #sidebar1 h3, .thrColLiqHdr #sidebar2 p, .thrColLiqHdr #sidebar2 h3 {
  130.     margin-left: 10px; /* the left and right margin should be given to every element that will be placed in the side columns */
  131.     margin-right: 10px;
  132. }
  133.  
  134. /* Tips for mainContent:
  135. 1. the space between the mainContent and sidebars is created with the left and right margins on the mainContent div.
  136. 2. to avoid float drop at a supported minimum 800 x 600 resolution, elements within the mainContent div should be 300px or smaller (this includes images).
  137. 3. in the Internet Explorer Conditional Comment below, the zoom property is used to give the mainContent "hasLayout." This avoids several IE-specific bugs.
  138. */
  139. .thrColLiqHdr #mainContent { 
  140.     margin: 0 24% 0 23.5%; /* the right and left margins on this div element creates the two outer columns on the sides of the page. No matter how much content the sidebar divs contain, the column space will remain. You can remove this margin if you want the #mainContent div's text to fill the sidebar spaces when the content in each sidebar ends. */
  141. }
  142.  
  143. .thrColLiqHdr #footer { 
  144.     padding: 0 10px; /* this padding matches the left alignment of the elements in the divs that appear above it. */
  145.     background:#FFFFFF;
  146. .thrColLiqHdr #footer p {
  147.     margin: 0; /* zeroing the margins of the first element in the footer will avoid the possibility of margin collapse - a space between divs */
  148.     padding: 10px 0; /* padding on this element will create space, just as the the margin would have, without the margin collapse issue */
  149. }
  150.  
  151. /* Miscellaneous classes for reuse */
  152. .fltrt { /* this class can be used to float an element right in your page. The floated element must precede the element it should be next to on the page. */
  153.     float: right;
  154.     margin-left: 8px;
  155. }
  156. .fltlft { /* this class can be used to float an element left in your page The floated element must precede the element it should be next to on the page. */
  157.     float: left;
  158.     margin-right: 8px;
  159. }
  160. .clearfloat { /* this class should be placed on a div or break element and should be the final element before the close of a container that should fully contain its child floats */
  161.     clear:both;
  162.     height:0;
  163.     font-size: 1px;
  164.     line-height: 0px;
  165. }
  166. #apDiv1 {
  167.     position:absolute;
  168.     left:206px;
  169.     top:83px;
  170.     width:151px;
  171.     height:34px;
  172.     z-index:1;
  173. }
  174. #apDiv2 {
  175.     position:absolute;
  176.     left:363px;
  177.     top:84px;
  178.     width:519px;
  179.     height:37px;
  180.     z-index:2;
  181. }
  182. --> 
  183. </style><!--[if IE]>
  184. <style type="text/css"> 
  185. /* place css fixes for all versions of IE in this conditional comment */
  186. .thrColLiqHdr #sidebar2, .thrColLiqHdr #sidebar1 { padding-top: 30px; }
  187. .thrColLiqHdr #mainContent { zoom: 1; padding-top: 15px; }
  188. /* the above proprietary zoom property gives IE the hasLayout it needs to avoid several bugs */
  189. </style>
  190. <![endif]-->
  191. <script src="SpryAssets/SpryMenuBar.js" type="text/javascript"></script>
  192. <link href="SpryAssets/SpryMenuBarHorizontal.css" rel="stylesheet" type="text/css" />
  193. </head>
  194.  
  195. <body class="thrColLiqHdr">
  196.  
  197. <div id="container">
  198.  <div id="header">
  199.     <h1>&nbsp;</h1>
  200.     <p>&nbsp;</p>
  201.     <p>&nbsp;</p>
  202.   <!-- end #header --></div>
  203.   <div id="sidebar1">
  204.   <h3>&nbsp;    </h3>
  205.   <!-- show users ratings overall and per list -->
  206.   <!-- end #sidebar1 --></div>
  207.   <div id="sidebar2">
  208.     <h3>&nbsp;</h3>
  209.   <!-- display current viewers of user list in space and show count of number of views -->
  210.  
  211.   <!-- end #sidebar2 --></div>
  212.   <div id="mainContent">
  213.     <h1>&nbsp;</h1>
  214.     <blockquote>
  215.       <table border="1" cellpadding="1" cellspacing="1">
  216.         <tr></tr>
  217.         <caption align="center">
  218.           List  Click to Edit
  219.         </caption>
  220.         <?php do { ?>
  221.         <tr>
  222.           <td><a href="list.php"><?php echo $myList_Array['Id']; ?></a></td> <-- this is the column that i want to have access the session id -->
  223.           <td><?php echo $row_List['Subject'];?></td>
  224.           <td><?php echo $row_List['Date']; ?></td>
  225.         </tr>
  226.         <?php } while($row_List = mysql_fetch_assoc($List));
  227.         ?>
  228.       </table>
  229.       <h2>&nbsp;</h2>
  230.       <p>&nbsp;</p>
  231.     </blockquote>
  232. <!-- end #mainContent --></div>
  233.     <!-- This clearing element should immediately follow the #mainContent div in order to force the #container div to contain all child floats --><br class="clearfloat" />
  234.   <div id="footer">
  235.     <p>Footer</p>
  236.   <!-- end #footer --></div>
  237. <!-- end #container --></div>
  238. </body>
  239. </html>
  240. <?php
  241. mysql_free_result($List);
  242. ?>
  243.  
  244.  
Reply