help with dynamic field link
Moderator: General Moderators
-
gilbertwang
- Forum Commoner
- Posts: 32
- Joined: Sun Jun 30, 2002 11:08 pm
- Location: Calgary
help with dynamic field link
I am retrieving 4 fields(name, surveyname, survey, surveytime and from the database) I have placed the three fields in a table (name, surveyname and surveytime) in this code.
----------------------------------------------------------------------------
$array = array();
for( $i = 0; $i < $row = mysql_fetch_array($result); $i++){
array_push($array,
array(
'name' => $row["name"],
'surveyname' => $row["surveyname"],
'survey' => $row["survey"],
'surveytime' => $row["surveytime"],
));
}
for ($i = $start; $i < ($num_items + $start); $i++)
{
echo "<TR>\n"
."<TD width=320 height=30 bgcolor=".row_color($i)."> ".$array[$i]["name"]."</TD>\n"
."<TD width=70 bgcolor=".row_color($i)."> ".$array[$i]["surveyname"]."</TD>\n"
."<TD bgcolor=".row_color($i)."> ".$array[$i]["surveytime"]."</TD>\n"
."</TR>";
}
-----------------------------------------------------------------------------
My problem is I want to make a hyperlink in the field for surveyname and when the user click the link, it will go to a new page with only the field (survey) populated. And there will be a back button, so the user could go back to the populated table.
----------------------------------------------------------------------------
$array = array();
for( $i = 0; $i < $row = mysql_fetch_array($result); $i++){
array_push($array,
array(
'name' => $row["name"],
'surveyname' => $row["surveyname"],
'survey' => $row["survey"],
'surveytime' => $row["surveytime"],
));
}
for ($i = $start; $i < ($num_items + $start); $i++)
{
echo "<TR>\n"
."<TD width=320 height=30 bgcolor=".row_color($i)."> ".$array[$i]["name"]."</TD>\n"
."<TD width=70 bgcolor=".row_color($i)."> ".$array[$i]["surveyname"]."</TD>\n"
."<TD bgcolor=".row_color($i)."> ".$array[$i]["surveytime"]."</TD>\n"
."</TR>";
}
-----------------------------------------------------------------------------
My problem is I want to make a hyperlink in the field for surveyname and when the user click the link, it will go to a new page with only the field (survey) populated. And there will be a back button, so the user could go back to the populated table.
-
gilbertwang
- Forum Commoner
- Posts: 32
- Joined: Sun Jun 30, 2002 11:08 pm
- Location: Calgary
Let me make my problem more understandable
This is the table that I have displayed on the page with 3 fields.
------------------------------------------------------
name surveyname surveytime
joseph site1 2002-05-12
amy site2 2002-07-12
-------------------------------------------------------
when user click on site1. it brings them to a new page with the following data
-------------------------------------------------------
survey
this survey is regarding blah blah blah......
back button
-------------------------------------------------------
what I was trying to do is the survey name will have hyperlink and when the user click it will display the survey field. Just one field. and will back a back button so the user can come back to this table and click on other survey name that they would like to see.
All the fields (surveyid, name, surveyname, survey, surveytime) are already being retrieved in the page, but how can I put the hyperlink in the array so that when the user click on site1, it will match the surveyid and retreive the survey information on a new page.
thanks for helping out....
------------------------------------------------------
name surveyname surveytime
joseph site1 2002-05-12
amy site2 2002-07-12
-------------------------------------------------------
when user click on site1. it brings them to a new page with the following data
-------------------------------------------------------
survey
this survey is regarding blah blah blah......
back button
-------------------------------------------------------
what I was trying to do is the survey name will have hyperlink and when the user click it will display the survey field. Just one field. and will back a back button so the user can come back to this table and click on other survey name that they would like to see.
All the fields (surveyid, name, surveyname, survey, surveytime) are already being retrieved in the page, but how can I put the hyperlink in the array so that when the user click on site1, it will match the surveyid and retreive the survey information on a new page.
thanks for helping out....
I haven't tested it, but try this--
Add this right before the for() loop:
Then use this for the "survey name" field:
And finally, create a new file (such as view_survey.php) and add something like this to it (you'll also need to set $result to something):
Add this right before the for() loop:
Code: Select all
if (!empty($_GETї'start'])) $start = $_GETї'start'];Code: Select all
."<TD width=70 bgcolor=".row_color($i)."> <a href="view_survey.php?survey=$i&start=$start">".$arrayї$i]ї"surveyname"]."</a></TD>\n"Code: Select all
$survey_index = 0;
if (!empty($_GETї'survey'])) $survey_index = $_GETї'survey'];
$survey = "Survey $survey_index not found.";
// Untested:
for ($i = 0; $i < $row = mysql_fetch_array($result); $i++)
{
if ($i == $survey_index)
{
$survey = $rowї"survey"];
break;
}
}
$start = 0;
if (!empty($_GETї'start'])) $start = $_GETї'start'];
print "$survey<br><br>\n";
print "<a href="table.php?start=$start">Back</a><br>\n";-
gilbertwang
- Forum Commoner
- Posts: 32
- Joined: Sun Jun 30, 2002 11:08 pm
- Location: Calgary
I have a few probelem understanding the code, please correct me if I misinterpret wrong.
-------------------------------------------------------------
if (!empty($_GET['start'])) $start = $_GET['start'];
-------------------------------------------------------------
this means if not empty ($_GET['start']))
what does the $_GET means and the start
I had this code above
--------------------------------------------------------------
if (isset($_GET{'start'})) $start = $_GET{'start'};
$MAX_ITEMS = 20; // We'll use 3 for this example
$start -= ($start % $MAX_ITEMS);
$array_count = count($array); // The number of items in the array
$num_items = ($array_count - $start);
if ($num_items > $MAX_ITEMS) $num_items = $MAX_ITEMS;
------------------------------------------------------------------
what does the if(isset($_GET{'start'})) mean and what's it different from the if(!empty)
how do I interpret $start -= ($start % $MAX_ITEMS);
does the array_count the items is a row or total items, which means
10 rows return 10 items, or if 10 rows has 2 field, then it returns 20 items.
I am a bit confused by some of the code, I am trying hard to understand the code, so that I would get the logic behind it for a good foundation.
thanks
-------------------------------------------------------------
if (!empty($_GET['start'])) $start = $_GET['start'];
-------------------------------------------------------------
this means if not empty ($_GET['start']))
what does the $_GET means and the start
I had this code above
--------------------------------------------------------------
if (isset($_GET{'start'})) $start = $_GET{'start'};
$MAX_ITEMS = 20; // We'll use 3 for this example
$start -= ($start % $MAX_ITEMS);
$array_count = count($array); // The number of items in the array
$num_items = ($array_count - $start);
if ($num_items > $MAX_ITEMS) $num_items = $MAX_ITEMS;
------------------------------------------------------------------
what does the if(isset($_GET{'start'})) mean and what's it different from the if(!empty)
how do I interpret $start -= ($start % $MAX_ITEMS);
does the array_count the items is a row or total items, which means
10 rows return 10 items, or if 10 rows has 2 field, then it returns 20 items.
I am a bit confused by some of the code, I am trying hard to understand the code, so that I would get the logic behind it for a good foundation.
thanks
The isset() function checks to see if a variable exists, or if an array contains a certain item (meaning "key").
Using empty() is the same as !isset(), except it checks to see if a variable doesn't have a value.
The $_GET variable is an array that contains each of the parameters given after the ? in a url. If you had mypage.php?query=test&len=10, the $_GET array would contain keys 'query' and 'len'--which would be set to 'test' and 10, respectively.
In this case, if you did mypage.php?start=43, the $_GET['start'] variable would be set to 43 (and therefore $start would then be set to 43 also).
However, since you already had if (isset($_GET{'start'})) $start = $_GET{'start'};, you can just ignore the first code snippet I posted above.
The $array_count variable would be set to 10 (the number of rows), because the count() function returns the number of keys in an array.
About the $start -= ($start % $MAX_ITEMS) code--

Using empty() is the same as !isset(), except it checks to see if a variable doesn't have a value.
The $_GET variable is an array that contains each of the parameters given after the ? in a url. If you had mypage.php?query=test&len=10, the $_GET array would contain keys 'query' and 'len'--which would be set to 'test' and 10, respectively.
In this case, if you did mypage.php?start=43, the $_GET['start'] variable would be set to 43 (and therefore $start would then be set to 43 also).
However, since you already had if (isset($_GET{'start'})) $start = $_GET{'start'};, you can just ignore the first code snippet I posted above.
The $array_count variable would be set to 10 (the number of rows), because the count() function returns the number of keys in an array.
About the $start -= ($start % $MAX_ITEMS) code--
- 1. The % means "modulus," or the "remainder" after division. If you did 57 / 10, you would get 5.7; but if you did 57 % 10, you would get 7.
2. The $start variable is subtracted by $start % $MAX_ITEMS in order to make sure $start is an integer if it is divided by $MAX_ITEMS. So, since $MAX_ITEMS is 20, $start can only be 0, 20, 40, 60, 80, etc.
-
gilbertwang
- Forum Commoner
- Posts: 32
- Joined: Sun Jun 30, 2002 11:08 pm
- Location: Calgary
I have place the if(isset above the for loop)
but I am wondering I have this code above it.
are the $start (in isset) and the $start(in $start%$max) the same or they serve different function. I think they serve different purpose, I was just wondering would it affect the code if either of them is above or below.
when I click the row 3 surveyname(link)on the surveyname, on the url it reads
http://url/view_survey.php?survey=2&start=0
the start should be 2 right if the code is correct.
how can I check what's wrong with it.
I am wondering what's the logic to retreive the survey.
Is it to get the surveyid and go through mysql and pick the one with the same id. From the url, I am getting the row number, am I getting the survey through the array so I dont have to retrieve from the database again.
Code: Select all
if (isset($_GET{'start'})) $start = $_GET{'start'};
for ($i = $start; $i < ($num_items + $start); $i++)Code: Select all
$MAX_ITEMS = 20; // We'll use 3 for this example
$start -= ($start % $MAX_ITEMS);when I click the row 3 surveyname(link)on the surveyname, on the url it reads
http://url/view_survey.php?survey=2&start=0
the start should be 2 right if the code is correct.
how can I check what's wrong with it.
I am wondering what's the logic to retreive the survey.
Is it to get the surveyid and go through mysql and pick the one with the same id. From the url, I am getting the row number, am I getting the survey through the array so I dont have to retrieve from the database again.
Actually, the
if (isset($_GET{'start'})) $start = $_GET{'start'};
code should be above
$start -= ($start % $MAX_ITEMS);
because $start is set to the value of $_GET['start']
Just so you know, the reason I used the $start variable in the first place was because it's easier to write than $_GET['start'], so just think of $start as a "shortcut" to $_GET['start'].
Also, the 'survey=2' part of the URL is OK, too. The 2 doesn't mean the second row. It is the third row, since 0 is the first index (so 0=first, 1=second, 2=third, etc.).
Another thing to understand is that the 'survey=2' parameter is the index of the row that has the survey--the row index in the array called $array. This is why it is set to the value of $i in the for() loop.
if (isset($_GET{'start'})) $start = $_GET{'start'};
code should be above
$start -= ($start % $MAX_ITEMS);
because $start is set to the value of $_GET['start']
Just so you know, the reason I used the $start variable in the first place was because it's easier to write than $_GET['start'], so just think of $start as a "shortcut" to $_GET['start'].
No, really, 0 is the right number for 'start'. The reason 'start' is passed to the "view survey" page is to keep track of the $start variable when going from page to page. It isn't actually used on the view_survey.php page, except on the "back" link.gilbertwang wrote:the start should be 2 right if the code is correct.
Also, the 'survey=2' part of the URL is OK, too. The 2 doesn't mean the second row. It is the third row, since 0 is the first index (so 0=first, 1=second, 2=third, etc.).
Another thing to understand is that the 'survey=2' parameter is the index of the row that has the survey--the row index in the array called $array. This is why it is set to the value of $i in the for() loop.
-
gilbertwang
- Forum Commoner
- Posts: 32
- Joined: Sun Jun 30, 2002 11:08 pm
- Location: Calgary
oh..I get what you mean now.
so if I get the survey from another page, the url shown as
http://url/view_survey.php?survey=3&start=0 would be right and it would be referring to row 2.
But you mention that I should set the results to something.
I set the results as the following:am I setting the $result to the same as the page before. but when I click the link, i have this error.
Parse error: parse error, unexpected '<' in /view_survey.php
I try looking at what's wrong, but couldn't find the error.
and what do you mean by $survey_index=0 - is it the id of the survey in the database or it's a variable and is set to 0.
and what is table.php is it the page before I should set to.
so if I get the survey from another page, the url shown as
http://url/view_survey.php?survey=3&start=0 would be right and it would be referring to row 2.
But you mention that I should set the results to something.
I set the results as the following:am I setting the $result to the same as the page before. but when I click the link, i have this error.
Parse error: parse error, unexpected '<' in /view_survey.php
I try looking at what's wrong, but couldn't find the error.
and what do you mean by $survey_index=0 - is it the id of the survey in the database or it's a variable and is set to 0.
and what is table.php is it the page before I should set to.
Code: Select all
<?
$surveyid = 0;
if (!empty($_GETї'survey'])) $surveyid = $_GETї'survey'];
$survey = "surveyid $surveyid not found.";
// Untested:
for ($i = 0; $i < $row = mysql_fetch_array($result); $i++)
{
if ($i == $surveyid)
{
$survey = $rowї"survey"];
break;
}
}
$start = 0;
if (!empty($_GETї'start'])) $start = $_GETї'start'];
print "$survey<br><br>\n";
print "<a href="table.php?start=$start">Back</a><br>\n";</td>
? >Yes, but in reverse--when survey=2 it is the third row.so if I get the survey from another page, the url shown as
http://url/view_survey.php?survey=3&start=0 would be right and it would be referring to row 2.
Right. That's what I had in mind.I setting the $result to the same as the page before.
It's because there's an extra space: ? > (It should be ?>)but when I click the link, i have this error.
Parse error: parse error, unexpected '<' in /view_survey.php
Yes. It's the page that they come from. I called it table.php because I didn't know the correct name.what is table.php is it the page before I should set to
Yes. The $survey_index variable (or $survey_id as you're now using) is the id of the survey--or rather the row index.what do you mean by $survey_index=0 - is it the id of the survey in the database or it's a variable and is set to 0
-
gilbertwang
- Forum Commoner
- Posts: 32
- Joined: Sun Jun 30, 2002 11:08 pm
- Location: Calgary
is there anything wrong with this code
I didn't manage to pull the survey on this page, but when I click on the link on first row, it returns surveyid 0 not found and if I click on
second row, it return surveyid 1 not found and third row surveyid 2 not found.
second row, it return surveyid 1 not found and third row surveyid 2 not found.
Code: Select all
<?
$connection = mysql_connect("localhost","user","pass");
$db = mysql_select_db("database");
$sql = "select * from tablesurvey order by survey_time desc";
$result = mysql_query("$sql"); // Database Query result
$array = array();
for( $i = 0; $i < $row = mysql_fetch_array($result); $i++){
array_push($array,
array(
'surveyid' => $rowї"surveyid"],
'name' => $rowї"name"],
'title' => $rowї"title"],
'survey' => $rowї"survey"],
'surveytime' => $rowї"surveytime"],
));
}
$surveyid = 0;
if (!empty($_GETї'survey'])) $surveyid = $_GETї'survey'];
$survey = "surveyid $surveyid not found.";
// Untested:
for ($i = 0; $i < $row = mysql_fetch_array($result); $i++)
{
if ($i == $surveyid)
{
$survey = $rowї"survey"];
break;
}
}
$start = 0;
if (!empty($_GETї'start'])) $start = $_GETї'start'];
print "$survey<br><br>\n";
print "<a href="survey.php?start=$start">Back</a><br>\n";
?>Does it work if you remove this?
The problem is that the "fetching" is continued on the next for() loop, but there's nothing left to "fetch."
Code: Select all
$array = array();
for( $i = 0; $i < $row = mysql_fetch_array($result); $i++){
array_push($array,
array(
'surveyid' => $rowї"surveyid"],
'name' => $rowї"name"],
'title' => $rowї"title"],
'survey' => $rowї"survey"],
'surveytime' => $rowї"surveytime"],
));
}-
gilbertwang
- Forum Commoner
- Posts: 32
- Joined: Sun Jun 30, 2002 11:08 pm
- Location: Calgary
wonderful!
wonderful.....Finally it works like a charm....I really appreciate your patience.
