I have an array ($fields_order_obj) which contains the names and orderindex of a list I want to print in a table.
In another array ($data) I have all the values I want to print, but not in the same order as in the $fields_order_obj array. I fetch this array out of SQL with a query with fetchmode ADODB_FETCH_ASSOC and I go trought each row as with foreach ($data as $row) {}
$fields_order_obj ([name] => Del_Month [size] => 10 [reportindex] => 0, [name] => Del_Date [size] => 10 [reportindex] => 1, [name] => Article [size] => 10 [reportindex] => 3, ect.)
$data [0]([name] => Article [value] => "A", [name] => Del_Date [value] => "03-03-21", [name] => Del_Month [value] => "March"),[1]([name] => Article [value] => "B", [name] => Del_Date [value] => "01-02-21", [name] => Del_Month [value] => "Feb"), ect.
What I have so far:
print "<table style=\"width:100%\">";
print " <tr>\n";
foreach ($fields_order_obj as $obj){
print "<th>".$obj->name."</th>\n";
}
print " </tr>\n";
foreach ($data as $row) {
print " <tr>\n";
foreach ($row as $name => $value) {
print "<td>".$value."</td>\n";
}
}
print " </tr>\n";
Obviously the data is not printed in the order I want, I need to check and compare the name of the field and then check the reportindex to print it in the correct order, but I don't seem to be able to write the right code...
This did the trick:
print "<table style=\"width:100%\">";
print " <tr>\n";
foreach ($fields_order_obj as $obj){
print "<th>".$obj->name."</th>\n";
}
print " </tr>\n";
foreach ($data as $row) {
print " <tr>\n";
foreach ($fields_order_obj as $obj){
$fieldName = $obj->name;
print "<td>".$row[$fieldName]."</td>\n";
}
}
print " </tr>\n";
I suppose your code should be something like this:
print "<table style=\"width:100%\">";
print " <tr>\n";
foreach ($fields_order_obj as $obj){
print "<th>".$obj->name."</th>\n";
}
print " </tr>\n";
foreach ($data as $row) {
print " <tr>\n";
foreach ($fields_order_obj as $obj){
print "<td>".$row[$obj->name]."</td>\n";
}
}
print " </tr>\n";
You should iterate over your fields array to keep printing in the intended order and get row data by the field name. Keep in mind that you should aware of situations if your data has no all fields specified in the $fields_order_obj.
Related
How to insert each product with its id, qty, title, price, type from below multidimensional associative array(which comes from JSON) to MySQL Database using PHP? I have tried foreach loop but not allowing to echo or insert it, gives an error. I want to insert values into database i.e to insert each product with its id, qty, title, price, type
$array = Array
(
[page] => 1
[current] => 100
[count] => 5
[pageCount] => 1
[data] => Array
(
[0] => Array
(
[product] => Array
(
[id] => 11
[qty] => 30
[title] => abc
[price] => 200
[type] => men
)
[info] => Array
(
[brand] => xyz
)
)
[1] => Array
(
[product] => Array
(
[id] => 12
[qty] => 50
[title] => dfg
[price] => 300
[type] => girl
)
[info] => Array
(
[brand] => jkl
)
)
)
)
</pre>
Have you tried something like this?
foreach ($array["data"] as $item) {
echo $item["product"]["id"];
echo $item["product"]["qty"];
// and so on for each field
echo $item["info"]["brand"];
}
Else can you be more specific on the format that you need printed?
If you want to insert into a database, iterate over your array, break out the fields manually and use code like below to insert into a database
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john#example.com')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
Finally, I found the correct solution. Please find code below.
$i=0;
foreach($array['data'][$i] as $a){
$id = $a['product']['id'];
$qty = $a['product']['qty'];
$title = $a['product']['title'];
$price = $a['product']['price'];
$type = $a['product']['type'];
$sql = "INSERT INTO table1 (id,qty,title,price,type) values ('$id','$qty','$title','$price','$type')";
$i++;
}
$i=0;
I have an entity bundle called Map Points that has a taxonomy term reference field of countries. I'm using EntityFieldQuery in a custom module to list out all the map point entities but I need to group them by the country reference field.
Here is the code I have right now which filters the query by tid 1 and I know I can repeat the query for each country to group the results but I'm looking for a more refined way to group the results by the country term id's without having to write a new query for each country.
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', 'givepower')
->entityCondition('bundle', 'map_points')
->propertyCondition('type', 'map_points', '=')
->fieldCondition('field_map_point_country', 'tid', '1');
$result = $query->execute();
// set empty map point id array
$map_point_ids = array();
// loop through all givepower entities
foreach($result['givepower'] as $record) {
$map_point_ids[] = $record->id;
}
// load entities
$map_point = entity_load('givepower', $map_point_ids);
// set entity view
$entities = entity_view('givepower', $map_point);
return $entities;
I believe I figured this out. Here's the finished code:
//load recent_work_countries taxonomy
$vocabulary = taxonomy_vocabulary_machine_name_load('recent_work_countries');
// load all terms in the recent_work_countries taxonomy
$terms = entity_load('taxonomy_term', FALSE, array('vid' => $vocabulary->vid));
// set empty countries array
$countries = array();
// loop through each recent_work_countries term
foreach($terms as $tid => $value) {
// set country term name and term id(tid) vars
$country_name = $terms[$tid]->name;
$country_tid = $terms[$tid]->tid;
// add each country to the counties array
$countries[$country_name] = array();
// create new query to grab all map point entities with the current recent_work_countries tid
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', 'givepower')
->entityCondition('bundle', 'map_points')
->propertyCondition('type', 'map_points', '=')
->fieldCondition('field_map_point_country', 'tid', $country_tid);
$result = $query->execute();
// set empty tab id array
$map_point_ids = array();
// loop through all givepower entities that have the map_point_ids
foreach($result['givepower'] as $record) {
$map_point_ids[] = $record->id;
}
// load entities
$map_point = entity_load('givepower', $map_point_ids);
// set entity view
$entities = entity_view('givepower', $map_point);
// loop through each entities results
foreach($entities['givepower'] as $eid => $e_val) {
// add entity result data to countries array
$countries[$country_name][] = array(
'title' => $e_val['field_map_point_title']['#items'][0]['safe_value'],
'description' => $e_val['field_map_point_description']['#items'][0]['safe_value'],
'latitude' => $e_val['field_map_point_latitude']['#items'][0]['safe_value'],
'longitude' => $e_val['field_map_point_longitute']['#items'][0]['safe_value'],
);
}
}
This will create an array that looks like this:
Array (
[Kenya] => Array (
[0] => Array (
[title] => Test Map Point 1
[description] => lorum ipsum
[latitude] => 1.765404
[longitude] => 40.079880 )
[1] => Array (
[title] => Test Map Point 1
[description] => Lorum ipsum
[latitude] => 0.633657
[longitude] => 37.350050 ) )
[Haiti] => Array (
[0] => Array (
[title] => GA University
[description] => lorum ipsum
[latitude] => 18.574420
[longitude] => -72.310981 ) )
[Nepal] => Array ( )
[Ghana] => Array ( )
)
I'm working on a permission system and I'd like to get information about all the admin classes loaded in Sonata.
I have a list of all these classes via
$this->getConfigurationPool()->getAdminClassas();
I would like to get the defined tags (Group / Label) for these services.
Thanks!
You can get Groups / Labels by calling getDashboardGroups() on getConfigurationPool()
$groups=$this->getConfigurationPool()->getDashboardGroups();
$adminGroups=array();
$i = 0;
foreach ($groups as $key => $val) {
$adminGroups['groups'][$i]['label']=$val['label'];
foreach ($val['items'] as $items) {
$adminGroups['groups'][$i]['items'][]=$items->getLabel();
}
$i++;
}
echo '<pre>';print_r($adminGroups);echo '</pre>';
So the resultant $adminGroups array will have all groups and its admin titles in below form
Array
(
[groups] => Array
(
[0] => Array
(
[label] => Group 1
[items] => Array
(
[0] => Menu item 1
)
)
[1] => Array
(
[label] => Group 2
[items] => Array
(
[0] => Menu item 1
[1] => Menu item 2
[2] => Menu item 3
)
)
[2] => Array
(
[label] => Group 3
[items] => Array
(
[0] => Menu item 1
[1] => Menu item 2
)
)
/*And so on ....*/
)
)
Above will return admin labels which are set to show_in_dashboard = true to all admin labels you can get them by their service ids
$services = $this->getConfigurationPool()->getAdminServiceIds();
$container = $this->getConfigurationPool()->getContainer();
$adminLabels = array();
foreach ($services as $key => $val) {
$admin = $container->get($val);
$adminLabels['label'][] = $admin->getLabel();
}
echo '<pre>';print_r($adminLabels);echo '</pre>';
I try to create a table where a row is looking like this:
|Text1|Text2|Button1|
As soon as the user clicks on the button I want to exchange the Button1 with tow textFields and another button... so a normal AJAX-request.
I've tried to implement this, not sure if this is the correct way, let me know if I could to it in a completely other way :S
So what I've already done:
In my hook_menu i link to a function that returns the data:
return theme ( 'my_theme_function', array (
'running' => true,
'form' => $database->getData ()
) );
What happens in the getData()-Function:
I create the form using drupal_get_form(..).. returned from the given form is this:
$data = array (
'items' => $finder->getGamesToPlayForm ( $form_state ),
'#title' => 'Title'
);
the $finder returns the list of items I want to show in the table. So nothing special.
my_theme_function is set to use a template-file where I would like to show the whole stuff.
This looks like this:
$header = array (
'Col1',
'Col2',
''
);
$rows = array ();
foreach ( element_children ( $formData ['items'] ) as $gameId ) {
$row = array ();
$row [] = drupal_render ( $formData ['items'] [$gameId] ['#col1'] );
$row [] = drupal_render ( $formData ['items'] [$gameId] ['#col2'] );
$row [] = drupal_render ( $formData ['items'] [$gameId] ['#form'] );
$rows [] = $row;
}
$output = theme ( 'table', array (
'header' => $header,
'rows' => $rows
) );
unset($formData['items']);
$output .= drupal_render_children ( $formData );
print $output
this way, everything is printed correct. But there are no "form"-tags, which stops my form from working..
So, any idea/hint how to solve this kind of problem? Probably I'm on the completely wrong way..
try
'render element' => 'form'
in your theme
how could I make a nice search result like google
I cannot wrap my head around this many. thanks for any help you can give.
Array
(
[summary] => Array
(
[what] => pizza
[where] => city
)
[listings] => Array
(
[0] => Array
(
[parent] =>
[contents] => Array
(
[Video] => Array
(
[avail] =>
)
)
[id] => 1114638
[name] => Sexy house
[address] => Array
(
[street] => 3 King St E
[city] => loversLane
[prov] => AB
[pcode] => L8N1A1
)
[geoCoded] => Array
(
[latitude] => 43.256373
[longitude] => -79.868167
)
)
)
)
this works good at printing:
function recursivePrint($elem) {
foreach ($elem as $key => $value) {
if (is_array($value))
$this->recursivePrint($value);
else
print $value.'<br>';
}
}
But I want to be able to place links over the results etc. have the geocode as variables so I can use maps. Just as much control with as little lines as possible.
The HTML you choose to wrap round each one is up to you, but this should make it obvious how you use arrays in PHP:
echo 'Results for '.$elem['summary']['what'].' '.$elem['summary']['where'].'<br />';
foreach($elem['listings'] as $listing)
{
echo $listing['name'].'<br />';
echo $listing['address']['street'].'<br />';
echo $listing['address']['city'].'<br />';
echo ''.$listing['address']['pcode'].'';
echo '<br /><br />';
}