I have 3 anchor form in my view. When i click each one it redirects me to the getFeed function in the controller.
The view:
echo anchor('site/getFeed','Tehnology');
echo anchor('site/getFeed','Art');
echo anchor('site/getFeed','Science');
Controller:
function getFeed($feed_url){
$content = file_get_contents($feed_url)
$x= new SimpleXmlElement($content);}
My question in what to do to assign for each link a $feed_url variable.When i click Technology put in $feed_url a address and calls the getFeed function($feed_url). When click Art $feed_url take another address and put into getFeed function.
echo anchor('site/getFeed/some_value','Tehnology');
echo anchor('site/getFeed/another_value','Art');
echo anchor('site/getFeed/some_value_etc','Science');
You need to add a third URL parameter to each of your anchors. For example
echo anchor('site/getFeed/' . $feed_url,'Tehnology');
echo anchor('site/getFeed/' . $feed_url,'Art');
echo anchor('site/getFeed/' . $feed_url,'Science');
So the first url parameter site/ is the name of the controller, getFeed/ is the name of the function in your controller and then after that any other URL parameters will be passed to your controller function. You could have more if you wanted
echo anchor('site/getFeed/parameter1/parameter2/parameter3','Tehnology');
function getFeed($parameter1, $parameter2, $parameter3) {
$content = file_get_contents(base_url() . $feed_url);
try{
$x = new SimpleXMLElement($content);
} catch (Exception $e) {
echo 'Couldn't form XML';
exit();
}
}
Related
I displayed a table from database and created a delete link to every row. I want to delete a particular row when its corresponding link is clicked. Can anyone help me please..
foreach($model as $obj)
{
echo '<tr>';
echo '<td>'.$obj->getTestId().'</td>';
echo '<td>'.$obj->getTitle().'</td>';
echo '<td>'.$obj->getFilename().'</td>';
echo '<td>'.$obj->getContent().'</td>';
echo '<td>'.$obj->getStatus().'</td>';
echo '<td>'.$obj->getCreatedTime().'</td>';
echo '<td>'.$obj->getUpdateTime().'</td>';
echo '<td>'.'delete'.'</td>';
First, add this line inside the loop and replace modulefrontname with your frontname and replace controllername with your controller.
Add the deleteRowAction in your controller where you want to redirect when you click delete link.
Delete
public function deleteRowAction(){
try{
$model = Mage::getModel("practice/practice");
$model->setId($this->getRequest()->getParam("id"))->delete();
}catch(Exception $e){
Mage::getSingleton("core/session")->addError($e->getMessage());
}
//then redirect corresponds url
}
I passed the array that have been fetched from the model into the view section. The values are fetched on controller part as well as on the view part. The used the foreach() method to fetch the array elements as shown below
Controller Part
function view_savings()
{
$user = $this->session->userdata(uname);
$report['savings'] = $this->money_m->get_savings($user);
$this->load->view('showsavings',$report);
}
View Part
<?php
foreach($savings as $vs)
{
echo $vs->username;
echo $vs->stype;
echo $vs->inst_name;
echo $vs->acc_name;
echo $vs->smonth;
echo $vs->syear;
}
?>
The array values are displayed from the $savings array(). Is there any problem with my code. Please help me..
Before calling the view,
print_r($report['savings']);
With above code are you getting any result??
just try it
<?php
if(isset($savings) && count($savings) > 0)
{
foreach($savings as $vs)
{
echo $vs['username'];
echo $vs['stype'];
echo $vs['inst_name'];
echo $vs['acc_name'];
echo $vs['smonth'];
echo $vs['syear'];
}
}
?>
Good Luck ['}
Use this in your view
<?php
foreach($savings->result() as $vs)
{
echo $vs->username;
echo $vs->stype;
echo $vs->inst_name;
echo $vs->acc_name;
echo $vs->smonth;
echo $vs->syear;
}
?>
I have to show flash data which contains a link. I tried the following but link is not shown as it should.
How can I add link in flashdata?
I tried this:
$cancell = "<a href='<?php echo site_url('document/index');?>Cancell</a>";
$this->session->set_flashdata('success',
'You successfully created the document! ' . $cancell .', if you want to cancell it!');
redirect('document/index/');
Try some thing like this code below, but make sure url helper autoloaded
URL Helper Codeigniter
Controller
$link = anchor('document/index', 'cancel');
$message = 'You successfully created the document!' .' '. $link .' '. 'if you want to cancell it!';
$this->session->set_flashdata('success', $message);
redirect('document/index');
View
<p><?php echo $this->session->flashdata('success');?></p>
I have a problem when i try to add other parameter to URL.
before i use Codeigniter i add those parameters using JavaScript like this
test
but when i tried to do it with Codeigniter i don't know how.
<?php echo anchor("home/index/param1","test"); ?>
as i said i want to add this parameter for example my URL looks like this
home/index/param2
so when i click on test i want the URL to be like this
home/index/param2/param1
Take a look at CodeIgniter's URL Helper Documentation
The first parameter can contain any segments you wish appended to the URL. As with the site_url() function above, segments can be a string or an array.
For your example, you could try:
<?php
$base_url = 'home/index/';
$param1 = 'param1';
$param2 = 'param2';
$segments = array($base_url, $param1, $param2);
echo anchor($segments,"test");
?>
You can't do that with the form helper, you have to use your js function again :
echo anchor("home/index/param2", "test", array("onClick" => "javascript:addParam(window.location.href, 'display', 'param1');"));
It will produce :
test
But I don't see the point of dynamically change the href on the click event. Why don't you set it directly at the beginning ?
echo anchor("home/index/param2/param1", "test");
I have tried the following but it is giving me errors:
print_r($this->session->userdata());
How can I show all session data in CodeIgniter?
print_r($this->session->userdata);
or
print_r($this->session->all_userdata());
Update:
As of version 3.1.11 the above methods are deprecated. Use the below method to get all session data,
print_r($this->session->userdata());
echo "<pre>";
print_r($this->session->all_userdata());
echo "</pre>";
Display yet formatting then you can view properly.
For print session data you do not need to use print_r() function every time .
If you use it then it will be non-readable format.Data will be looks very dirty.
But if you use my function all you have to do is to use p()-Funtion and pass data into it.
//create new file into application/cms_helper.php and load helper cms into //autoload or on controller
/*Copy Code for p function from here and paste into cms_helper.php in application/helpers folder */
//#parram $data-array,$d-if true then die by default it is false
//#author Your name
function p($data,$d = false){
echo "<pre>";
print_r($data);
echo "</pre>";
if($d == TRUE){
die();
}
}
Just remember to load cms_helper into your project or controller using $this->load->helper('cms'); use bellow code into your controller or model it will works just GREAT.
p($this->session->all_userdata()); // it will apply pre to your sesison data and other array as well
here is code:
<?php echo '<pre>' . print_r($_SESSION, TRUE) . '</pre>'; ?>