Solved

PHP do not show first two fields

Posted on 2009-07-14
10
308 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

785 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