Jump to content

need assistance with variable in a URL


slashpine

Recommended Posts

I am not sure this is doable but then it again it may be easy...that's why I'm here, I don't have a clue...

I am looking for a way to create (syntax) a variable (row id) in a generic link that can be placed in a database column that when displayed (in a browser) with the other columns in any particular row...the link will end up looking something like ths:

../foo.php?query=123 

Where "123" is the id for that particular row.

i.e., I want to be able to insert the the same "link" in all the rows but in the returned display they all need to link to
../foo.php?query=$id  (where '$id' will be the actual row id )

hope this is doable...?




Link to comment
Share on other sites

thanks for the reply...

what I am looking for the is the exact syntax of the "link" that gets placed in the database cell...i.e:

I thought it should look something like this: [b]<a href="h**p://foo.com/foo.php?query=$id">click here</a>[/b]

but this does not work...it does not capture the row id
Link to comment
Share on other sites

thanks for the replies...

I probably did not do a good job of citing what I need here...

I don't want to have to alter any existing php scripts that presently query my db... I am looking for something (described above) that can be placed in a cell of  db that will reflect the id of the row in which it resides.... when displayed, the row id is parsed into the "link"

again...I am not looking for a php script... I am looking for the correct syntax (if it exists) of a hyperlink that will do as said.

Link to comment
Share on other sites

[quote]I thought it should look something like this: <a href="h**p://foo.com/foo.php?query=$id">click here[/url]
[/quote]


syntax using php:
[code]
echo "<a href=\"http://foo.com/foo.php?query=$id\">click here</a>"[/code]

Is that what you are looking for?
Link to comment
Share on other sites

[quote]...foo.php?query=.$id['id'].">click here[/url]"
[/quote]

Yeah I thought about that earlier. You use the wrong syntax, no quotes inside the array $id[''], you do indeed need the brackets, you were missing them earlier, although I wouldnt use brackets here becuase it is easier to parse the info out yourself, like:

[code]
foreach ($id as $value){
  echo "<a href=\"http://foo.com/foo.php?query=$value\">click here</a>"}
[/code]

This will find each element of the array $id and parse it out into a new single variable $value.  Then you can use it as you were.

Does that help?


Link to comment
Share on other sites

Can you post the code you use to query your database for your results.

That will help us better understand what information you are looking to print out for each result, and then we can make suggestions on how to format that result printing for a hyperlink.

;)
Link to comment
Share on other sites

I am sorry I can't seem to make myself clear on this....

what I am looking for is the HTML/php code that when placed in a database row it will be displayed as link to a script that runs a second query on the same database based on the id of the row that HTML code originall resided...

This (row id) must be a variable so the (HTML/php) code can be placed on every row (50k+) but when displayed will parse the row id ($variable) as unique to the row.

Link to comment
Share on other sites

I think we've got a grasp on what you're trying to do. I just wanted to see the code you are using to query the database, that way I know what data we need to print out. However, let's just make an example database table called "companies".

[b]companies[/b]
company_id
company_name
company_desc
etc...

When we run the first query, we'll just grab the company names and IDs for printing a big list:

[b]company_list.php[/b]
[code]
<?php

// Query for all companies
$sql = "SELECT company_id, company_name FROM companies ORDER BY company_name";
$result = mysql_query($sql) or die("Error Querying Companies: ".mysql_error());
?>
[/code]

Then we'll print out a row for each company, echoing the company name, and using the ID in a hyperlink to a page that will show the details for that specific company only (company_detail.php).

[b]company_list.php[/b] continued....
[code]
<?php

// Print a table for company results
echo '<table border="1" width="100%">
        <tr>
<th>Company Name</th>
<th>Company Details</th>
</tr>';

// Print the results
while($row = mysql_fetch_array($result)){
echo '<tr>
<td>'.$row['company_name'].'</td>
<td><a href="company_detail.php?company_id='.$row['company_id'].'">Details Link</a></td>
      </tr>';
}

// Close the table
echo '</table>';
?>
[/code]

Now that we have our company list page with link to each company, we can make a detail page, and the
links we created will pass in the company ID, so that we can pull information for just that company.

[b]company_detail.php[/b]
[code]
<?php

// Get the company ID from the URL
$company_id = $_GET['company_id'];

// Query for the company details
$sql = "SELECT * FROM companies WHERE company_id = ".$company_id." LIMIT 1";
$result = mysql_query($sql) or die("Error Getting Company Details: ".mysql_error());

// Print the Company Details
echo '<table border="1" width="600">
<tr>
<td>Company: '.$row['company_name'].'</td>
</tr>
<tr>
<td>Company Description: '.$row['company_desc'].'</td>
</tr>
<tr>
<td>Other company info goes here...</td>
</tr>
      </table>';
?>
[/code]

That should get you the basic functionality. Now you can pretty it up and make it your own!  ;)
Link to comment
Share on other sites

I really do appreciate the efforts but this is not what I am seeking....My whole point is not having to edit or ammend the existing query scripts... thus the want for something (hyperlinked text) that I could put into existing row cells that would capture the id of the row in which it resides...and reflect this (unique row id) in the initially displayed link...
Link to comment
Share on other sites

bad advice sorry if going to use the $_GET
// Get the company ID from the URL
$company_id = $_GET['company_id'];

use it this way ok

set the session first

$_SESSION['company_id']=$row['company_id'];

the link
a href="company_detail.php?company_id=company'">Details Link</a>


code correct way

if ($_GET['company_id']=="company")){

$company_id = $_SESSION['company_id'];

}

hope that clears that up ok mate.

Link to comment
Share on other sites

[b]redarrow[/b], there is no reason to pull the company ID into a $_SESSION variable, because the value only needs to exist on [b]ONE[/b] page.

So, apart from me not adding data validation code (which I excluded for reasons of simplicity in my example) [b]no[/b], my suggestion was not bad advice, but commonly used code for this type of situation.

[quote author=slashpine link=topic=104631.msg417613#msg417613 date=1155844726]
I really do appreciate the efforts but this is not what I am seeking....My whole point is not having to edit or ammend the existing query scripts... thus the want for something (hyperlinked text) that I could put into existing row cells that would capture the id of the row in which it resides...and reflect this (unique row id) in the initially displayed link...
[/quote]

[b]slashpine[/b], it seems that we're on different pages here. Can you post the code and queries you currently have so that I can get a better of what data we have to work with, and we can go from there.
Link to comment
Share on other sites

that is my point this quest has nothing to do with the current query scripts... what I am seeking has no bearing on the originating queries...

Right now I have an empty column in an existing tabel row... what I am seeking is the an "<href" string that will parse the actual id of that particular row and return it to the string being displayed in the original results display...

the main question of all this is...How do I get the actual row id from a variable that resides in that row?
Link to comment
Share on other sites

Actually, it has everything to do with your current queries, because I can't help you determine what row data to parse if you can't provide me with what row data you are querying.

Post your query code here so I can what data you are pulling, so then I can tell help you access that data.
Link to comment
Share on other sites

I do appreceiate all the efforts but the fact of the matter is....

what I am seeking  and what I have described in finite detail within this thread could theoretically be used in [u][b]ANY[/b][/u] databse that has an "id" field and one to contain the "link" string...regardless of  any other  fields.

i.e.,...what I asked for is the [u]syntax of a string[/u] that when returned as a query result (regardless of any other columns being queried) so that said string would contain the id of the row where the string originally resided.
Link to comment
Share on other sites

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.