Jump to content

Getting data by its ID


divinedesigns1

Recommended Posts

ok, i still dont get how you can use the <a> tag to redirect to a page which is calling upon a certain data by its id. i been at this since, i think 2 weeks now, still dont get it. i did the usual thing <a href="display.php?id='$id'"> -_- </a> but that didnt work. i even use another query to select it from its id

$query = select from disdis(dis_name, dis_that, dis_this, dis_date order by dis_date(date, %m %d %y) desc where $id=id) or die('did not work grrrrrrrr');

so can someone kindly explain it to me fully -_- if you have time that is

Link to comment
Share on other sites

Why do you want to use an id? <a> will take you anywhere you want to go in or outside a page without an id.

Edited by niche
Link to comment
Share on other sites

Why do you want to use an id? <a> will take you anywhere you want to go in or outside a page without an id.
the id is meant to be used with a database. For instance a list of products, would take you a products page, with the id retrieving that specific product from the table, filtered by that ID. the issue I see is that the value id should actually be a number, and you seem to have it backwards
"where id=$id"

the sql would be something like

"where id=1"

when you display the links, in the source it should look something like this

<a href="display.php?id=1">Thing 1</a><a href="display.php?id=2">Thing 2</a><a href="display.php?id=3">Thing 3</a>

Link to comment
Share on other sites

So if you clicked "Thing 1" you'd be taken to the page that lists all the rows "where id=1"?

Link to comment
Share on other sites

Guest So Called
i did the usual thing <a href="display.php?id='$id'"> -_- </a> but that didnt work.
"Didn't work" is not an adequate explanation of how it failed.
Link to comment
Share on other sites

So if you clicked "Thing 1" you'd be taken to the page that lists all the rows "where id=1"?
yes. I'm assuming that is what the OP is trying to do. It's a pretty standard way of development, to have a template page that gets populated based on some sort of parameter or filter.
Link to comment
Share on other sites

Then you get the id from the $_GET array. In this case, $_GET['id'], right?

Link to comment
Share on other sites

Guest So Called

That's funny, I would assume there's only one row with id=1, and that the resultant page will be filled with data from that row. I guess we won't know unless the OP tells us. The OP should type the link produced from <a href="display.php?id='$id'"> directly into a browser, to determine if it's the link that's failing or display.php that's failing. In other words divide the problem in two.

Edited by So Called
Link to comment
Share on other sites

Funny? I probably agree though I'm always amused to think about code in ways I've not thought of before. This is another one of those times for me and the second time just today.

Link to comment
Share on other sites

the id is meant to be used with a database. For instance a list of products, would take you a products page, with the id retrieving that specific product from the table, filtered by that ID. the issue I see is that the value id should actually be a number, and you seem to have it backwards
"where id=$id"

the sql would be something like

"where id=1"

when you display the links, in the source it should look something like this

<a href="display.php?id=1">Thing 1</a><a href="display.php?id=2">Thing 2</a><a href="display.php?id=3">Thing 3</a>

but isnt there a way to automatically obtain the id without putting in id number manually? since i believe that would be better.
Link to comment
Share on other sites

I think scientist is saying instead of this:

$query = select from disdis(dis_name, dis_that, dis_this, dis_date order by dis_date(date, %m %d %y) desc where $id=id) or die('did not work grrrrrrrr');

Change it to this: $query = select from disdis(dis_name, dis_that, dis_this, dis_date order by dis_date(date, %m %d %y) desc where id=$id) or die('did not work grrrrrrrr'); I believe that's what he meant when he said it seems you have it backwards. I also noticed you forgot to surround the query in quotes?(Just mentioning) Yes you can grab the id's automatically and place them in the links within the while loop:

$mysqli = new mysqli("localhost", "my_user", "my_password", "db");$result = $mysqli->query("SELECT id FROM table");  while ($row = $result->fetch_assoc()){	 echo '<a href="display.php?id='. $row[id] . '">something here</a>';}$mysqli->close();

Then on the display.php page, use $_GET to retrieve the id and then use it to query the database table.

$id = $_GET['id']; $query = "SELECT product_title, product_descrip WHERE id = $id";  // just an example 

Edited by Don E
Link to comment
Share on other sites

I think scientist is saying instead of this:
$query = select from disdis(dis_name, dis_that, dis_this, dis_date order by dis_date(date, %m %d %y) desc where $id=id) or die('did not work grrrrrrrr');

Change it to this: $query = select from disdis(dis_name, dis_that, dis_this, dis_date order by dis_date(date, %m %d %y) desc where id=$id) or die('did not work grrrrrrrr'); I believe that's what he meant when he said it seems you have it backwards. I also noticed you forgot to surround the query in quotes?(Just mentioning) Yes you can grab the id's automatically and place them in the links within the while loop:

$mysqli = new mysqli("localhost", "my_user", "my_password", "db");$result = $mysqli->query("SELECT id FROM table");  while ($row = $result->fetch_assoc()){	 echo '<a href="display.php?id='. $row[id] . '">something here</a>';}$mysqli->close();

Then on the display.php page, use $_GET to retrieve the id and then use it to query the database table.

$id = $_GET['id']; $query = "SELECT product_title, product_descrip WHERE id = $id";  // just an example 

:wub: i love you, thats what i was doing wrong all this time, on the home page, i did not had the id in the query also "dont think this part matters" but in the url i had <?php echo $id; ?> so when i change it to what you said by adding the id to the query and removing the php from the url and placing the $row[id] it worked :D woot woot. thanks don E and thesci. oo yeah in the original script, i had placed the select statement in the quotes :D good now i know how to use that woot woot hehehehehehehe. :blink:the one error ill figure it out :Dmany thanks and i greatly appreciate all the help
Link to comment
Share on other sites

correct me if im wrong, im just making sure i fully understand, what needs to be done when your trying to get something to display by its id. so if i want to display anything by its id only through a url i need to make sure i have the id include in the query and also have a fetch statement and in the url itself i need to call on the id by using the $row[id]/$row['id'] and on the play you want the id to be display, you need to have a $_get variable which defines the id to help get that id to display on that page correct? sorry if im asking too much question, just want to make sure i understand it fully, so i dont have to ask for the same help again.

Link to comment
Share on other sites

it does not limited to $_GET it could be $_POST or may be $_COOKIE or may be $_SESSION. the main idea is that where you will pull data by its id you need to pass an id value in it to run the query.here you are using $_GET because you are passing the id via URL. it does not matter how your GET query string prepared (auto generated or manually inserted). you have to just pass it to other page.to be clear

i need to call on the id by using the $row[id]/$row['id']
$row[id] and $row['id'] is not same. when you use associative array you have to quote its the key. other wise it will be treated as constant and if constant does not exist it will evaluate its literal value which is same as "id" but will throw a error.
Link to comment
Share on other sites

$row[id] and $row['id'] is not same. when you use associative array you have to quote its the key. other wise it will be treated as constant and if constant does not exist it will evaluate its literal value which is same as "id" but will throw a error.
so thats why i got an error when i didnt put quote the id, ok i got it now, ill write this down. thanks birbal
Link to comment
Share on other sites

Yes, my mistake.. I forgot to quote that in my example. It should be $row['id']; within the while loop. Correction:

while ($row = $result->fetch_assoc()){         echo '<a href="display.php?id='. $row['id'] . '">something here</a>';}

Link to comment
Share on other sites

Yes, my mistake.. I forgot to quote that in my example. It should be $row['id']; within the while loop. Correction:
while ($row = $result->fetch_assoc()){		 echo '<a href="display.php?id='. $row['id'] . '">something here</a>';}

yeah i fixed it because it was a weird error, the first link give out the error and the second link was ok, so i read the error and changed it. thanks again don e
Link to comment
Share on other sites

  • 1 year later...

This is an old topic, but it's what I've been looking for.

 

I have a question: on my "display.php" page, how do I, for example, make a <h2> element saying:

<h2>"display id"</h2>

In my case, I have a "viewclients.php" file that I want to pull information from depending on the certain client you clicked from the previous page. My <h2> element keeps returning all client names, when I want it to return only the name of the client based on the id in the url... does that make sense?

Link to comment
Share on other sites

Alright, I have two pages. "resultsClients.php" is the page that lists all clients in the db with links to the file "viewclient.php" that displays details about the specified client. I've been working on redoing some naming conventions, like file names, to make more sense. Anyway, here comes the code...

 

resultsClients.php:

<body><div id="container">	<!-- Include Navigation -->	<?php include ('includes/header.php'); ?>	<!-- Include Navigation -->	<?php include ('includes/navigation.php'); ?><h2>Clients</h2><?phpecho "<table><tr><th>Client</th></tr>";$result = mysqli_query($con,"SELECT * FROM clients");while($row = mysqli_fetch_array($result)){	echo "<tr>";	echo '<td><a href="viewclient.php?id='.$row['clientid'].'">'.$row['name'].'</a></td>';	echo "</tr>";}echo "</table>";?></div><!-- / main container -->

I've made a config.php file located in an "includes" dir for handing db connection. In the db there is a table called "clients" with two columns for now: "clientid" and "name". the first column is an auto-incremented primary key.

 

Here is viewclient.php:

<div id="container">	<!-- Include Navigation -->	<?php include ('includes/header.php'); ?>	<!-- Include Navigation -->	<?php include ('includes/navigation.php'); ?>	<?php		$result = mysqli_query($con,"SELECT DISTINCT name FROM clients");		while($row = mysqli_fetch_array($result))		{			echo '<h2>'.$row['name'].'</h2>';		}	?></div><!-- /container -->

In this page I know my $result is wrong for what I want to do, it's retrieving all the client names and basically forming a list of <h2>'s with all the client names. How do I make $result grab the clientid that is in the page url? I'm guessing you don't actually tell your PHP code to grab the id from the url, but that's the best way I can explain what I want.

Link to comment
Share on other sites

In this page I know my $result is wrong for what I want to do, it's retrieving all the client names and basically forming a list of <h2>'s with all the client names. How do I make $result grab the clientid that is in the page url? I'm guessing you don't actually tell your PHP code to grab the id from the url, but that's the best way I can explain what I want.

 

actually, you dohttp://www.w3schools.com/php/php_forms.asp

$_GET is an array of variables passed to the current script via the URL parameters.

Link to comment
Share on other sites

Thanks for the help so far.

 

But I'm having trouble with those links you gave me. They are dealing with static information, how do I deal with the id in the url changing?

 

Here's what I have:

	<?php		$id = $_GET['clientid'];		$result = mysqli_query($con,"SELECT name FROM clients WHERE clientid=$id");				while($row = mysqli_fetch_array($result))		{			echo '<h2>'.$_GET['$id'].$row['name'].'</h2>';		}	?>

The page returns this error:

 

Notice: Undefined index: clientid in C:...viewclient.php on line 25Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:xampphtdocsdsi-logviewclient.php on line 28

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...