Solved

PHP do not show first two fields

Posted on 2009-07-14
10
306 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 200 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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now