Page 1 of 1

Link in a php table

Posted: Mon Apr 18, 2011 7:29 am
by happyneil
Is there anyone out there that can help me with some code.

Say I have displayed a table with "SELECT * FROM details" below



| date | division | start_mile | end_mile | total_mile | Report |
-----------------------------------------------------------------------------
01/02/2011 Division1 25 36 11 Yes
06/03/2011 Division2 365 380 15 Yes
01/02/2011 Division3 67 80 13 No
01/02/2011 Division4 115 135 20 Yes
01/02/2011 Division5 255 275 20 No
01/02/2011 Division6 230 245 15 Yes

I am using this code to output the information into a table

Code: Select all

while($row = sqlsrv_fetch_array( $result )) {
	// Print out the contents of each row into a table
	echo "<tr><td>"; 
	echo $row['date'];
	echo "</td><td>"; 
	echo $row['division'];
	echo "</td><td>";
	echo $row['Start_Mile'];
	echo "</td><td>";
	echo $row['End_Mile'];
	echo "</td><td>";
	echo $row['Total_Mile'];
	echo "</td><td>";
	echo $row ['Report']; //this column clickable

	echo "</td></tr>"; 
} 

echo "</table>";


When I click on the 'Yes' for example on the fourth row... It will output:

You have chosen to view report for "Division4" from start mile "115" to end mile "135".

Hope this makes sense... Any advice would be much appreciated!


Thanks,

Neil

Re: Link in a php table

Posted: Mon Apr 18, 2011 12:15 pm
by incubi
Hi,

I don't see a question here.
Are you saying you would like a button in HTML that's called Yes and pulls records based on which button you click?


Lee

Re: Link in a php table

Posted: Mon Apr 18, 2011 2:31 pm
by happyneil
Sorry I just re-read what I typed and didn't make sense... Thank you for your quick reply! I just need some code so that when a 'yes' in the report column is selected, it pulls data from that row and puts that information into a message such as:

You have chosen to view report for "Division4" from start mile "115" to end mile "135".

What you are saying Lee is correct.


Many thanks,

Neil

Re: Link in a php table

Posted: Mon Apr 18, 2011 3:07 pm
by incubi
Okay, a couple of things, if you want to display "Division4" from start mile "115" to end mile "135"
You should do that in the query and I don't see it here. As for the form submit when you select Yes that's in HTML.
Start by reading this. http://www.w3schools.com/php/php_forms.asp

Remember that you need to put your query code in its own function or if statement and call it from the POST action in the HTML part.

Example,

Code: Select all

if (isset($_POST['my_query']) && $_POST['my_hidden_value'] == "go"): 

while($row = sqlsrv_fetch_array( $result )) {
        // Print out the contents of each row into a table
        echo "<tr><td>";
        echo $row['date'];
        echo "</td><td>";
        echo $row['division'];
        echo "</td><td>";
        echo $row['Start_Mile'];
        echo "</td><td>";
        echo $row['End_Mile'];
        echo "</td><td>";
        echo $row['Total_Mile'];
        echo "</td><td>";
        echo $row ['Report']; //this column clickable

        echo "</td></tr>";
}

echo "</table>";

endif;
Lee

Re: Link in a php table

Posted: Mon Apr 18, 2011 3:11 pm
by strafingmoose
SELECT * FROM details WHERE division = "whatever_the_user_chose" and each "Yes" should be an hyperlink to the script with this code in, roughly.

Re: Link in a php table

Posted: Mon Apr 18, 2011 4:14 pm
by incubi
Okay, no problem, the same applies.

Code: Select all



<a href="<?php echo $PHP_SELF?>?&ID=10">Yes</a></TD>


<?php
if (isset($_GET['ID'])): //The ID of the range to show based on the Yes that's secected. 

	$yes_id = $_GET['ID'];
	$result = mysql_query("SELECT * FROM details WHERE division = '$yes_id' ");

while($row = sqlsrv_fetch_array( $result )) {
        // Print out the contents of each row into a table
        echo "<tr><td>";
        echo $row['date'];
        echo "</td><td>";
        echo $row['division'];
        echo "</td><td>";
        echo $row['Start_Mile'];
        echo "</td><td>";
        echo $row['End_Mile'];
        echo "</td><td>";
        echo $row['Total_Mile'];
        echo "</td><td>";
        echo $row ['Report']; //this column clickable

        echo "</td></tr>";
}

echo "</table>";

endif;

?>

Didn't test it and you should put some security in. To start with mysql_real_escape_string

hope it helps.

Lee

Re: Link in a php table

Posted: Tue Apr 19, 2011 4:22 pm
by happyneil
Thank you very much for your time incubi and strafingmoose! I was able to make the code you provided work for my system.

Cheers,

Neil

Re: Link in a php table

Posted: Tue Apr 19, 2011 5:47 pm
by pickle
FYI as a sidenote, you can make your life & code a bit simpler with regards to outputing the row:

Code: Select all

while($row = sqlsrv_fetch_array( $result )) {
        // Print out the contents of each row into a table
        echo <<<ROW
<tr>
  <td>
    {$row['date']}
  </td>
  <td>
    {$row['division']}
  </td>
  <td>
    {$row['Start_Mile']}
  </td>
  <td>
    {$row['End_Mile']}
  </td>
  <td>
    {$row['Total_Mile']}
  </td>
  <td>
    {$row['Report']}
  </td>
</tr>
ROW;
}