Link in a php table

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

Post Reply
happyneil
Forum Newbie
Posts: 8
Joined: Tue Apr 12, 2011 10:56 am

Link in a php table

Post 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
incubi
Forum Contributor
Posts: 119
Joined: Mon Dec 07, 2009 1:47 pm

Re: Link in a php table

Post 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
happyneil
Forum Newbie
Posts: 8
Joined: Tue Apr 12, 2011 10:56 am

Re: Link in a php table

Post 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
incubi
Forum Contributor
Posts: 119
Joined: Mon Dec 07, 2009 1:47 pm

Re: Link in a php table

Post 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
strafingmoose
Forum Newbie
Posts: 15
Joined: Mon Apr 18, 2011 2:56 pm

Re: Link in a php table

Post 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.
incubi
Forum Contributor
Posts: 119
Joined: Mon Dec 07, 2009 1:47 pm

Re: Link in a php table

Post 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
happyneil
Forum Newbie
Posts: 8
Joined: Tue Apr 12, 2011 10:56 am

Re: Link in a php table

Post 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
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Link in a php table

Post 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;
}
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply