Solved

PHP do not show first two fields

Posted on 2009-07-14
10
311 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
WordPress Tutorial 2: Terminology

An important part of learning any new piece of software is understanding the terminology it uses. Thankfully WordPress uses fairly simple names for everything that make it easy to start using the software.

 

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

623 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