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
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++;
}

?>

Link to comment
Share on other sites

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>';

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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