Passing variable to a controller in CodeIgniter - php

Hello I am displaying data in the view but each time that i display i want to store the Student_id in a variable the pass that variable to a controller but it is giving me errors
foreach($results as $row)
{
$this->load->helper('url');
echo '<a href="http://localhost/amref/mama/get_student?var1=<?php echo $row->student_id;?>" '.'<tr><td>'.$row->student_fname.' '.$row->student_lname.'</td><td>'.$row->gender.'</td><td>'. $row->res_district.' , '.$row->res_region.'</td><td>'.$row->collage_name.'</td><td>'.$row->course_name.'</td></tr>'.'</a>';
}

Your syntax is incorrect and I am pretty sure you want that anchor tag nested inside a <td> element, maybe like this:
echo '<tr><td>'.$row->student_fname.' '.$row->student_lname.'</td><td>'.$row->gender.'</td><td>'. $row->res_district.' , '.$row->res_region.'</td><td>'.$row->collage_name.'</td><td>'.$row->course_name.'</td></tr>';

Related

View included by $this->include does note recieve passed params

Is possible pass parameter to view when i use $this->include method in another view?
In example:
<?php
foreach ($destaques as $camping) {
$this->include('partial', ['camping' => $camping])
}
?>
But partial.php dont recieve $camping value.
When using $this->include you're making an echo of a view into an other one. So by default, the view you're loading will have acces to any data you gave to the parent view but not variables you declared into it.
A few options in your case :
Using the view method :
foreach ($destaques as $camping) {
echo view('partial', ['camping' => $camping]);
}
Moving your foreach loop in the partial view so you'll use $destaques into it.
<?php
// dont forget to echo
echo $this->include('partial')
?>
// or this way with short tags enabled
<?= $this->include('partial') ?>
And just embed your partial view in your previous foreach loop
foreach ($destaques as $camping) {
// whatever your partial view is
}
Hello I try using the parameter option. I test code and it s working. But note the include view layout method work like php default include function and it does not display until you echo it.
Here is my code which I use to test yours and it worked for me. Check it
$inc = '';
foreach ($destaques as $camping) {
$inc .= $this->include('partial', ['camping' => $camping])
}
echo $inc;
And this displayed and worked for me check it. if this is not what you were expecting, just call my attention

How to use a PHP classes array as a name of a select tag HTML

i want to use what we call a struct in C but using PHP, I know Here they are called classes, but i need to use that array of classes for a select tag name, I am doing this
<?php
class info_subject{
public $code_su;
public $time_su;
public $selecction_su;
}
$subjects= new info_subjects();
$i=0;
//THE DATABASE CONNECTION WORKS FINE, I IGNORED CODING ABOUT DATABASE BECAUSE THAT'S NOT THE
//PROBLEM, JUST FOCUS IN THE STATEMENT OF THE ARRAYS IN THE TAGS NAMES PLEASE
while($line = pg_fetch_array($result, null, PGSQL_NUM))//getting some stuff from postgrest
{
echo "$line[0]";//I am printing this
echo "$line[1]";//I am printing this
//here i am creating selects in every loop with some options, and i want to save the
//result of the selection in the field code_su of the array of classes
echo "<select name=$subjects[$i]->code_su>";
echo "<option value='hola'>hola</option>";
//here i am creating checkbox in every loop, and i want to save the
//result of the checkbox in the field selection_su of the array of classes
echo "<input type='checkbox' name=$subjects[$i]->selection_su>";
$i++;
}
?>
The problem is that it is not working, i think i am making a mistake with the statement in the names of the inputs and the selects, like i said before, i need a classes array.
The problem is that youre not adding the vars properly (nor the quotes). Try with:
echo "<select name=\"".$asignaturas[$i]->codigo_as . "\">";
and
echo "<input type='checkbox' name=\"".$asignaturas[$i]->seleccion_as."\">"
Regards.
1) You are echoing HTML wrong (missing ': echo "<select name='{$asignaturas[$i]->codigo_as}'>";
2) Your $asignaturas is not an array. It's only single class. Use it like this: echo $asignaturas->coding_as;
3) (as side note) By standarts, class names is CamelCases and it's name is same as file name.
It seems like you are trying to use the class itself as an array which can not be done.
Put in an constructor to define some of your variables here:
class info_asignatura{
public $codigo_as;
public $periodo_as;
public $seleccion_as;
function __construct(){
$this->seleccion_as = array();
}
}
The change this statement to:
echo "<input type='checkbox' name=$asignaturas->seleccion_as[$i]>";
Although, I fear this will not do the trick for you. Because every time this page is loaded, seleccion_as will be defined as an array when the class is constructed. This will overwrite anything previously declared.
What you will need to obtain your goal is to implement sessions to your code.

HREF to call a PHP function and pass a variable?

Is it possible to create an HREF link that calls a PHP function and passes a variable along with it?
<?php
function sample(){
foreach ($json_output->object ){
$name = "{$object->title}";
$id = "{$object->id}";
print "<a href='search($id)' >$name</a>";
}
}
function search($id){
//run a search via the id provide by the clicking of that particular name link
}
?>
You can do this easily without using a framework. By default, anything that comes after a ? in a URL is a GET variable.
So for example, www.google.com/search.html?term=blah
Would go to www.google.com/search.html, and would pass the GET variable "term" with the value "blah".
Multiple variables can be separated with a &
So for example, www.google.com/search.html?term=blah&term2=cool
The GET method is independent of PHP, and is part of the HTTP specification.
PHP handles GET requests easily by automatically creating the superglobal variable $_GET[], where each array index is a GET variable name and the value of the array index is the value of the variable.
Here is some demo code to show how this works:
<?php
//check if the get variable exists
if (isset($_GET['search']))
{
search($_GET['search']);
}
function Search($res)
{
//real search code goes here
echo $res;
}
?>
Search
which will print out 15 because it is the value of search and my search dummy function just prints out any result it gets
The HTML output needs to look like
anchor text
Your function will need to output this information within that format.
No, you cannot do it directly. You can only link to a URL.
In this case, you can pass the function name and parameter in the query string and then handle it in PHP as shown below:
print "<a href='yourphpscript.php?fn=search&id=$id' >$name</a>";
And, in the PHP code :
if ($_GET['fn'] == "search")
if (!empty($_GET['id']))
search($id);
Make sure that you sanitize the GET parameters.
No, at least not directly.
You can link to a URL
You can include data in the query string of that URL (<a href="myProgram.php?foo=bar">)
That URL can be handled by a PHP program
That PHP program can call a function as the only thing it does
You can pass data from $_GET['foo'] to that function
Yes, you can do it. Example:
From your view:
<p>Edit
Where 1 is a parameter you want to send. It can be a data taken from an object too.
From your controller:
function test($id){
#code...
}
Simply do this
<?php
function sample(){
foreach ($json_output->object ){
$name = "{$object->title}";
$id = "{$object->id}";
print "<a href='?search=" . $id . "' > " . $name . "</a>";
}
}
if (isset($_REQUEST['search'])) {
search($_REQUEST['search']);
}
function search($id){
//run a search via the id provide by the clicking of that particular name link
}
?>
Also make sure that your $json_output is accessible with is the sample() function. You can do it either way
<?php
function sample(){
global $json_output;
// rest of the code
}
?>
or
<?php
function sample($json_output){
// rest of the code
}
?>
Set query string in your link's href with the value and access it with $_GET or $_REQUEST
<?php
if ( isset($_REQUEST['search']) ) {
search( $_REQUEST['search'] );
}
function Search($res) {
// search here
}
echo "<a href='?search='" . $id . "'>" . $name . "</a>";
?>
Yes, this is possible, but you need an MVC type structure, and .htaccess URL rewriting turned on as well.
Here's some reading material to get you started in understanding what MVC is all about.
http://www.phpro.org/tutorials/Model-View-Controller-MVC.html
And if you want to choose a sweet framework, instead of reinventing the MVC wheel, I highly suggest, LARAVEL 4

Passing value from controller to layout

I have tried to pass a value from controller to layout using this but it does not work:
foreach ($user_info_details as $details):
$first_name = $details['first_name'];
endforeach;
Zend_Layout::getMvcInstance()->assign('first_name',$first_name);
and retrieve it using
<?php echo $this->layout()->first_name; ?>
but it shows blank in every case
To output values to your view easily, in your controller use:
$this->view->first_name = $first_name;
And in your view, access it like this:
echo $this->first_name;

Variable inside a php echo function

I have a php function which displays a rating bar with the arguments. I have a variable called itemID inside my php page which holds the unique item number. I need to send this value to my function and also echo command must stay. Is there a way to achieve this?
Here is the code, which does not work. When I try it on the server, it does not show the id of item, it prints the variable name as it is.
<?php echo rating_bar('$as',5) ?>
What I get at html file:
<div id="unit_long$as">
instead of the item id in place of $as.
Single Quotes do not support variable replace,
$as = "test";
echo '$as'; //$as in your end result
echo "$as"; // test in your end result
echo $as; // test in your end result
//For proper use
echo " ".$as." "; // test in your end result
Update for newer PHP versions you should now use Template Syntax
echo "{$as}"
If I get what you are saying, this is what you are asking.
<?php echo rating_bar($itemID,5); ?>
With the limited code you are providing, thats what looks like you are asking.

Categories