0

I just don't understand why I get this message:

Parse error: syntax error, unexpected '[', expecting ')' on line 11

At this file:

    <?php 
class Telegram
{   
    protected $notice = array();
    private $db;
    public function __construct()
    {
        $this->db = new Connection();
        $this->db = $this->db->dbConnect();
    }
    public function AddNew($token,$cat,$ads[$i],$key[$j])
    {
        if(!empty($token)&&!empty($cat)&&!empty($ads))
        {
            $new = $this->con->prepare("INSERT INTO channels (token_number, category_name, ads_set, keyboard_status) VALUES (?, ?, ".$ads[$i].", ".$key[$i].")");
            $new->bindParam(1,$token);
            $new->bindParam(2,$cat);
            $new->bindParam(3,$ads);
            $new->bindParam(4,$key);
            $new->execute();
            $notice['success_message'] = "New Telegram Channel was successfully added";
            return $this->notice;

        }
    }
    public function getNotice()
    {
        return $this->notice;
    }
}
?>

And line 11 is this:

public function AddNew($token,$cat,$ads[$i],$key[$j])
  • 2
    Because the syntax is invalid. What are you trying to achieve, it doesnt make much sense – Steve Sep 25 '17 at 21:41
  • You can see my other question to understand the theory behind this: https://stackoverflow.com/questions/46404664/how-to-insert-multiple-radio-button-values-with-php-oop –  Sep 25 '17 at 21:44
  • The value should just be passed to the variable. – chris85 Sep 25 '17 at 21:46
  • Someone might pass in a value to like $ads[3], but you can't grab as a parameter $ads[3], you just get the value $ads – user1854438 Sep 25 '17 at 21:46
  • If you followed the advice of **either** of the answerers in your original question, you wouldn't even be having this problem. This is a perfect example of an [**XY problem**](https://meta.stackexchange.com/a/66378). Note that the very first comment here states "*doesn't make much sense*" -- that's a great indicator. – Obsidian Age Sep 25 '17 at 21:49

3 Answers3

2

You can't use array's index in functions parameters.

public function AddNew($token,$cat,$ads,$key)
{

}
Nathanael Demacon
  • 1,169
  • 7
  • 19
0

Try to just pass the array in parameter (and the $i variable separately if you want to access to specific data in your array) :)

YaatSuka
  • 231
  • 1
  • 9
0

Try this you can pass Array and key like this

public function AddNew($token,$cat,$ads,$key)
{
    if(!empty($token)&&!empty($cat)&&!empty($ads))
    {
        $new = $this->con->prepare("INSERT INTO channels (token_number, category_name, ads_set, keyboard_status) VALUES (?, ?, ".$ads[$i].", ".$key[$i].")");
        $new->bindParam(1,$token);
        $new->bindParam(2,$cat);
        $new->bindParam(3,$ads);
        $new->bindParam(4,$key);
        $new->execute();
        $notice['success_message'] = "New Telegram Channel was successfully added";
        return $this->notice;

    }
}
  • I can't do that, because at the other page I'm calling this: $tel = new Telegram($token, $cat, $ads[$i], $key[$j]); $notice[] = $tel->AddNew(); –  Sep 25 '17 at 21:48
  • @dasoudeabiat You need to pass the value in the AddNew(param, param, param, param) so like in your function AddNew($token,$cat,$ads,$key) so in your example just put at $tel->AddNew($token,$cat,$ads[$i],$key[$j]); here you can. –  Sep 25 '17 at 21:53