Page 1 of 1

Automatically Generated Tables not generating quite right

Posted: Tue Apr 21, 2009 4:25 pm
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.

Re: Automatically Generated Tables not generating quite right

Posted: Tue Apr 21, 2009 7:08 pm
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 :)

Re: Automatically Generated Tables not generating quite right

Posted: Tue Apr 21, 2009 10:01 pm
by Teonnyn
Hey, that actually fixed it! Thanks :D