Automatically Generated Tables not generating quite right

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Teonnyn
Forum Commoner
Posts: 58
Joined: Tue Dec 23, 2008 4:07 am

Automatically Generated Tables not generating quite right

Post by Teonnyn »

I've been working on a function to add to my database classes in order to format the output. The function mostly works, but when it comes to adding actions / links to the tables, for some odd reason it refuses to start at the first row in the system.

This is the entire function:

Code: Select all

 
    function displayAsTables() {
    
    //  echo $this->_sql;
    $sql = mysql_query($this->_sql);
    if (mysql_num_rows($sql) == 0) {
        echo "No results found";
        return;
    }
    echo "<table border='2' tablecolor=black>\n<tr>\n";
    $fieldCount = mysql_num_fields($sql);
    for ($i = 0; $i < $fieldCount; $i++) {
        echo "<th>" . mysql_field_name($sql, $i) . "</th>\n";
    }
[b] echo "<th>Links</th></tr>\n";[/b]
    while ($row = mysql_fetch_array($sql)) {
        echo "<tr>\n";
        for ($i = 0; $i < $fieldCount; $i++) {
            echo "<td>" . htmlentities($row[$i]) . "</td>\n";
        }
        $action = str_replace("username", $row[0], $actions);
        $actions = '<a href="index.php?option=com_user&view=user&Itemid=68&extuser=username">Profile</a>';
        echo "<td nowrap>&nbsp;$action</td>\n</tr>\n";
[b] }[/b]
    echo "</table>";
 
I have highlighted the start and finish of the problem area with Bold - but the rest of it works just fine.
This is the actual table itself: Image
Users can link to their own profiles, this is just something to allow other people to view their profiles.
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

Re: Automatically Generated Tables not generating quite right

Post by liljester »

i only took a quick look.. but this seems odd:

Code: Select all

$action = str_replace("username", $row[0], $actions);
$actions = '<a href="index.php?option=com_user&view=user&Itemid=68&extuser=username">Profile</a>';
its in a loop, and you use $actions in your str_replace before its defined, or atleast in the code snippet you posted. try defining $actions before your str_replace line.

it looks like the way the code in its current state that the profile links wouldnt match the correct username, its not really missing the first profile link, its just on the wrong line. its actually missing the last link.

let me know if im right :)
Teonnyn
Forum Commoner
Posts: 58
Joined: Tue Dec 23, 2008 4:07 am

Re: Automatically Generated Tables not generating quite right

Post by Teonnyn »

Hey, that actually fixed it! Thanks :D
Post Reply