I am trying using the jqgrid. I set the column, and i attached to that the widget. Everything is working except the grid does not contain data. It only contains an empty row. Column, widgets (like the calendar) works.
The following is the beginning of my grid setting:
// Create the jqGrid instance
$grid = new jqGridRender($conn);
// Write the SQL Query
$grid->SelectCommand = "SELECT * FROM `$table_name`";
// set the ouput format to json
$grid->dataType = 'json';
$grid->table = "$table_name";
$grid->setPrimaryKeyId("matter_party_ID");
$grid->serialKey = false;
$grid->setColModel();
// Set the url from where we obtain the data
$grid->setUrl(????????);
$grid->addCol(array( etc. etc.
As you can see I am retriving the data with a a database query and returning a jason object to the grid:
// Write the SQL Query
$grid->SelectCommand = "SELECT * FROM `$table_name`";
// set the ouput format to json
$grid->dataType = 'json';
But the data is not there. After few invane searches I have been suggested to add the line:
$grid->setUrl(????????);
But I don't understand it. Why do I need to set a url if the data has been fetch on the current url using a select? Can you help?
Thank you
Watch the link. As given here when you are not assigning arrays to the jqGrid you need to provide a url from where the data can be retrieved. This is because the jqgrid needs formatted data in the form of json or xml and doesn't process resultset directly. You could select the data and process it as required as shown in the link and then use the php as a url for your jqgrid which is defined in another php file.
I am answering my own question in case someone needs to solve the same or a similar problem. In the:
$grid->setUrl(????????);
I pass a php file like grid.php that calls the function that builds the grid again!
$grid->setUrl('grid.php');
The problem is that when I generate a file through my own MVC it automatically adds a header and a footer compromising the json object returned by the grid constructor and not allowing the grid to be populated! I got rid of header and footer and the data magically reappeared!
Related
As part of a search routine for a specific crop, I'm making two calls to the same database table, merging the results and sending them down the line. The first call looks for data relating to the searched-for crop (e.g. "beans"). The second call looks for data relating to the crop group of that crop (e.g. legumes).
Data returned from the first call will be more relevant/focused than that from the second call. I want to add an identifier to the respective data sets that reflects this so that I can subsequently sort/present the data on the basis of relevance in my Vue component.
The following code extracts the crop-specific information from the database; how can I add/insert/append a new variable (e.g. "relevance" = 1) to each row in $factsheets before I "array_merge" it with the data returned from the crop-group sql call?
(For sake of simplicitly, I've not included the code that determines the crops.id value from the name of the crop entered by the user.)
public function getFactsheets($cropId){
$factsheets = Factsheet::whereIn('crop_id',$cropId)
->join("crop_factsheet as cf","factsheets.id","=","cf.factsheet_id")
->join("crops as crops","crops.id","=","cf.crop_id")
->select('crops.name','title', 'factsheets.id', 'shortdesc', 'shortimg', 'factsheets.slug')
->orderBy('crops.name')
->get()->toArray();
return $factsheets;
}
Thanks, Tom.
If you give & to value so whatever changes will happen to value will directly saved on its address.
foreach($factsheets as $key => &$factsheet){
$factsheet['relevance'] = 1;
}
Working demo.
Here is concise explanation of references in official doc.
you can simply do a foreach
foreach($factsheets as $key => $factsheet){
$factsheet['relevance'] = 1;
}
I have a php,jquery,foundation ecommerce website with a topbar menu that when document ready i do a ajax call to get the top images and description of each category in the menu.
the top images will not suffer daily changes so i could store the result in a json file and by that i wouldn't do a mysql conection on every page.
(but i have to do a page when the admin of ecommerce does an update that updates json file)
my question: is it worth it? is there other alternative like store the incoming ajax call on cache?
thanks
You can create a database table and save a cached version of the JSON response server side, updating it after each update (manually or with some versioning method)
Example:
php (pseudocode):
if (is_JSON_changed()) //some mechanism to check if the cache is not valid
{
$query = Get infos from DB;
$array = convert $query to array;
$string = json_encode($array);
file_put_contents("/tmp/cache.json", $string);
echo $string;
} else {
file_get_contents("/tmp/cache.json");
}
You can, in addition, use http cache headers:
How to use HTTP cache headers with PHP
Im trying to set a working variable i can use later on in my code, ive got an id in the url that references an attribute in an external data feed. You can see a copy of the xml feed HERE
the id comes in the url like this - /page.php?id=52115351
At the moment im setting my working variable as bellow, buts its just being set to the first instance of 'market' rather than being set against the instance thats got the same id as the one in the url.
$wh_odds = $wh_xml->response->williamhill->class->type->market->participant;
$wh_odds_attrib = $wh_odds->attributes();
$wh_odds_attrib['name'];//name
How would i implement $_GET['id'] with this block so that it would be making the working variable $wh_odds_attrib['name'] from the participant of the correct instance of 'market' in the xml feed ?
If you're using simpleXML you could try something like this:
$simpleXml = simplexml_load_file('test.xml');
$marketNode = $simpleXml->xpath("/oxip/response/williamhill/class/type/market[#id='{$_GET['id']}']");
$attributes = $marketNode[0]->participant->attributes();
echo $attributes['name'];
I am not entirely sure what you are attempting to do but you can just assign the $_GET['id'] to whatever you want, ie $id = $_GET['id'] allowing you to use it in string manipulation (via sprintf or whatever).
A little more information about what you are trying to do, would help
I have a mySQL database table setup called site.
Rather than qet the column headings to produce a PHP object of values I want to produce data from the rows.
TABLE: site
:::field:::::value:::::
url www.example.com
name example site
etc Example Etcetera
So I want to be able to get this information from the server by calling the column name I want and the row I am after. I want to do this for all fields in site as I don't want to do multiple calls for the various different rows in site; I'd rather store all the information from the beginning in the object:
eg. <? echo $site['url']; ?>
I created this put it appears to be causing an error:.
$sql = "SELECT * FROM `site`";
$site[];
foreach($sodh->query($sql) as $sitefield){
$site[$sitefield['field']] = $sitefield['value'];
}
Obviously I've missed something. ¿Any idea as to what?
$site[];
should be
$site = array();
I'm trying to use the Joomla framework to make a create in the main content table.[http://docs.joomla.org/How_to_use_the_JTable_class] This works fine except that some data comes from posted variables and some from logic that happens when a file is uploaded moments before (store the random image name of a jpg)
$data=&JRequest::get('post');
this takes a ref to the posted values and I want to add to this Array or Object my field. The code I have makes the new record but the column images, doesnt get my string inserted.
I am trying to do something like$data=&JRequest::get('post');
$newdata=(array)$data;
array_push($newdata,"images"=>"Dog");
i make newdata as data is a ref to the posted variables and i suspect wont there fore allow me to add values to $data.
I'm a flash guy normally not a php and my knowledge is letting me down here.
Thanks for any help
Right, first thing:
$data=&JRequest::get('post');
$data is an array, you do not have to cast it. To add another element to the array as described in the comments do this:
$data['images'] = 'cats';
If you are using normal SQL to do the insert then you would do something like this to get the last inserted id e.g. the id of the row you just inserted:
$db = $this->getDBO();
$query = 'Some sql';
$db->setQuery($query);
if (!$db->query()) {
JError::raiseWarning(100, 'Insert failed - '.$db->getErrorMsg());
}
$id = $db->insertid();
If you are developing in Joomla I suggest you use the db functions provided to you rather than mysql_insert_id();
[EDIT]
If you want to use store then you can get the last inserted id like so:
$row->bind($data);
$row->check();
$row->store();
$lastId = $row->id;