Jump to content

Archived

This topic is now archived and is closed to further replies.

slashpine

need assistance with variable in a URL

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...?




Share this post


Link to post
Share on other sites
Yes it is.

[code]<?php

while ($row = mysql_fetch_assoc($result)) {
    echo '<a href="foo.php?=' . $row['id'] . '">Clicky clicky</a>' . "\n";
}

?>[/code]

Share this post


Link to post
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

Share this post


Link to post
Share on other sites
why store all that in the database?

Just echo out the link as jenk suggests...

Share this post


Link to post
Share on other sites
can you tell me what to put in the database cell so that when results of that particular row are displayed it displays a link that executes another query based on the id of the row that the link originall resided?

Share this post


Link to post
Share on other sites
[code]<?php

$query = "SELECT * FROM `yourtable` WHERE `id` = '" . mysql_real_escape_string($_GET['query']) . "'";

?>[/code]

Share this post


Link to post
Share on other sites
that does not do anything...

I need something that is going to put a hyperlink in the initial results display

is this possible?

Share this post


Link to post
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.

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites
something to that effect is what I am looking for but that does not work it just prints "...=$id"

it does not parse the actual row id...I have also tried using

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

nothing works...that is why I am here...

Share this post


Link to post
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?


Share this post


Link to post
Share on other sites
no I don't think it does... whatever I put into the cell that gest displayed  running the original query has to start with " <a herf="  to get displayed as a hyperlink...

how do I ge the this to get the row id ?

Share this post


Link to post
Share on other sites
Ohh so you dont wont to echo it, just save it into the array?

$i=-1;
while{$i < $number_of_loops_you_want){
  $i++; 
  $id[$i]="<a href=\"http://foo.com/foo.php?query=$value\">click here</a>"}

Like that?

Share this post


Link to post
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.

;)

Share this post


Link to post
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.

Share this post


Link to post
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!  ;)

Share this post


Link to post
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...

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
sorry but I can't make any of that work (too many errors)

also the "link" does not parse the row id it only prints :

?id=id

Share this post


Link to post
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.

Share this post


Link to post
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?

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

×

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.