getting function from class not working - php

I am trying to get a function from class to another file but when I run the code it says " Fatal error: Call to a member function start_order() on a non-object in C:\AppServ\www\store\start.php on line 4"
This is the calling code:
<?php
#session_start();
require_once ( 'include/functions.php' );
$users->start_order();
?>
And this is the class and the function code:
<meta charset='utf-8' />
<?php
require_once ( 'include/config.php' );
class users {
public function start_order () {
if($_SESSION['id']) {
$services=mysql_query("SELECT * FROM services");
print ("
<form action='<?php echo $PHP_SELF; ?>' method='post'>
<input name='orderlink' type='text'></input>
<input name='orderquantity' type='text'></input>
<input name='ordersubmit' type='text'></input>
</form>
");
$order_submit=$_POST ['ordersubmit'];
$order_link=$_POST ['orderlink'];
$order_quantity=$_POST ['orderquantity'];
if($order_submit AND !empty($order_link) AND !empty($order_quantity)){
while ($fetch_services=mysql_fetch_object($services)) {
print ("<select>");
print ("<option value='$fetch_services->id'>$fetch_services->service_name ($fetch_services->service_price)</option>");
print ("</select>"); }
} else { echo "خطاء";}
$s_id=$_POST ['service'];
$g_s_i=mysql_query("SELECT * FROM services WHERE id='$s_id'");
$fetch_service=mysql_fetch_object($g_s_i);
$s_price=$fetch_service->servic_price;
$service_n=$fetch_service->service_name;
$charge=$s_price/$order_quantity*$order_quantity;
$date=date ("Y-m-d");
$userbalance=mysql_query("SELECT * FROM users WHERE id='$_SESSION[id]'");
$fetch_userbalance=mysql_fetch_object($userbalance);
$u_balance=$fetch_userbalance->balance;
if ($u_balance >= $charge) {
$insert_order_values=mysql_query("INSERT INTO orders(id,u_id,u_name,date,link,charge,quantity,service_name,status) VALUES ('','$_SESSION[username]','$date','$order_link','$charge','$order_quantity','$service_n','0')");
$update_balance=mysql_query("UPDATE users SET balance=$u_balance-$charge WHERE id='$_SESSION[id]'");
}
else { echo "not enough fund";}
}}}
?>

You haven't made $users yet.
$users = new users();
$users->start_order();
Also, it's good practice to capitalize your class names, so they don't look so much like your instance.
$users = new Users();
$users->start_order();

You need to create an instance of the object before using it:
<?php
#session_start();
require_once ( 'include/functions.php' );
$users = new users();
$users->start_order();
?>

Related

php refreshing only data in page

i'm a php nearby. I have a problem with my form. I connect the controller with db and extract a question and his three answers. If i click on one of this, the system return the second question, under the first question. If i click on one of the seconds answers, the system return the thirth question overwrite the second question.
I would like that second question (and its answers) overwrite the first (and its answers). How i can?
db structure
view
1) index.php
<?php
error_reporting(E_ERROR | E_PARSE);
require_once 'controllers/controller.php';
$controllers = new Controller();
$controllers->cercaDomanda(1);
$controllers->cercaRisposta(1);
$controllers->cercaNuovoId($id);
?>
2) Controller.php
<?php
require_once('models/Domanda.php');
require_once('models/Risposta.php');
require_once ('models/Model.php');
class Controller {
public $domanda;
public $risposta;
public $model;
public function __construct(){
$this->domande = new Domanda(); //istanziamo la classe domanda
$this->risposte = new Risposta(); //istanziamo la classe risposta
$this->model = new Model(); //istanziamo la classe risposta
}
public function cercaDomanda($id){
$reslt = $this->domande->getDomanda($id);
include 'views/basicView.php';
}
public function cercaRisposta($id){
$reslt2 = $this->risposte->getRisposta($id);
include 'views/basicView.php';
}
public function cercaNuovoId($id){
include 'views/basicView.php';
$reslt3 = $this->model->getIdNext();
$reslt4 = $this->cercaDomanda($reslt3);
$reslt5 = $this->cercaRisposta($reslt3);
}
}
?>
3)Domanda.php
<?php
require_once 'models/Domanda.php';
class Domanda {
public function getDomanda($id){
$dbconnection = pg_connect("host=***port=***dbname=***user=***password=***");
$query = 'SELECT domanda FROM public."Domande" WHERE "id"='.$id;
$result = pg_query($query);
return $result;
pg_close($dbconnection);
}
}
?>
4)Risposta.php
<?php
require_once 'models/Risposta.php';
class Risposta {
public function getRisposta($id){
$dbconnection = pg_connect("host=***port=***dbname=***user=***password=***");
$query = 'SELECT * FROM public."Risposte" WHERE "id_domanda"='.$id;
$result = pg_query($query); //or die ('Query fallita: ' .pg_last_error());
return $result;
pg_close($dbconnection);
}
}
?>
5)Model.php
<?php
require_once 'models/Model.php';
class Model {
public function getIdNext(){
if(isset($_POST['btnScelta'])){
$result = $_POST['btnScelta'];
return $result;
}
}
}
6)View
<html>
<head></head>
<body>
<form action='index.php' name='chatbotyForm' method="POST">
<table>
<?php
echo "<tr>";
$domanda = pg_fetch_array($reslt, null, PGSQL_ASSOC);
print $domanda['domanda'];
echo "</tr>";
?>
</table>
<?php
while ($rispost = pg_fetch_object($reslt2)) {
print "<td><button type='submit' name='btnScelta'
value='$rispost->id_domanda_successiva'>$rispost->risposta</button>
</td>";
}
?>
</form>
</body>
</html>
In your view:
...
<?php
while ($rispost = pg_fetch_object($reslt2)) {
print "<td><button type='submit' name='btnScelta'
value='$rispost->id_domanda_successiva'>$rispost->risposta</button>
</td>";
}
?>
you are setting only one value in each button. which is available as $_POST['btnScelta'] and that is used to render your last question.
Edit previous contents removed because they are not relevant to the question.
To render two questions pass two ids as the button value.
To remove the first question remove the two lines from your index.php
1) index.php
<?php
error_reporting(E_ERROR | E_PARSE);
require_once 'controllers/controller.php';
$controllers = new Controller();
/* remove the following two lines the first question wont be rendered.*/
//$controllers->cercaDomanda(1);
//$controllers->cercaRisposta(1);
$controllers->cercaNuovoId(/*$id*/);
?>
Now make controller process two question display.
class Controller {
public function cercaNuovoId(/*$id*/){
// include 'views/basicView.php';
$reslt3 = $this->model->getIdNext();
// render previous question only if a value exists.
if ($result3) {
list($current, $previous) = explode(',', $result3);
$this->cercaDomanda($previous);
$this->cercaRisposta($previous);
}
// show the first question on first visit.
if (empty($current)){
$current = 1;
}
$reslt4 = $this->cercaDomanda($current);
$reslt5 = $this->cercaRisposta($current);
}
}
Now in the view make modification for button to have two values seperated by comma. current question id is available as $id from Controller::cercaRisposta()
<?php
while ($rispost = pg_fetch_object($reslt2)) {
print "<td><button type='submit' name='btnScelta'
value='{$rispost->id_domanda_successiva},{id}'>$rispost->risposta</button>
</td>";
}
?>
</form>
you can also pass the previous question id by changing form's action property with a query like index.php?previous=$id.

how do i get the data from view in codeigniter

I'm not good in codeigniter i'm still learning this. So i need your help guys.
I want to get the data from the database that the id is equal to the value of the dropdown list button.so heres is my code.
This is my controller: controller.php
function getdataload(){
$this->load->view('data',$data);
}
I really don't know what to put in the controller.
This is my view: view.php
<html>
<body>
<label for="member">Member</label>
<select class="form-control" id="member" name="member" required onchange="showCustomer(this.value)">
<option selected="" value="">--select--</option>
<?php foreach ($members as $row): ?>
<option value="<?php echo $row->mem_id; ?>"><?php echo ucwords($row->mem_fname.' '.$row->mem_lname) ?></option>
<?php endforeach ?>
</select>
</body>
</html>
<script>
$('#member').on('change',function(){
$.post('<?php echo base_url("transactions/getdataload")?>',
{
mem_id:$(this).val()
}).done(function(res)
{
$('#select_member').text(res);
});
});
</script>
This is the other view that the called from the controller data.php
<?php
$q = intval($_GET['member']);
$con = mysqli_connect('localhost','root','','global89_point');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"global89_point");
$sql="SELECT * FROM loading_service WHERE member='".$q."'";
$result = mysqli_query($con,$sql);
echo "<table>";
echo "<tr>";
echo " <th>";
echo "Member ID";
echo "</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['member'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
please help me with this guys.
You should call database lib from controller if you do not use models and database logic write to controller see this:
public function result()
{
$this->load->library('database')
$data = array();
$service_list = $this->db->query("SELECT * FROM loading_service WHERE member='".$q."'")->result_array();
$data['service'] = $service_list;
$this->load->view('result', $data);
}
Here result view you get $service array and you can easily iterate this.
Our model in CodeIgniter will be placed in application/models/form_model.php
<?php
class Form_model extends Model {
function Formmodel() {
// load the parent constructor
parent::Model();
}
function submit_posted_data() {
// db is initialized in the controller, to interact with the database.
$this->db->insert('loading_service ',$_POST);
}
function get_all_data() {
// again, we use the db to get the data from table ‘form’
$data['result']=$this->db->get('loading_service');
return $data['result'];
}
}
?>
controller
ss Form extends Controller {
function Form(){
// load Controller constructor
parent::Controller();
// load the model we will be using
$this->load->model('form_model');
// load the database and connect to MySQL
$this->load->database();
// load the needed helpers
$this->load->helper(array('loading_service','url'));
}
//Display the posted entries
function index() {
$data['member']='Form Data';
//use the model to get all entries
$data['result'] = $this->form_model->get_all_data();
// load 'forms_view' view
$this->load->view('forms_view',$data);
}
//Process the posted loading_service
function submit() {
//use the model to submit the posted data
$this->form_model->submit_posted_data();
redirect('loading_service');
}
}
?>
you can refer this code..It will helpful for u...

Fatal error: Call to a member function return_module_option() on string in C:\xampp\htdocs\ges\ges-admin\application_list.php on line 107

This is my code
require "../ges-admin/class/modules.php";
$module = new modules();
echo $module->return_module_option("");
Page:application_list.php
This is my class code:
class modules {
private function get_module($selid){
$getconn = new dbconnect();
$conn = $getconn->getconn();
$sel_module = mysqli_query($conn,"SELECT * FROM `ielts_module` WHERE `status`='1'");
while($fetch_module = mysqli_fetch_assoc($sel_module))
{
?>
<option <?php if($selid==$fetch_module['id']){ echo "selected=selected"; }?> value="<?php echo $fetch_module['id']; ?>"><?php echo $fetch_module['modules']; ?></option>
<?php
}
}
private function module_name($mid){
$getconn = new dbconnect();
$conn = $getconn->getconn();
$sel_module = mysqli_query($conn,"SELECT `modules` FROM `ielts_module` WHERE `id`='$mid'");
$fetch_module = mysqli_fetch_assoc($sel_module);
return $fetch_module['modules'];
}
public function return_module_option($selid){
return $this->get_module($selid);
}
public function get_module_name($mid)
{
return $this->module_name($mid);
}
}
This echo $module->return_module_option(""); code is works fine when page loads after submitting the form through get method, it shows
Fatal error: Call to a member function return_module_option() on string in C:\xampp\htdocs\ges\ges-admin\application_list.php on line 107
You need to confirm what new modules() is returning. Try outputting $module across both the GET and POST requests for the route. Then correct the reason why new modules() isn't returning the correct return type.
var_dump($modules);
Also, if you can share your modules class that'd be a start, or at least the constructor.

Learning OOP in PHP. Is this the correct way to do this?

I've just started learning to do oop and I just wanted to put the most basic set of code together to make sure I'm understanding things correctly. I wanted to capture a form entry in the $_POST variable and pass it to an object to have it output something back to the browser. No SQL, no Security measures, just proof of understanding.
Here is the form:
<html>
<head>
<title>SignUp Form</title>
</head>
<body>
<?php
if(!empty($_POST['name'])) {
include_once "class.php";
} else {
?>
<form method="post" action="signup.php">
<label for="name">Enter name below:</label></br>
<input type="text" name="name" id="name"></br>
<input type="submit" value="Submit">
</form>
<?php
}
echo $name->processName($_POST['name']); ?>
</body>
</html>
And here is the class:
<?php
class Process {
public $entry;
function __construct($entry) {
$this->entry = $entry;
}
public function processName($entry) {
return "You entered " . $this->entry . ".";
}
}
$name = new Process($_POST['name']); ?>
This is working without error right now but it doesn't seem like I should have to enter the $_POST in the echo statement on the form page and in the object on the class page. Is this correct? Should I instead be collecting that in the $entry property. It's working, but I don't think the execution is correct. Thanks in advance!
Your right you don't need to enter the $_POST variable into that function, you could change it to this and it would work without entering the post:
public function processName() {
return "You entered " . $this->entry . ".";
}
Because right now processName function doesn't do anything with the class's public $entry variable, it just echoes out what you put in when you call the function.
What you likely want to do instead is:
Change public $entry; to protected $entry;
Then:
public function getEntry() {
return $this->entry;
}
Then in your html, after constructing the class, you can just put this to get the $entry variable:
echo $name->getEntry();
Coming from Symfony framework background. You could do something right this:
<?php
class Process
{
protected $post_var;
public function __construct($p)
{
$this->post_var = $p;
}
public function getData()
{
//checking if not post request
if(count($this->post_var) == 0) {
return false;
}
$result_arr = [];
//populating $result_arr with $_POST variables
foreach ($this->post_var as $key => $value) {
$result_arr[$key] = $value;
}
return $result_arr;
}
}
$process = new Process($_POST);
$data = $process->getdata();
if($data)
{
echo $data["name"];
}
?>
<form action="" method="post">
<input type="text" name="name"/>
<input type="submit" name="submit"/>
</form>

save not working properlly

controller
userdetails.php
class Controller_Userdetails extends Controller {
public function action_index() {
$view = new View('userdetails/index');
$this->response->body($view);
}
public function action_add() {//load adddetails.php
$userdetails = new Model_Userdetails();
$view = new View('userdetails/adddetails');
$view->set("userdetails", $userdetails);
$this->response->body($view);
}
public function action_post() {//save the post data
$userdetails_id = $this->request->param('id');
$userdetails = new Model_Userdetails($userdetails_id);
$userdetails->values($_POST);
$userdetails->save();
$this->request->redirect('index.php/userdetails');
}
}
views
adddetails.php
<?php echo Form::open('userdetails/post/'.$userdetails->id); ?>
<?php echo Form::label("first_name", "First Name"); ?>
<?php echo Form::input("first_name", $userdetails->first_name); ?>
<br />
<?php echo Form::label("last_name", "Last Name"); ?>
<?php echo Form::input("last_name", $userdetails->last_name); ?>
<br />
<?php echo Form::label("email", "Email"); ?>
<?php echo Form::input("email", $userdetails->email); ?>
<br />
<?php echo Form::submit("submit", "Submit"); ?>
<?php echo Form::close(); ?>
I am trying to insert the data into database.The name and email field are loading correctly,if i enter values and hit enter it is redirecting to other page but objects not saved in database.Need help to solve this.
I sometimes find problems in saving while using
$userdetails->values($_POST);
try using
$userdetails->first_name = $this->request->post('first_name');
$userdetails->last_name= $this->request->post('last_name');
$userdetails->email= $this->request->post('email');
Assuming the model is an ORM model (class Model_Userdetails extends ORM {}), you should use ORM::factory() to load the model. Furthermore it's recommended to use the expected parameter to be sure only the values you want to be inserted are used.
$user_details = ORM::factory('Userdetails');
$user_details->values($this->request->post(),
array('first_name', 'last_name', 'email')
);
$user_details->save();

Categories