3066 Posts in 866 Topics by 648 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 1999 Views |
-
Problem with dataobjectset and array

26 April 2010 at 7:26pm
Hi!
i have a function,function GetNews(){
$doSet = new DataObjectSet();
$news = DataObject::get("News","");
for($i=0; $i<$news->count(); $i++){
$header = $news->HEADER_;
$summary = $news->$SUMMARY_;
$date = $news->$DAYNEWS_;
$records = array (
'HEADER' => $header,
'SUMMARY' => $summary,
'DATE' => $date
'NUM' => $i+1);
$doSet->push(new ArrayData($records));
}
return $doSet;
}and when I call the template function GetNew
<% control GetNew %>
$HEADER
-------$NUM
<% end_control %>only the output value of $NUM. And $HEADER does not return anything, database has data
-
Re: Problem with dataobjectset and array

27 April 2010 at 2:20am
Is this a copy of your code?
In that case you missed an s at the end of GetNews in the template.
If this isn't a copy the next question is why are you simply copying over a DataObjectSet to ArrayData?You aren't adding anything to the data and you aren't filtering so the function could simply be
function GetNews(){
return DataObject::get("News");
}
Simply use $Pos in the template to get the order number instead of iterating through it and adding it manually as NUM. -
Re: Problem with dataobjectset and array

27 April 2010 at 10:40am
And don't use a for, use a foreach.
If you were set on using a for, don't use $i < $news->Count() because that evaluates the count EVERY ITERATION to compare it to $i. Store the count as a var if you must really use a for.
-
Re: Problem with dataobjectset and array

27 April 2010 at 9:40pm
Thanks, I also had use $Pos. But when the next page, the next number does not increase. I want the new page it continues to increase. For example, page number 1 i was getting 1,2,3,4,5, then the next page i will getting 6,7,8,9,10 ...
Is this a copy my code:
function GetNews(){
if(!isset($_GET['start']) || !is_numeric($_GET['start'])) $_GET['start'] = 0;
return $this->start = (int)$_GET['start'];
$doSet = new DataObjectSet();
$news = DataObject::get("News","",,"ORDER_ DESC","", "$this->start,5");
for($i=0; $i<$news->count(); $i++){
$header = $news->HEADER_;
$summary = $news->$SUMMARY_;
$date = $news->$DAYNEWS_;
$records = array (
'HEADER' => $header,
'SUMMARY' => $summary,
'DATE' => $date
'NUM' => $i+1+$this->start);
$doSet->push(new ArrayData($records));
}
return $doSet;
}I use paging is available in silverstripe, and $start is the number of records in a page.
-
Re: Problem with dataobjectset and array

28 April 2010 at 1:54am
Right, there are so many things wrong with that code.
1. for loop. You are accessing $news in every loop of the for, thus creating 4 references to the DOS.
2. you are refering to $news->$SUMMARY_; and $HEADER_; thus, returning null for you.
3. you have too many commas in your get().
4. its inefficient in many ways that i wont go into.is that really your code? because you have a return right at the top.. so how on earth it would ever create a DOS for you, i have no idea.
try this:
function GetNews(){
if(!isset($_GET['start']) || !is_numeric($_GET['start']) || $_GET['start'] < 0) $_GET['start'] = 0;
$this->start = $_GET['start'];
$news = DataObject::get("News",null,"ORDER_ DESC",null,$this->start . ',5');
foreach ($news as $item) {
$item->Pos = $item->Pos()+$this->start;
}
return $news;
}This is *not* tested, but that should be some kind of starting point for you.
Enjoy
| 1999 Views | ||
|
Page:
1
|
Go to Top |


