Displaying MySQL data in HTML with PHP

Discussion in 'PHP' started by dunnkers, Jul 15, 2010.

  1. #1
    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:

    [​IMG]
    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*/
    
    PHP:
     
    dunnkers, Jul 15, 2010 IP
  2. Deacalion

    Deacalion Peon

    Messages:
    438
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Use mysql_fetch_row instead of mysql_fetch_array. Example:

    
    <?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;
    ?>
    
    PHP:
    I haven't tested that, but it should work. Are you a C coder by any chance? :)
     
    Deacalion, Jul 15, 2010 IP
  3. dunnkers

    dunnkers Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for your reply sir Deacalion.

    Though PHP, CSS, HTML, Java, JavaScript and MySQL are my main languages, i have actually done a very little C coding.. Probably to help you in any way.
     
    dunnkers, Jul 15, 2010 IP
  4. Deacalion

    Deacalion Peon

    Messages:
    438
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #4
    No it's just, your PHP code has the hallmarks of C :).
     
    Deacalion, Jul 15, 2010 IP
  5. dunnkers

    dunnkers Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Haha i've done alot of JAVA.

    BTW: Thanks alot for your code. it is working very well.
    I'm going to use it to make a highscore function. :)
    Thanks.
     
    dunnkers, Jul 15, 2010 IP