Jump to content

Displaying MySQL data in HTML with PHP

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
dunnkers

dunnkers

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
Hello, I'm trying to create a html table for all the data in my database (mysql).
My goal is to keep it dynamic, so if i'd add one variable to the array of columns we must load, it would also display that one. But it is not working.
It currently displays this:

Posted Image
Can anyone help me?

The code:
	/*START------TABLE WITH USERDATA*/

	$table = 'mytable';

	$vars = array(

	1 => 'user', 2 => 'runtime', 3 => 'created', 4 => 'xpgained', 5 => 


'profit'

	);

	echo "<table border='1'><tr>";

	

	/*here we make the 'header' of the table*/

	$length = sizeof($vars);

	for ($i = 1; $i < $length + 1; $i ++) {

		echo "<td>$vars[$i]</td>";

	}

	echo "</tr>";

	

	/*and here the table itself*/

	$first = true;

	$imploded = implode(",", $vars);

	$sql = mysql_query("SELECT $imploded FROM $table");

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

		if ($first == true) {

			for ($i = 1; $i < $length + 1; $i ++) {

				$string .="<td>".$row[$vars


[$i]]."</td>";

			}

		}

		echo "<tr>";

		echo $string;

		echo "</tr>";

		$first = false;

	} 

	echo "</table>";

	/*END------TABLE WITH USERDATA*/


Edited by dunnkers, 15 July 2010 - 12:29 PM.


#2
dunnkers

dunnkers

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
EDIT: [SOLVED] -> Used mysql_fetch_row instead of mysql_fetch_array.

php Code:
<
?php

    // table to use

    $table = 'mytable';

    

    // fields to include

    $fieldHeaders = array('user','runtime','created','xpgained','profit');

  

    // begin the table

    $output = '<table border="1"><tr>';

    foreach ($fieldHeaders as $field) $output .= '<td>'.$field.'</td>';

    $output .= '</tr>';

    

    // table body

    $sql = mysql_query('SELECT '.implode(',', $fieldHeaders).' FROM '.$table);

    while ($row = mysql_fetch_row($sql)) {

        $output .= '<tr>';  

        foreach ($row as $el) $output .= '<td>'.$el.'</td>';

        $output .= '</tr>';

    } 

    

    // finish the table

    $output .= '</table>';

    

    

    // output the generated table

    echo $output;

?>


#3
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
What's the $first for?

#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
Watch your IF statement , you're cutting off the whole functioning of placing the content within a <TD>, I agree with oxano, it makes no sense.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#5
Guest_johnny.dacu_*

Guest_johnny.dacu_*
  • Guests
First i thought that first is used for table head.. but after a second look... i guess is only a remain from a previews code