How to display flashdata in codeigniter - php

Following is my view I am catching the data from the model and displaying on the view using
flashdata in codeigniter
My Controller cart.php
public function coupon(){
for ($i = 0; $i <= $this->input->post("products_in_cart"); $i++) {
if (!empty($this->input->post("coupn-" . $i))) {
$couponname = $this->input->post("coupn-" . $i);
$products_id = $this->input->post("product_id" . $i);
$data = $this->home_model->getCoupon($couponname, $products_id);
$data1 = 'hello';
$info = array(
"PromotioanlName" => $data->PromotionalName,
);
} else {
$info = 'Thers in no value<br>';
}
}
echo $this->session->set_flashdata('message', $info);
redirect(site_url('cart'));
}
My view cart.php
$message = $this->session->flashdata('message');
print_r($message);
But my problem is that my data is overwritten by the next value

in for loop, you have written if and in if, $info is an array and in else, $info is string!! Thus in loop when condition of if will be true, it'll be worked as an array and it'll be overwritten if condition will be true again in second recurs of loop!! And while condition false, it'll return string that will overwrite your array..
Try by using, $info[] instead of $info.. May be it solved your problem..
public function coupon(){
for ($i = 0; $i <= $this->input->post("products_in_cart"); $i++) {
if (!empty($this->input->post("coupn-" . $i))) {
$couponname = $this->input->post("coupn-" . $i);
$products_id = $this->input->post("product_id" . $i);
$data = $this->home_model->getCoupon($couponname, $products_id);
$data1 = 'hello';
$info[] = array(
"PromotioanlName" => $data->PromotionalName,
);
} else {
$info[] = 'Thers in no value<br>';
}
}
echo $this->session->set_flashdata('message', $info);
redirect(site_url('cart'));
}

Related

Script not Iterating through all files in directory

PHP Script written for SuiteCRM improting is supposed to loop through all files in the directory and import each to Database. Having a lot of trouble.
Script reads and works on the first file only then finishes when it should loop :(
Importing works fine and data is added the the database from first file.
function MemberImportJob()
{
try
{
$config = new Configurator();
$config->loadConfig();
$xmlDataDir = 'custom/wimporter/eidimport';
$directoryContent = scandir($xmlDataDir);
//foreach ($directoryContent as $itemFile)
foreach (glob($xmlDataDir) as $itemfile)
{
var_dump($itemfile);
if (is_dir($xmlDataDir . DIRECTORY_SEPARATOR . $itemFile)) continue;
if (strcasecmp(substr($itemFile, -4), ".csv") != 0) continue;
$oFile = fopen($xmlDataDir . DIRECTORY_SEPARATOR . $itemFile, 'r');
if ($oFile !== FALSE)
{
$header = NULL;
$data = Array();
while (($data[] = fgetcsv($oFile, 90000, ',')) !== FALSE) { }
fclose($oFile);
//combine into a nice associative array:
$arow=Array();
$fields = array_shift($data);
foreach ($data as $i=>$arow)
{
array_combine " . $i);
if (is_array($arow)) {
$data[$i] = array_combine($fields, $arow);}
}
unset($arow);
$num = count($data);
for ($row=0; $row < $num - 1; $row++)
{
$Member = BeanFactory::getBean("locte_Membership");
$Member=$Member->retrieve_by_string_fields(array('last_name' => $data[$row]["LAST NAME"], 'first_name' => $data[$row]["FIRST NAME"], 'lcl_affiliate_number' => $data[$row]["AFFILIATE"]));
$MemberID = $Member->id;
if (is_null($Member)) {
$Member = BeanFactory::newBean('locte_Membership');
$delta = fillPerson($data[$row], $Member, "FULL NAME");
if(count($delta))
{
$Member_id = $Member->save();
}
} else {
v
var_dump($Member->id);
$delta = fillFoundrecord($data[$row], $Member, "FULL NAME");
echo("record Updated!");
$Member_id = $Member->save();
}
unset($data[$row]);
}
}
return true;
}
} catch (Exception $e)
{
return false;
}
}

Notice: Undefined variable: arr in /opt/lampp/htdocs/IRIS/controllers/get_category.php on line 9

I am trying to get access to a static variable from another class, but I keep getting an error (the one in the title) that says my variable is not being recognized.
Below is my Categorize class where the static variable, which is an array named $arr, is located:
class Categorize extends Controller{
public static $arr = array();
function run($xml){
global $FILE_ROOT, $STORAGE, $REQ_ID, $CMD_EXTRA, $LIB,
$BIN;
$numCategories = intval($xml->numCategories);
self::$arr;
/*self::$arr = array();*/
/*if(!pe($xml, "resourceList")) die(err("No resources found"));*/
for($i=0;$i < $numCategories; $i++){
$name = intval($xml->nameCat);
if($i=0){
$arr[0][0] = $name;
}else{
$arr[$i][0] = $name;
}
}
$j = 0;
while($j < $numCategories){
$numDoc = intval($xml->numDoc);
$k = 0;
foreach($xml->resourceList->resource as $res){
$arr[$j][$k] = $res;
$k++;
}
$j++;
}
$output = "Done!";
$response = "<parameters><requestType>categorize</requestType><requestID>". $REQ_ID . "</requestID><resourceList>". $output . "</resourceList></parameters>";
return $response;
}
}
Here is a class called Get_category where I am trying to access the static variable $arr from my the Categorize class:
class Get_category extends Controller{
function run($xml){
global $FILE_ROOT, $STORAGE, $REQ_ID, $CMD_EXTRA, $LIB,
$BIN;
include_once __DIR__.'/categorize.php';
$file = $xml->filename;
Categorize::$arr;
/*$arrlength = count($arr);*/
$arrlength = max(array_map('count', $arr));
$response = "<parameters>\n<requestID>" . $REQ_ID ."</requestID>\n<requestType>get_category</requestType>";
for($i = 0; $i < $arrlength; $i++){
$lengthcolumn = count($arr[$i]);
for($j = 0; $j < $lengthcolumn; $j++){
if($arr[$i][$j] == $file){
echo $arr[$i][$j];
$response .= "<resource><id>" . $arr[$i][$j] . "</id>";
$response .= "</resource>";
}
}
}
$response .= "</parameters>";
return $response;
}
}
I don't understand why my $arr variable is being unrecognized.
Looks like you forgot to assign $arr = Categorize::$arr;
Categorize::$arr;
/*$arrlength = count($arr);*/
$arrlength = max(array_map('count', $arr));
In PHP you can't access class properties using regular variable syntax. Static properties are accessed using self::$property, object properties are accessed using $this->property.
So where you have $arr it should be self::$arr. This change is needed in both functions that you posted. It doesn't cause an error in the first function because it's assigning to the variable rather than reading it, so it creates the variable as well. But I assume that the intent was to fill in the public static $arr property, which is not happening because it's being accessed incorrectly.
class Categorize extends Controller{
public static $arr = array();
function run($xml){
global $FILE_ROOT, $STORAGE, $REQ_ID, $CMD_EXTRA, $LIB,
$BIN;
$numCategories = intval($xml->numCategories);
self::$arr;
/*self::$arr = array();*/
/*if(!pe($xml, "resourceList")) die(err("No resources found"));*/
for($i=0;$i < $numCategories; $i++){
$name = intval($xml->nameCat);
if($i=0){
self::$arr[0][0] = $name;
}else{
self::$arr[$i][0] = $name;
}
}
$j = 0;
while($j < $numCategories){
$numDoc = intval($xml->numDoc);
$k = 0;
foreach($xml->resourceList->resource as $res){
self::$arr[$j][$k] = $res;
$k++;
}
$j++;
}
$output = "Done!";
$response = "<parameters><requestType>categorize</requestType><requestID>". $REQ_ID . "</requestID><resourceList>". $output . "</resourceList></parameters>";
return $response;
}
}
class Get_category extends Controller{
function run($xml){
global $FILE_ROOT, $STORAGE, $REQ_ID, $CMD_EXTRA, $LIB,
$BIN;
include_once __DIR__.'/categorize.php';
$file = $xml->filename;
/*self:$arrlength = count(self:$arr);*/
$arrlength = max(array_map('count', self::$arr));
$response = "<parameters>\n<requestID>" . $REQ_ID ."</requestID>\n<requestType>get_category</requestType>";
for($i = 0; $i < self::$arrlength; $i++){
$lengthcolumn = count(self::$arr[$i]);
for($j = 0; $j < $lengthcolumn; $j++){
if(self::$arr[$i][$j] == $file){
echo self::$arr[$i][$j];
$response .= "<resource><id>" . self::$arr[$i][$j] . "</id>";
$response .= "</resource>";
}
}
}
$response .= "</parameters>";
return $response;
}
}
This is a significant difference between PHP and some other OOP languages like C++ and Java. See Could not retrieve a property of class in PHP for my explanation of the rationale of this.

Display values without array (print_r)

I have a function that displays the actors photos from TMDB to my website is there a way that I can make it print without an array, it prints only with print_r I want to know if I can print it like echo or print.
This is the code:
public function getCasts($movieID)
{
if (empty($movieID)) return;
function cmpcast($a, $b)
{
return ($a["order"]>$b["order"]);
}
$temp = $this->_call("movie/" . $movieID . "/casts");
$casts = $temp['cast'];
$temp = array();
if (count($casts) > 0)
{
usort($casts, "cmpcast");
foreach ($casts as &$actor) {
if (!empty($actor['profile_path'])) {
for ($i=6; $i<count($temp['id']); $i++)
if ($temp['name'][$i] == $actor['name']) $temp['char'][$i] .= " / ".str_replace('(voice)', '(hang)', $actor['character']);
if (!in_array($actor['name'], (array) $temp['name'])) {
$temp['pic'][] = "<div style='margin-top:15px;' align='center'><div style='width:140px;margin-right:8px;display:inline-block;vertical-align:top;'><img style='width:130px;height:130px;border-radius:50%;' src='".$this->getImageURL().$actor['profile_path']."'><br />".$actor['name']."<br />".$actor['character']."</div></div>";
}
}
}
}
return $temp;
}
You could use some kind of loop. Use a for loop if you want to limit the number of item you echo.
For example :
$casts = getCasts(1);
for ($i = 0; $i < 5; $i++) {
if (isset($casts['pic'][$i])) {
echo $casts['pic'][$i];
}
}
Hope it helps.

Adding PHP code in extension after every nth list element in TYPO3

I'm using old fashion way (kickstarter) to build extension in TYPO3. I would like to ad some PHP code after third element of list, but I really don't know how to do this.
My code looks like that:
protected function makeList($res) {
$items = array();
// Make list table rows
while (($this->internal['currentRow'] = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) !== FALSE) {
$items[] = $this->makeListItem();
}
$out = '<div' . $this->pi_classParam('listrow') . '>list items</div>';
return $out;
}
And:
protected function makeListItem() {
$out = 'list item details';
return $out;
}
If I understood correctly, you would need something like this:
protected function makeList($res) {
$items = array();
// Make list table rows
$i = 0;
$out = '<div' . $this->pi_classParam('listrow') . '>';
while (($this->internal['currentRow'] = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) !== FALSE) {
$out .= $this->makeListItem();
$i++;
if ($i == 3) {
$out .= '<img src="whateverjpg">';
$i = 0; // if you want to do it every 3 images
}
}
$out .= '</div>';
return $out;
}

Calculate all possible combinations from sets/groups

I need to create a list of urls for all possible combinations from a set of filters/parameters.
Input
$data = array(
array(
'vehicle=car',
'vehicle=bike',
'vehicle=plane',
),
array(
'fruit=apple',
'fruit=banana',
'fruit=strawberry'
),
array(
'music=pop',
'music=rock',
'music=jazz'
)
);
The generated items must have the parameters in alphabetical order.
For example:
INCORRECT: ?vehicle=bike&fruit=apple&music=rock
CORRECT: ?fruit=apple&music=rock&vehicle=bike
Output
?vehicle=car
?vehicle=bike
?vehicle=plane
?fruit=apple&vehicle=car
?fruit=banana&vehicle=car
?fruit=strawberry&vehicle=car
?fruit=apple&vehicle=bike
?fruit=banana&vehicle=bike
?fruit=strawberry&vehicle=bike
?fruit=apple&vehicle=plane
?fruit=banana&vehicle=plane
?fruit=strawberry&vehicle=plane
?fruit=apple&music=pop&vehicle=car
?fruit=apple&music=rock&vehicle=car
?fruit=apple&music=jazz&vehicle=car
?fruit=banana&music=pop&vehicle=car
?fruit=banana&music=rock&vehicle=car
?fruit=banana&music=jazz&vehicle=car
?fruit=strawberry&music=pop&vehicle=car
?fruit=strawberry&music=rock&vehicle=car
?fruit=strawberry&music=jazz&vehicle=car
?fruit=apple&music=pop&vehicle=bike
?fruit=apple&music=rock&vehicle=bike
?fruit=apple&music=jazz&vehicle=bike
?fruit=banana&music=pop&vehicle=bike
?fruit=banana&music=rock&vehicle=bike
?fruit=banana&music=jazz&vehicle=bike
?fruit=strawberry&music=pop&vehicle=bike
?fruit=strawberry&music=rock&vehicle=bike
?fruit=strawberry&music=jazz&vehicle=bike
?fruit=apple&music=pop&vehicle=plane
?fruit=apple&music=rock&vehicle=plane
?fruit=apple&music=jazz&vehicle=plane
?fruit=banana&music=pop&vehicle=plane
?fruit=banana&music=rock&vehicle=plane
?fruit=banana&music=jazz&vehicle=plane
?fruit=strawberry&music=pop&vehicle=plane
?fruit=strawberry&music=rock&vehicle=plane
?fruit=strawberry&music=jazz&vehicle=plane
?music=pop&vehicle=car
?music=rock&vehicle=car
?music=jazz&vehicle=car
?music=pop&vehicle=bike
?music=rock&vehicle=bike
?music=jazz&vehicle=bike
?music=pop&vehicle=plane
?music=rock&vehicle=plane
?music=jazz&vehicle=plane
?fruit=apple
?fruit=banana
?fruit=strawberry
?fruit=apple&music=pop
?fruit=apple&music=rock
?fruit=apple&music=jazz
?fruit=banana&music=pop
?fruit=banana&music=rock
?fruit=banana&music=jazz
?fruit=strawberry&music=pop
?fruit=strawberry&music=rock
?fruit=strawberry&music=jazz
?music=pop
?music=rock
?music=jazz
Is there anyone that could help me out with this. I've been struggling with it for two days now but I can't seem so find a correct solution. There are a lot of (almost) similar issues on Stackoverflow but none of them seems to solve/fit my problem.
[SOLVED]
Here is the final working version based on Dusan Plavak's answer:
function createFilterCombinations($data, &$urls = array(), $index = 0, $query = false){
$keys = array_keys($data);
$_query = $query;
if ($index == count($data)) {
return;
}
for($i=0; $i < count($data[$keys[$index]]); $i++){
$query = $_query;
if($index == 0){
$query = "?" . $data[$keys[$index]][$i];
}else{
if($query != "?"){
$query .= "&" . $data[$keys[$index]][$i];
}else{
$query .= $data[$keys[$index]][$i];
}
}
$urls[] = $query;
createFilterCombinations($data, $urls, $index+1, $query);
}
if($index == 0){
$query = "?";
} else {
$query = $_query;
}
createFilterCombinations($data, $urls, $index+1, $query);
}
function prepareArray($array){
$newArray = array();
foreach ($array as $subArray) {
sort($subArray);
$newArray[substr($subArray[0], 0, strpos($subArray[0], '='))] = $subArray;
}
ksort($newArray);
return $newArray;
}
createFilterCombinations(prepareArray($data), $result);
var_dump($result);
So look at this http://codepad.org/TZWf7Vxd
and code for a time when link will be dead :D
<?php
$data = array(
"vehicle" => array(
'vehicle=car',
'vehicle=bike',
'vehicle=plane',
),
"fruit" => array(
'fruit=apple',
'fruit=banana',
'fruit=strawberry'
),
"music" => array(
'music=pop',
'music=rock',
'music=jazz'
)
);
function hop($index, $query, $data){
$keys = array_keys($data);
if($index == count($data)){
return;
}
$queryBackup = $query;
for($i=0;$i<count($data[$keys[$index]]);$i++){
$query = $queryBackup;
if($index == 0){
$query = "?".$data[$keys[$index]][$i];
}else{
if($query != "?"){
$query .= "&".$data[$keys[$index]][$i];
}else{
$query .= $data[$keys[$index]][$i];
}
}
echo $query."\n";
hop($index+1, $query, $data);
}
if($index == 0){
$query = "?";
}else{
$query = $queryBackup;
}
hop($index+1, $query, $data);
}
ksort($data);
hop(0,"", $data);
?>
This is not a ready-made solution but you can use a function that returns an array combinations. I hope this could help You.
<?
$collect = false;
function combinations($arr, $temp_string, &$collect) {
if ($temp_string != "")
$collect[] = $temp_string;
for ($i = 0; $i < sizeof($arr); $i++) {
$arrcopy = $arr;
$elem = array_splice($arrcopy, $i, 1);
if (sizeof($arrcopy) > 0) {
combinations($arrcopy, $temp_string . " " . $elem[0], $collect);
} else {
$collect[] = $temp_string . " " . $elem[0];
}
}
return $collect;
}
var_dump(combinations(array('abc', 'cde', 'fgi'),'',$collect));
?>
see WORKING CODE

Categories