Skip to main content
added 1 character in body
Source Link
mickmackusa
  • 4.8k
  • 4
  • 18
  • 48

Just to clear up a possible misconception about the script in the question...

$result = $db->loadResult(); does not have an active/necessary role to play in the process. $result is not be used after it is declared and it does not "set up" loadRow().

As for how to echo out each column value, I'll recommend implode() as the most concise method. In doing so, it won't matter if you build your result set from loadRow() or loadAssoc() because implode() will ignore the keys. What DOES matter is the columns that you write into your SELECT clause -- this will dictate exactly which columns are presented. It is unlikely that you will want to display ALL of the columns so * should be swapped out for literal column declarations (either as a comma separated string or an array of strings). The snippet to follow will work flawlessly even if you only select a single column (and generate a one dimensional result set).

$db->loadRow() with implode()` with implode():

if (!$row = $db->loadRow()) {  // declare and check
    echo "No Row Found";
} else {
    echo '<table>';
        echo '<tr>';
            echo '<td>' , implode('</td><td>', $row) , '</td>';
        echo '</tr>';
    echo '</table>';
}

If you would like to display a row of column headings, then use loadAssoc() to generate the result set. If you wish to adjust one or more column headings to be printed to screen, use a "column alias" where appropriate in the SELECT clause of your query.

if (!$row = $db->loadAssoc()) {  // declare and check
    echo "No Row Found";
} else {
    echo '<table>';
        echo '<tr>';
            echo '<th>' , implode('</th><th>', array_keys($row)) , '</th>';
        echo '</tr>';
        echo '<tr>';
            echo '<td>' , implode('</td><td>', $row) , '</td>';
        echo '</tr>';
    echo '</table>';
}

Just to clear up a possible misconception about the script in the question...

$result = $db->loadResult(); does not have an active/necessary role to play in the process. $result is not be used after it is declared and it does not "set up" loadRow().

As for how to echo out each column value, I'll recommend implode() as the most concise method. In doing so, it won't matter if you build your result set from loadRow() or loadAssoc() because implode() will ignore the keys. What DOES matter is the columns that you write into your SELECT clause -- this will dictate exactly which columns are presented. It is unlikely that you will want to display ALL of the columns so * should be swapped out for literal column declarations (either as a comma separated string or an array of strings). The snippet to follow will work flawlessly even if you only select a single column (and generate a one dimensional result set).

$db->loadRow() with implode()`:

if (!$row = $db->loadRow()) {  // declare and check
    echo "No Row Found";
} else {
    echo '<table>';
        echo '<tr>';
            echo '<td>' , implode('</td><td>', $row) , '</td>';
        echo '</tr>';
    echo '</table>';
}

If you would like to display a row of column headings, then use loadAssoc() to generate the result set. If you wish to adjust one or more column headings to be printed to screen, use a "column alias" where appropriate in the SELECT clause of your query.

if (!$row = $db->loadAssoc()) {  // declare and check
    echo "No Row Found";
} else {
    echo '<table>';
        echo '<tr>';
            echo '<th>' , implode('</th><th>', array_keys($row)) , '</th>';
        echo '</tr>';
        echo '<tr>';
            echo '<td>' , implode('</td><td>', $row) , '</td>';
        echo '</tr>';
    echo '</table>';
}

Just to clear up a possible misconception about the script in the question...

$result = $db->loadResult(); does not have an active/necessary role to play in the process. $result is not be used after it is declared and it does not "set up" loadRow().

As for how to echo out each column value, I'll recommend implode() as the most concise method. In doing so, it won't matter if you build your result set from loadRow() or loadAssoc() because implode() will ignore the keys. What DOES matter is the columns that you write into your SELECT clause -- this will dictate exactly which columns are presented. It is unlikely that you will want to display ALL of the columns so * should be swapped out for literal column declarations (either as a comma separated string or an array of strings). The snippet to follow will work flawlessly even if you only select a single column (and generate a one dimensional result set).

$db->loadRow() with implode():

if (!$row = $db->loadRow()) {  // declare and check
    echo "No Row Found";
} else {
    echo '<table>';
        echo '<tr>';
            echo '<td>' , implode('</td><td>', $row) , '</td>';
        echo '</tr>';
    echo '</table>';
}

If you would like to display a row of column headings, then use loadAssoc() to generate the result set. If you wish to adjust one or more column headings to be printed to screen, use a "column alias" where appropriate in the SELECT clause of your query.

if (!$row = $db->loadAssoc()) {  // declare and check
    echo "No Row Found";
} else {
    echo '<table>';
        echo '<tr>';
            echo '<th>' , implode('</th><th>', array_keys($row)) , '</th>';
        echo '</tr>';
        echo '<tr>';
            echo '<td>' , implode('</td><td>', $row) , '</td>';
        echo '</tr>';
    echo '</table>';
}
Post Undeleted by mickmackusa
several incorrect assertions were fixed (untested code was posted)
Source Link
mickmackusa
  • 4.8k
  • 4
  • 18
  • 48

Just to clear up a possible misconception about the script in the question...

$result = $db->loadResult(); does not have an active/necessary role to play in the process. $result is not be used after it is declared and it does not "set up" loadRow().

As for how to echo out each column value, I'll recommend implode() as the most concise method. In doing so, it won't matter if you build your result set from loadRow() or loadAssoc() because implode() will ignore the keys. What DOES matter is the columns that you write into your SELECT clause -- this will dictate exactly which columns are presented. It is unlikely that you will want to display ALL of the columns so * should be swapped out for literal column declarations (either as a comma separated string or an array of strings). The snippet to follow will work flawlessly even if you only select a single column (and generate a multidimensionalone dimensional result set).

To correct/extend what Lodder has posted with implode$db->loadRow() with implode()`:

if (!$rows$row = $db->loadRow()) {  // declare and check
    echo "No RowsRow Found";
} else {
    echo '<table>';
        foreach($rows as $row) {
            echo '<tr>';
                echo '<td>' , implode('</td><td>', $row) , '</td>';
            echo '</tr>';
        }
    echo '</table>';
}

If you would like to display a row of column headings, then use loadAssoc() to generate the result set. If you wish to adjust one or more column headings to be printed to screen, use a "column alias" where appropriate in the SELECT clause of your query.

if (!$rows$row = $db->loadAssoc()) {  // declare and check
    echo "No RowsRow Found";
} else {
    echo '<table>';
        echo '<tr>';
            echo '<th>' , implode('</th><th>', array_keys($rows[0]$row)) , '</th>';
        echo '</tr>';
        foreach($rows as $row) {
            echo '<tr>';
                echo '<td>' , implode('</td><td>', $row) , '</td>';
            echo '</tr>';
        }
    echo '</table>';
}

Just to clear up a possible misconception about the script in the question...

$result = $db->loadResult(); does not have an active/necessary role to play in the process. $result is not be used after it is declared and it does not "set up" loadRow().

As for how to echo out each column value, I'll recommend implode() as the most concise method. In doing so, it won't matter if you build your result set from loadRow() or loadAssoc() because implode() will ignore the keys. What DOES matter is the columns that you write into your SELECT clause -- this will dictate exactly which columns are presented. It is unlikely that you will want to display ALL of the columns so * should be swapped out for literal column declarations (either as a comma separated string or an array of strings). The snippet to follow will work flawlessly even if you only select a single column (and generate a multidimensional result set).

To correct/extend what Lodder has posted with implode():

if (!$rows = $db->loadRow()) {  // declare and check
    echo "No Rows Found";
} else {
    echo '<table>';
        foreach($rows as $row) {
            echo '<tr>';
                echo '<td>' , implode('</td><td>', $row) , '</td>';
            echo '</tr>';
        }
    echo '</table>';
}

If you would like to display a row of column headings, then use loadAssoc() to generate the result set. If you wish to adjust one or more column headings to be printed to screen, use a "column alias" where appropriate in the SELECT clause of your query.

if (!$rows = $db->loadAssoc()) {  // declare and check
    echo "No Rows Found";
} else {
    echo '<table>';
        echo '<tr>';
            echo '<th>' , implode('</th><th>', array_keys($rows[0]) , '</th>';
        echo '</tr>';
        foreach($rows as $row) {
            echo '<tr>';
                echo '<td>' , implode('</td><td>', $row) , '</td>';
            echo '</tr>';
        }
    echo '</table>';
}

Just to clear up a possible misconception about the script in the question...

$result = $db->loadResult(); does not have an active/necessary role to play in the process. $result is not be used after it is declared and it does not "set up" loadRow().

As for how to echo out each column value, I'll recommend implode() as the most concise method. In doing so, it won't matter if you build your result set from loadRow() or loadAssoc() because implode() will ignore the keys. What DOES matter is the columns that you write into your SELECT clause -- this will dictate exactly which columns are presented. It is unlikely that you will want to display ALL of the columns so * should be swapped out for literal column declarations (either as a comma separated string or an array of strings). The snippet to follow will work flawlessly even if you only select a single column (and generate a one dimensional result set).

$db->loadRow() with implode()`:

if (!$row = $db->loadRow()) {  // declare and check
    echo "No Row Found";
} else {
    echo '<table>';
        echo '<tr>';
            echo '<td>' , implode('</td><td>', $row) , '</td>';
        echo '</tr>';
    echo '</table>';
}

If you would like to display a row of column headings, then use loadAssoc() to generate the result set. If you wish to adjust one or more column headings to be printed to screen, use a "column alias" where appropriate in the SELECT clause of your query.

if (!$row = $db->loadAssoc()) {  // declare and check
    echo "No Row Found";
} else {
    echo '<table>';
        echo '<tr>';
            echo '<th>' , implode('</th><th>', array_keys($row)) , '</th>';
        echo '</tr>';
        echo '<tr>';
            echo '<td>' , implode('</td><td>', $row) , '</td>';
        echo '</tr>';
    echo '</table>';
}
Post Deleted by mickmackusa
Source Link
mickmackusa
  • 4.8k
  • 4
  • 18
  • 48

Just to clear up a possible misconception about the script in the question...

$result = $db->loadResult(); does not have an active/necessary role to play in the process. $result is not be used after it is declared and it does not "set up" loadRow().

As for how to echo out each column value, I'll recommend implode() as the most concise method. In doing so, it won't matter if you build your result set from loadRow() or loadAssoc() because implode() will ignore the keys. What DOES matter is the columns that you write into your SELECT clause -- this will dictate exactly which columns are presented. It is unlikely that you will want to display ALL of the columns so * should be swapped out for literal column declarations (either as a comma separated string or an array of strings). The snippet to follow will work flawlessly even if you only select a single column (and generate a multidimensional result set).

To correct/extend what Lodder has posted with implode():

if (!$rows = $db->loadRow()) {  // declare and check
    echo "No Rows Found";
} else {
    echo '<table>';
        foreach($rows as $row) {
            echo '<tr>';
                echo '<td>' , implode('</td><td>', $row) , '</td>';
            echo '</tr>';
        }
    echo '</table>';
}

If you would like to display a row of column headings, then use loadAssoc() to generate the result set. If you wish to adjust one or more column headings to be printed to screen, use a "column alias" where appropriate in the SELECT clause of your query.

if (!$rows = $db->loadAssoc()) {  // declare and check
    echo "No Rows Found";
} else {
    echo '<table>';
        echo '<tr>';
            echo '<th>' , implode('</th><th>', array_keys($rows[0]) , '</th>';
        echo '</tr>';
        foreach($rows as $row) {
            echo '<tr>';
                echo '<td>' , implode('</td><td>', $row) , '</td>';
            echo '</tr>';
        }
    echo '</table>';
}