Board index   FAQ   Search  
Register  Login
Board index PHP PHP General

Hyperlink Variables in PHP?

General discussions related to php

Moderators: macek, egami, gesf

Hyperlink Variables in PHP?

Postby chris122380 » Mon Jul 16, 2012 12:05 pm

I am polling a query from my database. It displayed correctly. I am trying to now get it to auto hyperlink so that each spec field is linked to a file name with the same spec number. Each file is a pdf.
Code: Select all
WHILE ($rows = mysql_fetch_array($query)): //Fetch data from $query as long as there is data to fetch

$rows = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $rows);

      // assign fields from the query to Variables
      $first_name = $rows['first_name'];
      $last_name = $rows['last_name'];
      $company = $rows['company'];
      $spec = $rows['spec'];
      $discription = $rows['discription'];
   
   //   echo "<a href="spec/$spec.pdf">$spec&nbsp;$discription</a>
   //   <br />";
   // Trying to link each line to spec.pdf were the spec is the corresponding spec number.    
      echo "<a href='spec/$spec.pdf'>$spec&nbsp;$discription</a><br />"

   endwhile;

?>


How can I get a link to use a variable in the link name as well description of the link and make a new line after each new link?
chris122380
New php-forum User
New php-forum User
 
Posts: 30
Joined: Mon Jun 25, 2012 12:41 pm

Re: Hyperlink Variables in PHP?

Postby johnj » Tue Jul 17, 2012 6:56 am

Give this a try:

change echo "<a href='spec/$spec.pdf'>$spec&nbsp;$discription</a><br />"

to

echo '<a href="spec/$spec.pdf">'.$spec.'&nbsp;'.$discription.'</a><br />';
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: Hyperlink Variables in PHP?

Postby chris122380 » Tue Jul 17, 2012 7:06 am

johnj wrote:Give this a try:

change echo "<a href='spec/$spec.pdf'>$spec&nbsp;$discription</a><br />"

to

echo '<a href="spec/$spec.pdf">'.$spec.'&nbsp;'.$discription.'</a><br />';


echo "<a href='spec/$spec.pdf'>$spec&nbsp;$discription</a><br />"
Parse error: syntax error, unexpected T_ENDWHILE, expecting ',' or ';' in \test_iFrame\Spec List.php on line 26

echo '<a href="spec/$spec.pdf">'.$spec.'&nbsp;'.$discription.'</a><br />';
doesn't insert the variable as the link it inserts exactly the text so the link goes to /test_iFrame/spec/$spec.pdf

I have named my PDF files the same as the spec field so I would like the loop to open the spec.pdf based on whatever query I decide to run.
chris122380
New php-forum User
New php-forum User
 
Posts: 30
Joined: Mon Jun 25, 2012 12:41 pm

Re: Hyperlink Variables in PHP?

Postby tkdbb84 » Tue Jul 17, 2012 1:28 pm

try:

Code: Select all

echo 
'<a href="spec/',$spec,'.pdf">',$spec,'&nbsp;',$discription,'</a><br/>';
 
tkdbb84
New php-forum User
New php-forum User
 
Posts: 137
Joined: Tue Mar 08, 2011 9:25 am

Re: Hyperlink Variables in PHP?

Postby chris122380 » Tue Jul 17, 2012 1:41 pm

tkdbb84 wrote:try:

Code: Select all

echo 
'<a href="spec/',$spec,'.pdf">',$spec,'&nbsp;',$discription,'</a><br/>';
 


Looks a little better but not working yet.
This is what it looks like
http://localhost:10955/test_iFrame/spec ... %20%20.pdf

I took out the spec/ and I get
http://localhost:10955/test_iFrame/spec ... %20%20.pdf

This is what it should look like
http://localhost:10955/test_iFrame/spec ... 000%20.pdf

Where is the extra %20 coming from?
The file name is the same as the spec number that is in the database.
chris122380
New php-forum User
New php-forum User
 
Posts: 30
Joined: Mon Jun 25, 2012 12:41 pm

Re: Hyperlink Variables in PHP?

Postby tkdbb84 » Tue Jul 17, 2012 1:57 pm

can you post the whole code snip?
tkdbb84
New php-forum User
New php-forum User
 
Posts: 137
Joined: Tue Mar 08, 2011 9:25 am

Re: Hyperlink Variables in PHP?

Postby chris122380 » Tue Jul 17, 2012 2:48 pm

tkdbb84 wrote:can you post the whole code snip?

Code: Select all
<?php
include('../conect.php');

$query = mysql_query("SELECT `contacts`.`first_name`, `contacts`.`last_name`, `contacts`.`company`, `spec`.`spec`, `spec`.`discription` FROM `canthony`.`project_spec` AS `project_spec`, `canthony`.`project` AS `project`, `canthony`.`spec` AS `spec`, `canthony`.`contacts` AS `contacts` WHERE `project_spec`.`Project_ID` = `project`.`Project_ID` AND `project_spec`.`spec` = `spec`.`spec`"); //query the database

//fetch the results and convert results into an array
WHILE ($rows = mysql_fetch_array($query)): //Fetch data from $query as long as there is data to fetch

$rows = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $rows);

      // assign fields from the query to Variables
      $first_name = $rows['first_name'];
      $last_name = $rows['last_name'];
      $company = $rows['company'];
      $spec = $rows['spec'];
      $discription = $rows['discription'];
   
   //   echo "<a href="spec/$spec.pdf">$spec&nbsp;$discription</a>
   //   <br />";
      
echo '<a href="spec/',$spec,'.pdf">',$spec,'&nbsp;',$discription,'</a><br/>';      
      
//   echo "<a href=\"$spec.pdf\">$spec&nbsp;$discription</a><br />";




   endwhile;

?>
chris122380
New php-forum User
New php-forum User
 
Posts: 30
Joined: Mon Jun 25, 2012 12:41 pm

Re: Hyperlink Variables in PHP?

Postby tkdbb84 » Tue Jul 17, 2012 3:21 pm

code looks good at glance; your query could use an optimization, but nothing that would effect the output. It looks like there is an extra space in the name from the database, maybe trim using

Code: Select all
$spec trim($rows['spec']); 


although I admit I'm terrible with regex so I'm not sure what this:
Code: Select all
$rows preg_replace('/[\x00-\x1F\x80-\xFF]/'''$rows); 

is doing or why; but I don't think it would add %20 or spaces anywhere....

beyond that i'd check the database entry to make sure there are no extra spaces or %20 in the database itself

sorry I can't be much more help then that :-\
tkdbb84
New php-forum User
New php-forum User
 
Posts: 137
Joined: Tue Mar 08, 2011 9:25 am

Re: Hyperlink Variables in PHP?

Postby chris122380 » Wed Jul 18, 2012 5:53 am

Code: Select all
$rows = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $rows); 
is supposed to remove all non print friendly characters. I commented it out and put in the
Code: Select all
$spec = trim($rows['spec']); 
in it's place. I am still getting the same results. There are no extra spaces in the database entry they are exactly the same as the file name. Is there a way to strip all spaces from a string/Variable?
chris122380
New php-forum User
New php-forum User
 
Posts: 30
Joined: Mon Jun 25, 2012 12:41 pm

Re: Hyperlink Variables in PHP?

Postby chris122380 » Fri Jul 20, 2012 1:36 pm

I removed the spaces in the file names and then I used
Code: Select all
$spec = str_replace (" ", "", $spec);
to remove the spaces from the $spec variable/string and now the
Code: Select all
echo '<a href="spec/',$spec,'.pdf">',$spec,'&nbsp;',$discription,'</a><br/>';
works.

Now how do I change the link to open in a iframe of a diffident page. Example Index.php has a iframe on the right that the spec.php loads in (that have my links) and a iframe on the right side that I would like the PDF files to open in.
chris122380
New php-forum User
New php-forum User
 
Posts: 30
Joined: Mon Jun 25, 2012 12:41 pm

Re: Hyperlink Variables in PHP?

Postby chris122380 » Wed Jul 25, 2012 12:46 pm

Was easier to place the hyperlink in HTML and then make some small echo statements
Code: Select all
<?php echo $spec or $_$SESSION['spec'] ?>
in the link.

Example of a hyperlink in HTML using PHP session variables:
Code: Select all
<body>
<a href="spec/<?php echo $_SESSION['spec']; ?>.pdf" target="pdf"> <?php echo $_SESSION['spec'],'&nbsp;',$_SESSION['discription'], '<br />'; ?>   </a>
<?php endwhile; ?>
</body>
chris122380
New php-forum User
New php-forum User
 
Posts: 30
Joined: Mon Jun 25, 2012 12:41 pm


Return to PHP General

Who is online

Users browsing this forum: No registered users and 0 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron