Page 1 of 1

help with dynamic field link

Posted: Sun Jul 21, 2002 9:35 pm
by gilbertwang
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).">&nbsp;&nbsp;".$array[$i]["name"]."</TD>\n"
."<TD width=70 bgcolor=".row_color($i).">&nbsp;&nbsp;".$array[$i]["surveyname"]."</TD>\n"
."<TD bgcolor=".row_color($i).">&nbsp;&nbsp;".$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.

Posted: Mon Jul 22, 2002 3:05 am
by gnu2php
I'm kind of new when it comes to these things, but what do you mean by the survey field being "populated"? Do you mean that after the link is clicked, you want to display all the "survey" fields in the table?

Let me make my problem more understandable

Posted: Mon Jul 22, 2002 10:21 am
by gilbertwang
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.... :)

Posted: Mon Jul 22, 2002 2:21 pm
by gnu2php
I haven't tested it, but try this--

Add this right before the for() loop:

Code: Select all

if (!empty($_GET&#1111;'start'])) $start = $_GET&#1111;'start'];
Then use this for the "survey name" field:

Code: Select all

."<TD width=70 bgcolor=".row_color($i).">&nbsp;&nbsp;<a href="view_survey.php?survey=$i&start=$start">".$array&#1111;$i]&#1111;"surveyname"]."</a></TD>\n"
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):

Code: Select all

$survey_index = 0;
if (!empty($_GET&#1111;'survey'])) $survey_index = $_GET&#1111;'survey'];

$survey = "Survey $survey_index not found.";

// Untested:
for ($i = 0; $i < $row = mysql_fetch_array($result); $i++)
&#123;
	if ($i == $survey_index)
	&#123;
		$survey = $row&#1111;"survey"];
		break;
	&#125;
&#125;

$start = 0;
if (!empty($_GET&#1111;'start'])) $start = $_GET&#1111;'start'];

print "$survey<br><br>\n";
print "<a href="table.php?start=$start">Back</a><br>\n";

Posted: Mon Jul 22, 2002 6:33 pm
by gilbertwang
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

Posted: Mon Jul 22, 2002 9:31 pm
by gnu2php
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--
  • 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.
Let me know if you need further explanations. I hope I didn't make things even more confusing. :)

Posted: Mon Jul 22, 2002 10:38 pm
by gilbertwang
I have place the if(isset above the for loop)

Code: Select all

if (isset($_GET&#123;'start'&#125;)) $start = $_GET&#123;'start'&#125;;
for ($i = $start; $i < ($num_items + $start); $i++)
but I am wondering I have this code above it.

Code: Select all

$MAX_ITEMS = 20; // We'll use 3 for this example 
$start -= ($start % $MAX_ITEMS);
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.

Posted: Tue Jul 23, 2002 12:30 am
by gnu2php
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'].
gilbertwang wrote:the start should be 2 right if the code is correct.
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.

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.

Posted: Tue Jul 23, 2002 4:59 pm
by gilbertwang
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.

Code: Select all

<?
		  
		  $surveyid = 0; 
if (!empty($_GET&#1111;'survey'])) $surveyid = $_GET&#1111;'survey']; 

$survey = "surveyid $surveyid not found."; 

// Untested: 
for ($i = 0; $i < $row = mysql_fetch_array($result); $i++) 
&#123; 
   if ($i == $surveyid) 
   &#123; 
      $survey = $row&#1111;"survey"]; 
      break; 
   &#125; 
&#125; 

$start = 0; 
if (!empty($_GET&#1111;'start'])) $start = $_GET&#1111;'start']; 

print "$survey<br><br>\n"; 
print "<a href="table.php?start=$start">Back</a><br>\n";</td>

? >

Posted: Tue Jul 23, 2002 7:46 pm
by gnu2php
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.
Yes, but in reverse--when survey=2 it is the third row.

I setting the $result to the same as the page before.
Right. That's what I had in mind.

but when I click the link, i have this error.
Parse error: parse error, unexpected '<' in /view_survey.php
It's because there's an extra space: ? > (It should be ?>)

what is table.php is it the page before I should set to
Yes. It's the page that they come from. I called it table.php because I didn't know the correct name.

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
Yes. The $survey_index variable (or $survey_id as you're now using) is the id of the survey--or rather the row index.

is there anything wrong with this code

Posted: Wed Jul 24, 2002 1:09 am
by gilbertwang
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.

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++)&#123; 

   array_push($array, 
            array( 
               'surveyid' => $row&#1111;"surveyid"],
               'name' => $row&#1111;"name"],  
               'title' => $row&#1111;"title"], 
               'survey' => $row&#1111;"survey"],  
               'surveytime' => $row&#1111;"surveytime"], 
            )); 
&#125;


$surveyid = 0; 
if (!empty($_GET&#1111;'survey'])) $surveyid = $_GET&#1111;'survey']; 

$survey = "surveyid $surveyid not found."; 

// Untested: 
for ($i = 0; $i < $row = mysql_fetch_array($result); $i++) 
&#123; 
   if ($i == $surveyid) 
   &#123; 
      $survey = $row&#1111;"survey"]; 
      break; 
   &#125; 
&#125; 

$start = 0; 
if (!empty($_GET&#1111;'start'])) $start = $_GET&#1111;'start']; 

print "$survey<br><br>\n"; 
print "<a href="survey.php?start=$start">Back</a><br>\n";
?>

Posted: Wed Jul 24, 2002 3:09 am
by gnu2php
Does it work if you remove this?

Code: Select all

$array = array(); 


for( $i = 0; $i < $row = mysql_fetch_array($result); $i++)&#123; 

   array_push($array, 
            array( 
               'surveyid' => $row&#1111;"surveyid"], 
               'name' => $row&#1111;"name"],  
               'title' => $row&#1111;"title"], 
               'survey' => $row&#1111;"survey"],  
               'surveytime' => $row&#1111;"surveytime"], 
            )); 
&#125;
The problem is that the "fetching" is continued on the next for() loop, but there's nothing left to "fetch."

wonderful!

Posted: Wed Jul 24, 2002 9:31 am
by gilbertwang
wonderful.....Finally it works like a charm....I really appreciate your patience.
:D