943,777 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 503
  • PHP RSS
Nov 18th, 2008
0

fetching unkown column names

Expand Post »
i'm building a website where users can add articles. on the admin side, they can also add new fields on the add articles page.
to allow the user edit the articles, i have to query the database and then assign the results from the query to variables, usually i use the $fieldname.
the problem is, if a user added a new field while he added the article, i can't fetch the content by field anme from the rusult set of the query.
eg:
PHP Syntax (Toggle Plain Text)
  1. $select = $article->select();
  2. $select->where('id =?', $id);
  3. while ($rows = $article->fetchAll($select)){
  4. $this->cat_id = $rows->cat_id;
  5. $this->article_title = $rows->title;
  6. $this->description = $rows->description;
  7. :
  8. :
  9. }
but if i dont know the name of the new field(s) the user added, i can't assign it to a variable.
PS: i was thinking of a solution that would need me to fetch the name of the fields form the article_fields table, but then again i got stuck trying to assign the field name to a variable that had the same name i.e:

PHP Syntax (Toggle Plain Text)
  1. $select = $article_fields->select();
  2. while ($rows = $article_fields->fetchAll($select)){
  3. ${$rows->field_name}= $rows->field_name;
  4. ;

Does anyone have any solutions??
Last edited by cali_dotcom; Nov 18th, 2008 at 9:03 pm.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
cali_dotcom is offline Offline
51 posts
since Apr 2008
Nov 18th, 2008
0

Re: fetching unkown column names

Your select can always be ' select * from whatever'.
Then you just retrieve an associative array using mysql_fetch_assoc, and then run a foreach loop like this

PHP Syntax (Toggle Plain Text)
  1. foreach( $result as $key => $value)
  2. {
  3. echo 'Field name:'.$key."\n Field value".$value;
  4. }
Last edited by Rhyan; Nov 18th, 2008 at 9:04 pm.
Reputation Points: 21
Solved Threads: 26
Posting Whiz in Training
Rhyan is offline Offline
240 posts
since Oct 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: new coder having trouble
Next Thread in PHP Forum Timeline: Learning PHP...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC