Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba

how we display fetched data in page-results.ss file, i am new in silver strip


Go to End
Reply


9 Posts   1528 Views

Avatar
sunil_bisht

Community Member, 5 Posts

17 April 2012 at 9:38pm

Edited: 17/04/2012 10:03pm

<?php
class Page extends SiteTree {

static $db = array(

);

public static $has_one = array(

);
public static $has_many = array(

);

public function getCMSFields() {
$fields = parent::getCMSFields();

return $fields;
}

}
class Page_Controller extends ContentController {

//Define our form function as allowed
static $allowed_actions = array(
'Searchform'
);

//The function which generates our form
function Searchform()
{
// Create fields
$fields = new FieldSet(
new DropdownField('search_user','',
array ('used' => 'used product', 'new' => 'new product')),
new TextField('search_product', '')

);

// Create action
$actions = new FieldSet(
new FormAction('results', 'Send')
);

// Create action
$validator = new RequiredFields('search_user', 'search_product');

return new Form($this, 'Searchform', $fields, $actions, $validator);
}

function results($data, $form)
{
//Set data
echo $From = $data['search_user'];

echo $From1 = $data['search_product'];

$sql_search ="select * from productuseditem where ProductNameUsed LIKE '%".$From1."%'";
$sql_search1=mysql_query($sql_search);
$result = mysql_num_rows($sql_search1);
$data = mysql_fetch_array($sql_search1);
print_r($data);

return $this->customise($data)->renderWith(array('Page_results', 'Page'));

}

}

?>

the fetched data will be on $data in results function but i cant understand that how we display that data on page_results.ss page.

for this line /* return $this->customise($data)->renderWith(array('Page_results', 'Page')); */
it will redirect into page_results.ss page but how we display that data.. please help me i ma new in silver strip..

Avatar
Willr

Forum Moderator, 5514 Posts

18 April 2012 at 9:34pm

You shouldn't use direct mysql commands in SS but DataObject::get() and SQLQuery() as wrappers. Your results function might look something like

function results($data, $form) {
$From = $data['search_user'];
$From1 = Convert::raw2sql($data['search_product']); // ALWAYS ESCAPE your inputs when putting into sql

return DataObject::get('ProductUsedItem', "ProductNameUsed LIKE '%". $from1 ."%'");
}

If your ProductUsedItem is not a DataObject and you need to use sql see http://doc.silverstripe.org/sapphire/en/reference/sqlquery

Avatar
sunil_bisht

Community Member, 5 Posts

18 April 2012 at 10:07pm

Thanks willar for giving me your time but my problem is 70% solved according to my coding. my another problem is :-

function results($data, $form)
{

$From = $data['search_user'];

$From1 = $data['search_product'];
if($From == 'used')
{

$sql_search ="select a.ConditionText AS conditions,b.Filename AS filename,a.Description AS descr,a.ProductNameUsed AS productname,a.Price AS price,a.PhotoID AS photo from Productuseditem a INNER JOIN File b where a.ProductNameUsed LIKE '%".$From1."%' AND a.PhotoID = b.ID";
$sql_search1=mysql_query($sql_search);
$result = mysql_num_rows($sql_search1);
if($result !=0)
{
$data = mysql_fetch_array($sql_search1);

}
else
{
echo "<script type='text/javascript'>
alert('sorry , Your Search Query Didnot Return Any Result'); </script>";
}
return $this->customise($data)->renderWith(array('Page_results', 'Page'));
}
}

like i am searching "sony" then 60 records found but only 1 shown in ss file . so please help me that how i show all 60 records in my ss file . my ss file is :---------

<div id="middlecolumnmain">

<% if data %>

<ul id="SearchResults">

<li>Product Name : - $productname </li>
<li>Conditions :- $conditions</li>
<li>Description :- $descr</li>
<li>Price :- $price (ex vat)</li>

<li> <img src = "http://localhost/esbroadcast/$filename" alt="img" /></li>

<li>$data</li>

</ul>

<% else %>
<h1>Sorry, your search query did not return any results.</h1>
<% end_if %>
</div>

Avatar
Willr

Forum Moderator, 5514 Posts

18 April 2012 at 10:18pm

Well like I said, don't use mysql_query directly. Your query could be reduced down to a DataObject::get() call and returned to the template. In the template you can then use <% control %> to loop

function results($data, $form) {
$From = $data['search_user'];
$From1 = $data['search_product'];

if($From == 'used') {
return new ArrayData(array(
'Results' => DataObject::get('ProductUsedItem', "ProductNameUsed LIKE '%".$From1."')
));
}

return false;
}

Then in your Page_results template use
<% if Results %>
<ul id="searchResults">
<% control Results %>
<li>Product Name : - $productname </li>
<li>Conditions :- $conditions</li>
<li>Description :- $descr</li>
<li>Price :- $price (ex vat)</li>
<li>$File.URL</li>
<% end_control %>
</ul>
<% else %>
No Results!
<% end_if %>

Avatar
sunil_bisht

Community Member, 5 Posts

18 April 2012 at 10:57pm

thanks For your Reply, but i have an error in my code:--

function results($data, $form)
{
//Set data
$From = $data['search_user'];

$From1 = $data['search_product'];

$sqlQuery = new SQLQuery();

$sqlQuery->select = array(
'productnewItem.ConditionText AS conditions',
'file.Filename AS filename',
'productnewItem.ProductNameNew AS productname',
'productnewItem.Description AS descr',
'productnewItem.Price AS price',
'productnewItem.PhotoID AS photo'
);
$sqlQuery->from = array(
"productnewItem",
"INNER JOIN file ON productnewItem.PhotoID = file.ID"
);
$sqlQuery->where = array("productnewItem.ProductNameNew LIKE '%".$From1."%'");

$Results = $sqlQuery->execute()->value();
return $this->customise($Results)->renderWith(array('Page_results', 'Page'));
}

and in ss file i ahev used :--

<% if Results %>
<ul id="searchResults">
<% control Results %>
<li>Product Name : - $productname </li>
<li>Conditions :- $conditions</li>
<li>Description :- $descr</li>
<li>Price :- $price (ex vat)</li>
<li>$File.URL</li>
<% end_control %>
</ul>
<% else %>
No Search Results!
<% end_if %>

and my error is :------- [User Error] Uncaught InvalidArgumentException: ViewableData->customise(): $data must be an associative array or a ViewableData instance
POST /esbroadcast/index.php/home/Searchform

Line 210 in D:\xampp\htdocs\esbroadcast\sapphire\core\ViewableData.php
Source

201 public function customise($data) {
202 if(is_array($data) && (empty($data) || ArrayLib::is_associative($data))) {
203 $data = new ArrayData($data);
204 }
205
206 if($data instanceof ViewableData) {
207 return new ViewableData_Customised($this, $data);
208 }
209
210 throw new InvalidArgumentException (
211 'ViewableData->customise(): $data must be an associative array or a ViewableData instance'
212 );
213 }
214
215 /**
216 * @param ViewableData $object

Avatar
sunil_bisht

Community Member, 5 Posts

19 April 2012 at 12:06am

Please help me, where is my mistake and how i show the fetched data in page-results.ss file.
function results($data, $form)
{
//Set data
$From = $data['search_user'];

$From1 = $data['search_product'];

$sqlQuery = new SQLQuery();

$sqlQuery->select = array(
'file.Filename AS filename',
'productnewItem.ConditionText AS conditions',
'productnewItem.ProductNameNew AS productname',
'productnewItem.Description AS descr',
'productnewItem.Price AS price',
'productnewItem.PhotoID AS photo'
);
$sqlQuery->from = array(
"productnewItem",
"INNER JOIN file ON productnewItem.PhotoID = file.ID"
);
$sqlQuery->where = array("productnewItem.ProductNameNew LIKE '%".$From1."%'");

$data = $sqlQuery->execute()->value();

$Data = array(
'data' => $data
);

return $this->customise($Data)->renderWith(array('Page_results', 'Page'));
}

and my page_results.ss page coding is :---------

<div id="middlecolumnmain">

<% if Data %>

<ul id="searchResults">
<% control Data %>
<li>Product Name : - $data </li>
<li>Conditions :- $conditions</li>
<li>Description :- $descr</li>
<li>Price :- $price (ex vat)</li>
<li>$File.URL</li>
<% end_control %>
</ul>
<% else %>
No Search Results!
<% end_if %>

</div>

Avatar
Willr

Forum Moderator, 5514 Posts

19 April 2012 at 6:02pm

Remember, templates work with Viewable data objects (e.g dataobjectsets) so you have to return that to your template:

http://doc.silverstripe.org/sapphire/en/reference/sqlquery#transforming-a-result-to-dataobjectset-http-api-silverstripe-org-search-lookup-q-dataobjectset-and-version-2-4-and-module-sapphire

..
$result = $sqlQuery->execute();
$set = singleton('productnewItem')->buildDataObjectSet($result);

return $this->customise(new ArrayData(array(
'Data' => $set
)))->renderWith(array('Page_results', 'Page'));

Then use <% control Data %>

Avatar
sunil_bisht

Community Member, 5 Posts

19 April 2012 at 6:14pm

Thanks willar Thanks you so much for hundred times.. please few more help.

Please help me to provide the code of paging in mysql data. i have an successfully executed query and an result of 60 records and i want 10 records in one page...
my page.php code is :--

function results($data, $form)
{
//Set data
$From = $data['search_user'];

$From1 = $data['search_product'];

if($From =='new1')
{
$sqlQuery = new SQLQuery();

$sqlQuery->select = array(
'file.Filename AS filename',
'productnewItem.ConditionText AS conditions',
'productnewItem.ProductNameNew AS productname',
'productnewItem.Description AS descr',
'productnewItem.Price AS price',
'productnewItem.PhotoID AS photo'
);
$sqlQuery->from = array(
"productnewItem",
"INNER JOIN file ON productnewItem.PhotoID = file.ID"
);
$sqlQuery->where = array("productnewItem.ProductNameNew LIKE '%".$From1."%'");

$rawSQL = $sqlQuery->sql();

$result = $sqlQuery->execute();

$dataObject1 = new DataObjectSet();
foreach($result as $row)
{
$dataObject1->push(new ArrayData($row));
}
return $this->customise(array("Pictures" => $dataObject1))->renderWith(array('Page_results', 'Page'));
}
}

and my displaying page_results.ss page is :-

<div id="middlecolumnmain">

<% if Pictures %>
<% control Pictures %>
<h2>Your Search Result :-- </h2>
<ul id="SearchResults">

<li>$productname</li>
<li>$conditions</li>
<li>$descr</li>
<li>$price></li>
<li> <img src = "http://localhost/esbroadcast/$filename" alt="img" width="100" height="70" /></li>

</ul>
<% end_control %>
<% else %>
<h1>Sorry, your search query did not return any results.</h1>
<% end_if %>

</div>

Please help me regarding to the pagination code . i have an 60 records and i want 10 records in one page so Please help me ???

Go to Top