Welcome Guest, Not a member yet? Register   Sign In
Problems with arrays
#1

[eluser]Perkin5[/eluser]
I have a database table with product information - one column is called price but instead of a number it has a letter, so it's actually a price category.

I have another database table with a column for price category (cat) and a column for value (val), so it ties down what each price cat means in numerical terms eg price cat A is $3.00.

So in the controller I fetch the product information in an array and I fetch the price cat information in another array and they are both passed to the view as $records and $price respectively.

What I now want to do is display the product information in a foreach loop, but instead of a price category, I want to replace it with the actual price. Although the necessary information is available, I've been unable to find a solution that works.

I thought something like:

Code:
foreach ($records as $item):
   foreach ($price as $code){
if($code->cat == $item->price){$actual_price = $code->val;}

...more code

endforeach;

would do but CI throws an error.

I have an uneasy feeling I'm setting about this the wrong way. Can anyone suggest how it might be done?
#2

[eluser]InsiteFX[/eluser]
Post the error your getting!
#3

[eluser]Perkin5[/eluser]
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: actual_price

Filename: views/cards_view.php

Line Number: 189
#4

[eluser]kr1pt[/eluser]
else { $actual_price = 0;}
#5

[eluser]InsiteFX[/eluser]
You need to define it before you can use it...
Code:
<?php
// define the variable.
var $actual_price;

foreach ($records as $item):
   foreach ($price as $code){
if($code->cat == $item->price){$actual_price = $code->val;}

...more code

endforeach;
?>
#6

[eluser]Perkin5[/eluser]
Copy pasted your var $actual_price; script into mine and now get this:

Parse error: syntax error, unexpected T_VAR in C:\wamp\www\historystoreNew\application\views\cards_view.php on line 136
#7

[eluser]Perkin5[/eluser]
Thanks kr1pt, else { $actual_price = 0;} prevented the error but it echoed $actual_price as 0.

Going back to my code:

Code:
foreach ($records as $item):
   foreach ($price as $code){
if($code->cat == $item->price){$actual_price = $code->val;}

I have done the usual:

Code:
echo xxxx; die();

for $code->cat which was A
for $item->price which was A
so in this instance the condition evaluates to true
so $actual_price should be set to equal $code->val

but echo...die on $code_val shows it to be 3.00
so why isn't $actual_price equal to that?
#8

[eluser]InsiteFX[/eluser]
Try casting it to a float.
Code:
$actual_price = (float) $code->val;

Also see php.net format_number
Code:
string number_format ( float $number [, int $decimals = 0 ] )
#9

[eluser]Perkin5[/eluser]
Now I've just tried this:

Code:
$actual_price;
foreach ($records as $item):
foreach ($price as $code){
$var1 = $code->cat;
$var2 = $item->price;
$var3 = $code->val;
if($var1 == $var2){
$actual_price = $var3;
}
}
...
endforeach;

and amazingly it works, so presumably CI didn't like me putting $code->cat etc inside an if statement.

Not sure I understand what has happened.




Theme © iAndrew 2016 - Forum software by © MyBB