Data through controller - view in CodeIgniter - codeigniter

I have this snippet code in my Controller:
// load model
$this->load->model('article');
$data['article'] = $this->article->get_data_article();
// add more array in data['article']
$data['article'][] = array( 'newcontent' => 'dummy string' );
$this->load->view('index',$data);
then I have 2 views, first is index.php:
$this->load->view('content',$article[0]);
second view is content.php:
<?php echo $title;?> <br>
<?php echo $newcontent;?>
I've got error on second line above. The error message is:
Severity: Notice
Message: Undefined variable: newcontent
Filename: views/content.php
What's wrong with my code?

You have to display like this,
echo $article[0]['newcontent'];
Because you have created it as a two dimensional Array.

This is the right way to do it.
$data['article']['article1'] = array( 'newcontent' => 'dummy string' );
$data['article']['article2'] = $this->article->get_data_article();
And in view
echo '<pre>';
print_R($article1);
echo '<pre>';
print_R($article2);
Now you can access. Numeric arrays cause problem passing data to views. Use indexed arrays.
EDITS :
You can pass it to view like this
$this->load->view('content',$data);
And in the view now you have $article.
echo '<pre>';
print_R($article);
if you apply php extract on article
extract($article);
You will be able to access
echo '<pre>';
print_R($article1);
echo '<pre>';
print_R($article2);
But the each way is to do that
Controller
$data['article1'] = array( 'newcontent' => 'dummy string' );
$data['article2'] = $this->article->get_data_article();
Pass it to view
$this->load->view('content',$data);
and access like this
echo '<pre>';
print_r($article1);
echo '<pre>';
print_r($article2);
No extraction required
More EDITS:
You can do it like this
$result['article1'] = array( 'newcontent' => 'dummy string' );
$this->load->view('content',$result);
$result1['article2'] = $this->article->get_data_article();
$this->load->view('index',$result1);

Related

Codeigniter 4 Passing Array to view

Fairly new to codeigniter and i just cant find the right way to load an array into a view.
for example lets say i have an array like
$data = [
'title' => 'my title,
'desc' => 'my desc,
];
i can pass that to my view like
return view('myview',$data);
then simply echo it out in my view like
<h1><?= $title ?></h1>
<p><?= $desc ?></p>
That works fine. but now lets say i have another array like :
$moredata =[
'more_data' => 'some more data',
'even_more_data' => 'even more data',
];
if i try to add that to my data array like
$data[] = $moredata
when i try to access 'more_data' or 'even_more_data' in my view like
<?= $more_data ?>
i get a undefined variable error for $moredata. So how do i access the variables within that new array? am i declaring them properly?
also if i wanted to loop through the secondary array how do i do that. trying
<?php foreach($moredata as $items){ ?>
<li><?php echo $items; ?></li>
<?php } ?>
also gives me an undefined variable error for $moredata
any help on how to do this correctly in codeigniter 4 appreciated.
Codeigniter uses the key of the array you're giving him to create variables name.
You should init it this way :
$moredata =[
'more_data' => 'some more data',
'even_more_data' => 'even more data',
];
// key of your array will be a variable name in your view
$data['my_var_name_in_view'] = $moredata
return view('myview',$data);
Then in your view you will be able to perform this :
<?php foreach($my_var_name_in_view as $items){ ?>
<li><?php echo $items; ?></li>
<?php } ?>

Connecting with multiple resources in PHRETS

I have to retrieve the data from multiple resources by using the Rets server. Is there any way to do so?
For example,
I have resources and classes like:
Property(Resource), Listing(Class)
Media(Resource), Media(Class)
Some data should be fetched from the first and a little bit from the second.
I am using the Phrets to retrieve the data from the Rets server.
I am using the source code:
<?php
date_default_timezone_set('America/New_York');
require_once("vendor/autoload.php");
$log = new \Monolog\Logger('PHRETS');
$log->pushHandler(new \Monolog\Handler\StreamHandler('php://stdout', \Monolog\Logger::DEBUG));
$config = new \PHRETS\Configuration;
$config->setLoginUrl('Url to connect with')
->setUsername('Username to login')
->setPassword('Password to login');
->setRetsVersion('1.5')
$rets = new \PHRETS\Session($config);
$rets->setLogger($log);
$connect = $rets->Login();
$resource = 'Property';
$class = 'Listing';
$query = "(Acres=0+)";
$options = array(
'Count' => 1,
'Format' => 'COMPACT-DECODED',
'Limit' => 50,
'StandardNames' => 0,
'Select'=>'Acres,City,ClosePrice,BathsFull,BathsHalf,PhotoCount,VirtualTourLink'
);
$results = $rets->Search($resource,$class,$query,$options);
foreach($results as $record){
echo "<table>";
echo "<tr>";
echo "<td>".$record['Acres']."</td>&nbsp";
echo "<td>".$record['City']."</td>";
echo "<td>".$record['ClosePrice']."</td>";
echo "<td>".$record['BathsFull']."</td>";
echo "<td>".$record['BathsHalf']."</td>";
echo "<td>".$record['VirtualTourLink']."</td>";
echo "</tr>";
echo "</table>";
}
?>
Thanks in advance!
Fetch your records from the first class and then fetch your records from the second class using the second classes dependent id. Media classes are always dependent on the property classes, so it will have some id that corresponds to the main id of the property class (usually the mls # of the listing). Without seeing your RETS server's metadata I won't be able to tell you the names of the fields you need to pull from. Also, your version is more than likely 1.7.2, AFAIK no one really uses 1.5 anymore.
You can fetch by using loop of class name.
$resource = 'Property';
$classes = array('Listing','Media');
$query = "(Acres=0+)";
$options = array(
'Count' => 1,
'Format' => 'COMPACT-DECODED',
'Limit' => 50,
'StandardNames' => 0,
'Select'=>'Acres,City,ClosePrice,BathsFull,BathsHalf,PhotoCount,VirtualTourLink'
);
foreach($classes as $class){
$results[$class] = $rets->Search($resource,$class,$query,$options);
}

Dependanat Drop down is not working when it has only one value in yii framework

I am using dependant dropdownlist for get value for subject dropdown when select value from 'Grade' dropdown. It is working fine. But the problem is when there are only one value in grade dropdown the subject dropdown is not updated.
this is my code:-
Grade DropDown----------
($data is consist of grade)
<?php echo CHtml::dropDownList('myDropDown1','',$data,array(
'id'=>'gr',
'empty' => '(Select Grade)',
'style'=>'width:200px',
'ajax' =>
array(
'type'=>'POST', //request type
'url'=>CController::createUrl('sub'), //action to call
'update'=>'#cls', // which HTML element to update
)
)); ?>
Subject Dropdown (which depend on grade dropdown)------------
<?php echo CHtml::label('Subject',''); ?>
<?php echo CHtml::dropDownList('myDropDown3','',array(),array(
'id'=>'sub',
'prompt'=> 'Please select a class', 'style'=>'width:150px',
'style'=>'width:200px',
)); ?>
<?php //echo $form->error($model,'sub_id'); ?>
In controller-------------------
public function actionClass()
{
$grd = $_POST['myDropDown1'];
$c_id = TbClass::model()->findAll('id=:id',
array(':id'=>$grd,));
$data3 = CHtml::listData($c_id,'id','grade');
$grd2 = array_shift($data3);
$sub1 = TbClass::model()->findAll('grade=:grade',
array(
':grade'=>$grd2,
));
$data4 = CHtml::listData($sub1,'id','class');
foreach($data4 as $value=>$name)
{
echo CHtml::tag('option',
array('value'=>$value),CHtml::encode($name),true);
}
}
This code is working fine. Problem is when grade has only one value in the dropdown , cannot update the subject dropdown.
I think your problem is in array_shift,
have you checked the value of $grd2 ?

Non-static method Pager::factory() should not be called statically

I am a beginner in php.Whenever i try to implement Pager class in pear i get this error
Non-static method Pager::factory() should not be called statically in D:\xampp\htdocs\sam\temp\youtube_playlist.php on line 21
My code is
<?php
include("header.php");
include("connect.php");
//connect to db to get video data
$qr = mysql_query("SELECT vid FROM videos",$con);
$data="";
while($row = mysql_fetch_array($qr))
{
$data[]=$row['vid'];
}
//pageing
require('Pager/Pager.php');
$pg_op= array(
'itemData'=>$data,
'append' => true,
'perPage' => 3,
'mode' => 'Sliding',
'delta' => 2
);
$pager = Pager::factory($pg_op);
$pdata = $pager -> getPageData();
$plinks = $pager -> getLinks();
//Display the video
foreach ($pdata as $vd)
{
?>
<iframe width="510" height="265" src="http://www.youtube.com/embed/<?php echo $vd; ?>" frameborder="0" allowfullscreen></iframe>
<?php
}
echo "<br />";
echo $plinks['all'];
include("footer.php");
?>
Please help
I just had the same issue.
This Pager class seems to be outdated. If you still want to use it, just suppress error by prepending the command with #
$pager = #Pager::factory($pg_op);

Codeigniter and Tank_auth sending validation to view not working

Hi I am new to Codeigniter but have hit a brick wall.
I am trying to see if a user already exists.
First I upload the data via a form to a controller which does its validation etc but breaks on only that issue. I managed to find where it breaks but cant fix it from there.
Prior to all this it querys the database finds there is in fact a match username and then reaches the snippet below
$errors = $this->tank_auth->get_error_message();
//find the correct error msg
foreach ($errors as $k => $v) $data['errors'][$k] =$this->lang->line($v);
//loop and find etc
$temp_mess = $data['errors'][$k];
//stores relevant stuff in the string
}
}
//echo $temp_mess; it outputs to the html so i can see it "says user exists"
$data['temp_mess'] = $tempmess; /// put this into a array
$this->load->view('layout', $data); ///send
}
}
Now for the view, it then calls the layout view etc but alas there is no output
$username1 = array(
'name' => 'username1',
'id' => 'username1',
'value' => set_value('username1'),
'maxlength' => $this->config->item('username_max_length', 'tank_auth'),
'size' => 30,
);
<?php echo form_open('register', $form_reg_id ); ?>
<fieldset>
<legend align="center">Sign up</legend>
<?php echo form_label('Username', $username1['id']); ?>
<?php echo form_input($username1); ?>
<?php $tempmess; ?>
<div class="error"><?php echo form_error($username1['name']); ?>
<?php echo isset($errors[$username1['name']])?$errors[$username1['name']]:''; ?>
<?php echo form_close(); ?>
</fieldset>
Thanks for any help on this
also could some one explain this line please. (for a really dumb person)
<?php echo isset($errors[$username1['name']])?$errors[$username1['name']]:''; ?>
Tank_auth automatically returns an error when the username exists. Juste have to check the errors array.

Resources