OsirisElKeleni Posted October 5, 2014 Share Posted October 5, 2014 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! Quote Link to comment https://forums.phpfreaks.com/topic/291448-php-variables-in-a-link/ Share on other sites More sharing options...
Solution Ch0cu3r Posted October 5, 2014 Solution Share Posted October 5, 2014 These two lines need to be inside your while loop $cardname = $row['Name']; $url = "http://mtgimage.com/card/$cardname.jpg"; Variables need to be defined before you can use them. Quote Link to comment https://forums.phpfreaks.com/topic/291448-php-variables-in-a-link/#findComment-1492769 Share on other sites More sharing options...
QuickOldCar Posted October 5, 2014 Share Posted October 5, 2014 (edited) 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 October 5, 2014 by QuickOldCar Quote Link to comment https://forums.phpfreaks.com/topic/291448-php-variables-in-a-link/#findComment-1492776 Share on other sites More sharing options...
jcbones Posted October 5, 2014 Share Posted October 5, 2014 I'm in disagreement with the last post, I think the way it is will process faster, and is more readable. Quote Link to comment https://forums.phpfreaks.com/topic/291448-php-variables-in-a-link/#findComment-1492782 Share on other sites More sharing options...
OsirisElKeleni Posted October 5, 2014 Author Share Posted October 5, 2014 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> Quote Link to comment https://forums.phpfreaks.com/topic/291448-php-variables-in-a-link/#findComment-1492784 Share on other sites More sharing options...
QuickOldCar Posted October 5, 2014 Share Posted October 5, 2014 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"; Quote Link to comment https://forums.phpfreaks.com/topic/291448-php-variables-in-a-link/#findComment-1492785 Share on other sites More sharing options...
ginerjm Posted October 5, 2014 Share Posted October 5, 2014 (edited) And while you are changing things, you really should change your db interface to be mysqlI or PDO instead of the DEPRECATED MYSQL Edited October 5, 2014 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/291448-php-variables-in-a-link/#findComment-1492786 Share on other sites More sharing options...
OsirisElKeleni Posted October 5, 2014 Author Share Posted October 5, 2014 Thanks QuickOldCar, How ever i have one more question.. 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! Quote Link to comment https://forums.phpfreaks.com/topic/291448-php-variables-in-a-link/#findComment-1492787 Share on other sites More sharing options...
QuickOldCar Posted October 5, 2014 Share Posted October 5, 2014 It would be better and simpler to use _ in them. Otherwise can use urlencode() and urldecode() on them back and forth. Or a str_replace $encodedcardname = str_replace(" ","%20",trim($cardname)); Quote Link to comment https://forums.phpfreaks.com/topic/291448-php-variables-in-a-link/#findComment-1492788 Share on other sites More sharing options...
OsirisElKeleni Posted October 5, 2014 Author Share Posted October 5, 2014 Thanks alot! it works like a charm! just need to figure out how i can input ' into names cuz i cant store them into my database and its as good as done! Really helpfull forum! Quote Link to comment https://forums.phpfreaks.com/topic/291448-php-variables-in-a-link/#findComment-1492790 Share on other sites More sharing options...
ginerjm Posted October 5, 2014 Share Posted October 5, 2014 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! Quote Link to comment https://forums.phpfreaks.com/topic/291448-php-variables-in-a-link/#findComment-1492791 Share on other sites More sharing options...
OsirisElKeleni Posted October 5, 2014 Author Share Posted October 5, 2014 (edited) 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 But as you bring up the malicious users, i will try and convert it into MySQLI Thanks alot guys! Edited October 5, 2014 by OsirisElKeleni Quote Link to comment https://forums.phpfreaks.com/topic/291448-php-variables-in-a-link/#findComment-1492792 Share on other sites More sharing options...
ginerjm Posted October 5, 2014 Share Posted October 5, 2014 pdo is a better choice. At least that's what most people say here. I find it pretty easy to use. Just make sure you read Jacques1 writeup on how to use it properly and how to make your standard connection logic do it correctly. Quote Link to comment https://forums.phpfreaks.com/topic/291448-php-variables-in-a-link/#findComment-1492798 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.