Using a record for a link
Posted: Fri Nov 20, 2009 1:45 pm
Any help is appreciated! The user enters data into the database including a url and a name for that URL. For example: One might enter http://www.google.com for the URL and then "Visit Google" for the name. Once queried I want "Visit Google" to show up and when clicked it will take them to the link. I'm really close I know it. So far the code below produces what I need except rather than showing the Name it shows the link and then links the same. I know I need to define a new variable that is the Name, but I'm not sure how to do it. So the code below produces <a href="http://www.google.com">http://www.google.com</a>
Code: Select all
<html>
<head>
<title></title>
</head>
<body>
<?php
$con = mysql_connect('localhost','username','password');
if (!$con){
die('Could not connect: ' . mysql_error());
}
$varlength = mysql_real_escape_string($_POST['varlength']);
$varwidth = mysql_real_escape_string($_POST['varwidth']);
$vardepth = mysql_real_escape_string($_POST['vardepth']);
mysql_select_db("database", $con); //Replace with your MySQL DB Name
if(isset($_POST["varmargin"]))
$selection = $_POST["varmargin"];
else
$selection = null;
switch ($selection)
{
case "0":
$query = "SELECT Link, Length, Width, Depth, Product, Description FROM allcases WHERE ($varlength) <= Length AND Length <= ($varlength + 0) AND ($varwidth) <= Width AND Width <= ($varwidth + 0) AND ($vardepth) <= Depth AND Depth <= ($vardepth + 0) ORDER BY Length ASC";
break;
case "All":
$query = "SELECT Link, Length, Width, Depth, Product, Description FROM allcases ORDER BY Length ASC";
break;
default:
echo 'Nothing to do here';
}
$q = mysql_query($query)
or die("This is not a valid query: " . $query);
$fieldCount = mysql_num_fields($q);
echo "<b>Search Cases by Dimension</b>.<br />
All the cases below match your search in order of accuracy<br />
Length= Left to Right, Width= Front to Back, Depth= Top to Bottom<br /><br />
<table border='1'>
<tr>";
// print table headers
for($i=0; $i<$fieldCount; $i++)
{
$field = mysql_fetch_field($q);
echo "<th>{$field->name}</th>";
}
echo "</tr>\n";
// Print table rows
while($record = mysql_fetch_row($q))
{
echo "<tr>";{
[b] foreach ($record as $field => $cell) {
if ($field == "Link") {
echo "<td><a href=$cell>$cell</a></td>";
}
else {
echo "<td>$cell</td>";
}[/b]
}
}
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
</body>
</html>