Cakephp set function not passing variables from controller to the view - php

I'm trying to pass variable to the view and this one is very weird as the naming and directory structure is correct. Below is the function in my controller:
public function validate_apply_link(){
App::uses('CakeEmail', 'Network/Email');
$this->layout = 'blank';
$listings = $this->CareersAndJob->query("
SELECT l.sid, l.title, lp.value, u.CompanyName, u.WebSite
FROM listings l
LEFT JOIN listings_properties lp
ON lp.object_sid = l.sid
LEFT JOIN users u
ON u.sid = l.user_sid
WHERE l.active = 1
AND lp.add_parameter = 2
AND l.JobGateSenderReference IS NULL
AND u.CompanyName != 'AECOM'
ORDER BY u.CompanyName ASC
LIMIT 5
");
$doc = new DOMDocument();
ob_start();
$listing_count = count($listings);
echo nl2br("Checking $listing_count active jobs...\n\n");
$i=0;
foreach($listings as $listing){
$sid = $listing['l']['sid'];
$url = $listing['lp']['value'];
$company_name = $listing['u']['CompanyName'];
$title = htmlspecialchars($listing['l']['title']);
$length = strpos($title, "-");
if($length != 0){
$title = substr($title, 0, $length-1);
}
$title = substr($title, 0, $length-1);
$title = substr($title, 0, 10);
$data = $this->curl($url);
$check_pdf = strpos($data['info']['content_type'], "pdf");
if($check_pdf != false){
$outputs['data'][$i]['url'] = $url;
$outputs['data'][$i]['sid'] = $sid;
$outputs['data'][$i]['title'] = $title;
$outputs['data'][$i]['company_name'] = $company_name;
$outputs['data'][$i]['our_link'] = "http://careersandjobs.com.au/display-job/{$sid}";
$outputs['data'][$i]['content_type'] = $data['info']['content_type'];
$outputs['data'][$i]['data_type'] = 'pdf';
$i++;
continue;
}
#$doc->loadHTML($data['results']);
$html = $doc->saveHTML();
$xpath = new DOMXpath($doc);
$body = $doc->getElementsByTagName('body')->item(0);
$parsed_url = parse_url($url);
switch($parsed_url['host']){
case "www.michaelpage.com.au":
parse_str($url);
$exist = $xpath->query("//*[contains(#value,'{$ref}')]");
break;
case "https://vacancies.mackay.qld.gov.au":
parse_str($url);
$exist = $xpath->query("//*[contains(#value,'{$title}')]");
break;
default:
$exist = $xpath->query("//*[contains(text(),'{$title}')]");
break;
}
if($exist->length == 0){
if(strpos($url, '#') == false){
$outputs['data'][$i]['url'] = $url;
$outputs['data'][$i]['sid'] = $sid;
$outputs['data'][$i]['title'] = $title;
$outputs['data'][$i]['company_name'] = $company_name;
$outputs['data'][$i]['our_link'] = "http://careersandjobs.com.au/display-job/{$sid}";
$outputs['data'][$i]['content_type'] = $data['info']['content_type'];
$response_code = $this->http_response_codes($data['info']['http_code']);
$outputs['data'][$i]['response_code'] = $response_code;
$outputs['data'][$i]['data_type'] = 'title_not_found';
}else{
$outputs['data'][$i]['data_type'] = 'no_iframe';
}
$i++;
}
flush();
ob_flush();
}
$this->set(compact('outputs'));
}
I can do pr on the outputs variable in the view but this outputs to NULL but when I delete the entire bunch of code inside the controller function and just pass a test variable through it works.
Is there something wrong with the function that I am not aware of?
No errors were found in the above function by the way
app/Controller/CareersAndJobsController.php (line 1048)
array(
'data' => array(
(int) 0 => array(
'url' => 'http://bawbawshire.currentjobs.com.au/cvbuilder/apply+for+this+job/no/1225055',
'sid' => '3649',
'title' => 'Graduate P',
'company_name' => 'Baw Baw Shire Council',
'our_link' => 'http://careersandjobs.com.au/display-job/3649',
'content_type' => 'text/html; charset=utf-8',
'response_code' => 'OK',
'data_type' => 'title_not_found'
),
(int) 1 => array(
'url' => 'http://bawbawshire.currentjobs.com.au/cvbuilder/apply+for+this+job/no/1225724',
'sid' => '3726',
'title' => 'Program &a',
'company_name' => 'Baw Baw Shire Council',
'our_link' => 'http://careersandjobs.com.au/display-job/3726',
'content_type' => 'text/html; charset=utf-8',
'response_code' => 'OK',
'data_type' => 'title_not_found'
),
(int) 2 => array(
'url' => 'http://bawbawshire.currentjobs.com.au/cvbuilder/apply+for+this+job/no/1225826',
'sid' => '3727',
'title' => 'Road Netwo',
'company_name' => 'Baw Baw Shire Council',
'our_link' => 'http://careersandjobs.com.au/display-job/3727',
'content_type' => 'text/html; charset=utf-8',
'response_code' => 'OK',
'data_type' => 'title_not_found'
)
)
)
This is what I am getting from outputs variable just before it gets set by the set function in controller

Any reason you chose to use CakePHP? Because you seem to not make use of its functionality!
You're using literal SQL queries, therefore basically skipping the Models functionality.
You're outputting your content from your Controller? Be careful when using output buffering, this may conflict with CakePHP's inner workings, which also relies on output buffering in many cases. Because you're already outputting the content here (ob_flush()), you'll be outputting your content before your View is reached..
Normally I would point to specific points in the manual, however, because there's so much wrong here, I would suggest to start reading at the beginning

Related

PHP Horde imap how to fetch

I saw the similar questions, but it has not helped me. I am trying to fetch message. I need full message with all parts, headers, attachments.
$fetchQuery = new Horde_Imap_Client_Fetch_Query();
$fetchQuery->fullText();
/** #var Horde_Imap_Client_Fetch_Results $mail */
$results = $client->fetch('INBOX', $fetchQuery, ['ids' => new Horde_Imap_Client_Ids(11632)]);
var_dump($results->first()->getEnvelope()->subject);
I tried a lot of variants. But I can't get any info about message. The subject is empty string. I am sure, such mail with that uid exists, I got this uid with Horde also.
Try the code mentioned below. $results array has all the items you need.
$uids = new \Horde_Imap_Client_Ids($thread_uids);
$query = new \Horde_Imap_Client_Fetch_Query();
$query->envelope();
$query->structure();
$messages = $oClient->fetch($mailbox, $query, array('ids' => $uids));
$results = [];
foreach($messages as $message){
$envelope = $message->getEnvelope();
$structure = $message->getStructure();
$msghdr = new StdClass;
$msghdr->recipients = $envelope->to->bare_addresses;
$msghdr->senders = $envelope->from->bare_addresses;
$msghdr->cc = $envelope->cc->bare_addresses;
$msghdr->bcc = $envelope->bcc->bare_addresses;
$msghdr->subject = $envelope->subject;
$msghdr->timestamp = $envelope->date->getTimestamp();
$query = new Horde_Imap_Client_Fetch_Query();
$query->fullText();
$typemap = $structure->contentTypeMap();
foreach ($typemap as $part => $type) {
// The body of the part - attempt to decode it on the server.
$query->bodyPart($part, array(
'decode' => true,
'peek' => true,
));
$query->bodyPartSize($part);
}
$id = new Horde_Imap_Client_Ids($message->getUid());
$messagedata = $oClient->fetch($mailbox, $query, array('ids' => $id))->first();
$msgdata = new StdClass;
$msgdata->id = $id;
$msgdata->contentplain = '';
$msgdata->contenthtml = '';
$msgdata->attachments = array(
'inline' => array(),
'attachment' => array(),
);
$plainpartid = $structure->findBody('plain');
$htmlpartid = $structure->findBody('html');
foreach ($typemap as $part => $type) {
// Get the message data from the body part, and combine it with the structure to give a fully-formed output.
$stream = $messagedata->getBodyPart($part, true);
$partdata = $structure->getPart($part);
$partdata->setContents($stream, array('usestream' => true));
if ($part == $plainpartid) {
$msgdata->contentplain = $partdata->getContents();
} else if ($part == $htmlpartid) {
$msgdata->contenthtml = $partdata->getContents();
} else if ($filename = $partdata->getName($part)) {
$disposition = $partdata->getDisposition();
$disposition = ($disposition == 'inline') ? 'inline' : 'attachment';
$attachment = new StdClass;
$attachment->name = $filename;
$attachment->type = $partdata->getType();
$attachment->content = $partdata->getContents();
$attachment->size = strlen($attachment->content);
$msgdata->attachments[$disposition][] = $attachment;
}
}
$data = [
'uid' => implode("",$id->ids),
'from' => implode(",",$msghdr->senders),
'cc' => implode(",",$msghdr->cc),
'bcc' => implode(",",$msghdr->bcc),
'to' => implode(",",$msghdr->recipients),
'date' => $msghdr->timestamp,
'subject' => $envelope->subject,
'hasAttachments' => $structure->getDisposition(),
'folder' => $mailbox,
'messageId' => $envelope->message_id,
'attachment' => $msgdata->attachments
];
$data['body'] = empty($msgdata->contenthtml) ? $msgdata->contenttext: $msgdata->contenthtml;
array_push($results,$data);
}
$fetchQuery = new Horde_Imap_Client_Fetch_Query();
$fetchQuery->fullText();
/** #var Horde_Imap_Client_Fetch_Results $mail */
$results = $client->fetch('INBOX', $fetchQuery, ['ids' => new Horde_Imap_Client_Ids(11632)]);
var_dump($results->first()->getFullMsg());

Display product price when searching

I purchased a search module that displays a popup and would like to add the price of the product I searched for in it, I found the file that I should make the changes, but what should I add to display the product price
This is the responsible function for displaying the product characteristics
protected function _prepareProducts()
{
$isEnabledImage = (bool) Mage::getStoreConfig(self::ENABLE_IMAGE_CONFIG);
$imageHeight = (int) Mage::getStoreConfig(self::IMAGE_HEIGHT_CONFIG);
$imageWidth = (int) Mage::getStoreConfig(self::IMAGE_WIDTH_CONFIG);
$isEnabledDescription = (bool) Mage::getStoreConfig(self::ENABLE_DESCRIPTION_CONFIG);
$lengthDescription = (int) Mage::getStoreConfig(self::DESCRIPTION_LENGTH_CONFIG);
$collection = $this->_getAlternativeProductCollection();
// $this->_prepareQueryPopularity($collection->getSize());
$toolbar = $this->getToolbarBlock();
$toolbar->setCollection($collection);
$size = (int) Mage::getStoreConfig(self::RESULT_SIZE_CONFIG);
$collection->setPageSize($size);
// $collection->getSelect()->limit($size);
$sortOrder = Mage::getStoreConfig(self::SORT_ORDER_PRODUCT);
if (0 < count($collection)) {
$this->_suggestions[$sortOrder][] = array('html' =>
'<p class="headercategorysearch">' . $this->__("") . '</p>'
);
}
if ($isEnabledImage) {
$helper = Mage::helper('catalog/image');
}
foreach ($collection as $_row) {
$_product = Mage::getModel('catalog/product')
->setStoreId($this->getStoreId())
->load($_row->getId());
$_image = $_srcset = $_description = '';
if ($isEnabledImage) {
$_image = (string) $helper->init($_product, 'thumbnail')->resize($imageWidth, $imageHeight);
$_srcset = (string) $helper->init($_product, 'thumbnail')->resize($imageWidth * 2, $imageHeight * 2);
$_srcset .= ' 2x';
}
if ($isEnabledDescription) {
$_description = strip_tags($this->_trim(
$_product->getShortDescription(),
$lengthDescription
));
}
// $store = Mage::app()->getStore();
// $path = Mage::getResourceModel('core/url_rewrite')
// ->getRequestPathByIdPath('product/' . $_product->getId(), $store);
// // $url = $store->getBaseUrl($store::URL_TYPE_WEB) . $path;
// $url = rtrim(Mage::getUrl($path, array('_store' => $store->getStoreId())), '/');
$url = $_product->getProductUrl();
$this->_suggestions[$sortOrder][] = array(
'name' => $_product->getName(),
'url' => $url,
'image' => $_image,
'srcset' => $_srcset,
'description' => $_description,
);
}
}
Simply add this line
'price' => $_product->getPrice() in suggestions array
$this->_suggestions[$sortOrder][] = array(
'name' => $_product->getName(),
'price' => $_product->getPrice(),
'url' => $url,
'image' => $_image,
'srcset' => $_srcset,
'description' => $_description,
);
you may use
$_product->getFinalPrice()
inside this _suggestions property, but i guess there is also a template or a bit of js responsible for output of all this.

PHP Save array in database and miss data

I need to save in database category like "a & b" but the model save in database just "a" miss the blank space and the &.
This is the array:
$data = array('avenvu' => $_POST['avenvu'],
'brand' => $_POST['brand'],
'category' => $_POST['category'],
'description' => $_POST['description'],
'man_women_shop'=> $_POST['man_women_shop'],
'postdatetime' => date("Y-m-d H:i:s",time()),
'publish' => 1,
'user_id' => $this->session->userdata('user_id'));
$result = $this->personal_closest->insertCloset($data);
And this is the model:
function insertCloset($data) {
$this->db->insert('personal_closest',$data);
}
Change your insertCloset function:
function insertCloset($data) {
$personal['avenvu'] = $data['avenvu'];
$personal['brand'] = $data['brand'];
$personal['category'] = $data['category'];
$personal['description'] = $data['description'];
$personal['man_women_shop'] = $data['man_women_shop'];
$personal['postdatetime'] = $data['postdatetime'];
$personal['publish'] = $data['publish'];
$personal['user_id'] = $data['user_id'];
$this->db->insert('personal_closest',$personal);
}

Pass variables through include (another php file) in php

I'm working on a php web application for a website, and I would like to pass some variables from one php file to another. I have tried the old fashion way with the include file but was not successful. I also tried to set the variables in global scope but still not working. The code in first.php file is:
function rc_getAvailableVehicles($pickup_timestamp, $return_timestamp, $vehicle_classes=array()) {
global
$wpdb;
$rc_currency = RC_Registry::get('rc_currency');
$where_classes = "";
if ($vehicle_classes) {
foreach($vehicle_classes as $vehicle_class) {
$where_classes[] = " v.class = '". $wpdb->escape($vehicle_class) ."'";
}
$where_classes = "AND (".implode(' OR ', $where_classes).") ";
}
$sql = "SELECT *
FROM ".$wpdb->rc_vehicles." v
WHERE v.quantity > (SELECT COUNT(*) FROM ".$wpdb->rc_bookings." b WHERE b.vehicle_id = v.vehicle_id AND ((UNIX_TIMESTAMP(b.pickup_date) >= '". (int)$pickup_timestamp ."' AND UNIX_TIMESTAMP(b.pickup_date) < '". (int)$return_timestamp ."') || (UNIX_TIMESTAMP(b.pickup_date) < '". (int)$pickup_timestamp ."' AND UNIX_TIMESTAMP(b.return_date) >= '". (int)$pickup_timestamp ."')) AND (b.status != 'new' AND b.status != 'canceled') AND b.trash = '0') ".$where_classes."AND v.status = '1' AND v.archive = '0'
ORDER BY v.rent ASC";
$results = $wpdb->get_results($sql,ARRAY_A);
$vehicles = array();
$rental_days = ceil(($return_timestamp-$pickup_timestamp)/91800);
$xfee = get_field('another_location_fee');
$xfee2 = get_field('return_to_another_location_fee_2');
if ($results) {
foreach ($results as $result) {
if ($result['image'] && file_exists(RC_UPLOADS_DIR . "vehicle_" . $result['image'])) {
$image = RC_UPLOADS_URL . 'vehicle_' . $result['image'];
$image_thumb = RC_UPLOADS_URL . 'cache/' . rc_image_resize(RC_UPLOADS_DIR . "vehicle_" . $result['image'], 220, 160);
} else {
$image = '';
$image_thumb = '';
}
$result['description'] = do_shortcode($result['description']);
$description = html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8');
global $GeneralPrice, $discount_price, $rent1, $rent5;
include_once ('file2.php');
//$GeneralPrice = $result['rent'];
$rent1 = $vehicle_meta['rent1'];
$rent2 = $vehicle_meta['rent2'];
$rent3 = $vehicle_meta['rent3'];
$rent4 = $vehicle_meta['rent4'];
$rent5 = $vehicle_meta['rent5'];
$rent6 = $vehicle_meta['rent6'];
$rent7 = $vehicle_meta['rent7'];
$rent8 = $vehicle_meta['rent8'];
$rent9 = $vehicle_meta['rent9'];
$rent10 = $vehicle_meta['rent10'];
$rent11 = $vehicle_meta['rent11'];
$rent12 = $vehicle_meta['rent12'];
$rent13 = $vehicle_meta['rent13'];
$rent14 = $vehicle_meta['rent14'];
$rent15 = $vehicle_meta['rent15'];
$rent16 = $vehicle_meta['rent16'];
$rent17 = $vehicle_meta['rent17'];
$rent18 = $vehicle_meta['rent18'];
$rent19 = $vehicle_meta['rent19'];
$rent20 = $vehicle_meta['rent20'];
$rent21 = $vehicle_meta['rent21'];
$rent22 = $vehicle_meta['rent22'];
$rent23 = $vehicle_meta['rent23'];
$rent24 = $vehicle_meta['rent24'];
$rent25 = $vehicle_meta['rent25'];
$rent26 = $vehicle_meta['rent26'];
$rent27 = $vehicle_meta['rent27'];
$rent28 = $vehicle_meta['rent28'];
$rent29 = $vehicle_meta['rent29'];
$rent30 = $vehicle_meta['rent30'];
$rent31 = $vehicle_meta['rent31'];
$rent32 = $vehicle_meta['rent32'];
$rent33 = $vehicle_meta['rent33'];
$rent34 = $vehicle_meta['rent34'];
$rent35 = $vehicle_meta['rent35'];
$rent36 = $vehicle_meta['rent36'];
$vehicle_meta = rc_getVehicleMeta($result['vehicle_id']);
$vehicles[$result['vehicle_id']] = array(
'vehicle_id' => $result['vehicle_id'],
'title' => $result['manufacturer']." ".$result['series'],
'manufacturer' => $result['manufacturer'],
'series' => $result['series'],
'year' => $result['year'],
'class' => $result['class'],
'image' => $image,
'image_thumb' => $image_thumb,
'description' => $description,
'seats' => $vehicle_meta['seats'],
'doors' => $vehicle_meta['doors'],
'baggages' => $vehicle_meta['baggages'],
'conditioning' => $vehicle_meta['conditioning'],
'transmission' => $vehicle_meta['transmission'],
'rent1' => $vehicle_meta['rent1'],
'rent2' => $vehicle_meta['rent2'],
'rent3' => $vehicle_meta['rent3'],
'rent4' => $vehicle_meta['rent4'],
'rent5' => $vehicle_meta['rent5'],
'rent6' => $vehicle_meta['rent6'],
'rent7' => $vehicle_meta['rent7'],
'rent8' => $vehicle_meta['rent8'],
'rent9' => $vehicle_meta['rent9'],
'rent10' => $vehicle_meta['rent10'],
'rent11' => $vehicle_meta['rent11'],
'rent12' => $vehicle_meta['rent12'],
'rent13' => $vehicle_meta['rent13'],
'rent14' => $vehicle_meta['rent14'],
'rent15' => $vehicle_meta['rent15'],
'rent16' => $vehicle_meta['rent16'],
'rent17' => $vehicle_meta['rent17'],
'rent18' => $vehicle_meta['rent18'],
'rent19' => $vehicle_meta['rent19'],
'rent20' => $vehicle_meta['rent20'],
'rent21' => $vehicle_meta['rent21'],
'rent22' => $vehicle_meta['rent22'],
'rent23' => $vehicle_meta['rent23'],
'rent24' => $vehicle_meta['rent24'],
'rent25' => $vehicle_meta['rent25'],
'rent26' => $vehicle_meta['rent26'],
'rent27' => $vehicle_meta['rent27'],
'rent28' => $vehicle_meta['rent28'],
'rent29' => $vehicle_meta['rent29'],
'rent30' => $vehicle_meta['rent30'],
'rent31' => $vehicle_meta['rent31'],
'rent32' => $vehicle_meta['rent32'],
'rent33' => $vehicle_meta['rent33'],
'rent34' => $vehicle_meta['rent34'],
'rent35' => $vehicle_meta['rent35'],
'rent36' => $vehicle_meta['rent36'],
'total_price' => $rc_currency->format((($rental_days*$GeneralPrice)-(($rental_days*$GeneralPrice) * ($discount_price)/100)+ $utime)),
'total_price_return_fee' => $rc_currency->format((($rental_days*$result['rent']) - ($rental_days*$result['rent']) * ($discount_price)/100)+ $xfee),
'total_price_return_fee2' => $rc_currency->format((($rental_days*$result['rent']) - ($rental_days*$result['rent'])*($discount_price)/100)+ $xfee2),
'daily_price' => $rc_currency->format((($GeneralPrice)-($GeneralPrice)*($discount_price)/100)+ ($utime / $rental_days)),
'daily_price_return_fee' => $rc_currency->format(($result['rent']-($result['rent'])* ($discount_price)/100)+ ($xfee / $rental_days)),
'daily_price_return_fee2' => $rc_currency->format(($result['rent']-($result['rent'])*($discount_price)/100) + ($xfee2 / $rental_days))
);
}
}
return $vehicles;
}
and I want to echo all the $rent variables in file2.php
I have tried the
echo $rent; without success!
You have problems with the order of the code
$vehicle_meta = rc_getVehicleMeta($result['vehicle_id']);
Should be BEFORE you populate rent variables, and
include_once ('file2.php');
Should be AFTER you populated rent variables.
You need to call a function in file1.php that exists in file2.php
ex. in file1.php you could call the function return_rent
return_rent($rent);
in file2 then you make the function
function return_rent($var){
echo $rent;
}
$pass = "anything";
include_once ('file2.php');
From file2.php you can access $pass variable.

PHP multiple array - echoing the value of an array key using object

<?php
/* SC: Shop Category */
$SCStatement = "SELECT * FROM shop_categories";
$SCQuery = mysql_query($SCStatement);
while($SCFetch = mysql_fetch_array($SCQuery)){
$SCItems[] = array(
'id' => $SCFetch['id'],
'name' => $SCFetch['cat_name'],
'desc' => $SCFetch['cat_description']
);
}
$SCNumCols = 2;
$SCNumItems = count($SCItems);
$SCNumRows = ceil($SCNumItems / $SCNumCols);
function bindArrayToObject($array) {
$return = new stdClass();
foreach ($array as $k => $v) {
if (is_array($v)) {
$return->$k = bindArrayToObject($v);
}
else {
$return->$k = preg_replace ('/<[^>]*>/', '', $v);
}
}
return $return;
}
$newObject = bindArrayToObject($SCItems);
echo $newObject->name;
?>
The data i retrieve from the database is stored in $SCItems[] array. The problem is, when i echo the $newObject->name; nothing will appear. What to add this code to show the data using $newObject->name;
Thanks in advance.
Well, judging from this code, what you have is something like
$SCItems = Array(
0 => Array(
'id' => 1,
'name' => 'name 1',
'desc' => 'description 1'
),
1 => Array(
'id' => 2,
'name' => 'name 2',
'desc' => 'description 2'
),
);
And then from that your bindArrayToObject function trying to build the object
$newObject = new stdClass();
$newbject->0 = new stdClass();
$newbject->0->id = 1;
$newbject->0->name = 'name 1';
$newbject->0->desc = 'description 1';
$newbject->1 = new stdClass();
$newbject->1->id = 2;
$newbject->1->name = 'name 2';
$newbject->1->desc = 'description 2';
So, what you probably should do is loop over your '$SCItems' and then on each entry use bindArrayToObject
Such as
$SCObject = Array();
foreach($SCItems as $SCItem) {
$SCObjects[] = bindArrayToObject($SCItem);
}
From there you should be able to access $SCObjects[0]->name, which would make much more sense to me

Categories