Domanda

I am aware there are similar questions but none I could find which answers this specifically. I am new to PHP, so apologies.

Currently this information is listed but I would like it to be in a table:

<?php

include ('connect.php');

$sql ="SELECT * FROM tbl_venues";
$res = mysql_query ($sql) or die( mysql_error() );

if ( mysql_num_rows($res) > 0 ) {
    while ( $row = mysql_fetch_assoc($res) ) {


        echo 'Venue Name:' . $row["venue_name"] ."<br />";
        echo 'Venue Description:' . $row["venue_description"] ."<br />";
        echo 'Venue Address:' . $row["venue_adress"] ."<br />";
        echo 'Venue Type:' . $row["venue_type"] ."<br />";
        echo '<a href="venueedit.php?venueid='.$row["venue_id"].'">Edit</a>';

        echo '<hr>';
        }
    }

?>

I haven't got any suggestions from similar cases working and tried to manually echo each element out. Is it possible to automate it? Syntactically I am not great with this. Any help would be appreciated, thanks.

È stato utile?

Soluzione

To include in a table you need to add HTML tags to table:

<?php

    include ('connect.php');

    $sql ="SELECT * FROM tbl_venues";
    $res = mysql_query ($sql) or die( mysql_error() );

    if ( mysql_num_rows($res) > 0 ) {
        echo '<table>';

        while ( $row = mysql_fetch_assoc($res) ) {

            echo '<tr>';
            echo '<td> Venue Name:' . $row["venue_name"] ."</td>";
            echo '<td>Venue Description:' . $row["venue_description"] ."</td>";
            echo '<td>Venue Address:' . $row["venue_adress"] ."</td>";
            echo '<td>Venue Type:' . $row["venue_type"] ."</td>";
            echo '<td><a href="venueedit.php?venueid='.$row["venue_id"].'">Edit</a</td>';
            echo '</tr>';
        }
        echo '</table>'
    }

?>

Altri suggerimenti

Try to using EOF maybe. It's a really big timesaver most of the cases, and very usefull when you insert php code snippets into html it a php file.

 <?php
    include ('connect.php');

    $sql ="SELECT * FROM tbl_venues";
    $res = mysql_query ($sql) or die( mysql_error() );

    if ( mysql_num_rows($res) > 0 ) {

        echo "<table>";

        while ( $row = mysql_fetch_assoc($res) ) {

            echo <<<EOF
                <tr>
                    <td>Venue Name: {$row['venue_name']}</td>
                    <td>Venue Description: {$row['venue_description']}<td>
                    <td>Venue Address: {$row['venue_adress']}</td>
                    <td>Venue Type: {$row['venue_type']}</td>
                    <td><a href="venueedit.php?venueid={$row['venue_id']}">Edit</a></td>
                </tr>
EOF;
        }

        echo "</table>";
    }
?>
<?php

include ('connect.php');

$sql ="SELECT * FROM `tbl_venues`";
$res = mysql_query ($sql) or die( mysql_error() );

if ( mysql_num_rows($res) > 0 ) {
    echo '<table>';

    while ( $row = mysql_fetch_assoc($res) ) {

        echo '<tr>';
        echo '<td> Venue Name:' . $row["venue_name"] ."</td>";
        echo '<td>Venue Description:' . $row["venue_description"] ."</td>";
        echo '<td>Venue Address:' . $row["venue_adress"] ."</td>";
        echo '<td>Venue Type:' . $row["venue_type"] ."</td>";
        echo '<td><a href="venueedit.php?venueid='.$row["venue_id"].'">Edit</a</td>';
        echo '</tr>';
    }
    echo '</table>'
}

?>

You can use HTML in PHP, you'd do it the same but using echo("");.

Also I suggest you use PDO or MySQLi seeing as MySQL is now deprecated, which means it is insecure.

This code should output your data into a properly formatted table:

<?php

include ('connect.php');

$sql ="SELECT * FROM tbl_venues";
$res = mysql_query ($sql) or die( mysql_error() );
?>
<table>
    <tr>
        <th>Venue Name</th>
        <th>Description</th>
        <th>Address</th>
        <th>Type</th>
        <th>Edit</th>
    </tr>
<?php
if ( mysql_num_rows($res) > 0 ) {
    while ( $row = mysql_fetch_assoc($res) ) {

?>
    <tr>
        <td><?php echo $row["venue_name"]; ?></td>
        <td><?php echo $row["venue_description"]; ?></td>
        <td><?php echo $row["venue_address"]; ?></td>
        <td><?php echo $row["venue_type"]; ?></td>
        <td><?php echo '<a href="venueedit.php?venueid='.$row["venue_id"].'">Edit</a>'; ?></td>
    </tr>
<?php
        }
    }
?>
</table>

I would suggest you append a table like this Its much easier and cleaner.. Plus well organized too

<?php

include ('connect.php');

$sql ="SELECT * FROM tbl_venues";
$res = mysql_query ($sql) or die( mysql_error() );
$result_table = "";
if ( mysql_num_rows($res) > 0 ) {
    while ( $row = mysql_fetch_assoc($res) ) {

//collect rows from database
$name = $row["venue_name"] ;
$v_desc = $row["venue_description"] ;
$v_add =$row["venue_adress"] ;
$v_type=$row["venue_type"] ;

//append table with results and echo the variables
$result_table .="<table><tr><td>$name</td>.    <td>$v_desc</td> <td>$v_add</td> <td>$v_type</td><td>'<a href="venueedit.php?venueid='.$row["venue_id"].'">Edit</a>'</td><tr></table>";
        }
    } Else { $result_table .= "No data found";}
?>

<html>
<body>
<div>
       <?php echo $result_table;  ?>
</div>
</body>
</html>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top