Jump to content

Inserting mysql data into HTML tables?


ifusion

Recommended Posts

Hey,

 

Just need some help on how to insert information from my mysql database so it displays in an HTML table. This what i have so far, i realize that if i place:

echo $row['title'];

into the html it echos out what i want. But how do you write it so it prints each value out into its respective column etc.

 

As you can see i've created a loop so it prints out every id, title, budget and url until its done.

 

<?php


$mycon = mysql_connect('localhost', 'ifusion', 'password') or die(mysql_error());
$db_con = mysql_select_db('kieran') or die(mysql_error());

if($mycon)
{
echo "Connected 2 mysql";
}

if($db_con)
{
	echo "Connected 2 db";
}

$result = mysql_query("SELECT id, title, budget, url FROM jobs") or die(mysql_error());
while($row = mysql_fetch_array($result)){
 //echo $row['title'];
 }
?>

<html>

<body>
        
        <?php echo $result = mysql_query("SELECT id, title, budget, url FROM jobs") or die(mysql_error());

        while($row = mysql_fetch_array($result)){ 	




echo '<table border="1">';
echo '<tr>';
echo '<td>Number</td>';
echo '<td>Title</td>';
echo '<td>Budget</td>';
echo '<td>Url</td>';
echo '</tr>';
echo '<tr>';
echo '<td>1.</td>';
echo '<td>Junior Web Programmer</td>';
echo '<td>$1000</td>';
echo '<td>www.google.com</td>';
echo '</tr>';

echo '</table>';
}
?>

</body>


</html>

Link to comment
https://forums.phpfreaks.com/topic/97730-inserting-mysql-data-into-html-tables/
Share on other sites

Change your loop to:

 

<?php       
        
$counter = 1;

while($row = mysql_fetch_array($result)) {

    echo '<table border="1">';
    echo '<tr>';
    echo '<td>Number</td>';
    echo '<td>Title</td>';
    echo '<td>Budget</td>';
    echo '<td>Url</td>';
    echo '</tr>';
    echo '<tr>';
    echo "<td>$counter.</td>";
    echo "<td>{$row['title']}</td>";
    echo "<td>{$row['budget']}</td>";
    echo "<td>{$row['url']}</td>";
    echo '</tr>';
    echo '</table>';
    
    $counter++;
}

?>

Of if you just wanted one table, just do this:

 

<?php       
        
$counter = 1;

echo '<table border="1"><tr><td>Number</td><td>Title</td><td>Budget</td><td>Url</td></tr>';
while($row = mysql_fetch_array($result)) {

    echo '<tr>';
    echo "<td>$counter.</td>";
    echo "<td>{$row['title']}</td>";
    echo "<td>{$row['budget']}</td>";
    echo "<td>{$row['url']}</td>";
    echo '</tr>';
    
    $counter++;
}
echo '</table>';

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.