83031

Run a function without a parameter but needed a variable outside of the function - PHP

I have this class called Campaign and I'm trying to echo $campaign->getName() without using any parameters like echo $campaign->getName($user_id, $campaign_id)

<?php $campaign = new Campaign($db); class Campaign { private $db; public function __construct($db) { $this->db = $db; } public function getName() { $query = $this->db->prepare("SELECT name FROM campaign WHERE campaign_id = :campaign_id AND user_id = :user_id"); $status = $query->execute(array(':campaign_id' => $campaign_id, ':user_id' => $user_id)); return ($query->rowCount() == 1) ? $query->fetchObject()->name : false; } }

What I'm getting is

Missing argument 1 for Campaign::getName() Missing argument 2 for Campaign::getName()

Well logically thinking that's supposed to happen.

What I'm trying to call is $user_id and $campaign_id that's been retrieved at <strong>session</strong>

Here's the structure of my init.php where all the classes/functions are being stored.

<img src="https://i.stack.imgur.com/17cgr.png" alt="enter image description here">

Is it possible to call a function without a parameter, but that function needed a variable outside of the function?

Answer1:

You cannot access that variable since its only not defined locally inside the function. If you want to use the var inside when its defined outside you use global before the var.

$foo = 'bar'; function baz() { global $foo; // now u can use it inside. }

Php variable scope

I dont like globals personally, you could also do something like:

function foo($arg1 = null, $arg2 = null) { // if they are not set retrieve from a session $arg1 = ($arg1 !== null) ? $arg1 : $_SESSION['arg1']; // rinse repeat. }

Answer2:

You havent called the $user_id and $campaign_id from session

Should have $user_id and $campaign_id as global variables:

global $user_id; global $campaign_id; $user_id = $_SESSION['user_id']; $campaign_id = $_SESSION['campaign_id'];

Place the above variable declaration after session_start() on your screenshot

Try this:

<?php session_start(); $campaign = new Campaign($db); class Campaign { private $db; public function __construct($db) { $this->db = $db; } public function getName() { $query = $this->db->prepare("SELECT name FROM campaign WHERE campaign_id = :campaign_id AND user_id = :user_id"); $status = $query->execute(array(':campaign_id' => $_SESSION['campaign_id'], ':user_id' => $_SESSION['user_id'])); return ($query->rowCount() == 1) ? $query->fetchObject()->name : false; } }

Recommend

  • Spring Social for Facebook - get user location
  • org.json.JSONObject$1 cannot be converted to JSONObject error while parsing json string
  • Datagridview to Clipboard with formatting
  • Clean way to get foreign key objects in PHP MySQL query
  • Increment inside angular expression
  • Why Is My Azure SQL Database Table Permanently Locked?
  • Credit card balance exercise in Python
  • QStandardItemModel::removeRows() does not work in my use case
  • Can't remove headers after they are sent
  • Security issues with PHP's Readfile method
  • How to use JavaScript to determine whether a file exists in a directory?
  • onBackPressed() not being executed
  • How do I pass the string value parameter of the selected list item from an auto-populated dropdown l
  • Perl system calls when running as another user using sudo
  • Can I have the cursor start on a particular column by default in jqgrid's edit mode?
  • Statically linking a C++ library to a C# process using CLI or any other way
  • Adding custom controls to a full screen movie
  • ORA-29908: missing primary invocation for ancillary operator
  • How to get next/previous record number?
  • Delete MySQLi record without showing the id in the URL
  • Rearranging Cells in UITableView Bug & Saving Changes
  • SVN: Merging two branches together
  • Benchmarking RAM performance - UWP and C#
  • Load html files in TinyMce
  • How do you join a server to an Active Directory (domain)?
  • Angular 2 constructor injection vs direct access
  • Change div Background jquery
  • How does Linux kernel interrupt the application?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Bitwise OR returns boolean when one of operands is nil
  • sending mail using smtp is too slow
  • Django query for large number of relationships
  • Programmatically clearing map cache
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Binding checkboxes to object values in AngularJs
  • How can I use `wmic` in a Windows PE script?
  • Why do underscore prefixed variables exist?
  • How to push additional view controllers onto NavigationController but keep the TabBar?