Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

PHP do not show first two fields

Posted on 2009-07-14
10
Medium Priority
?
314 Views
Last Modified: 2012-05-07
Hello experts.

I''m extracting rows of data from a table dependant on a user_id. The table currently looks like this:

user_id     type_id     first_name     last_name     age    city
00001         00001       John                Smith            30    London

If I use the code I have below the result looks roughly like this:
User Id     00001
Type Id     00001
First Name   John
Last Name     Smith
Age     30
City      London

The thing I want to do is not show the user_id and type _Id as they mean nothing to the the end user. is there an easy way of doing this? thanks
$query = mysql_query('select * from users where user_id = 00001');
$row = mysql_fetch_assoc($query);
echo '<table>';
	foreach ($row as $key => $value) {
		$key = str_replace("_", " ", $key);
		$key = ucwords($key);
		echo '<tr>';
		echo '<td>'.$key.'</td>';
		echo '<td>'.nl2br($value).'</td>';
		echo '</tr>';
	}
echo '</table>';

Open in new window

0
Comment
Question by:allanch08
  • 7
  • 3
10 Comments
 
LVL 28

Expert Comment

by:gamebits
ID: 24854349
Just select what you need to show

'select first_name, last_name, age, city from users where user_id = 00001'
0
 

Author Comment

by:allanch08
ID: 24854411
thanks for help. I forgot to mention the tricky bit. if 'users' is a variable, ie:

select * from $var where user_id = 00001'

$var would be the name of any table in the database,  so I won't know exactly what the fields to show are as they vary from table to table but they all have user_id in them.
0
 
LVL 28

Assisted Solution

by:gamebits
gamebits earned 800 total points
ID: 24854453
If they all have user_id and type_id than you could do something like this

if('.$key.' != 'user_id'){echo '<td>'.$key.'</td> <td>'.nl2br($value).'</td>';}
if('.$key.' != 'type_id'){echo '<td>'.$key.'</td> <td>'.nl2br($value).'</td>';}
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

Author Comment

by:allanch08
ID: 24854532
thanks, tried that but it has no effect on the results displayed
0
 

Author Comment

by:allanch08
ID: 24854544
changed the order and it works. but what if I have some tables that may not have type_id but a different id?
	foreach ($row as $key => $value) {
		if($key != 'user_id'){
		$key = str_replace("_", " ", $key);
		$key = ucwords($key);
		echo '<tr>';
		echo '<td>'.$key.'</td> <td>'.nl2br($value).'</td>';
		echo '</tr>';
		}

Open in new window

0
 
LVL 28

Expert Comment

by:gamebits
ID: 24854656
How many tables are we talking about? How many different field names?

If you want to make a very dynamic system you may want to look to implement some naming convention.

Meanwhile if you where going to add some more if statement even though they may not be related to the table you are querying I don't think it would hurt anything, theif statement is checking for a field name and if doesn't exist then there will be nothing to display anyway.

I would experiment with that.
0
 

Author Comment

by:allanch08
ID: 24856644
okay thanks, another way of putting it is I'm trying to hide the id's from the end user so all fields that end in '_id' will hidden. I wonder if there is a wildcard that I can us so that all fields suffixed '_id' are not displayed.
0
 

Author Comment

by:allanch08
ID: 24857250
I was wondering if I can use preg_match to determine if any fields have '_id' and then discount them?
   foreach ($row as $key => $value) {
                if(preg_match('/^._id$/', $key){
                $key = str_replace("_", " ", $key);
                $key = ucwords($key);
                echo '<tr>';
                echo '<td>'.$key.'</td> <td>'.nl2br($value).'</td>';
                echo '</tr>';
                }

Open in new window

0
 

Author Comment

by:allanch08
ID: 24861755
no that doesn't work as preg_match just includes the files that do have '_id'
0
 

Accepted Solution

by:
allanch08 earned 0 total points
ID: 24861796
think i got it!
	foreach ($row as $key => $value) {
		if(!preg_match('/_id$/', $key)) {
		$key = str_replace("_", " ", $key);
		$key = ucwords($key);
		echo '<tr>';
		echo '<td>'.$key.'</td> <td>'.nl2br($value).'</td>';
		echo '</tr>';
		}
	}

Open in new window

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
In this article, I’ll talk about multi-threaded slave statistics printed in MySQL error log file.
The viewer will learn how to count occurrences of each item in an array.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question