PHPTalk.com: Displaying MySQL data in HTML with PHP - PHPTalk.com

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Displaying MySQL data in HTML with PHP

#1 User is offline   dunnkers Icon

  • Newbie
  • Pip
  • Group: Members
  • Posts: 3
  • Joined: 15-July 10

Posted 15 July 2010 - 08:02 PM

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 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*/

0

#2 User is offline   Shoel Icon

  • Administrator
  • Icon
  • View blog
  • Group: Administrators
  • Posts: 38
  • Joined: 22-April 10

Posted 15 July 2010 - 08:36 PM

Hi there,

Welcome to PHPTalk! :)

The first thing that strikes me, before I take a closer look at it, is this part:

(I took the liberty of cleaning up the indention.)

<?php
$first = true;

(...)

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

What happens here is that for the first database row retrieved, $first equals to true. That means that the "if ($first == true)" condition evaluates to true, and the block inside the curly-brackets runs. It then loops through the column names defined in $vars and adds table cells (<td>foo</td>) with the values retrieved from the corresponding colums in the database to the string $string.

More specifically, this is done in this snippet:

<?php
  if ($first == true) {
    for ($i = 1; $i < $length + 1; $i ++) {
      $string .="<td>".$row[$vars[$i]]."</td>";
    }
  }
?>

Next, you wrap the table cells in a table row:

<?php
  echo "<tr>";
  echo $string;
  echo "</tr>";
?>

So far this is fairly OK - it should work.

Next, you set $first to false.

<?php
  $first = false;
?>

If there's another matching row in the database at this point, the while() loop starts over. The if() condition will then evaluate to false, the the code inside will never run, because you set $first to false in the first while() loop.

<?php
  if ($first == true) {
    (...)
  }
?>

The result is that for any row but the first one, the only code that will run within your while() loop is the following:

<?php
  echo "<tr>";
  echo $string;
  echo "</tr>";
  $first = false;
?>

(And $string is not being repopulated for these rows.)

Kind regards,
- S.
Hi there! If you found this post useful, or used this information to help others, we would greatly appreciate a link back to our forum from your website/blog. Thanks! =)
0

#3 User is offline   dunnkers Icon

  • Newbie
  • Pip
  • Group: Members
  • Posts: 3
  • Joined: 15-July 10

Posted 15 July 2010 - 09:01 PM

Thanks for your reply, Shoel.

First off i've changed this part slightly:
	$imploded = implode(",", $vars);
	$sql = mysql_query("SELECT $imploded FROM $table");
	while ($row = mysql_fetch_array($sql)) {
		for ($i = 1; $i < $length + 1; $i ++) {
			$string .="<td>".$row[$vars[$i]]."</td>";
		}
		echo "<tr>";
		echo $string;
		echo "</tr>";
		$first = false;
	} 

This results the same as the image above, only without the first two rows with user 'total'
The point is, I do not know how to make it that way that the code being spawned, is exact what you defined to select in the table. So i only say like.. Give me this this and this. and the rest of the 'method' gives you that.
0

#4 User is offline   dunnkers Icon

  • Newbie
  • Pip
  • Group: Members
  • Posts: 3
  • Joined: 15-July 10

Posted 15 July 2010 - 09:41 PM

[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;
?>

0

#5 User is offline   Shoel Icon

  • Administrator
  • Icon
  • View blog
  • Group: Administrators
  • Posts: 38
  • Joined: 22-April 10

Posted 15 July 2010 - 09:46 PM

Glad you managed to solve it yourself, congrats. :) (I had to go AFK.)

I was going to suggest the following: (I'll drop the walk-through seeing as you've already solved it.)

<?php
// Define table.
$table = 'mytable';

// Define colum names.
$vars = array(
  'user', 
  'runtime', 
  'created', 
  'xpgained', 
  'profit'
);

// Run query to fetch the specified colums.
$sql = mysql_query(sprintf("SELECT %s FROM %s", implode(",", $vars), $table));

// Print opening tag of our table + first row.
echo '<table border="1"><tr>';

// Walk through the $vars array and print the column names wrapped in table cells.
foreach ($vars as $value){
  echo '<td>' . $value . '</td>';
}

// Close the first row.
echo '</tr>';

// Fetch rows from mysql result one by one
while ($row = mysql_fetch_row($sql)){
  
  // Open new row.
  echo '<tr>';
  
  // Print the result values wrapped in table cells.
  foreach ($row as $value){
    echo '<td>' . $value . '</td>';
  }
  
  // Close the row.
  echo '</tr>';
}

// Close the table.
echo '</table>'';
?>

(Which is pretty much the same, just with direct output - following the method you originally used.)

PS: Good to see you use '' instead of "" as well - it does actually matter performance vise. When you use double quotes, PHP tries to interpret/parse the content - while if you use single quotes, you tell it that it's just a plain string.
Hi there! If you found this post useful, or used this information to help others, we would greatly appreciate a link back to our forum from your website/blog. Thanks! =)
0

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users