Jump to content

Recommended Posts

Hello everyone,

i'm struggeling with posting a link with variables in a table with info i receive from a database.

 

The variable

$url = "http://mtgimage.com/card/$cardname.jpg";

works but the variable

$cardname = $row['Name'];

doesnt. i dont know what i am doing wrong in that line of code.

my actual goal with this is so when i hover over the card name which i receive from the database it shows the image.

 

This is the actual full file.

<!DOCTYPE html>
<html>
<head>
<title>MTG Card List</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style type="text/css" media="screen">
@import "filtergrid.css";
body{ 
	margin:15px; padding:15px; border:0px solid #666;
	font-family:Arial, Helvetica, sans-serif; font-size:88%; 
}
h2{ margin-top: 50px; }
caption{ margin:10px 0 0 5px; padding:10px; text-align:left; }
pre{ font-size:13px; margin:5px; padding:5px; background-color:#f4f4f4; border:1px solid #ccc;  }
.mytable{
	width:100%; font-size:12px;
	border:1px solid #ccc;
}
th{ background-color:#003366; color:#FFF; padding:2px; border:1px solid #ccc; }
td{ padding:2px; border-bottom:1px solid #ccc; border-right:1px solid #ccc; border-left:1px solid #ccc; border-top:1px solid #ccc; }
body p {
	font-size: 50%;
	color: #000;
}
a:link {
	color: #000;
}
a:visited {
	color: #000;
}
</style>
<script language="javascript" type="text/javascript" src="actb.js"></script>
<script language="javascript" type="text/javascript" src="tablefilter.js"></script>
</head>
<body>


<form align="center" action="password.php" method="POST">
<b>Username:</b> <input type="text" name="username">
<b>Password:</b> <input type="text" name="password">
<input type="submit">
</form>
<?php
$dbhost = 'hostname';
$dbuser = 'user';
$dbpass = 'pw';
$cardname = $row['Name'];
$url = "http://mtgimage.com/card/$cardname.jpg";
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
mysql_select_db('e_industries');
$sql = 'SELECT `Name`, `Color`, `Type`, `Subtype`, `Power`, `Toughness`, `Manacost`, `Rarity`, `Expansion`, `Foil`, `Stock` FROM `Osiris`';


$retval = mysql_query( $sql, $conn );
if(! $retval )
	{
	die('Could not get data: ' . mysql_error());
	}
	echo '<table align="center" id="mytable" cellspacing="0" class="table" >';
	echo "<tr><th >Name</th><th >Color</th><th >Type</th><th >Sub Type</th><th >Power</th><th >Toughness</th><th >Converted mana cost</th><th >Rarity</th><th >Expansion</th><th >Foil</th><th >Stock</th></tr>";
	while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
		{
			echo "<tr><td>";
			echo "<a href=$url>. $cardname .</a>";
			echo "</td><td>";
			echo $row['Color'];
			echo "</td><td>";
			echo $row['Type'];
			echo "</td><td>";
			echo $row['Subtype'];
			echo "</td><td>";
			echo $row['Power'];
			echo "</td><td>";
			echo $row['Toughness'];
			echo "</td><td>";
			echo $row['Manacost'];
			echo "</td><td>";
			echo $row['Rarity'];
			echo "</td><td>";
			echo $row['Expansion'];
			echo "</td><td>";
			echo $row['Foil'];
			echo "</td><td>";
			echo $row['Stock'];
			echo "</td></tr>";
		}
		echo "</table>";
mysql_close($conn);
?>

<script language="javascript" type="text/javascript">
//<![CDATA[
	var table2_Props = 	{
							col_1: "select",
							col_2: "select",
							col_3: "select",
							col_7: "select",
							col_8: "select", 
							col_9: "select",
							col_10: "none",
							display_all_text: " [ Show all ] ",
							sort_select: true 
						};
	setFilterGrid( "mytable",table2_Props );
//]]>
</script>
</body>
</html>

 Thanks in advance!

Link to comment
https://forums.phpfreaks.com/topic/291448-php-variables-in-a-link/
Share on other sites

Besides what Ch0cu3r wrote, also use the single quotes for a hyperlink.

$cardname = $row['Name'];
$url = "http://mtgimage.com/card/" . $cardname . ".jpg";
echo "<a href='" . $url . "'>" . $cardname . "</a>";

I also noticed this.

echo '<table align="center" id="mytable" cellspacing="0" class="table" >';

change to

echo "<table align='center' id='mytable' cellspacing='0' class='table' >";

An easy way to do it is to use all single quotes within and wrapped with double quotes, can wrap variables with curly braces or concatenate

Edited by QuickOldCar

Okay thanks that works,

but now all the other data isnt showing anymore...

Anyone knows what the cause is?

 

This is a link to the actual website

<!DOCTYPE html>
<html>
<head>
<title>MTG Card List</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style type="text/css" media="screen">
@import "filtergrid.css";
body{ 
	margin:15px; padding:15px; border:0px solid #666;
	font-family:Arial, Helvetica, sans-serif; font-size:88%; 
}
h2{ margin-top: 50px; }
caption{ margin:10px 0 0 5px; padding:10px; text-align:left; }
pre{ font-size:13px; margin:5px; padding:5px; background-color:#f4f4f4; border:1px solid #ccc;  }
.mytable{
	width:100%; font-size:12px;
	border:1px solid #ccc;
}
th{ background-color:#003366; color:#FFF; padding:2px; border:1px solid #ccc; }
td{ padding:2px; border-bottom:1px solid #ccc; border-right:1px solid #ccc; border-left:1px solid #ccc; border-top:1px solid #ccc; }
body p {
	font-size: 50%;
	color: #000;
}
a:link {
	color: #000;
}
a:visited {
	color: #000;
}
</style>
<script language="javascript" type="text/javascript" src="actb.js"></script>
<script language="javascript" type="text/javascript" src="tablefilter.js"></script>
</head>
<body>


<form align="center" action="password.php" method="POST">
<b>Username:</b> <input type="text" name="username">
<b>Password:</b> <input type="text" name="password">
<input type="submit">
</form>
<?php
$dbhost = '';
$dbuser = '';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
mysql_select_db('e_industries');
$sql = 'SELECT `Name`, `Color`, `Type`, `Subtype`, `Power`, `Toughness`, `Manacost`, `Rarity`, `Expansion`, `Foil`, `Stock` FROM `Osiris`';


$retval = mysql_query( $sql, $conn );
if(! $retval )
	{
	die('Could not get data: ' . mysql_error());
	}
	echo '<table align="center" id="mytable" cellspacing="0" class="table" >';
	echo "<tr><th >Name</th><th >Color</th><th >Type</th><th >Sub Type</th><th >Power</th><th >Toughness</th><th >Converted mana cost</th><th >Rarity</th><th >Expansion</th><th >Foil</th><th >Stock</th></tr>";
	while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
	$cardname = $row['Name'];
	$url = "http://mtgimage.com/card/$cardname.jpg";
		{
			echo "<tr><td>";
			echo "<a href=$url>$cardname</a>";
			echo "</td><td>";
			echo $row['Color'];
			echo "</td><td>";
			echo $row['Type'];
			echo "</td><td>";
			echo $row['Subtype'];
			echo "</td><td>";
			echo $row['Power'];
			echo "</td><td>";
			echo $row['Toughness'];
			echo "</td><td>";
			echo $row['Manacost'];
			echo "</td><td>";
			echo $row['Rarity'];
			echo "</td><td>";
			echo $row['Expansion'];
			echo "</td><td>";
			echo $row['Foil'];
			echo "</td><td>";
			echo $row['Stock'];
			echo "</td></tr>";
		}
		echo "</table>";
mysql_close($conn);
?>

<script language="javascript" type="text/javascript">
//<![CDATA[
	var table2_Props = 	{
							col_1: "select",
							col_2: "select",
							col_3: "select",
							col_7: "select",
							col_8: "select", 
							col_9: "select",
							col_10: "none",
							display_all_text: " [ Show all ] ",
							sort_select: true 
						};
	setFilterGrid( "mytable",table2_Props );
//]]>
</script>
</body>
</html>

change this

while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
$cardname = $row['Name'];
$url = "http://mtgimage.com/card/$cardname.jpg";
  {

to this

while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
$cardname = $row['Name'];
$url = "http://mtgimage.com/card/$cardname.jpg";

 

Thanks QuickOldCar,

 

How ever i have one more question.. :P

 

It seems that when i use spaces in the card names that the variable doesn't coppy it in the HREF link.

Is there an easy fix for it? Or should i use _ instead of spaces?

 

Thanks for all the help Guys!

If you switched to either mysqlI or pdo you would be able to fix the problem of unescaped input (contining quote marks) by using prepared queries.

 

You really need to read up on the dangers of running queries against your db with unsanitized inputs - probably from malicious users!

I will do that, its just i'm still a beginner at this and now that i (kinda) understand this code its really not that easy to jump and change everything around :P

But as you bring up the malicious users, i will try and convert it into MySQLI

 

Thanks alot guys!

Edited by OsirisElKeleni
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.