Jump to content


Photo

need assistance with variable in a URL


  • Please log in to reply
23 replies to this topic

#1 slashpine

slashpine
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts

Posted 17 August 2006 - 02:52 PM

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






#2 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 17 August 2006 - 03:07 PM

Yes it is.

<?php

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

?>


#3 slashpine

slashpine
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts

Posted 17 August 2006 - 03:12 PM

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: <a href="h**p://foo.com/foo.php?query=$id">click here</a>

but this does not work...it does not capture the row id

#4 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 17 August 2006 - 03:15 PM

why store all that in the database?

Just echo out the link as jenk suggests...
follow me on twitter @PHPsycho

#5 slashpine

slashpine
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts

Posted 17 August 2006 - 03:36 PM

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?

#6 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 17 August 2006 - 03:39 PM

<?php

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

?>


#7 slashpine

slashpine
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts

Posted 17 August 2006 - 03:53 PM

that does not do anything...

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

is this possible?

#8 slashpine

slashpine
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts

Posted 17 August 2006 - 04:07 PM

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.



#9 slashpine

slashpine
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts

Posted 17 August 2006 - 04:43 PM

so this is not possible? >:(

thanks for the efforts anyway

#10 akrytus

akrytus
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts

Posted 17 August 2006 - 05:00 PM

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



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

Is that what you are looking for?

#11 slashpine

slashpine
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts

Posted 17 August 2006 - 05:24 PM

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

#12 akrytus

akrytus
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts

Posted 17 August 2006 - 05:35 PM

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


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:

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

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?




#13 slashpine

slashpine
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts

Posted 17 August 2006 - 05:48 PM

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 ?

#14 akrytus

akrytus
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts

Posted 17 August 2006 - 06:00 PM

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?

#15 HeyRay2

HeyRay2
  • Members
  • PipPipPip
  • Advanced Member
  • 223 posts

Posted 17 August 2006 - 06:18 PM

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.

;)

#16 slashpine

slashpine
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts

Posted 17 August 2006 - 06:49 PM

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.



#17 HeyRay2

HeyRay2
  • Members
  • PipPipPip
  • Advanced Member
  • 223 posts

Posted 17 August 2006 - 07:17 PM

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

companies
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:

company_list.php
<?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());
?>

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

company_list.php continued....
<?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>';
?>

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.

company_detail.php
<?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>';
?>

That should get you the basic functionality. Now you can pretty it up and make it your own!  ;)

#18 slashpine

slashpine
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts

Posted 17 August 2006 - 07:58 PM

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

#19 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 17 August 2006 - 08:14 PM

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.


Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#20 slashpine

slashpine
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts

Posted 17 August 2006 - 08:49 PM

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users