1
votes

I am trying to pass an array with keys numbered 0 through 9 to my view. However, when I try to echo a specific key or a foreach for all keys to display the data, nothing shows.

My controller:

    $i=0;
    $cleanarray = array();

    foreach ($phrase as $innerphrase) {
        if (is_array($innerphrase)) {
            foreach ($innerphrase as $value) {
                $cleanarray[$i] = $value;
                $i++;
            }
        }
    }

    $this->load->view('blogview', $cleanarray);

Outputs:

Array
(
    [0] => On Friday evenings in Thailand, sandwiched between the evening news and a popular soap opera, is a prime-time programme that has been running for three years, or ever since the military took power in a May 22, 2014 coup. 
    [1] => The Reds pulled off their biggest comeback in a year to finally end their pesky losing streak.
    [2] => A co-founder of Twitter says he's sorry if the popular social media platform helped put Donald Trump in the White House, as the president has suggested.
    [3] => Daniel Suarez is thrilled to be competing in the NASCAR All-Star race after struggling in his rookie season.
    [4] => Lexi Thompson remained in position for her first victory since a rules infraction cost her a major title, shooting a 2-under 69 in tricky wind conditions Saturday to take a three-stroke lead over In Gee Chun into the final round of the Kingsmill Championship.
    [5] => Boston Celtics guard Isaiah Thomas will miss the remainder of the NBA playoffs after aggravating a hip injury during Game Two of the Eastern Conference final, the team announced on Saturday.
    [6] => Yankees manager Joe Girardi has been ejected during an animated argument and New York pitching coach Larry Rothschild and Tampa Bay starter Matt Andriese also have been tossed in a testy game at Tropicana Field.
    [7] => Ed Carpenter turned a tough draw into a winning hand Saturday.
    [8] => Cloud Computing pulled off a major surprise in the Preakness Stakes in Maryland on Saturday, charging down the stretch to overtake Classic Empire and win the second race of U.S. thoroughbred racing's Triple Crown.
    [9] => Sometimes it pays to have a fresh horse.
)

My view:

<html>
<head>
</head>
<body>
        <h1>I am terrible at PHP!</h1>

        <h2><?php echo $0; ?></h2>  

</body>
</html>

However nothing shows. I have also tried using print_r in the same fashion. I am aware that CodeIgniter does not allow me to pass the variables in the controller and instead it allows me access directly to the keys in the array.

Ultimately I would like to iterate through the array and display all results but I'm trying to start "small" and at least get a single element of the array to pass through to the view. If someone could explain why this isn't working and then what one would do to use a for or foreach to iterate through the array, I would be very grateful.

I am aware that this question has likely been answered many times but for the life of me I cannot extrapolate the responses to these questions into a solution that works in my case.

5
Many thanks to everyone who responded! - Sean

5 Answers

2
votes

Use this code:

controller:

$i=0;
$cleanarray = array();

foreach ($phrase as $innerphrase) {
    if (is_array($innerphrase)) {
        foreach ($innerphrase as $value) {
            $cleanarray[$i] = $value;
            $i++;
        }
    }
}

$array2 =  array();  // create a new array
$array2['contents']= $cleanarray; // add $cleanarray to the new array
$this->load->view('blogview', $array2); // pass the new array as the parameter

Inside view file:

echo ( "Contents: ");

// print the elements in $cleanarray

foreach ($contents as $val)

echo "$val<br/>";

Note: We can't access $array2 here. Instead, use '$contents[]'.

1
votes

As per my knowledge codeigniter doesn't pass data without arrays from controller to view so you can try the below code i must sure that it works.

In your controller

    $i=0;
    $cleanarray = array();
    foreach ($phrase as $innerphrase) 
    {
         if (is_array($innerphrase)) 
         {
                foreach ($innerphrase as $value) 
                {
                        $cleanarray[$i] = $value;
                        $i++;
                }
         }
    }
    $data['cleanarray'] = $cleanarray
    $this->load->view('blogview', $data);

In your view print the array like this print_r($cleanarray); i am sure that this code will works.

Happy Coding :)

1
votes

That is not how you access array variables. $ does not represent an array, it represents the start of a variable name. The syntax for accessing the first element of $cleanarray is $cleanarray[0].

I'll admit I'm not a codeigniter coder, so I don't even know if that is how you'd access it in the "view". I'm guessing not, because of that $this business in the controller. Surely someone else can offer that detail.

1
votes

After creating $cleanarray in your controller apply this

$data['cleanarray'] = $cleanarray;//assign $cleanarray to $data
$this->load->view('blogview', $data);//pass $data to your view

And in the view to access your array apply this

<h2><?php echo $cleanarray[0]; ?></h2>
0
votes

Here's my advice:

I don't recommend you to use number as variable name. To solve this, you can make its name started with '_'. Ex: $_1.

So in your looping change $cleanarray[$i] = $value; into $cleanarray['_'.$i] = $value;.

Of course you need to call it on view echo $_0;