Jump to content

Where am I going wrong?


suttercain

Recommended Posts

Hello,

 

I am generating a basic list, which is showing up fine, from MySQL and am having a problem making the links work. I am pulling the ID and Title from a table. The Title are clickable and should be taken to that page based on the ID, but instead the ID is showing up blank:

 

How it's coming up:

http://supermandatabase.com/modules.php?name=4ndvddb&rop=show_dvd&id=

 

How it should come up:

http://www.supermandatabase.com/modules.php?name=4ndvddb&rop=show_dvd&id=1282

 

Where am I going wrong? As a bonus I would also like to make the list sortable by the end user. They can click Title and have it alphabetize or ID and have it order by number.

<?php
$sql = "SELECT id,title FROM tablename";
$result = mysql_query($sql);
$link = "<a href='modules.php?name=4ndvddb&rop=show_dvd&id=$row[id]'>";
//uncomment $link and $el if you want it linkable
$td = "<td>";
$el = "</a>";
echo "<table border=0 cellspacing=0 cellpadding=2 width=200>";
echo "<tr><td>ID<td>Title";
while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
    echo "<tr><td>";
    printf ("%s $td $link %s $el", $row[0], $row["title"]);
}

mysql_free_result($result);
?> 

 

Any help would be appreciated. Thanks.

Link to comment
Share on other sites

You might try doing it this way:

 

<?php
while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
    echo "<tr><td>";
    echo "<a href='modules.php?name=4ndvddb&rop=show_dvd&id=" . $row['id'] . "'> Title: " . $row['title'] . "</a> ";
    echo "</td></tr>";
}
?>

Link to comment
Share on other sites

That is great. Thank you. Not to be too picky, but I hoping to keep the layout as:

 

ID          TITLE

1          Super

2          Happy

 

With the code in the reply it allows the links to work great but I lose that layout and get the TITLE echoing and I lose the ID so it look as

 

Title: Super

Title: Happy

Title: Yo Yo Yo

 

 

Link to comment
Share on other sites

Yes, I assumed you wanted the title to be a hyperlink so I set it up that way. If you want to display the id and title in separate cells and also make the id the hyperlink then you'd do this. Also note that there's a <TH> tag in there that will name your fields for you.

 

<?php
   echo "<table><tr>";
   echo "<th>ID</th><th>Title</th></tr>";
while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
   
    echo "<tr><td><a href='modules.php?name=4ndvddb&rop=show_dvd&id=" . $row['id'] . "'>ID</a><td> " . $row['title'] . "</td></tr> ";
}
echo "</table><br>\n";
?>

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.