Jump to content

[SOLVED] printing individually selectable mysql rows to screen


Recommended Posts

Hi

I'm printing out a series of individual customer requests dragged from a mysql database.

At moment I'm just printing out the whole set of data as one block.

 

$result = mysql_query("SELECT * FROM proposal WHERE username = '$username'");
while($row = mysql_fetch_array($result))
		  echo "<tbody>";
			echo "<tr>";
			  echo "<td>" . $row['county'] . "</td>";
			  echo "<td>" . $row['starrating'] . "</td>";
			 echo "</tr>";
		  echo "</tbody>";			  

 

I want a user to be able to hover over each request individually so that they can select that particular request and go to another page.

In other words I want each individual row of data to be individually selectable.

Just wondering if anyone had any suggestions on how I would go about doing this?

Any help would be really appreciated.

Thanks.

 

Hi debuitls,

 

How about something like:

 

$result = mysql_query("SELECT * FROM proposal WHERE username = '$username'");
while($row = mysql_fetch_array($result))
		  echo "<tbody>";
			echo "<tr>";
			  echo "<td><a href="STICK THE LINK HERE">" . $row['county'] . "</a></td>";
			  echo "<td>" . $row['starrating'] . "</td>";
			echo "</tr>";
		  echo "</tbody>";			  

 

This would make the "county" text clickable.  Or, using JS you could make the whole row clickable by:

 

$result = mysql_query("SELECT * FROM proposal WHERE username = '$username'");
while($row = mysql_fetch_array($result))
		  echo "<tbody>";
echo "<script type=\"text/javascript\">
  function DoNav(theUrl)
  {
  document.location.href = theUrl;
  }
  </script>";

			echo "<tr onclick="DoNav('http://STICK THE LINK HERE/');">";
			  echo "<td>" . $row['county'] . "</td>";
			  echo "<td>" . $row['starrating'] . "</td>";
			echo "</tr>";
		  echo "</tbody>";			  

Thanks for getting back to me so quickly!

I suppose I just realised that my question is actually a different one to the one I originally asked.

So all the rows printed to the screen will be links.

And when you press on one of the links it will go to a php page that performs a sql search of the db for more info on that particular proposal.

But I suppose my question is how do you link the particular proposal you press on and the subsequent php page that is returned on pressing the link?

I'm looking for the same effect as www.tenderme.ie when you press the one of the links

 

Hope I haven't confused.

Thanks

 

 

You will want to set a get variable to the primary key of the specific entry of the row. for example, if your primary key was the column "id" you would make the link like:

 

<td><a href=\"data.php?id=" . $row['id'] . "\" >" . $row['county'] . "</a></td>

 

hope that helps!

 

EDIT: sorry forgot the second part

 

on the other page (in my example, data.php) you would do a simple SQL query looking for the row with the ID of $_GET['id'], and voila!

 

Hope that helps!

Hi debuitls,

 

The website you linked to uses the same method as my second code example, with a slight difference in that they don't call a separate JS function, using your code for example it would look like this:

 

$result = mysql_query("SELECT * FROM proposal WHERE username = '$username'");
while($row = mysql_fetch_array($result))
		  echo "<tbody>";
			echo "<tr onclick=\"window.location='/SOME LINK HERE'\">";
			  echo "<td>" . $row['county'] . "</td>";
			  echo "<td>" . $row['starrating'] . "</td>";
			echo "</tr>";
		  echo "</tbody>";			  

 

But I'm guessing you also want to know how to make the link go to the specific data you want to display?  Just create a function which will display the required data via a $_GET, or create a new page which will grab an identifier via a $_GET and then output it the relevant data based on that identifier.

 

To do it using an external page you would do something like:

 

Create a new page called "results.php" (for example) and populate it with the below:

 

<?php

function make_safe($unsafe)
{
require("YOUR CONNECT FILE");
$safe = mysql_real_escape_string(strip_tags(trim($unsafe)));
return $safe;
}

require("YOUR CONNECT FILE");

$id = make_safe($_GET['id']);

$sql = mysql_query("SELECT * FROM YOUR_TABLE WHERE id = '".$id."' ORDER BY id");
	while ($a=mysql_fetch_array($sql)) 
	{
echo $a['name'];
echo $a['email'];
etc, etc.
	}
	}

 

Then, change your other code to:

 

$result = mysql_query("SELECT * FROM proposal WHERE username = '$username'");
while($row = mysql_fetch_array($result))
		  echo "<tbody>";
			echo "<tr onclick=\"window.location='/results.php&id=$row['id']'\">";
			  echo "<td>" . $row['county'] . "</td>";
			  echo "<td>" . $row['starrating'] . "</td>";
			echo "</tr>";
		  echo "</tbody>";			  

 

 

 

 

The above is very basic code but should give you an idea of how to achieve what you want.

 

Hope this helps.

Thanks for your help BrickTop!

I have altered the code as you have suggested however when i go to load the first page which has the code

while($row = mysql_fetch_array($result))
		  

		  {
			echo "<tbody>";
			  
	echo "<tr onclick=\"window.location='/results.php&id=$row['[proposalid']'\">";

			  echo "<td>" . $row['county'] . "</td>";
			  echo "<td>" . $row['starrating'] . "</td>";
			echo "</tr>";
			echo "</tbody>";
		  		 
		  }

 

...I'm only getting a blank screen. If I remove the echo "<tr onclick=\"window.location='/results.php&id=$row['[proposalid']'\">"; line the data is printed out as normal. So i'm thinkin there's a small syntax error im missing but havent been able to find it.

 

I also tried mikesta707 suggestion but got a similar blank screen when i loaded the page.

 

Not sure what im doing wrong. Just wondering if anyone has any suggestions?

you have an extra square bracket around your $row array's key.

 

this

\"window.location='/results.php&id=$row['[proposalid']'\"

should be

\"window.location='/results.php&id=$row['proposalid']'\"

 

Hope that helps!

 

EDIT: another thing, you must escape the single quotes, or concatenate the array entry to the string.

 

\"window.location='/results.php&id=$row[\'proposalid\']'\"

 

or

 

\"window.location='/results.php&id=" . $row['proposalid'] . "'\"

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.