Christopher_Peters Posted May 15, 2013 Share Posted May 15, 2013 Hey guys new to programming here, learning because I want to (personal interest) been programming for 2-3 days now and started learning PHP today, below is the code i am using, I have successfully got it to access my database and display my results in a table in the order I want, however I want variable "$f6" to anchor the link it displays on the echo ($f6 is a website url). First post here but probably won't be the last, if anyone has any suggestions please do tell <html> <body> <?php $username=" "; $password=" "; $database=" "; mysql_connect(" ",$username,$password); @mysql_select_db($database) or die("Database Error"); $query="SELECT * FROM legs"; $result=mysql_query($query); $num=mysql_numrows($result); mysql_close(); ?> <table border="1" cellspacing="2" cellpadding="3"> <tr> <td><font face="Arial, Helvetica, sans-serif"><b><u>Body Area</font></td> <td><font face="Arial, Helvetica, sans-serif"><b><u>Difficulty</font></td> <td><font face="Arial, Helvetica, sans-serif"><b><u>Time Needed</font></td> <td><font face="Arial, Helvetica, sans-serif"><b><u>Equipment Needed</font></td> <td><font face="Arial, Helvetica, sans-serif"><b><u>Gender</font></td> <td><font face="Arial, Helvetica, sans-serif"><b><u>Link</font></td> </tr> <?php $i=0; while ($i < $num) { $f1=mysql_result($result,$i,"Body Area"); $f2=mysql_result($result,$i,"Difficulty"); $f3=mysql_result($result,$i,"Time Needed"); $f4=mysql_result($result,$i,"Equipment Needed"); $f5=mysql_result($result,$i,"Gender"); $f6=mysql_result($result,$i,"Link"); ?> <tr> <td><font face="Arial, Helvetica, sans-serif"><?php echo $f1; ?></font></td> <td><font face="Arial, Helvetica, sans-serif"><?php echo $f2; ?></font></td> <td><font face="Arial, Helvetica, sans-serif"><?php echo $f3; ?></font></td> <td><font face="Arial, Helvetica, sans-serif"><?php echo $f4; ?></font></td> <td><font face="Arial, Helvetica, sans-serif"><?php echo $f5; ?></font></td> <td><font face="Arial, Helvetica, sans-serif"><?php echo $f6; ?></font></td> </tr> <?php $i++; } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/278006-new-to-programming-and-have-a-problem/ Share on other sites More sharing options...
andrewerhardt Posted May 15, 2013 Share Posted May 15, 2013 Hello! If I understand your question correctly, you can do this. <td><font face="Arial, Helvetica, sans-serif"><?php echo '<a href=". $f6 .">A Link</a>'; ?></font></td> Hope that helps! Quote Link to comment https://forums.phpfreaks.com/topic/278006-new-to-programming-and-have-a-problem/#findComment-1430099 Share on other sites More sharing options...
Christopher_Peters Posted May 15, 2013 Author Share Posted May 15, 2013 mmm i have tried this and i don't think its what i mean no ill try to explain better (still getting used to terminology) bascially variable $f6 is the "website url" column in my database so for example, it would currently display as www.google.com, but without an anchor to that website i want an anchor link to be there for whatever website $f6 brings up thanks for the quick reply though Quote Link to comment https://forums.phpfreaks.com/topic/278006-new-to-programming-and-have-a-problem/#findComment-1430103 Share on other sites More sharing options...
Q695 Posted May 15, 2013 Share Posted May 15, 2013 (edited) Hello! If I understand your question correctly, you can do this. <td><font face="Arial, Helvetica, sans-serif"><?php echo '<a href=". $f6 .">A Link</a>'; ?></font></td> Hope that helps! He forgot the while loop: <?php while ($row){ $f1=$row['f1']; $f6=$row['f6']; echo" <tr> <td><font face='Arial, Helvetica, sans-serif'> $f1</font></td> ... <td><font face='Arial, Helvetica, sans-serif'> $f6 </font></td> </tr>"; } ?> Edited May 15, 2013 by Q695 Quote Link to comment https://forums.phpfreaks.com/topic/278006-new-to-programming-and-have-a-problem/#findComment-1430115 Share on other sites More sharing options...
Christopher_Peters Posted May 15, 2013 Author Share Posted May 15, 2013 hey guys, thanks for the replys again but i am still having issues with this, here is my code again with the alterations. keep getting "( ! ) Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in C:\wamp\www\newdatabase\index.php on line 43" just so im fully clear with what im trying to achieve, $f6 will display a link like www.google.com (or whatever entry is in my db) but i want that link to be displayed with an anchor to the website too, instead of just plain text thanks again for the help so far. <?php $username=""; $password=""; $database=""; mysql_connect("",$username,$password); @mysql_select_db($database) or die("Unable to select database"); $query="SELECT * FROM legs"; $result=mysql_query($query); $num=mysql_numrows($result); mysql_close(); ?> <table border="1" cellspacing="2" cellpadding="3"> <tr> <td><font face="Arial, Helvetica, sans-serif"><b><u>Body Area</font></td> <td><font face="Arial, Helvetica, sans-serif"><b><u>Difficulty</font></td> <td><font face="Arial, Helvetica, sans-serif"><b><u>Time Needed</font></td> <td><font face="Arial, Helvetica, sans-serif"><b><u>Equipment Needed</font></td> <td><font face="Arial, Helvetica, sans-serif"><b><u>Gender</font></td> <td><font face="Arial, Helvetica, sans-serif"><b><u>Link</font></td> </tr> <?php $i=0; while ($i < $num) { $f1=mysql_result($result,$i,"Body Area"); $f2=mysql_result($result,$i,"Difficulty"); $f3=mysql_result($result,$i,"Time Needed"); $f4=mysql_result($result,$i,"Equipment Needed"); $f5=mysql_result($result,$i,"Gender"); $f6=mysql_result($result,$i,"Link"); ?> <?php while ($row){ $f1=$row['f1']; $f6=$row['f6']; echo" <tr>; <td><font face="Arial, Helvetica, sans-serif"><?php echo $f1; ?></font></td>; <td><font face="Arial, Helvetica, sans-serif"><?php echo $f2; ?></font></td>; <td><font face="Arial, Helvetica, sans-serif"><?php echo $f3; ?></font></td>; <td><font face="Arial, Helvetica, sans-serif"><?php echo $f4; ?></font></td>; <td><font face="Arial, Helvetica, sans-serif"><?php echo $f5; ?></font></td>; <td><font face="Arial, Helvetica, sans-serif"><?php echo '<a href=". $f6 .">A Link</a>'; ?></font></td>; </tr>"; } ?> <?php $i++; } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/278006-new-to-programming-and-have-a-problem/#findComment-1430311 Share on other sites More sharing options...
Jessica Posted May 16, 2013 Share Posted May 16, 2013 And the problem is? Quote Link to comment https://forums.phpfreaks.com/topic/278006-new-to-programming-and-have-a-problem/#findComment-1430327 Share on other sites More sharing options...
Christopher_Peters Posted May 16, 2013 Author Share Posted May 16, 2013 ( ! ) SCREAM: Error suppression ignored for ( ! ) Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in C:\wamp\www\newdatabase\index.php on line 44 these are the errors i am currently getting (with the code above) Quote Link to comment https://forums.phpfreaks.com/topic/278006-new-to-programming-and-have-a-problem/#findComment-1430442 Share on other sites More sharing options...
Jessica Posted May 16, 2013 Share Posted May 16, 2013 Well, for one, remove the error surpressor. Then solve the syntax error. Post the code for lines 42-46 or so, in CODE TAGS. Quote Link to comment https://forums.phpfreaks.com/topic/278006-new-to-programming-and-have-a-problem/#findComment-1430455 Share on other sites More sharing options...
Christopher_Peters Posted May 16, 2013 Author Share Posted May 16, 2013 (edited) <?php while ($row){ $f1=$row['f1']; $f6=$row['f6']; echo" <tr>; <td><font face="Arial, Helvetica, sans-serif"><?php echo $f1; ?></font></td>; <td><font face="Arial, Helvetica, sans-serif"><?php echo $f2; ?></font></td>; <td><font face="Arial, Helvetica, sans-serif"><?php echo $f3; ?></font></td>; <td><font face="Arial, Helvetica, sans-serif"><?php echo $f4; ?></font></td>; <td><font face="Arial, Helvetica, sans-serif"><?php echo $f5; ?></font></td>; <td><font face="Arial, Helvetica, sans-serif"><?php echo '<a href=". $f6 .">A Link</a>'; ?></font></td>; </tr>"; } ?> Edited May 16, 2013 by Christopher_Peters Quote Link to comment https://forums.phpfreaks.com/topic/278006-new-to-programming-and-have-a-problem/#findComment-1430490 Share on other sites More sharing options...
Jessica Posted May 16, 2013 Share Posted May 16, 2013 (edited) Code tags means the little button with the <> so we can read the code. Not type the <>. You have started a string with " - where you wrote echo" Then you never properly end that string before trying to do more PHP. An editor with syntax highlighting would make this obvious echo '<tr> <td><font face="Arial, Helvetica, sans-serif">'.$f1.'</font></td>'; etc etc would work. And for the love of god, get rid of those FONT tags. Edited May 16, 2013 by Jessica Quote Link to comment https://forums.phpfreaks.com/topic/278006-new-to-programming-and-have-a-problem/#findComment-1430492 Share on other sites More sharing options...
Christopher_Peters Posted May 16, 2013 Author Share Posted May 16, 2013 Thanks again for the help, I have edited my code but I am now getting an undefined variable error on line 37 which is "while($row){" <?php while ($row){ $f1=$row['f1']; $f6=$row['f6']; echo '<tr> <td><font face="Arial, Helvetica, sans-serif">' .$f1. '</td>'; '<td><font face="Arial, Helvetica, sans-serif">' .$f2. '</td>'; '<td><font face="Arial, Helvetica, sans-serif">' .$f3. '</td>'; '<td><font face="Arial, Helvetica, sans-serif">' .$f4. '</td>'; '<td><font face="Arial, Helvetica, sans-serif">' .$f5. '</td>'; '<td><font face="Arial, Helvetica, sans-serif">' .$f6. '</td>'; '</tr>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/278006-new-to-programming-and-have-a-problem/#findComment-1430494 Share on other sites More sharing options...
Jessica Posted May 16, 2013 Share Posted May 16, 2013 Well, sounds like $row doesn't exist. Not to mention, the code as shown would produce an infinate loop. Go read the php manual page for mysql_fetch_assoc or whatever function you want to use. Quote Link to comment https://forums.phpfreaks.com/topic/278006-new-to-programming-and-have-a-problem/#findComment-1430498 Share on other sites More sharing options...
Christopher_Peters Posted May 16, 2013 Author Share Posted May 16, 2013 (edited) ooh silly me, I have solved that error and my table is showing up fine, but my link row still isn't anchoring to the website. I really appreciate all the help you've given me and for being patient with me. Here's my code so far <?php $username=" "; $password=" "; $database=" "; mysql_connect(" ",$username,$password); @mysql_select_db($database) or die("Unable to select database"); $query="SELECT * FROM legs"; $result=mysql_query($query); $num=mysql_numrows($result); mysql_close(); ?> <table border="1" cellspacing="2" cellpadding="3"> <tr> <td><font face="Arial, Helvetica, sans-serif"><b><u>Body Area</td> <td><font face="Arial, Helvetica, sans-serif"><b><u>Difficulty</td> <td><font face="Arial, Helvetica, sans-serif"><b><u>Time Needed</td> <td><font face="Arial, Helvetica, sans-serif"><b><u>Equipment Needed</td> <td><font face="Arial, Helvetica, sans-serif"><b><u>Gender</td> <td><font face="Arial, Helvetica, sans-serif"><b><u>Link</td> </tr> <?php $i=0; while ($i < $num) { $f1=mysql_result($result,$i,"Body Area"); $f2=mysql_result($result,$i,"Difficulty"); $f3=mysql_result($result,$i,"Time Needed"); $f4=mysql_result($result,$i,"Equipment Needed"); $f5=mysql_result($result,$i,"Gender"); $f6=mysql_result($result,$i,"Link"); ?> <?php while ($row=mysql_fetch_assoc($result)){ $f1=$row['Body Area']; $f2=$row['Difficulty']; $f3=$row['Time Needed']; $f4=$row['Equipment Needed']; $f5=$row['Gender']; $f6=$row['Link']; echo '<tr> <td><font face="Arial, Helvetica, sans-serif">' .$f1. '</td>' . '<td><font face="Arial, Helvetica, sans-serif">' .$f2. '</td>' . '<td><font face="Arial, Helvetica, sans-serif">' .$f3. '</td>' . '<td><font face="Arial, Helvetica, sans-serif">' .$f4. '</td>' . '<td><font face="Arial, Helvetica, sans-serif">' .$f5. '</td>' . '<td><font face="Arial, Helvetica, sans-serif">' .$f6. '</td>'; '</tr>'; } ?> <?php $i++; } ?> </body> </html> Edited May 16, 2013 by Christopher_Peters Quote Link to comment https://forums.phpfreaks.com/topic/278006-new-to-programming-and-have-a-problem/#findComment-1430510 Share on other sites More sharing options...
ignace Posted May 16, 2013 Share Posted May 16, 2013 (edited) This is how your code should have looked. I, and so does the PHP manual, discourage the use of mysql_* functions and encourage the use of mysqli_* functions. Learn CSS. Nobody use <font/> anymore nor do they use <b/> or <u/>. <?php $host = ""; $user = ""; $pass = ""; $dbname = ""; $db = mysqli_connect($host, $user, $pass, $dbname); if (mysqli_connect_errno()) { echo mysqli_connect_error(); exit; } $query = "SELECT * FROM legs"; $result = mysqli_query($db, $query) or die(mysqli_error($db)); ?> <style> /* this is CSS preferably this should be in a .css file and linked through a <link/> tag. */ body { font-family: Arial, Helvetica, sans-serif; } table { border:1px solid gray; } table thead tr th { font-weight: bold; text-decoration: underline; } table tbody tr td, table thead tr th { padding: 3px; } </style> <table cellspacing="2"> <thead> <tr> <th>Body Area</th> <th>Difficulty</th> <th>Time Needed</th> <th>Equipment Needed</th> <th>Gender</th> <th>Link</th> </tr> </thead> <tbody> <?php while ($row = mysqli_fetch_assoc($result)) { ?> <tr> <td><?php echo $row['Body Area']; ?></td> <td><?php echo $row['Difficulty']; ?></td> <td><?php echo $row['Time Needed']; ?></td> <td><?php echo $row['Equipment Needed']; ?></td> <td><?php echo $row['Gender']; ?></td> <td><?php echo $row['Link']; ?></td> </tr> <?php } ?> </tbody> </table> Edited May 16, 2013 by ignace Quote Link to comment https://forums.phpfreaks.com/topic/278006-new-to-programming-and-have-a-problem/#findComment-1430519 Share on other sites More sharing options...
Christopher_Peters Posted May 16, 2013 Author Share Posted May 16, 2013 (edited) Thanks for the reply, yet again , I have changed my code but my links still won't anchor to the website they belong to? Here is a picture of the table (the data is sample data) I want the www.whatevereverever.com to display as an actual link rather than just text oh and yeah, I started learning CSS yesterday I will eventually be making style sheets for myself but getting this problem solved is my priority at the moment. Thanks again. Edited May 16, 2013 by Christopher_Peters Quote Link to comment https://forums.phpfreaks.com/topic/278006-new-to-programming-and-have-a-problem/#findComment-1430525 Share on other sites More sharing options...
Jessica Posted May 16, 2013 Share Posted May 16, 2013 So then wrap it in anchor tags like you did before. Quote Link to comment https://forums.phpfreaks.com/topic/278006-new-to-programming-and-have-a-problem/#findComment-1430528 Share on other sites More sharing options...
ginerjm Posted May 16, 2013 Share Posted May 16, 2013 <td><a href="$f6">$f6</a></td> Quote Link to comment https://forums.phpfreaks.com/topic/278006-new-to-programming-and-have-a-problem/#findComment-1430538 Share on other sites More sharing options...
Christopher_Peters Posted May 16, 2013 Author Share Posted May 16, 2013 Ah you guys are amazing, iv finally managed to get the link to display and nearly everything is perfect, the one problem is the link is displaying as http://localhost/newdatabase/www.google.com instead of just www.google.com <?php while ($row = mysqli_fetch_assoc($result)) { ?> <tr> <td><?php echo $row['Body Area']; ?></td> <td><?php echo $row['Difficulty']; ?></td> <td><?php echo $row['Time Needed']; ?></td> <td><?php echo $row['Equipment Needed']; ?></td> <td><?php echo $row['Gender']; ?></td> <td><a href="<?php echo $row['Link']?>"target=_blank">Link</a></td> </tr> <?php } ?> </tbody> </table> Quote Link to comment https://forums.phpfreaks.com/topic/278006-new-to-programming-and-have-a-problem/#findComment-1430540 Share on other sites More sharing options...
Solution Jessica Posted May 16, 2013 Solution Share Posted May 16, 2013 Try adding an "http://" in front of your link (either in the HTML or the DB.) Quote Link to comment https://forums.phpfreaks.com/topic/278006-new-to-programming-and-have-a-problem/#findComment-1430543 Share on other sites More sharing options...
Christopher_Peters Posted May 16, 2013 Author Share Posted May 16, 2013 Yeah that did it, again thanks a lot glad I can move on to the next task haha. Quote Link to comment https://forums.phpfreaks.com/topic/278006-new-to-programming-and-have-a-problem/#findComment-1430561 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.