Solved

PHP do not show first two fields

Posted on 2009-07-14
10
310 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
P3P validator , ASP, PHP ,Https and Http links 3 45
SQL Function 4 38
Code not executing correctly. 3 44
how can I see the time difference in only seconds? 4 40
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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 create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

751 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