This is my simple looper code
foreach( $cloud as $item ) {
if ($item['tagname'] == 'nicetag') {
echo $item['tagname'];
foreach( $cloud as $item ) {
echo $item['desc'].'-'.$item['date'];
}
} else
//...
}
I need to use if method in this looper to get tags with same names but diferent descriptions and dates. The problem is that I dont know every tag name becouse any user is allowed to create this tags.
Im not really php developer so I'm sory if it's to dummies question and thanks for any answers!
One possible solution is to declare a temporary variable that will hold tagname that is currently looped through:
$currentTagName = '';
foreach( $cloud as $item ) {
if ($item['tagname'] != $currentTagName) {
echo $item['tagname'];
$currentTagName = $item['tagname'];
}
echo $item['desc'] . '-' . $item['date'];
}
I presume that your array structure is as follows:
$cloud array(
array('tagname' => 'tag', 'desc' => 'the_desc', 'date' => 'the_date'),
array('tagname' => 'tag', 'desc' => 'the_desc_2', 'date' => 'the_date_2'),
...
);
BUT
This solution raises a problem - if your array is not sorted by a tagname, you might get duplicate tagnames.
So the better solution would be to redefine your array structure like this:
$cloud array(
'tagname' => array (
array('desc' => 'the_desc', 'date' => 'the_date'),
array('desc' => 'the_desc_2', 'date' => 'the_date_2')
),
'another_tagname' => array (
array('desc' => 'the_desc_3', 'date' => 'the_date_3'),
...
)
);
and then you can get the data like this:
foreach ($cloud as $tagname => $items) {
echo $tagname;
foreach($items as $item) {
echo $item['desc'] . '-' . $item['date'];
}
}
Related
I have following foreach loop
$selectedids = "1255;1256;1257";
$selectedidsarr = explode(';', $selectedids);
$idstand = '1';
foreach ($selectedidsarr as $item) {
$output1 = $idstand++;
echo "<li>product_id_$output1 = $item,</li>";
}
I want to add the output of the above loop inside following associative array
$paramas = array(
'loginId' => $cred1,
'password' => $credpass1,
'orderId' => $orderid,
'offer' => $offerid,
'shipid' => $shipcharge
)
So that the final array will look like this;
$paramas = array(
'loginId' => $cred1,
'password' => $credpass1,
'orderId' => $orderid,
'offer' => $offerid,
'shipid' => $shipcharge,
'product1_id' => 1255,
'product2_id' => 1256,
'product3_id' => 1257,
)
I tried creating following solution but its not working for me
$selectedids = $boughtitem;
$selectedidsarr = explode(';', $selectedids);
$idstand = '1';
foreach ($selectedidsarr as $item) {
$idoutput1 = $idstand++;
$paramas [] = array (
'product$idoutput1_id' => $item,
);
}
Need advice.
You don't need to define a new array, just set the key of the current array to the value you want, in the form of $array[$key] = $value to get an array that looks like [$key=>$value], or in your case...
$paramas['product' . $idoutput1 . '_id'] = $item;
I dont know exactly how to put this. I have this function:
protected function _do_thumb($temaid)
{
$firstPost = $this->registry->topics->getPostById( $temaid );
preg_match('/< *img[^>]*src *= *["\']?([^"\']*)/i', $firstPost['post'], $match);
return $match[1];
}
[...]
while ( $i = $this->DB->fetch() )
{
$forum = $this->registry->class_forums->forum_by_id[ $i['forum_id'] ];
if ( $this->registry->permissions->check( 'read', $forum ) != TRUE )
{
continue;
}
if ( $forum['password'] != "" )
{
continue;
}
$to_echo .= $this->_parseTemplate( $row, array (
'topic_title' => str_replace( '&#', '&#', $i['title'] ),
'topic_id' => $i['tid'],
'topic_link' => "showtopic=".$i['tid'],
'forum_title' => htmlspecialchars($forum['name']),
'forum_id' => $i['forum_id'],
'last_poster_id' => $i['last_poster_id'],
'last_post_name' => $i['last_poster_name'],
'last_post_time' => $this->registry->getClass('class_localization')->getDate( $i['last_post'] , 'LONG', 1 ),
'timestamp' => $i['start_date'],
'starter_id' => $i['starter_id'],
'starter_name' => $i['starter_name'],
'board_url' => $this->settings['board_url'],
'board_name' => $this->settings['board_name'],
'rfc_date' => date( 'j\-M\-Y', $i['start_date']),
'thumb' => $this->_do_thumb($i['topic_firstpost'])
) ) . "\r\n";
}
$firstPost is supposed to produce a value result of another function. The problem here is that that function seems to stop halfway the whole loop so it returns an incomplete result, only the first element of a list. $forum however works fine because it is expressed as a variable so I believe the solution might be to express $topic in the same way. Something like this:
$this->registry->topics->getPostById[ $temaid ];
However, I dont know how I ought do it. Is it even possible?
Thank you.
getBostById() and $i = $this->DB->fetch() in while loop where you call getPostById(), they both use $this->DB to store information from Database.
And when called getPostById(), it overrides information inside of $this->DB and moves hidden pointer to the end. This function returns POST data from database, and also uses something like $i = $this->DB->fetch() for this. Therefore pointer to data moves to the end.
Next, time when $i = $this->DB->fetch() called, it sees pointer at the end, and stop looping.
Solution is, when fetching result from database, don't override it. There for cache them before calling getPostById:
//while ( $i = $this->DB->fetch() )
$items = array();
$counter = 0 ;
while ($i = $this->DB->fetch()) {
$items[$counter++] = $i;
}
foreach ( $items as $i ){
$forum = $this->registry->class_forums->forum_by_id[ $i['forum_id'] ];
if ( $this->registry->permissions->check( 'read', $forum ) != TRUE )
{
continue;
}
if ( $forum['password'] != "" )
{
continue;
}
$to_echo .= $this->_parseTemplate( $row, array (
'topic_title' => str_replace( '&#', '&#', $i['title'] ),
'topic_id' => $i['tid'],
'topic_link' => "showtopic=".$i['tid'],
'forum_title' => htmlspecialchars($forum['name']),
'forum_id' => $i['forum_id'],
'last_poster_id' => $i['last_poster_id'],
'last_post_name' => $i['last_poster_name'],
'last_post_time' => $this->registry->getClass('class_localization')->getDate( $i['last_post'] , 'LONG', 1 ),
'timestamp' => $i['start_date'],
'starter_id' => $i['starter_id'],
'starter_name' => $i['starter_name'],
'board_url' => $this->settings['board_url'],
'board_name' => $this->settings['board_name'],
'rfc_date' => date( 'j\-M\-Y', $i['start_date']),
'thumb' => $this->_do_thumb($i['topic_firstpost'])
) ) . "\r\n";
}
This is a newbie question... I have a form in Joomla 3.3 and chronoforms v5 latest... When the form loads the database populates my first select input with "interview dates" from my DB.... works great, once you select the date, the second select input populates with available time slots.... the problem here is the way the DB is output in the array is
Data->
time->7:00am,7:15am,9:30am
Right now when the 2nd select loads it's showing up like this 7:00am,7:15am,9:30am....
I want to be able to make them individual values not all one value...
This is the code I am currently using for the "time" options for the second select input...
<?php
$options = array();
if ( !$form->data['Data'] || count($form->data['Data']) < 1 ) {
// no result was found
$options[] = 'Please select a category';
} else {
foreach ( $form->data['Data'] as $d ) {
$options[$d['interviewdate']] = ($d['time']);
}
}
echo json_encode ($options);
?>
is this possible?
The structure that is needed to create an options list is like this:
[0] => array ('text' => 'aaa', 'value' => 'xxx'),
[1] => array ( . . .
And your data appears to be be in a nested array like $form->data['Data']['time'] ?
In this case the text and value can probably be the same so the code would be something like this:
<?php
$options = array();
if ( !$form->data['Data']['time'] ) {
// no result was found
$options[] = array('text' => 'Please select a category', 'value' => '');
} else {
$data = explode(',', $form->data['Data']['time']);
foreach ( $data as $d ) {
$options[] = array('text' => $d['time'], 'value' => $d['time']);
}
}
echo json_encode($options);
?>
If i am not mistaken your $d['time'] holds values like '7:00am,7:15am,9:30am'. And if this is the case then you can just use explode(',', $d['time']) which will give you array of times instead of string.
$options = array();
$form = new stdClass();
$form->data['Data'] = array(
array(
'interviewdate' => 'date', 'time' => '7:02am,7:25am,9:40am'
),
array(
'interviewdate' => 'date2', 'time' => '7:05am,7:35am,19:40am'
)
);
if ( !$form->data['Data'] || count($form->data['Data']) < 1 ) {
// no result was found
$options[] = 'Please select a category';
} else {
foreach ( $form->data['Data'] as $d ) {
foreach(explode(',', $d['time']) as $time){
$options[] = array($d['interviewdate'] => $time);
}
}
}
echo json_encode ($options);
Hi I'm a newbie of FuelPHP. I'm makin' a demo use ACL. I have fetched all roles from database with format as the code below
$data = array(
array('admin'=>array(
'none' => array(
'crudform' => array('create','index')
)
)),
array('admin'=>array(
'none' => array(
'cruddept' => array('create','view')
)
)),
);
And now I want to convert that array to format as
$data = array(
'admin' => array(
'none'=>array(
'crudform' => array(
'create',
'index'
),
'cruddept'=>array(
'create',
'view'
)
)
)
)
How I can do that ?
After you retreive your data from database, you can convert your array using a function just like this one
function change_array($data)
{
$result = array('admin' => array('none' => array()));
foreach ($data as $key => $value)
foreach ($data[$key]['admin']['none'] as $key_child => $value_child)
$result['admin']['none'][$key_child] = $value_child;
return $result;
}
All you need to do, is to use it like this
$data = change_array($data);
Thanks Mr Khalid, your idea has provide a way for me how to resolve my solution. I have found the way to build array as I want. This is my code
function build_role_array($roles){
$final = array();
foreach($roles as $row){
foreach($row as $role=>$value){
if(!isset($final[$role])){
$final[$role] = array();
}
foreach($value as $module=>$area){
if(!isset($final[$role][$module])){
$final[$role][$module] = array();
}
foreach($area as $controller=>$rights){
$final[$role][$module][$controller] = $rights;
}
}
}
}
return $final;
}
Thanks for support
I have an array that looks like this:
array
0 =>
array
'title' => string 'Ireland - Wikipedia, the free encyclopedia'
'url' => string 'http://en.wikipedia.org/wiki/Ireland'
1 =>
array
'title' => string 'Ireland's home for accommodation, activities.'
'url' => string 'http://www.ireland.com/'
that I want to add a score of 0 to each element. I thought this simple foreach loop would do the trick but...well....it doesn't :/
public function setScore($result)
{
foreach($result as $key)
{
$key = array('title', 'url', 'score' => 0);
}
return $result;
}
Can someone help me out?
Thanks
foreach works on a copy of the array. You can modify $key all you want, it's not going to reflect on the original array.
You can use $key by reference though, then it'll work as expected:
foreach ($result as &$value) {
$value['score'] = 0;
}
Manual entry: http://php.net/manual/en/control-structures.foreach.php
You create a new array here and do nothing with it:
foreach($result as $key){
$key = array('title', 'url', 'score' => 0);
}
What you want to do is to modify a reference to existing one:
foreach($result as &$key){ # Note the '&' here
$key['score'] = 0;
}
Although deceze is right, you can also do this using array_walk(), like this:
array_walk( $result, function( &$el) { $el['score'] = 0; });
Here's an example of how to accomplish this.
$array = array( array( 'title' => "Ireland - Wikipedia, the free encyclopedia", 'url' => "http://en.wikipedia.org/wiki/Ireland"), array( 'title' => "Ireland's home for accommodation, activities.", 'url' => "http://www.ireland.com/" ) );
function setScore( $result )
{
foreach( $result as &$element )
{
$element['score'] = 0;
}
return $result;
}
$array = setScore( $array );
print_r( $array );
You could also do:
function setScore( &$result )
{...}
and then just:
setScore( $array );