• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 345
  • Last Modified:

Replace text from Colum names titles - PHP MYSQL


When I get  the results from a search engine, the column names comes up with the same name that has in the database.


I will like to change the titles for the columns:

name= Market Report Title
organizer_id= Publisher
no_pages= Nº of Pages
publication_date: Date Published
price: Price

This is an extract of the code where I get the results from the query and the columns


// DB Connection //
/* Change the following 3 values to reflect your MySQL Server */
	$MySQLPassword = "***";	
	$HostName = "***";	
	$UserName = "****";
	$Database = "****";

	or die("ERROR: Could not connect to database!");
 	mysql_select_db($Database) or die("cannot select db");

// Data Sorting Variables //
/* The following are used to set the columns for data sorting                */
/* Change these to the table field names in your MySQL database              */
	$default_sort = 'ID';
	$allowed_order = array ('name','publication_date', 'price');

/* if order is not set, or it is not in the allowed
 * list, then set it to a default value. Otherwise, 
 * set it to what was passed in. */
	if (!isset ($_GET['order']) || 
	    !in_array ($_GET['order'], $allowed_order)) {
	    $order = $default_sort;
	} else {
	    $order = $_GET['order'];

// The following line stops the undefined index error on initial page load
	if (isset($_GET['keyword'])) {

	        if(!$_GET['keyword']) {
	          die('<p>Please enter a search term.</p>');

// Database Query Settings //
/* Change these to match your database table, the fields you want displayed and the fields to run the query against. */
	$opcion_busqueda = $_GET['radio'];
	if ($opcion_busqueda == 2){
		$tables = 'swots';
	else {
		$tables = 'reports';
	$return_fields = 'id name organizer_id no_pages publication_date price currency';
	if  ($opcion_busqueda == 3){
		$check_fields = 'table_content description';

	else {
	$check_fields = 'name';

// Get the keyword from the search form.
 	$query_text = $_GET['keyword'];

// Sanitize Data Input
	$clean_query_text =cleanQuery($query_text);

// Call the bq_simple function and store the //
// resulting SQL Query in $newquery variable //
	$newquery=bq_simple ($return_fields, $tables, $check_fields, $clean_query_text);
	$newquery = $newquery . " ORDER BY $order;";

// sql data query construction //
	$result = mysql_query($newquery) or die(mysql_error());

/* make sure data was retrieved */
	$numrows = mysql_num_rows($result);
	if ($numrows == 0) {
	    echo "<H4>No data to display!</H4>";
	echo 	"<p>Your search: '$query_text' within </p>\n";	
	if ($opcion_busqueda == 1){
		echo " market reports titles";}
		else if ($opcion_busqueda == 3) {
		echo " All content";}
	 else {echo " SWOTS";} 	
	 echo "<p>returned ".$numrows. " results.</p>\n";
	echo 	"<p>Click on the headings to sort.</p>\n";

/* now grab the first row and start the table */
	$row = mysql_fetch_assoc ($result);
	echo "<TABLE cellspacing=\"3\" cellpadding=\"8\">\n";
	echo "</TR>\n";
	 foreach ($row as $heading=>$column) {
      if ($heading != 'id') { //don't create a column for ID!
         echo "<TD><b>";
         if (in_array ($heading, $allowed_order)) {
            echo "<a href=\"{$_SERVER['PHP_SELF']}?order=$heading&keyword=$query_text\">$heading</a>";
         } else {
            echo $heading;
         echo "</b></TD>\n";
echo "</TR>\n";

/* reset the $result set back to the first row and 
 * display the data */
	mysql_data_seek ($result, 0);
	while ($row = mysql_fetch_assoc ($result)) {
   //get the publisher name
  $organizerSql = mysql_query("SELECT nazwa FROM baza_obiektow_inne WHERE id=" . $row['organizer_id'] . " LIMIT 1");
   $organizer = mysql_fetch_assoc($organizerSql);
   $pretty_date = date('F Y', strtotime($row['publication_date']));
   echo "<tr>\n";
   $simbolos_reemplazar = array (" ","%","/");
   printf("<td><a href='http://embs-group.com/%s,%s'>%s</a></td>", $row['id'], str_replace( $simbolos_reemplazar, "_", $row['name']), $row['name']);
   printf("<td>%s</td>", $organizer['nazwa']);
   printf("<td>%s</td>", $row['no_pages']);
 printf("<td>%s</td>", $pretty_date);
   printf("<td>%s</td>", $row['price']);
   printf("<td>%s</td>", $row['currency']);
   echo "</tr>\n";



Open in new window

Klaus Andersen
Klaus Andersen
  • 2
1 Solution
if ($heading != 'id') { //don't create a column for ID!
	echo "<TD><b>";
	// Just add in here your changes
	if($heading=="name"){$new_heading="Market Report Title";}
	// etc
	// etc
	if (in_array ($heading, $allowed_order)) {

		echo "<a href=\"{$_SERVER['PHP_SELF']}?order=$heading&keyword=$query_text\">$new_heading</a>"; // Use new var here
	} else {
		echo $new_heading;
	echo "</b></TD>\n";

Open in new window

Klaus AndersenAuthor Commented:
It worked like a charm Gary, many thanks!

By the way, taking advance that you understood quite quickly my code, could you give me a hand with this question?


I´m stuck there from some days and will be amazing if you can give me a hand, thanks!
Klaus AndersenAuthor Commented:
@GaryC123 provided me a fast and exact answer for my problem. After the test it worked perfectly. Thanks!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now