Link to home
Start Free TrialLog in
Avatar of WavyGravy
WavyGravyFlag for United States of America

asked on

What is the best way to substitute a variable within a loop?

Alright, here's the deal.

I'm creating a simple CMS of sorts for managing video files.  Obviously, one of the fundamental things this script must do is create an HTML table to display the videos currently in the system.  To resolve this, I did some Googling for PHP code snippets that would accomplish what I'm needed.

What I found was a script (attached) that displays the database records in an HTML table, with one major flaw (in my case).  The top cell of each column (explaining what the data is) is the name derived from the MySQL database.  Makes perfect sense to me, but not what I want to be doing when other people begin to use it.

With that said, I was going to hardcode the values in, but I know there's a better way to do this.

The first idea that popped into my head was to create an IF statement on line 23 that would do the following:

if($field_name = "desc") {
   $field_name ="Description" }

Of course this could be a function too, but I think that I'm moving the wrong way on this one.  Any PHP / MySQL gurus that can lend a hand would be greatly appreciated.
function display_db_query($query_string, $connection,
	$header_bool, $table_params)
	{
		// perform the database query
		$result_id = mysql_query($query_string, $connection)
		or die("display_db_query:" . mysql_error());
		
		// find out the number of columns in result
		$column_count = mysql_num_fields($result_id)
		or die("display_db_query:" . mysql_error());
		
		// Here the table attributes from the $table_params variable are added
		print("<TABLE $table_params >\n");
		
		// optionally print a bold header at top of table
		if ($header_bool)
		{
			print("<TR>");
			for ($column_num = 0;
			$column_num < $column_count;
			$column_num++)
			{
				$field_name =
				mysql_field_name($result_id, $column_num);
				print("<TH>$field_name</TH>");
			}
			print("</TR>\n");
		}
		
		// print the body of the table
		while ($row = mysql_fetch_row($result_id))
		{
			print("<TR ALIGN=LEFT VALIGN=TOP>");
			for ($column_num = 0;
			$column_num < $column_count;
			$column_num++)
			{
				print("<TD>$row[$column_num]</TD>\n");
			}
				print("</TR>\n");
		}
		print("</TABLE>\n"); 
	}
 
	function display_db_table($tablename, $connection,
	$header_bool, $table_params)
	{
		$query_string = "SELECT * FROM $tablename";
		display_db_query($query_string, $connection,
		$header_bool, $table_params);
	}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of brodaseating
brodaseating

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial