# News + Custom DB Columns

**URL:** <https://forum.phpvms.net/t/news-custom-db-columns/2097>\
**Category:** phpVMS Legacy Support (v2/v5)\
**Created:** [February 25, 2010, 6:45pm UTC](https://forum.phpvms.net/t/news-custom-db-columns/2097 "2010-02-25T18:45:55Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kieran](https://forum.phpvms.net/letter_avatar_proxy/v4/letter/k/3d9bf3/32.png) [@Kieran](https://forum.phpvms.net/u/Kieran)\
**Post date:** [February 25, 2010, 6:45pm UTC](https://forum.phpvms.net/t/news-custom-db-columns/2097/1 "2010-02-25T18:45:55Z")

</div>

Hey there,

My VA has three/four separate divisions, and each of them would like their own news category.

The thing is, phpVMs does not have an option for this at the moment.

I can simply add a column to the database, but can somebody point me in the right direction as to how to edit the admin module to display a drop-down with possible menu categories, and to display news from only one category on certain pages?

If all else fails then I could use manual SQL queries but I’d prefer the proper method.

Thank you,

Kieran

---

<div class="post-metadata">

**Author:** ![nabeel](https://forum.phpvms.net/user_avatar/forum.phpvms.net/nabeel/32/2971_2.png) [@nabeel](https://forum.phpvms.net/u/nabeel)\
**Post date:** [February 26, 2010, 2:22am UTC](https://forum.phpvms.net/t/news-custom-db-columns/2097/2 "2010-02-26T02:22:33Z")

</div>

Poke around in the SiteCMS.class.php in core/common. you can modify those to include a category id, or an airline id. you can add in a drop-down with the airlines list, so that would simply that. so you’d just have to pass the $this-\>post-\>airlineid variable through to the query

---

<div class="post-metadata">

**Author:** ![Kieran](https://forum.phpvms.net/letter_avatar_proxy/v4/letter/k/3d9bf3/32.png) [@Kieran](https://forum.phpvms.net/u/Kieran)\
**Post date:** [February 28, 2010, 3:26pm UTC](https://forum.phpvms.net/t/news-custom-db-columns/2097/3 "2010-02-28T15:26:18Z")

</div>

Right I have a function like this (I have successfully coded the add-news section):

```
public static function GetNewsItemByCategory($category,$limit)
{
	if (!$limit) {
	return DB::get_results('SELECT *, UNIX_TIMESTAMP(postdate) AS postdate 
								FROM '.TABLE_PREFIX.'news WHERE category=`'.$category.'`');
	}
	elseif ($limit)
	{
	return DB::get_results('SELECT *, UNIX_TIMESTAMP(postdate) AS postdate 
								FROM '.TABLE_PREFIX.'news WHERE category=`'.$category.'` LIMIT '. $limit);
	}
}

```

Now how would I integrate this into my page, so that I could echo all of the news from a specific category?

---

<div class="post-metadata">

**Author:** ![nabeel](https://forum.phpvms.net/user_avatar/forum.phpvms.net/nabeel/32/2971_2.png) [@nabeel](https://forum.phpvms.net/u/nabeel)\
**Post date:** [February 28, 2010, 4:28pm UTC](https://forum.phpvms.net/t/news-custom-db-columns/2097/4 "2010-02-28T16:28:22Z")

</div>

I simplified it a bit for you, if you don’t mind.

```
public static function GetNewsItemByCategory($category,$limit)
{
$sql = 'SELECT *, UNIX_TIMESTAMP(postdate) AS postdate 
	FROM '.TABLE_PREFIX.'news WHERE category=`'.$category.'`';

if (!empty($limit))
{
	$sql .= ' LIMIT '.$limit;
}

return DB::get_results($sql);
}

```

You can call it as (assuming you put it in SiteCMS, or even better, just create a new dataclass for it so it doesnt get overwritten - to do that just follow the same format as the other classes in there, ClassName.class.php, then inside that is class ClassName { }… in fact, creating a class like that called CustomQueries (so CustomQueries.class.php, and inside class CustomQueries{ } ) to put all your custom code so it’s not overwritten in an update is probably a good idea)

```
$news = SiteCMS::getNewsItemByCategory($category, 1);

```

---

<div class="post-metadata">

**Author:** ![Kieran](https://forum.phpvms.net/letter_avatar_proxy/v4/letter/k/3d9bf3/32.png) [@Kieran](https://forum.phpvms.net/u/Kieran)\
**Post date:** [February 28, 2010, 5:47pm UTC](https://forum.phpvms.net/t/news-custom-db-columns/2097/5 "2010-02-28T17:47:41Z")

</div>

Thank you for that, I now have a Custom.class.php as well.

I have this code on my page now:

```
$news = Custom::GetNewsItemByCategory('notam', 1);

```

But I’m stuck as to what to do with it, an attempt at a For-each loop displays an ‘Invalid Argument Supplied’ error.

---

<div class="post-metadata">

**Author:** ![nabeel](https://forum.phpvms.net/user_avatar/forum.phpvms.net/nabeel/32/2971_2.png) [@nabeel](https://forum.phpvms.net/u/nabeel)\
**Post date:** [March 1, 2010, 5:10pm UTC](https://forum.phpvms.net/t/news-custom-db-columns/2097/6 "2010-03-01T17:10:43Z")

</div>

> Thank you for that, I now have a Custom.class.php as well.
> 
> I have this code on my page now:
> 
> $news = Custom::GetNewsItemByCategory(‘notam’, 1);
> 
> But I’m stuck as to what to do with it, an attempt at a For-each loop displays an ‘Invalid Argument Supplied’ error.

After that line, put a DB::debug() statement, like:

```
$news = Custom::GetNewsItemByCategory('notam', 1);
DB::debug();

```

There might be an error in the SQL, but it will tell you exactly what was returned from the database

---

<div class="post-metadata">

**Author:** ![Kieran](https://forum.phpvms.net/letter_avatar_proxy/v4/letter/k/3d9bf3/32.png) [@Kieran](https://forum.phpvms.net/u/Kieran)\
**Post date:** [March 1, 2010, 6:12pm UTC](https://forum.phpvms.net/t/news-custom-db-columns/2097/7 "2010-03-01T18:12:50Z")

</div>

Got it working now, the DB::debug was empty but I relaised that the ` character should really have been " or ’ .

However, it doesn’t seem to be ordering the query results correctly?

It always seems to return the FIRST post and never the most recent.

---

<div class="post-metadata">

**Author:** ![Kieran](https://forum.phpvms.net/letter_avatar_proxy/v4/letter/k/3d9bf3/32.png) [@Kieran](https://forum.phpvms.net/u/Kieran)\
**Post date:** [March 1, 2010, 7:02pm UTC](https://forum.phpvms.net/t/news-custom-db-columns/2097/8 "2010-03-01T19:02:08Z")

</div>

EDIT: Used an ORDER BY to fix that out…

Nabeel, we could do with you expertise over on the phpBB AutoRegistration Dev Thread,

Thanks…

Kieran
