Jump to content

new to programming and have a problem.


Go to solution Solved by Jessica,

Recommended Posts

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>

 

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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 by Q695
Link to comment
Share on other sites

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>

 

Link to comment
Share on other sites

 

<?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 by Christopher_Peters
Link to comment
Share on other sites

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 by Jessica
Link to comment
Share on other sites

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>'; 
 
}
?>
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by Christopher_Peters
Link to comment
Share on other sites

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 by ignace
Link to comment
Share on other sites

Thanks for the reply, yet again :P, 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)

Untitled.png

 

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 by Christopher_Peters
Link to comment
Share on other sites

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>
Link to comment
Share on other sites

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.