I have difficult times to figure out how to manage my loop to not override variables during the process, my example script looks as it follows
$targets = array(
array(
'site_id' => 1,
'url' => array('http://example.com','http://test.com'),
'title' => "Title_1",
'int_link' => "/internal_link/",
'icon' => '/icon_2.gif',
'teaser_index' => 5),
array(
'site_id' => 2,
'url' => array('http://example2.com','http://test2.com'),
'title' => "Title_2",
'int_link' => "/internal_link/",
'icon' => '/icon_2.gif',
'teaser_index' => 5)
)
foreach($targets as $target){
$images = array();
$links = array();
$name = array();
$loop=-1;
foreach($target['url'] as $url){
$loop++;
//parsing $url;
//the insider loop has 2 iterations
if ($loop=1){
$content .="<div>".$target['title'].$url"</div>";
}
else{
$content .="<div>".$target['title'].$url"</div>";
}
}
//write $content html to database, without to override the first $content
}
Your question is not clear but i think i understand what the likely problem might be
There are so many errors in the script which you need to fix maybe your script would run the way you want it to be .
A. $content was not define .. you need to define it
B. if ($loop=1){ should be if ($loop == 1){
C. $content .="<div>".$target['title'].$url"</div>"; is missing . and it should be something like this $content .= "<div>" . $target['title'] . $url . "</div>";
D. You if condition does not make sense since you are outputting the same information
Form what i can see you want to output Title & URL you can as well just use this simple script (Just Guessing)
$targets = array(
array(
'site_id' => 1,
'url' => array('http://example.com','http://test.com'),
'title' => "Title_1",
'int_link' => "/internal_link/",
'icon' => '/icon_2.gif',
'teaser_index' => 5),
array(
'site_id' => 2,
'url' => array('http://example2.com','http://test2.com'),
'title' => "Title_2",
'int_link' => "/internal_link/",
'icon' => '/icon_2.gif',
'teaser_index' => 5)
);
foreach ( $targets as $target ) {
$content = "";
$output = "<div> %s : %s </div>";
$content .= sprintf($output, $target['title'], implode(" , ",$target['url']));
echo $content;
// write $content html to database, without to override the first $content
}
Output
Title_1 : http://example.com , http://test.com
Title_2 : http://example2.com , http://test2.com
Related
I have a big piece of php code that builds a dropdown menu and I want to minimize it by loading the select options from a text file. How to do this in the right way with php?
An example of original code:
$field['options'] = array(
array('value' => 'Anenii Noi', 'text' => 'Anenii Noi', 'depth' => 0),
array('value' => 'Bălţi', 'text' => 'Bălţi', 'depth' => 0),
array('value' => 'Basarabeasca', 'text' => 'Basarabeasca', 'depth' => 0),
....
array('value' => 'Ungheni', 'text' => 'Ungheni', 'depth' => 0)
);
And this is what I try to do, but without success:
// read a text file with select options
// where each option is a seperate line
$file_array = str_replace("\n","", file($file_path));
// build the list of arrays with select options
foreach ($file_array as $location)
$options .= "array('value' => '" . $location . "', 'text' => '" . $location . "', 'depth' => 0),";
foreach($form['field'] as $k => $field) {
// replace the big list of arrays with $options variable
$field['options'] = array($options);
$form['field'][$k] = $field;
break;
}
Looks like you had it almost. But you tried to mimic the pure php code, that might have worked if you use eval() later on, but you need to mimic its functionality.
// read a text file with select options
// where each option is a seperate line
$file_array = str_replace("\n","", file($file_path));
// build the list of arrays with select options
$options = array();
foreach ($file_array as $location) {
$options[] = array('value' => $location, 'text' => $location, 'depth' => 0);
}
foreach($form['field'] as $k => $field) {
$field['options'] = $options;
$form['field'][$k] = $field;
break;
}
first of, just wanted to let you know that I am a newbie at CI. but I am having trouble with this piece of code where is breaking and I can't seem to be able to find the answer anywhere.
for some reason the code is breaking at the first if statement.. if possible could you help me out understand what is really happening there?
Thank you all for your help!
function main
{
$this->load->model(getData) psudo code for this area...
}
---model---
function getData....
{
Sql = this->db->query(sql code that returns all the information required.)
$result = $sql->result_array();
$types = array ( 'EVENT|TIME' => array( 'id' => 1, 'name' => 'Regular' ),
'PROPOSITION|REGULAR' => array( 'id' => 2, 'name' =>'Propositions'),
'EVENT|TIME' => array( 'id' => 3, 'name' => 'Now' ),
'PROPOSITION|FUTURES' => array( 'id' => 4, 'name' => 'Future' ));
$var = array();
foreach ($result as $event) {
$cat = $event['type'] . '|' . $event['sub_type'];
$typeId = $types[$cat]['id'];
if(!is_array($var[$event['event_id']]['var'][$event['type_id']]))
{
if(!is_array($var[$event['event_id']]))
{
$var[$event['event_id']] = array( 'event_name' =>
$event['event_name'],'event_abbreviation' =>
$event['event_abbreviation']);
}
$var[$event['event_id']]['var'][$event['type_id']] = array(
'type_name' => $event['abbreviation'],'type_abbreviation' => $event['name']
);
}
$event[$event['event_id']]['var'][$event['type_id']]['types'][$typeId] =
$types[$cat]['name'];
}
return $myResults;
}
In this line
if(!is_array($var[$event['event_id']]['var']$event['type_id']]))
You are missing a [ somewhere. I'm guessing before $event['type_id'].
So replace with:
if(!is_array($var[$event['event_id']]['var'][$event['type_id']]))
Here is a piece of my code:
if($all_pages)
foreach ($all_pages as $page)
{
$all_hokms = $mysqli->query("SELECT * FROM qm_hokm WHERE page_id = ".$page['page_id']."");
if($all_hokms)
foreach ($all_hokms as $hokm)
{
$one_page_ahkams[] = array(
'hokm_id_inPage' => $hokm['hokm_id_inPage'],
'type' => $page['type'],
);
}
else
$one_page_ahkams[] = array();
$all_pages_data[] = array(
'page_id' => $page['page_id'],
'done' => $page['done'],
'checked' => $page['checked'],
'ahkams' => array($one_page_ahkams), //Here Is the PROBLEM : (((
);
}
else
$all_pages_data[] = array();
echo json_encode($all_pages_data);
As you see, I want to send multidimensial array() in a json format (from server to client), but how can correct the insertion of an array in an other, I mean that I need to add $one_page_ahkams array in $all_pages_data one, any ideas
$one_page_ahkams = array();
$all_pages_data[] = array(
'page_id' => $page['page_id'],
'done' => $page['done'],
'checked' => $page['checked'],
'ahkams' => array($one_page_ahkams);//this comma (,) causes the problem i guess**
);
An other way to do it :
$all_pages_data[] = array(
'page_id' => $page['page_id'],
'done' => $page['done'],
'checked' => $page['checked'],
'ahkams' => array(['one_page_ahkams']=>$one_page_ahkams);
);
Just put that: 'ahkams' => array($one_page_ahkams), but check the content of $one_page_ahkams
Well, I am here again dealing with arrays in php. I need your hand to guide me in the right direction. Suppose the following array:
-fruits
--green
---limon
---mango
--red
---apple
-cars
--ferrari
---enzo
----blue
----black
---318
--lamborg
---spider
---gallardo
----gallado-96
-----blue
-----red
-----gallado-98
The - (hyphen) symbol only illustrates the deep level.
Well, I need to build another array (or whatever), because it should be printed as an HTML select as below:
-fruits
--green
---limon
---mango
--red
---apple
-cars
--ferrari
---enzo
----blue
----black
---318
--lamborg
---spider
---gallardo
----gallado-96
-----blue
-----red
-----gallado-98
Looks that for each level element, it should add a space, or hyphen to determinate that it belongs to a particular parent.
EDIT
The have provide an answer provideng my final code. The html select element will display each level as string (repeating the "-" at the begging of the text instead multi-level elements.
Here's a simple recursive function to build a select dropdown given an array. Unfortunately I'm not able to test it, but let me know if it works. Usage would be as follows:
function generateDropdown($array, $level = 1)
{
if ($level == 1)
{
$menu = '<select>';
}
foreach ($array as $a)
{
if (is_array($a))
{
$menu .= generateDropdown($a, $level+1);
}
else
{
$menu .= '<option>'.str_pad('',$level,'-').$a.'</option>'."\n";
}
}
if ($level == 1)
{
$menu = '</select>';
}
return $menu;
}
OK, I got it with the help of #jmgardhn2.
The data
This is my array:
$temp = array(
array(
'name' => 'fruits',
'sons' => array(
array(
'name' => 'green',
'sons' => array(
array(
'name' => 'mango'
),
array(
'name' => 'banana',
)
)
)
)
),
array(
'name' => 'cars',
'sons' => array(
array(
'name' => 'italy',
'sons' => array(
array(
'name' => 'ferrari',
'sons' => array(
array(
'name' => 'red'
),
array(
'name' => 'black'
),
)
),
array(
'name' => 'fiat',
)
)
),
array(
'name' => 'germany',
'sons' => array(
array(
'name' => 'bmw',
)
)
),
)
)
);
Recursive function
Now, the following function will provide an array with items like [level] => [name]:
function createSelect($tree, $items, $level)
{
foreach ($tree as $key)
{
if (is_array($key))
{
$items = createSelect($key, $items, $level + 1);
}
else
{
$items[] = array('level' => $level, 'text' => $key);
}
}
return $items;
}
Calling the funcion
Now, call the function as below:
$items = createSelect($temp, array(), 0);
Output
If you iterate the final $items array it will look like:
1fruits
2green
3mango
3banana
1cars
2italy
3ferrari
4red
4black
3fiat
2germany
3bmw
I have an array in this form:
$data = array(
array(
'id' => '1',
'bar' => 'foo',
'page' => 'front',
),
array(
'id' => 'bar',
'bar' => 'foo',
'page' => 'front',
),
array(
'id' => 'different,
'bar' => 'bar',
'page' => 'back',
),
array(
'id' => 'another',
'title' => __("Custom CSS",'solidstyle_admin'),
'foo' => 'bar',
'page' => 'back',
),
);
And I want to list all ids grouped by pages and saved as variables, so if the above array is an input then output will look just like this one:
$front = array('1','bar');
$back = array('different','another');
//$data['page'] = array($id1, $id2, (...));
I was trying to do that using foreach and this is how it starts:
function my_output() {
foreach($data as $something) {
$id = $something['id'];
$page = $something['page'];
}
return $output;
}
I was trying multiple foreach loops, and the best result I got was:
front = 1
front = bar
back = different
back = another
But I have absolutely no idea how to achieve what I want to do, I don't want anyone to do my job, just any hints? Keep in mind I'm a bit new to PHP and I don't know too much about arrays.
Thank you!
Sounds like you want:
$ids = array();
foreach ($data as $page) {
$pageName = $page['page'];
// create an empty array for your IDs
if (!isset($ids[$pageName])) {
$ids[$pageName] = array();
}
// add to the array of IDs
$ids[$pageName][] = $page['id'];
}
var_dump($ids); // array('front' => array('1', 'bar'), ...
Stick with the loop idea and do a conditional check.
function my_output() {
$front = array();
$back = array();
foreach($data as $something) {
$id = $something['id'];
$page = $something['page'];
if ($page === 'front') {
$front[] = $id;
} else if ($page === 'back') {
$back[] = $id;
}
}
// Not sure what you want to return here, but you could return an array of pages
$output = array('front' => $front, 'back' => $back);
return $output;
}
This will return something similar to:
$output = array(
'front' => array(
0 => '1',
1 => 'bar',
),
'back' => array(
0 => 'something',
1 => 'another',
)
)
Edit: Keep in mind that my answer only accounts for the two pages you listed in your answer. If you have more pages you can also use what cbuckley's answer showed.