Renaming Variables
2 posts by 2 authors in: Forums > CMS Builder
Last Post: October 10, 2013 (RSS)
Let's say I have two tables set-up called "articles" and "products". I also have a search which allows the user to search either table.
It works fine. The only trouble is that the products and articles tables have different field names. For example, the main body of text in an article is called the "content" - whereas for the product it is called the "description". The upload field for an article might be "image" but for a product it could be "photo". And so on...
I want the search page to show the results in the same format - regardless of which table is searched. Each entry would have a small thumbnail, the title of the item and a small extract of text.
Currently, however, this is a bit problematic due to the different variable names. I have to use lots of conditional if() statements to switch between the values being echoed based on whether the returned data is from the products or articles tables.
What I'm looking for is a useful technique for renaming a variable into something generic. So if either $var['description'] or $var['content'] is returned simply rename it $var['text'].
I can then echo $var['text'] on the results page.
:0S
Perchpole
By Daryl - October 10, 2013
Hi Perchpole,
Have you tried using ternary operators?
Example
$text = (@$var['description'] != '' ? @$var['description'] : @$var['content']);
Hope this helps.
Cheers!
PHP Programmer - interactivetools.com