1

I'm using codeigniter, and in the view I'm trying to save a php array that I pass from the controller to the view, in a variable from my script.

This is my code.

        var agenda;
        for (var index=0; index<7; index++) {
            switch (index) {
                case 0:
                    agenda[index] = <?php ($agenda[0] === NULL) ? echo "" : echo $agenda[0]; ?>; 
                    break;
                case 1:
                    agenda[index] = <?php ($agenda[1] === NULL) ? echo "" : echo $agenda[1]; ?>; 
                    break;
                case 2:
                    agenda[index] = <?php ($agenda[2] === NULL) ? echo "" : echo $agenda[2]; ?>; 
                    break;
                case 3:
                    agenda[index] = <?php ($agenda[3] === NULL) ? echo "" : echo $agenda[3]; ?>; 
                    break;
                case 4:
                    agenda[index] = <?php ($agenda[4] === NULL) ? echo "" : echo $agenda[4]; ?>; 
                    break;
                case 5:
                    agenda[index] = <?php ($agenda[5] === NULL) ? echo "" : echo $agenda[5]; ?>; 
                    break;
                case 6:
                    agenda[index] = <?php ($agenda[6] === NULL)  ? echo "" : echo $agenda[6]; ?>; 
                    break;

            }
        }

I have this error:

A PHP Error was encountered
Severity: Parsing Error

Message: syntax error, unexpected 'echo' (T_ECHO)

Filename: views/home2.php

Line Number: 1159

Backtrace:
Andreas
  • 23,610
  • 6
  • 30
  • 62
Pedro C.
  • 21
  • 5

2 Answers2

0

change the echo's to:

echo (empty($agenda[0]) ? "" : $agenda[0]);

Hope that helps,

Miroslav Glamuzina
  • 4,472
  • 2
  • 19
  • 33
0

You can't put the echo inside the ternary operator - you have to set a value in there, not execute a command.

echo ($agenda[0] === NULL) ? "" : $agenda[0];

will do what you were intending.

ADyson
  • 57,178
  • 14
  • 51
  • 63
  • now I'm having the error jquery.min.js: 2 Uncaught TypeError: Can not set property '0' of undefined. I can not save an empty array; ie agenda [0] = ""; give this mistake – Pedro C. Mar 17 '19 at 21:39
  • that sounds like a JavaScript error, but it's unclear which line is throwing it. You don't seem to have used any jQuery in the code you've shown above. – ADyson Mar 17 '19 at 21:41
  • actually looking at it you might need to declare your JS array properly. `var array = [];` on the first line - try that. – ADyson Mar 17 '19 at 21:42