set controller function as link - php

I have created simple mvc in php.
My controller is :
class front
{
function __construct()
{
}
function add_to_cart()
{
echo "123";exit;
}
}
Now, I want to pass id into this controller.
My view page is:
<span>ADD TO CART</span>
So, what link I have to write in href to get itemID in add_to_cart function?

I'm not sure about your question but this is simple example of using such functionality
Test.php
<?php
echo '<a href="../another_test.php/add_to_cart/?id=1231"/>Click Here</a>';
?>
another_test.php
<?php
class A{
function __construct(){
}
function add_to_cart(){
$id = $_GET['id'];
return $id;
}
}
$a = new A();
echo $a->add_to_cart();//1231

{BASE URL : http://localhost/projectname}/{indx.php(ifrequired)}/{CONTROLLER NAME : front}/{METHOD NAME : add_to_cart} / {VALUE : itemID}
In short {http://localhost/projectname}/{indx.php}/{front}/{add_to_cart}/{itemID}
function add_to_cart($itemID)
{
echo $itemID;exit;
}

Related

PHP function working differently for return and echo. Why?

By using the following class:
class SafeGuardInput{
public $form;
public function __construct($form)
{
$this->form=$form;
$trimmed=trim($form);
$specialchar=htmlspecialchars($trimmed);
$finaloutput=stripslashes($specialchar);
echo $finaloutput;
}
public function __destruct()
{
unset($finaloutput);
}
}
and Calling the function, by the following code, it works fine.
<?php
require('source/class.php');
$target="<script></script><br/>";
$forminput=new SafeGuardInput($target);
?>
But if in the SafeGuardInput class if I replace echo $finaloutput; with return $finaloutput; and then echo $forminput; on the index.php page. It DOES NOT WORK. Please provide a solution.
You can't return anything from a constructor. The new keyword always causes the newly created object to be assigned to the variable on the left side of the statement. So the variable you've used is already taken. Once you remember that, you quickly realise there is nowhere to put anything else that would be returned from the constructor!
A valid approach would be to write a function which will output the data when requested:
class SafeGuardInput{
public $form;
public function __construct($form)
{
$this->form=$form;
}
public function getFinalOutput()
{
$trimmed = trim($this->form);
$specialchar = htmlspecialchars($trimmed);
$finaloutput = stripslashes($specialchar);
return $finaloutput;
}
}
Then you can call it like in the normal way like this:
$obj = new SafeGuardInput($target);
echo $obj->getFinalOutput();

Get values from vars inside class across other functions

class dir_exam
{
public $db_ruta;
function __construct($db_ruta)
{
$this->db_ruta=$db_ruta;
}
function veritas()
{
$aa="ok";
$xx="ok2";
return $aa;
return $xx;
}
function create_d()
{
$r=$this->veritas();
echo $r->$aa;
echo $r->$xx;
}
}
I have this class and i try execute funtion veritas inside function create_d, but i want show the value from function veritas as individual values, showing value in create_d for $aa and $xx, when execute finally the class
<?php
$a=new dir_exam("db_p");
echo $a->create_d();
?>
But i can´t get this finally, i don´t know if it´s not possible or what, this it´s my question, thank´s in advanced
You can't have 2 or more returns in a function.
For you use the vars $aa and $xx like OOP, you must create the 2 var in the class
class dir_exam
{
public $db_ruta;
public $aa; // <--
public $xx; // <--
}
After, you need change the function veritas to pass the value for your attributes
function veritas()
{
$this->aa="ok";
$this->xx="ok2";
}
Now in your function you can call like that:
function create_d()
{
$this->veritas();
echo $this->aa;
echo $this->xx;
}

Passing GET value to WP widget

A im creating some custom widget in Word Press, but what i need is that i can pass GET value inside my widget, i have tried many ways but no luck, here is my widget code in WP for now
class Trucks extends WP_Widget{
function __construct(){
parent::__construct(false, $name = __('Truck API'));
}
function form(){
}
function update(){
}
function widget($args, $instance){
extract($args, EXTR_SKIP);
if(isset($_GET['a'])) {
$test = $_GET['a'];
echo $test;
}
else {
$test = 'Nothing';
}
}
}
add_action('widgets_init', function(){
register_widget('Trucks');
});
?>
It diplay for now nothing, it is much more complex class, but this is made simple to understand what i need :)

How to use variable inside a class in the public function??Maybe reference?

<?php class pac{
public $q=5;
public function yo($q){
$v=$q+2;
print $v;
}
}
$a=new pac;
$a->yo($q);
?>
Hi guys ,i am beginner,sorry for stupid questions!
So the question is xD : how to use $q?? I need to put reference on it ??
You use $q but $q isn't defined. If you want to use the object variable $q you can use $a->q.
<?php
class pac{
public $q=5;
public function yo($q){
$v=$q+2;
print $v;
}
}
$a=new pac;
$a->yo($a->q);
?>
Another option is to use the object variable in your method instead of pass it as parameter:
<?php
class pac{
public $q=5;
public function yo(){
$v=$this->q+2;
print $v;
}
}
$a=new pac;
$a->yo();
?>

Having problems on fetching variables on a class in PHP

I just want to ask if its possible to call variables on class to another page of the site. I have tried calling the function's name and inside the parenthesis. I included the variable found inside that function e.g:
<?php
$loadconv -> loadmsg($msgReturn);
echo $loadconv;
?>
But it didn't work.
Do you want something like this?
class Load
{
public $msgReturn;
__construct()
{
}
public function loadMsg($param)
{
$this->msgReturn = $param;
}
}
Then you could do
$loadConv = new Load();
$loadConv->loadMsg('just a string');
echo $loadConv->msgReturn; // 'just a string'

Categories