Page 1 of 1
echo table with link?
Posted: Fri Aug 12, 2005 8:09 am
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.
Posted: Fri Aug 12, 2005 8:13 am
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.
Posted: Fri Aug 12, 2005 8:14 am
by feyd
I don't really see how that doesn't work, assuming pretty basic php scripting..
Posted: Fri Aug 12, 2005 8:15 am
by ianhull
Thank you very much for the quick reply ad advise.
Very much appreciated.
Kind regards

Posted: Fri Aug 12, 2005 8:36 am
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
Posted: Fri Aug 12, 2005 8:39 am
by feyd
move your echo's into the loop.
Posted: Fri Aug 12, 2005 9:00 am
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
}
?>
Posted: Fri Aug 12, 2005 9:02 am
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.
Posted: Fri Aug 12, 2005 9:07 am
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.
Posted: Fri Aug 12, 2005 9:19 am
by feyd
change:
to
note the single quotes..
Posted: Fri Aug 12, 2005 9:21 am
by ianhull
You are a star!
Thank you very much for your help.
Very much appreciated.