I wish to display the passions for all the user that was displayed in the search results. Currently, this isnt dynamic, how do I make this dynamic? As I have to include the [0],[1],[2].. manually. I am using cakephp. The following code is located in my view.ctp page.
foreach ($data as $user) {
$cell .= $user['Passion'][0]['tag'].' '.$user['Passion'][1]['tag'].' '.$user['Passion'][2]['tag'];
}
what about a double foreach loop?
foreach ($data as $user) {
foreach ($user['Passion'] as $passion) {
$cell.= $passion['tag'];
}
}
The following code iterates through every tag, and also puts a space between each tag.
foreach ($data as $user) {
$passions = array(); //Reset the line
foreach ($user['Passion'] as $passion) {
array_push($passions, $passion['tag']); // Add each tag
}
$cell.= implode($passions, ' '); // Put a space between each tag
}
Related
Using the php SDK, I would like to get all the content for a landing page, this will include sections with different content types / components with different fields per entry.
eg.
Section 1 - hero
Section 2 - article
Section 3 - image
Q. How do I loop through and get the linked section content.
If an entry contains a link to an asset or another
entry, the SDK will automatically load it.
I am struggling to understand the documentation. How exactly do I break into these values?
<?php
$query = new \Contentful\Delivery\Query();
$query->setContentType('page_landing')
->where("fields.urlHandle[match]", $slug);
$products = $client->getEntries($query);
foreach ($products as $product) {
echo $product['name'];
// Example of what I am trying to achieve
if($product['sections']['id']=='hero'){
$title= $product['sections']['hero']['title'];
}
if($product['sections']['id']=='article'){
$text = $product['sections']['article']['text'];
}
}
?>
I would love to know if there is a better way, the documentation does not have many examples. Below is where I ended up but feel there must be a simpler way.
<?php
require_once 'vendor/autoload.php';
require_once 'vendor/credentials.php';
// if we need to use rich text
$renderer = new \Contentful\RichText\Renderer();
// Get the section entry id's
foreach ($products as $product) {
$dataJson = json_encode($product);
$revertJson[] = json_decode($dataJson, true);
foreach ($revertJson as $Json) {
foreach ($Json['fields']['sections'] as $entries) {
$entryID[] = $entries['sys']['id'];
}
}
}
// Loop out the sections
foreach ($entryID as $entry) {
// Get individual linked entries
$entry = $client->getEntry($entry);
$type = $entry->getSystemProperties()->getcontentType();
$json = json_encode($type);
$comp = json_decode($json, true);
if ($comp['sys']['id'] == 'Hero') {
// field
echo $entry['urlHandle'];
// rich text
echo $renderer->render($entry['text']);
// asset
$asset = $client->getAsset($entry['imageId']);
echo $asset->getFile()->getUrl();
} elseif ($comp['sys']['id'] == 'Landmark') {
echo $entry['title'];
}
}
I am processing a foreach(first) which gives the full array in the data, when I make another foreach(second) and pass the first foreach into second foreach the first value of the first foreach never appears in the second foreach. Does anyone have any idea why the foreach behaves this way? is there a solution for this?
I tried it this way no luck
$userIdsPerRoom = chatRoomUsersId($_SESSION['currentRoomID']);
foreach ($userIdsPerRoom as $value) {
$list[] =$value['UserID'];
}
foreach ($list as $value) {
$userInfo = chatRoomUsersEmail($value);
foreach ($userInfo as $info) {
echo $info['userEmail'];
}
}
echo count($list); ///gives the full list
}
I noticed it with this code
$userIdsPerRoom = chatRoomUsersId($_SESSION['currentRoomID']); //return an array of intergers with five values example array(1,2,3,4,5)
foreach ($userIdsPerRoom as $value){
$userInfo = chatRoomUsersEmail($value['UserID']);
foreach ($userInfo as $info){
echo $info['userEmail']; /// I only get four values example array(b,c,d,e)
}
}
Let's think that we have a db and a model function that retrieve a query result like this:
public function lista_ordini ($idcliente)
{
$this->db->select('ordini.num_fattura, misure.cosplay, ordini.data_richiesta,
ordini.anticipo, ordini.data_consegna,
ordini.saldo_totale, ordini.stato');
$this->db->join('misure', "ordini.id_cliente = $idcliente");
$query = $this->db->get('ordini');
if ($query && $query->num_rows() > 0){
return $query;
}else{
echo "errore generazione query ordini";
}
}
}//This is missing in your original, I'm not sure if it's a typo
Using codeigniter's table helper to generate a table:
<?php echo $this->table->generate($query); ?>
I can generate the table...and this is ok and works...
but, What if I want that every rows has a specific field (or the entire row if it's simpler) with href link? (this link will pass thorough GET method a variable for generate another query, but I don't figure out yet how that will work, this is just an hobby project)
I try this:
public function lista_ordini ($idcliente)
{
$this->db->select('ordini.num_fattura, misure.cosplay, ordini.data_richiesta,
ordini.anticipo, ordini.data_consegna, ordini.saldo_totale,
ordini.stato');
$this->db->join('misure', "ordini.id_cliente = $idcliente");
$query = $this->db->get('ordini');
if ($query && $query->num_rows() > 0) {
$rows = $query->row();
foreach ($rows as &$row) {
$row['num_fattura'] = ''.$row['num_fattura'].'';
}
return $rows;
}else{
echo "errore generazione query ordini";
}
}
I'm pretty sure that foreach loop is the way, but I can't understand how, cause I have so much errors that I cannot understand...to make this simply, my code doesn't work, and I think that I need some semantic advice.
thank you in advace
There is a logical problem in the foreach loop here:
foreach ($rows as &$row) {
$row['num_fattura'] = ''.$row['num_fattura'].'';
}
return $rows;
Each time the loop runs you are changing the value of $row, but when it completes you are returning $rows, which is unchanged.
Instead, you could edit the original array, using $k => $v to access the key and value inside the foreach loop:
foreach ($rows as $k => $v) {
$rows[$k]['num_fattura'] = ''.$rows[$k]['num_fattura'].'';
}
return $rows;
I know what you're trying to do, but I dont understand how you are trying to do it. Here's How I would do it:
//controller method
public function show_data()
{
$this->load->library('table');
$data['table_data'] = $this->some_model->get_the_data();
$this->load->view('some_view_that_will_contain_my_table', $data);
}
Now in my view, I'd generate the table:
<!doctype html>
...
<?if(!empty($table_data)):?>
<? $this->table->set_heading('column_1', 'column_2');?>
<?foreach($able_data as $table_row):
$this->table->add_row(
array('data'=>''.$table_row['column_1'].'.'),
array('data'=>''.$table_row['column_2'].'.'),
);
endforeach;?>
<?= $this->table->generate();?>
<?endif;?> //or display an empty table
I got a site that executes the following code
$keywords = ($_SESSION[$_POST['ts']]);
print_r($keywords);
foreach ($keywords as $keyword) {
foreach ($keyword['whitelist'] as $entry) {
foreach ($_POST as $key => $value) {
if ($key == $entry['encoded_url']) {
$entry['ignore'] = $value;
$decodedURL = $this->base64->url_decode($entry['encoded_url']);
if ($value == 'noignore') {
echo "found!<br />";
$this->blacklist_model->remove($decodedURL);
$html = $this->analyse->getHTML($decodedURL);
$entry['html'] = $html[0];
$entry['loading_time'] = $html[1];
}
if($value == 'alwaysignore') {
$this->blacklist_model->add($decodedURL);
}
}
}
}
}
print_r($keywords);
The output looks like this:
http://pastebin.com/B3PtrqjB
So, as you see, there are several "found!"s in the output, so the if clause actually gets executed a few times and I expected the second array to contain new data like 'html', but, as you see, nothing changes. Is there anything to attend when changing values in multidimensional foreach() loops?
foreach creates a copy of the array and loops through that. Modifying values doesn't work.
You can get around this, though, by using references.
foreach ($keywords as &$keyword) {
foreach ($keyword['whitelist'] as &$entry) {
foreach ($_POST as $key => &$value) {
...
}
}
}
With that you can modify $value and it WILL affect the original array.
You suppose to change the Original array.
$entry is just an instance / unconnected node.
you need to change $keywords, and not its on the fly created nodes.
I am trying to list all #titles in a Drupal form module; however, I can't seem to get it to work.
foreach ($form_values as $key => $value) {
echo $form['myForm'][$key]['#title'];
}
The method above does not return anything. I can list the $key values by doing the following:
foreach (...) {
echo $key . ' ';
}
but I can't find a way to list the titles (e.g., '#title' => t('Name')) for each form input.
Any help?
Thanks!
Your request a bit baffles me, but
function print_title($form) {
foreach (element_children($form) as $key) {
print_title($form[$key]);
}
if (isset($form['#title'])) {
// do something with your title
}
}