echo table with link?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

echo table with link?

Post by ianhull »

Hi guys, I am still trying to learn this php and I would apreciate any help.

I would like to display my data in a html table with one of the columns being a link.

All my variables are

$companyname = "whatever"; // would like this to be a link to nextpage.php
$address = "whatever";
$telephone = "whatever";
$type = "whatever";

I have tried:

echo "<table><td><a href="nextpage.php">$companyname</a></td></table>" but it does not seem to work.

Any ideas on how I can do this.

My database query has a limit of 30 and I would like to display 30 if possible.

thanks in advance.
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Watch out how you use single and double marks:

Will Cause an error because you start your string with a " and yo have a " around your link.

Code: Select all

echo "<table><td><a href="nextpage.php">$companyname</a></td></table>";
This can be fixed by escaping the quote:

Code: Select all

//Notice the \ before the "
echo "<table><td><a href=\"nextpage.php\">$companyname</a></td></table>";
Or the way I would do it:

Code: Select all

echo '<table><td><a href="nextpage.php">' . $companyname . '</a></td></table>';
You don't have to escape the double quotes if the string is wrapped in single quotes. (Or visa versa) Also, you'll see that I parsed my variable with a period (You can't imbed variables into strings wrapped with single quotes). Either way will work for you.
Last edited by hawleyjr on Fri Aug 12, 2005 8:15 am, edited 2 times in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I don't really see how that doesn't work, assuming pretty basic php scripting..
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

Post by ianhull »

Thank you very much for the quick reply ad advise.

Very much appreciated.

Kind regards :D
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

Post by ianhull »

Hi Again.

Can anyone please give me a hint on how to display this for multiple records?

Thanks

Here is what I have upto now but I would like to display more than one record.

Code: Select all

<?php
include_once("connect.php");

$sql = "SELECT DISTINCT companyname, description, address, phone, fax, type, postcode from companies WHERE companyname='$_REQUEST[form1]' ORDER BY postcode";

$result = mysql_query($sql)
 or die ("Couldn't select $_REQUEST[form1]");

echo "

\n";

while ($line = mysql_fetch_array($result))    
      {
      extract($line);      // extracts all line into variables with same name as fields 
      }
echo "\n";

echo '<table width="100%"  border="0" cellspacing="0" cellpadding="0">
  <tr>
    <th width="6%" height="26" scope="col"><div align="left"></div></th>
    <th width="59%" valign="top" scope="col"><div align="left"><a href="nextpage.php">' . $companyname . '</div></th>
    <th width="35%" scope="col"><div align="left">$phone</div></th>
  </tr>
  <tr>
    <th scope="row"><div align="left"></div></th>
    <th valign="top" scope="row"><div align="left">$description</div></th>
    <th scope="row"><div align="left"></div></th>
  </tr>
  <tr>
    <th scope="row"><div align="left"></div></th>
    <th valign="top" scope="row"><div align="left"></div></th>
    <th scope="row"><div align="left"></div></th>
  </tr>
</table>';

?>

feyd | please post php code appropriately in

Code: Select all

tags.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

move your echo's into the loop.
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

Post by ianhull »

Thanks for that, It works fine but do you have any ideas why the fax and phone numbers are not printed?

Thanks.

Code: Select all

<?php
include_once("connect.php");

$sql = "SELECT DISTINCT companyname, address, phone, fax, type, postcode from companies WHERE companyname='$_REQUEST[form1]' ORDER BY postcode";

$result = mysql_query($sql)
 or die ("Couldn't select $_REQUEST[form1]");

echo "

\n";

//-------------get each event type ------

while ($line = mysql_fetch_array($result))    
      {
      extract($line);
      echo '<table width="100%"  border="0" cellspacing="0" cellpadding="0">
  <tr>
    <th width="6%" height="26" scope="col"><div align="left"></div></th>
    <th width="59%" valign="top" scope="col"><div align="left"><a href="nextpage.php">' . $companyname . '</div></th>
    <th width="35%" scope="col"><div align="left"><?php echo $phone; ?></div></th>
  </tr>
  <tr>
    <th scope="row"><div align="left"></div></th>
    <th valign="top" scope="row"><div align="left"><?php echo $fax;?></div></th>
    <th scope="row"><div align="left"></div></th>
  </tr>
  <tr>
    <th scope="row"><div align="left"></div></th>
    <th valign="top" scope="row"><div align="left"></div></th>
    <th scope="row"><div align="left"></div></th>
  </tr>
</table>';      // extracts all line into variables with same name as fields 
      }




?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you're using a single quote string. Single quote strings do not parse variables inside of them. As with $companyname, break the other variables out of the string.
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

Post by ianhull »

Thanks for the quick response but I am not sure what you mean about breaking them of of the string.

Could you provide me with a little sample?

Thanks.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

change:

Code: Select all

<?php echo $phone; ?>
to

Code: Select all

' . $phone . '
note the single quotes..
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

Post by ianhull »

You are a star!

Thank you very much for your help.

Very much appreciated.
Post Reply