0

I have two arrays which I have passed to my Laravel view. I want to run a loop that will set a State variable equal to the total variable. For some reason the variables are not being set, even though I have defined them before the loop. I know I have the right variables lined up, because when I do a simple echo of the two I get the right results.

Complete code posted:

map.blade.php

<head>
<meta charset="utf-8">

<link rel="stylesheet" href="css/screen.css">
<link rel="stylesheet" href="css/stately.css">

@define $AK = ""
@define $AL = ""
@define $AR = ""
@define $AZ = ""
@define $CA = ""
....
@define $WY = ""
@define $arr = array_combine($allstates, $alltotals)

@foreach ($arr as $allstates => $alltotals)
  @define $array[$allstates] = $alltotals
  {{ $allstates }} {{ $alltotals }}<br>
  @define $allstates = $alltotals
  {{ $allstates }} {{ $alltotals }}<br>
@endforeach
</head>
<body>
  [Map here]
{{ $PA }}
</body>

Controller:

public function map()
{

    $allstates = DB::table('distributors')->lists('state');
    $alltotals = DB::table('distributors')->lists('total');

    $merged = DB::table('distributors')->lists('state','total');

    return View::make('map')
    ->with(compact('allstates'))
    ->with(compact('alltotals'))
    ->with(compact('merged'));
}

The outputs of this you can see on hennessey.io/map

User14289
  • 159
  • 2
  • 14
  • At the moment I am struggling with updating the database with my form, so I wanted to get the other half of my project done and go back to it with a clearer mind. My goal with this is to eventually, once I assign these variables to the state acronym, run a loop which will assign different colors to different states depending on what number range they fit into. – User14289 Aug 21 '15 at 19:44
  • have you tried `DB::table('distributors')->lists('state','total');` it will create an array for you and you don't have to use `array_combine` – mdamia Aug 21 '15 at 19:57
  • I did do this, but I didnt know how to use it and make the $state equal to the number in $total using it. – User14289 Aug 21 '15 at 20:01
  • I just created a third variable to be passed to the view from the controller, "merged' - $merged = DB::table('distributors')->lists('state','total'); return View::make('map') ->with(compact('merged')); I will try and figure it out... – User14289 Aug 21 '15 at 20:06
  • @user80648 What is $PA? – Sturm Aug 21 '15 at 20:18
  • When I declared variables @define $PA = ""... the value remains the same after the loop. Before I had it set to "0" and it remained "0". – User14289 Aug 21 '15 at 20:25
  • I am having the same issue using the code I put below, following nathan's example – User14289 Aug 21 '15 at 20:27
  • The way I suggested and the better way that mdamia suggested both work fine for me. Could you post your complete code for this? @user80648 – Sturm Aug 21 '15 at 20:34
  • You can see the outputs here: distributormap.hennessey.io/map – User14289 Aug 21 '15 at 20:38
  • I will post the complete code in one second. – User14289 Aug 21 '15 at 20:38
  • Using the answer I provided you would access `$PA` by doing $arr['PA'] instead. – Sturm Aug 21 '15 at 20:46

1 Answers1

0

You cant modify the array that way using a foreach loop. The value is being assigned inside of the loop and being discarded after each iteration. You're passing the key by value.

Try doing it this way:

@define $arr = array_combine($allstates, $alltotals)
@foreach ($the_array as $key => $value)
    @define $arr[$key] = $value
@endforeach

This will modify the original array, rather than just the copy inside of the loop. Here's some more information on how foreach loops work in PHP.

Community
  • 1
  • 1
Sturm
  • 3,645
  • 2
  • 20
  • 36
  • I am having difficulty using this and getting it to work. I can do the following and get the same output I had before, but $PA is still not defined as having a value of 15, which it actually does have in the database (and in the output I get using this:) – User14289 Aug 21 '15 at 19:58
  • define $arr = array_combine($allstates, $alltotals) foreach ($arr as $allstates => $alltotals) define $array[$allstates] = $alltotals {{ $allstates }} {{ $alltotals }} endforeach – User14289 Aug 21 '15 at 19:59